2017-11-26 10 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} '에 대한 하나의 노드, {8, 6}에 대한 하나의 노드,'{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