我必須在網絡增長的某些步驟(即在N = 100,N = 1000,N = 10000等)測量Barabasi Albert圖的某些屬性,如度分佈。 )。我知道如何使用networkx生成這樣一個圖表,但我真的不清楚如何在增長過程中訪問這些屬性。在中間時間步測量網絡屬性
沒有代碼可以顯示你,我需要一個提示算法本身,一些例子將不勝感激。我使用Python 2.7,但如果有必要,我也很喜歡R。
我必須在網絡增長的某些步驟(即在N = 100,N = 1000,N = 10000等)測量Barabasi Albert圖的某些屬性,如度分佈。 )。我知道如何使用networkx生成這樣一個圖表,但我真的不清楚如何在增長過程中訪問這些屬性。在中間時間步測量網絡屬性
沒有代碼可以顯示你,我需要一個提示算法本身,一些例子將不勝感激。我使用Python 2.7,但如果有必要,我也很喜歡R。
我認爲你正在尋找的功能是Barbabasi Albert Graph function在netwrokx。在研究上述函數on this link(基於networkx文檔)的源代碼時,步驟在註釋中給出。您可以在每個循環的while source<n:
循環中打印圖表的屬性,以獲得您所需的屬性。
基於@Mohammed卡希夫的回答,這裏是Barabasi阿爾伯特圖的經修改的源代碼,它返回鍵上的節點的數量和值的數組的度的詞典:
import random
import networkx as nx
def _random_subset(seq,m):
""" Return m unique elements from seq.
This differs from random.sample which can return repeated
elements if seq holds repeated elements.
"""
targets=set()
while len(targets)<m:
x=random.choice(seq)
targets.add(x)
return targets
def barabasi_albert_graph_modified(n, m, seed=None):
if m < 1 or m >=n:
raise nx.NetworkXError(\
"Barabási-Albert network must have m>=1 and m<n, m=%d,n=%d"%(m,n))
if seed is not None:
random.seed(seed)
# Add m initial nodes (m0 in barabasi-speak)
G=nx.empty_graph(m)
G.name="barabasi_albert_graph(%s,%s)"%(n,m)
# Target nodes for new edges
targets=list(range(m))
# List of existing nodes, with nodes repeated once for each adjacent edge
repeated_nodes=[]
# Start adding the other n-m nodes. The first node is m.
source=m
d = {}
while source<n:
# Add edges to m nodes from the source.
G.add_edges_from(zip([source]*m,targets))
# Add one node to the list for each new edge just created.
repeated_nodes.extend(targets)
# And the new node "source" has m edges to add to the list.
repeated_nodes.extend([source]*m)
# Now choose m unique nodes from the existing nodes
# Pick uniformly from repeated_nodes (preferential attachement)
targets = _random_subset(repeated_nodes,m)
deg = np.array(list(G.degree().values()))
deg.sort()
d[G.number_of_nodes()] = deg
source += 1
return G,d
讓我們嘗試它:
g,d = barabasi_albert_graph_modified(10,2)
#out:
#{3: array([1, 1, 2]),
# 4: array([1, 2, 2, 3]),
# 5: array([1, 2, 2, 3, 4]),
# 6: array([1, 2, 2, 2, 4, 5]),
# 7: array([1, 2, 2, 2, 3, 4, 6]),
# 8: array([2, 2, 2, 2, 2, 3, 4, 7]),
# 9: array([2, 2, 2, 2, 2, 2, 4, 5, 7]),
# 10: array([2, 2, 2, 2, 2, 2, 3, 4, 5, 8])}
如果這是作業的一部分,我懷疑教師指定中間細節需求的部分原因是強制您自己編寫算法,而不是僅使用內置命令。 – Joel