即使我在Networkx

时间:2016-04-03 19:16:09

标签: python networkx

我正在尝试将一个节点添加到带有属性的空图中,我需要将其作为整数。但是,它似乎没有添加它,因为当我打印(G)它给我没有

这是我的代码:

def heirarchy_graph(n,e,l):
'''
n is number of nodes in graph,
e is the Expansion Rate. This is the number of people on each level
   If n/e has a remainder then the last level has that many
   people in it.
l is the Level connection. It is the probability that a person is connected to someone
  within the level they belong to.
'''
G = nx.Graph()
G.name="heirarchy_graph(%s,%s,%s)"%(n,e,l)

r = (n-1)%e
s = (n-r-1)/e
h = s + 1
G = empty_graph(n=0)

G = G.add_node(0, level=int(0))
print(G)
for i in range(s):
    list = range(1,(e+1))
    A = nx.Graph()
    #for item in list:
        #create e nodes with attribute level='i'
    A.add_nodes_from(list,level=int(i))

    # add edges between nodes with probability l
    names = A.nodes()

    for name in names:
        B = non_neighbors(A,name)
        for u in B:
            q = random.uniform(0,1)
            if q <= l:
                A.add_edge(u,name)
    return A
    print(A)
    G = nx.disjoint_union(G,A)


if r != 0: 
    h = s+1
    list = range(1,(r+1))
    A = nx.Graph()
    #create e nodes with attribute level='i'
    A.add_nodes_from(list,level=int(h))
    # add edges between nodes with probability l
    names = A.nodes()
    for name in names:
        B = non_neighbors(A,name)
        for u in B:
            q = random.uniform(0,1)
            if q <= l:
                A.add_edge(u,name)
    return A

    G = nx.disjoint_union(G,A)

return G 

打印(G)的结果为无

非常感谢任何帮助。提前致谢。

1 个答案:

答案 0 :(得分:0)

方法add_node是有状态的,并直接修改图G。 由于add_node不会返回任何内容,因此当您尝试分配到G时,GNone

尝试:

G.add_node(0, level=int(0))