2017-11-26 12 views
1

内のノードのリストを締約:は私がノードを持つ辞書を持っているnetworkx

supernodes = list(nx.connected_components(G1)) 

print(supernodes)の結果は次のとおりです。

[{1, 2, 3, 5}, {8, 6}, {7, 9, 10, 12, 13}, {4}, {11}, {14}, {15}] 

にはどうすればいいのノードに各リストをマージすることができますか?私はこの機能を見つけましたnx.contracted_nodes(G, (1, 3))しかし、私は{1,2,3,5}, {8,6}などを入れて、7つの契約済みノードを作成できますか?

+0

出力をどのようにしたいですか?契約されたノードをどのように表現するかははっきりしていません。 – Imran

+0

@Imran出力は7ノードのグラフになりたい。 '{1,2,3,5} 'のための1つのノード、{8,6}のための1つのノード、' {7,9,10,12,13} 'のためのもの、' {4} 'のためのものなど。 –

答えて

2

あなたはこれを試すことができます。

import networkx as nx 
# Preamble, define G1 

supernodes = list(nx.connected_components(G1)) 
# contract nodes 
for supernode in supernodes: 
    nodes = sorted(list(supernode)) 
    for node in nodes[1:]: 
     G1 = nx.contracted_nodes(G1, nodes[0], node) 

G1のすべてのノードxが小さい要素としてのxを持つスーパーノードに対応しています。自己ループを削除する場合は、代わりにnx.contracted_nodes(G1, nodes[0], node, self_loops=False)と記述してください。

+1

コードは 'supernodes = list(nx.connected_components(G1))'なしでうまくいくでしょう。nx.connected_components(G1)のスーパーノードでは ' – Joel

関連する問題