对一个 Networkx 图进行节点排序

3
我有以下图表,可以删除和添加节点、边。
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]

我想知道如何创建一个排序图表。
2个回答

4
一个简单的解决方法是将节点复制到一个新图中:
H = nx.Graph()
H.add_nodes_from(sorted(G.nodes(data=True)))
H.add_edges_from(G.edges(data=True))

我们能不能只使用sorted(G.nodes)而不将节点复制到新图中? - undefined

1
通过使用最大连通组件中的节点构建子图,您正在删除第二个节点:
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]}}

因此,该节点作为新的字典 键/值 对被附加。
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))

如果 G.nodes 返回的是字符串节点值而不是整数,会怎么样? - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接