在python中使用networkx创建树数据

时间:2012-10-08 07:59:05

标签: python python-2.7 networkx

我正在尝试创建一个具有1111个节点的树,并且这样组织起来 节点1有10个子节点(2到11),节点2有10个子节点(12到21),依此类推...每个节点有10个子节点,根级别有1个节点,有10个子节点,每个子节点有10个孩子,这100个节点中的每一个都有10个子节点,每个子节点有1000个叶子节点。节点总数为1111。

import networkx as nx
G = nx.Graph()
L1 = [1]
L2 = [x for x in range(2,12)]
L3 = [x for x in range(12,112)]
L4 = [x for x in range(112,1112)]

G.add_node(1)
G.add_nodes_from(L1)
G.add_nodes_from(L2)
G.add_nodes_from(L3)
G.add_nodes_from(L4)

现在我想使用G.add_edges_from([(x,y) for x in L1 for y in L2])添加边缘,这对于第一级是可以的,但我该如何为其他级别执行此操作?

3 个答案:

答案 0 :(得分:6)

您可以在一行中“开箱即用”地获得所需的结果:

import networkx as nx

G = nx.balanced_tree(10,10)

答案 1 :(得分:4)

您可以将图表的创建概括为任意深度以及每个内部节点的任意数量的子项

如果从0开始编号级别 - 即根节点表示级别0 - 则每个级别包含n 级别节点。
n是每个内部节点上的子节点数。

因此,您可以识别每个级别上第一个和最后一个节点的索引 例如,在n = 10的情况下,级别0,1,2,3的最后节点是
10 0 = 1,
10 0 + 10 1 = 11,
10 0 + 10 1 + 10 2 = 111,
10 0 + 10 1 + 10 2 + 10 3 = 1111。

要查找给定节点的子节点索引,请获取该级别上最后一个节点的索引并添加偏移量。
如果给定节点是该级别上的第一个(最左侧)节点,则偏移量为0 如果它是该级别的最后一个节点,则偏移量为(n level - 1)* n 然后(n level - 1)是该级别上的前任节点数 通常,偏移量计算为[该级别上的前趋节点数] * n。

这个概念在代码中被涵盖为offset = ulim + i * n + 1 我已添加1,以便能够从0 - (n-1)而不是1 - n循环播放以下内容。

import networkx as nx

n = 10  # the number of children for each node 
depth = 3 # number of levels, starting from 0

G = nx.Graph()
G.add_node(1) # initialize root

ulim = 0
for level in range(depth): # loop over each level
  nl = n**level # number of nodes on a given level
  llim = ulim + 1 # index of first node on a given level 
  ulim = ulim + nl # index of last node on a given level
  for i in range(nl): # loop over nodes (parents) on a given level
    parent = llim + i
    offset = ulim + i * n + 1 # index pointing to node just before first child
    for j in range(n): # loop over children for a given node (parent)
      child = offset + j
      G.add_node(child)
      G.add_edge(parent, child)

      # show the results
      print '{:d}-{:d}'.format(parent, child),
    print ''
  print '---------'

对于depth = 3n = 3,这是输出:

1-2 1-3 1-4 
---------
2-5 2-6 2-7 
3-8 3-9 3-10 
4-11 4-12 4-13 
---------
5-14 5-15 5-16 
6-17 6-18 6-19 
7-20 7-21 7-22 
8-23 8-24 8-25 
9-26 9-27 9-28 
10-29 10-30 10-31 
11-32 11-33 11-34 
12-35 12-36 12-37 
13-38 13-39 13-40 
---------

答案 2 :(得分:1)

找出答案

import networkx as nx
G = nx.Graph()
L1 = [1]
L2 = [x for x in range(2,12)]
L3 = [x for x in range(12,112)]
L4 = [x for x in range(112,1112)]


G.add_node(1)
G.add_nodes_from(L1)
G.add_nodes_from(L2)
G.add_nodes_from(L3)
G.add_nodes_from(L4)
G.add_edges_from([(x,y) for x in L1 for y in L2])
temp2 = []
temp = []
temp2.extend(L4)
temp.extend(L3)
for i in range(1,11,1):
    G.add_edges_from([x,temp.pop()] for x in L2)
    G.add_edges_from([y,temp2.pop()] for y in L3)

print G.nodes()
print G.edges()
print G.number_of_nodes()
print G.number_of_edges()
print G.neighbors(1)

try:
    diameter_of_myGraph =nx.shortest_path_length(G)
    #print diameter_of_myGraph
except nx.NetworkXNoPath:
    print 'No path'

try:
    avg_distance_of_myGraph =nx.average_shortest_path_length(G)
    print avg_distance_of_myGraph
except nx.NetworkXNoPath:
    print 'No path'