簡體   English   中英

Python networkx遍歷子圖列表

[英]Python networkx iterating through list of subgraphs

我很難通過一個大的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)

    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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM