如何使 Networkx 生成具有排序节点的 GML 文件

How to make Networkx produce GML file with sorted nodes

我有以下代码:

#!/usr/bin/env python
import networkx as nx
def main():
    """docstring for main"""
    outerdict = {"A":["a1","a2","a3"], "B":["b1","b2","b3","b5","b6", "b7"], "C":["c2","c3"], "D":["d1","d2","d3"]}
    keynode = "Z"

    colorlist = [ "#beaed4", "#fdc086", "#ffff99", "#386cb0","#f0027f"]
    G = nx.Graph()
    G.add_node(keynode,graphics={"fill":"#7fc97f","w":27,"h":27, "d":27})
    for i,ky in enumerate(outerdict):
        nodes = outerdict[ky]
        nodecol = colorlist[i]
        print ky, nodecol
        for node in nodes:
            G.add_node(node,graphics={"fill":nodecol,"w":27,"h":27,"d":27})
            G.add_edge(keynode, node, value = 10)

    outfile = "test.gml"
    nx.write_gml(G,outfile)


if __name__ == '__main__':
    main()

它生成以下 GML 文件:

graph [
  node [
    id 0
    label "b6"
    graphics [ 
      h 27
      d 27
      w 27
      fill "#ffff99"
    ]
  ]
  node [
    id 1
    label "a1"
    graphics [ 
      h 27
      d 27
      w 27
      fill "#beaed4"
    ]
  ]
  node [
    id 2
    label "b5"
    graphics [ 
      h 27
      d 27
      w 27
      fill "#ffff99"
    ]
  ]
  node [
    id 3
    label "a3"
    graphics [ 
      h 27
      d 27
      w 27
      fill "#beaed4"
    ]
  ]
  node [
    id 4
    label "a2"
    graphics [ 
      h 27
      d 27
      w 27
      fill "#beaed4"
    ]
  ]
  node [
    id 5
    label "b7"
    graphics [ 
      h 27
      d 27
      w 27
      fill "#ffff99"
    ]
  ]
  node [
    id 6
    label "b1"
    graphics [ 
      h 27
      d 27
      w 27
      fill "#ffff99"
    ]
  ]
  node [
    id 7
    label "b2"
    graphics [ 
      h 27
      d 27
      w 27
      fill "#ffff99"
    ]
  ]
  node [
    id 8
    label "b3"
    graphics [ 
      h 27
      d 27
      w 27
      fill "#ffff99"
    ]
  ]
  node [
    id 9
    label "c3"
    graphics [ 
      h 27
      d 27
      w 27
      fill "#fdc086"
    ]
  ]
  node [
    id 10
    label "c2"
    graphics [ 
      h 27
      d 27
      w 27
      fill "#fdc086"
    ]
  ]
  node [
    id 11
    label "Z"
    graphics [ 
      h 27
      d 27
      w 27
      fill "#7fc97f"
    ]
  ]
  node [
    id 12
    label "d2"
    graphics [ 
      h 27
      d 27
      w 27
      fill "#386cb0"
    ]
  ]
  node [
    id 13
    label "d3"
    graphics [ 
      h 27
      d 27
      w 27
      fill "#386cb0"
    ]
  ]
  node [
    id 14
    label "d1"
    graphics [ 
      h 27
      d 27
      w 27
      fill "#386cb0"
    ]
  ]
  edge [
    source 0
    target 11
    value 10
  ]
  edge [
    source 1
    target 11
    value 10
  ]
  edge [
    source 2
    target 11
    value 10
  ]
  edge [
    source 3
    target 11
    value 10
  ]
  edge [
    source 4
    target 11
    value 10
  ]
  edge [
    source 5
    target 11
    value 10
  ]
  edge [
    source 6
    target 11
    value 10
  ]
  edge [
    source 7
    target 11
    value 10
  ]
  edge [
    source 8
    target 11
    value 10
  ]
  edge [
    source 9
    target 11
    value 10
  ]
  edge [
    source 10
    target 11
    value 10
  ]
  edge [
    source 11
    target 12
    value 10
  ]
  edge [
    source 11
    target 13
    value 10
  ]
  edge [
    source 11
    target 14
    value 10
  ]
]

注意节点没有按顺序排列。例如 a1 介于 b1b5 之间。这使得情节看起来像这样:

我期望图形是根据循环和初始数据结构按顺序排列的 outderdict。这样黄色节点将与其他黄色节点依次排列在一起。

如何使用 Networkx 对 GML 文件进行排序?欢迎其他解决方案(例如 Igraph),只要在 Python 框架内。

NetworkX 自 2015 年 1 月 1 日起提供有序图数据结构。OrderedGraph class 将按照添加顺序从 NetworkX 数据结构中输出节点和边。
您需要在 https://github.com/networkx/networkx/ 获取最新的开发版本,才能进行以下操作。

import networkx as nx
outerdict = {"A":["a1","a2","a3"], "B":["b1","b2","b3","b5","b6", "b7"], "C":["c2","c3"], "D":["d1","d2","d3"]}
keynode = "Z"
colorlist = [ "#beaed4", "#fdc086", "#ffff99", "#386cb0","#f0027f"]
G = nx.OrderedGraph()
G.add_node(keynode,graphics={"fill":"#7fc97f","w":27,"h":27, "d":27})
for i,ky in enumerate(outerdict):
    nodes = outerdict[ky]
    nodecol = colorlist[i]
    print ky, nodecol
    for node in nodes:
        G.add_node(node,graphics={"fill":nodecol,"w":27,"h":27,"d":27})
        G.add_edge(keynode, node, value = 10)
outfile = "test.gml"
nx.write_gml(G,outfile)

请注意,您的 'outerdict' 数据结构出现的顺序可能与您循环遍历 'enumerate(outerdict)' 时初始化它的顺序不同。如果节点 "A"、"B"、"C" 和 "D" 的顺序对您很重要,那么您将需要更改将它们添加到图表中的方式以确保它们得到按此顺序插入。