【发布时间】:2016-11-08 21:06:45
【问题描述】:
我搜索但发现有很多关于如何创建具有边权重的图形的示例,但没有一个显示如何创建具有顶点权重的图形。我开始怀疑这是否可能。
如果可以用igraph创建顶点加权图,那么是否可以用igraph计算加权独立性或其他加权数?
【问题讨论】:
标签: python graph igraph weighted
我搜索但发现有很多关于如何创建具有边权重的图形的示例,但没有一个显示如何创建具有顶点权重的图形。我开始怀疑这是否可能。
如果可以用igraph创建顶点加权图,那么是否可以用igraph计算加权独立性或其他加权数?
【问题讨论】:
标签: python graph igraph weighted
据我所知,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)
(其中 weights 是一个两列数据框,第 1 列 = 顶点 ID,第 2 列 = 权重)。这得到考虑到顶点权重的最大独立集。
【讨论】:
您想使用vs 类在igraph 中定义顶点及其属性。
作为设置顶点权重的示例,取自文档:
http://igraph.org/python/doc/igraph.VertexSeq-class.html
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]
注意,顶点序列是通过 g.vs 调用的,这里 g 是 Graph 对象的实例。
我向您推荐了这个页面,我发现在这里寻找 iGraph 方法很实用: http://igraph.org/python/doc/identifier-index.html
【讨论】: