【问题标题】:Sorting the nodes of a Networkx graph对 Networkx 图的节点进行排序
【发布时间】:2020-07-25 02:54:18
【问题描述】:

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

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]

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

【问题讨论】:

    标签: python-3.x sorting graph networkx


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      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))
      

      【讨论】:

        猜你喜欢
        • 2018-01-16
        • 1970-01-01
        • 2017-03-29
        • 1970-01-01
        • 2019-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-28
        相关资源
        最近更新 更多