如何使用igraph在python中创建顶点权重的图形?

时间:2016-11-08 21:06:45

标签: python graph igraph weighted

我进行了搜索,但发现有很多关于如何创建具有边缘权重的图形的示例,但没有一个示例显示如何创建具有顶点权重的图形。我开始怀疑它是否可能。

如果可以使用igraph创建顶点加权图,那么可以使用igraph计算加权独立性或其他加权数吗?

2 个答案:

答案 0 :(得分:1)

据我所知,igraph中没有接受加权顶点参数的函数。但是,作为R的Bioconductor套件的一部分的SANTA包确实具有加权顶点的例程,如果您愿意为此移动到R. (看起来像也许你可以运行bioconductor in python。)

另一个hacky选项是使用(如果可能的话)来自igraph的未加权例程然后返回权重。例如。对于加权最大独立集合这样的事情:

def maxset(graph,weight):
    ms = g.maximal_independent_vertex_sets()
    w = []
    t = []
    for i in range(0, 150):
        m = weights.loc[weights['ids'].isin(ms[i]),"weights"]
        w.append(m)
        s = sum(w[i])
        t.append(s) 
    return(ms[t.index(max(t))])
maxset(g,weights)

(其中权重是两列数据框,其中列1 =顶点id,列2 =权重)。这将获得考虑顶点权重的最大独立集。

答案 1 :(得分:0)

您希望使用igraph类在g=Graph.Full(3) # generate a full graph as example for idx, v in enumerate(g.vs): v["weight"] = idx*(idx+1) # set the 'weight' of vertex to integer, in function of a progressive index >>> g.vs["weight"] [0, 2, 6] 中定义顶点及其属性。

作为设置顶点权重的示例,取自文档:

http://igraph.org/python/doc/igraph.VertexSeq-class.html

ylim=c(-min,max)

请注意,通过g.vs调用一系列顶点,这里是图形对象的实例。

我建议你这个页面,我发现在这里寻找iGraph方法很实用: http://igraph.org/python/doc/identifier-index.html