Python networkx 遍历子图列表
Python networkx iterating through list of subgraphs
我在通过大型 nx 图 G 的所有可能子图获取迭代时遇到了一些麻烦,该图作为文本文件的输入。
以下是我在 Whosebug 上找到的一些代码,但没有产生我正在寻找的代码:
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 或更大的子图,然后将它们作为图执行某些功能。
谢谢!
我写这篇文章的前提是 "length 3" 意味着 "at least 3 nodes"。
import networkx as nx
g = read_graph(sys.argv[1])
for subgraph in nx.connected_component_subgraphs(g):
if subgraph.number_of_nodes()>2:
#your code here
我在通过大型 nx 图 G 的所有可能子图获取迭代时遇到了一些麻烦,该图作为文本文件的输入。
以下是我在 Whosebug 上找到的一些代码,但没有产生我正在寻找的代码:
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 或更大的子图,然后将它们作为图执行某些功能。
谢谢!
我写这篇文章的前提是 "length 3" 意味着 "at least 3 nodes"。
import networkx as nx
g = read_graph(sys.argv[1])
for subgraph in nx.connected_component_subgraphs(g):
if subgraph.number_of_nodes()>2:
#your code here