【发布时间】:2020-02-02 17:43:08
【问题描述】:
使用igraph 我可以为每个节点分配连接组件的唯一ID:
import igraph
def tag_components(vertices, edges):
graph = igraph.Graph()
graph.add_vertices(vertices)
graph.add_edges(edges)
graph_tags = graph.clusters().membership
return graph_tags
print(tag_components([0,1,2,3,4,5], [[1,2],[2,4],[3,5]]))
它输出[0, 1, 1, 2, 1, 2],这意味着3个连接的组件索引为0、1、2,与节点组[0]、[1, 2, 4]、[3, 5]一致。如何使用networkx? 实现相同的输出
我希望是这样的:
def tag_components_nx(vertices, edges):
G = nx.Graph()
G.add_nodes_from(vertices)
G.add_edges_from(edges)
...
return graph_tags
更新
我已经有了一个满意的答案,我想知道networkx 是否有比connected_components 更复杂的方法适合我的问题
【问题讨论】: