使用subgraph()并使用igraph python保留顶点属性

时间:2016-02-16 23:23:23

标签: python igraph subgraph vertex-attributes

我希望将我的断开连接图分区为块(使用community_spinglass)。但是,一旦我得到子图并使用community_spinglass(),原始图中顶点的标签就会丢失。我正在处理40多个顶点,因此跟踪它们并不容易。这是我的问题的“玩具示例”:

import igraph
from igraph import Graph
g4_matrix = [ [0,0,-1,0,0,0,0], [0,0,0,0.8,0.2,0,0], [-1,0,0,0,0,0,0], [0,0.8,0,0,1,0,0.1], [0,0.2,0,1,0,-0.3,-.7], [0,0,0,0,-0.3,0,1], [0,0,0,0.1,-0.7,1,0] ]
v_name = ["1", "2", "3", "4", "5", "6", "7"]
g4 = Graph.Weighted_Adjacency(g4_matrix, mode = 'undirected',attr = 'weight' )
igraph.plot(g4,bbox = (300, 300),vertex_label = v_name)

Original graph with labels

获得街区和社区后:

g4_blocks = g4.blocks()
g4_block = g4.vs.select(g4_blocks[1])
Block1 = g4.subgraph(g4_block)
igraph.plot(Block1, bbox = (200, 200), vertex_label = v_name)

Subgraph, as observed the vertices follow v_name list in order and don't preserve past label

但是我们看到顶点遵循v_name列表顺序,而不是它们之前的标签。此外,当我们获得社区时:

comm = Block1.community_spinglass()
for c in comm:
    print c

[2, 3, 4]
[0, 1]

我们得到子图的索引,但很难与原始图的索引相关联。

有没有办法获得引用原始图表的索引或标签的社区?

提前致谢。

1 个答案:

答案 0 :(得分:1)

使用Block1.vs[c]["name"]获取c内社区Block1中节点的名称。说明:

  1. Block1.vs是整个Block1图表的顶点序列。

  2. Block1.vs[c]将此顶点序列子集设置为索引位于名为c的可迭代中的节点,这是for循环中的情况。

  3. Block1.vs[c]["name"]检索您在上一步中选择的顶点的name顶点属性。由于名称保存在子图中,因此应该允许您将Block1的节点与原始图形相关联。

相关问题