对 Networkx 图的节点进行排序

Sorting the nodes of a Networkx graph

我有以下图表,我删除并添加了节点、边。

import networkx as nx
import matplotlib.pyplot as plt

G = nx.gnm_random_graph(n=10, m=15, seed=1)
pos = nx.spring_layout(G)
print(pos)
nx.set_node_attributes(G, pos, 'pos')
G.remove_edge(2, 9)
largest_cc = max(nx.connected_components(G), key=len)
G = G.subgraph(largest_cc).copy()
G.add_node(2, pos=pos[2])
G.add_edge(2, 8)
print(G.edges)
print(nx.get_node_attributes(G, 'pos'))
print(G.nodes)

最终输出没有排序,下面是节点(属性也没有排序)的编号

print(G.nodes)
[0, 1, 3, 4, 5, 6, 7, 8, 9, 2]

预期输出:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

我想知道如何创建排序图。

一个简单的解决方法是将节点复制到一个新图中:

H = nx.Graph()
H.add_nodes_from(sorted(G.nodes(data=True)))
H.add_edges_from(G.edges(data=True))

通过使用最大连通分量中的节点构建子图,您将删除第二个节点:

G = nx.gnm_random_graph(n=10, m=15, seed=1)
pos = nx.spring_layout(G)
nx.set_node_attributes(G, pos, 'pos')

G.remove_edge(2, 9)
largest_cc = max(nx.connected_components(G), key=len)
G.nodes()
# NodeView((0, 1, 2, 3, 4, 5, 6, 7, 8, 9))

G = G.subgraph(largest_cc).copy()
G.nodes()
# NodeView((0, 1, 3, 4, 5, 6, 7, 8, 9))

现在再次添加节点 2G.add_node(2, pos=pos[2]),这实际上是在更新节点数据字典(保存它的内部数据结构),字面意思是 dict.update

d = dict(G.nodes(data=True))
d.update({2:{'pos':[0.3, 0.5]}})
print(d)
{0: {'pos': array([ 0.33041585, -0.07185971])},
 1: {'pos': array([-0.19659528,  0.33972794])},
 3: {'pos': array([ 0.22691433, -0.1802301 ])},
 4: {'pos': array([0.22462413, 0.2452357 ])},
 5: {'pos': array([ 0.65037774, -0.18057473])},
 6: {'pos': array([-0.14587125, -0.13225175])},
 7: {'pos': array([0.05279257, 0.10579408])},
 8: {'pos': array([ 0.42384353, -0.46262269])},
 9: {'pos': array([-0.56650162,  0.13495046])},
 2: {'pos': [0.3, 0.5]}}

因此节点被附加为新字典 key/value 对,

G.add_node(2, pos=pos[2])
G.add_edge(2, 8)

G.nodes()
# NodeView((0, 1, 3, 4, 5, 6, 7, 8, 9, 2))

Graph.add_nodes_from,但它仅在节点存在时才更新属性(不会删除并重新添加节点),这是有道理的:

G.add_nodes_from(sorted(G.nodes(data=True)))
G.nodes()
NodeView((0, 1, 3, 4, 5, 6, 7, 8, 9, 2))

所以要走的路是重新创建一个图,并分配排序的节点,如 warped 的回答:

H = nx.Graph()
H.add_nodes_from(sorted(G.nodes(data=True)))
H.add_edges_from(G.edges(data=True))

H.nodes()
# NodeView((0, 1, 2, 3, 4, 5, 6, 7, 8, 9))