2016-04-03 6 views
0

属性を持つ空のグラフにノードを追加しようとしていますが、整数にする必要があります。私は(G)を印刷するとき、それはここで私にどれNetworkxにノードを追加しても空のグラフを取得する

を与えないので、しかし、それはそれを追加していないように私のコードです:

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)の結果はなし

です

ご協力いただければ幸いです。前もって感謝します。

+0

私はあなたが同様に他の問題を持っていると思います。あなたの関数がこの行に到達した瞬間、この関数ではそれを呼び出すために 'A'を返し、それ以上計算を停止します。したがって、最初のループで初めて実行されます。それ以上は得られません。 – Joel

+0

別の問題:これは単なる一時的なものだと思うが、今は確率1でエッジを追加する。乱数生成をスキップし、エッジを追加するだけでテストすることができる。 – Joel

+0

後でもっと小さな確率でエッジを追加しようとするなら、 'nx.fast_gnp_random_graph'を見てください。 – Joel

答えて

0

メソッドadd_nodeはステートフルであり、グラフGを直接変更します。 add_nodeは何も返しません。Gに割り当てようとすると、GNoneになります。

試してみてください。あなたはA`を返す `持っている:

G.add_node(0, level=int(0)) 
関連する問題