如何使用 igraph 创建顶点权重在 python 中的图?
How to create a graph with vertices weight in python with igraph?
我搜索了一下,发现有很多关于如何创建具有边权重的图的示例,但其中 none 显示了如何创建具有顶点权重的图。我开始怀疑这是否可能。
如果可以用igraph创建顶点加权图,那么是否可以用igraph计算加权独立性或其他加权数?
您想使用 vs
class 在 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
据我所知,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 列 = 权重)。这得到了考虑顶点权重的最大独立集。
我搜索了一下,发现有很多关于如何创建具有边权重的图的示例,但其中 none 显示了如何创建具有顶点权重的图。我开始怀疑这是否可能。
如果可以用igraph创建顶点加权图,那么是否可以用igraph计算加权独立性或其他加权数?
您想使用 vs
class 在 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
据我所知,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 列 = 权重)。这得到了考虑顶点权重的最大独立集。