Lee*_*aan 4 python algorithm graph networkx python-3.x
我有一个带有节点的字典:
supernodes = list(nx.connected_components(G1))
Run Code Online (Sandbox Code Playgroud)
结果print(supernodes)是:
[{1, 2, 3, 5}, {8, 6}, {7, 9, 10, 12, 13}, {4}, {11}, {14}, {15}]
Run Code Online (Sandbox Code Playgroud)
如何将每个列表合并到一个节点?我找到了这个函数,nx.contracted_nodes(G, (1, 3))但是我如何放置{1,2,3,5}, {8,6}等并创建 7 个签约节点?
你可以试试这个:
import networkx as nx
# Preamble, define G1
# contract nodes
for supernode in nx.connected_components(G1):
nodes = sorted(list(supernode))
for node in nodes[1:]:
G1 = nx.contracted_nodes(G1, nodes[0], node)
Run Code Online (Sandbox Code Playgroud)
每个节点 x inG1对应于具有 x 作为较小元素的超节点。如果要删除自循环,请改写nx.contracted_nodes(G1, nodes[0], node, self_loops=False).
| 归档时间: |
|
| 查看次数: |
2181 次 |
| 最近记录: |