【发布时间】:2017-05-04 15:15:57
【问题描述】:
我在通过大型 nx 图 G 的所有可能子图获得迭代时遇到了一些麻烦,该图 G 被作为文本文件的输入。
以下是我在 StackOverflow 上找到的一些代码,但没有产生我正在寻找的代码:
g = read_graph(sys.argv[1])
print ('Number of subgraphs:', len(g.subgraph(g.nodes())))
# extract subgraphs
sub_graphs = nx.connected_component_subgraphs(g)
for i, sg in enumerate(sub_graphs):
print "subgraph {} has {} nodes".format(i, sg.number_of_nodes())
print "\tNodes:", sg.nodes(data=True)
print "\tEdges:", sg.edges()
打印出来:
('Number of subgraphs:', 4039)
subgraph 0 has 4039 nodes
Nodes: <generator object nodes at 0x10ed77910>
Edges: <generator object edges at 0x10ed77910>
我想要做的是能够遍历所有可能的长度为 3 或更大的子图,然后将它们作为图执行某些功能。
谢谢!
【问题讨论】:
-
您能否提供一个链接到您获得该代码的位置?它似乎来自以前的版本。
-
还请澄清“长度为 3 的子图”的含义。我能想到你所说的几件事。
-
在 networkx 的旧版本中,
sg.nodes()和sg.edges()会生成节点和边列表,但现在它们会返回更高效的节点和边生成器。
标签: python iteration networkx subgraph