繁体   English   中英

即使我在Networkx中添加了一个节点也获得了一个空图

[英]Getting an empty graph even though I add a node in 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)为无

任何帮助将不胜感激。 提前致谢。

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

尝试:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM