Python networkx在子图列表中迭代
我在获得一个大型nx图G的所有可能子图的迭代时遇到了一点麻烦,作为文本文件的输入 以下是我在StackOverflow上找到的一些代码,但没有得到我想要的:Python networkx在子图列表中迭代,python,iteration,networkx,subgraph,Python,Iteration,Networkx,Subgraph,我在获得一个大型nx图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 = 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意味着至少3个节点
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
你能提供一个链接到你从哪里得到的代码吗?它似乎来自以前的版本。还请澄清长度为3的子图的含义。我能想到你所说的几件事。在networkx的旧版本中,sg.nodes和sg.edges本来可以生成节点和边的列表,但现在它们返回的节点和边生成器效率更高。