Python:为无标度网络实现一个逐步改进的Barabasi-Albert模型
在Python中,我需要创建一个指数网络,它不同于指数随机图 ,并源自用于创建无标度网络的Barabasi-Albert模型的微小变化。这种新算法仍然使用增长和优先依恋,但其方式如下:Python:为无标度网络实现一个逐步改进的Barabasi-Albert模型,python,algorithm,networkx,Python,Algorithm,Networkx,在Python中,我需要创建一个指数网络,它不同于指数随机图 ,并源自用于创建无标度网络的Barabasi-Albert模型的微小变化。这种新算法仍然使用增长和优先依恋,但其方式如下: 网络从初始节点数n增长到最终节点数n 新节点不会连接到连接度最高的节点,而是连接到具有最高平均度的邻居的节点 因此,现在驱动连接的不是现有节点的程度,而是其邻域的平均程度。这意味着生成Barabasi-Albert模型的算法需要修改,这是我的目标 我想编写一个代码,以简单的一步一步的方式实现这一点,使用嵌套f
- 网络从初始节点数n增长到最终节点数n李>
- 新节点不会连接到连接度最高的节点,而是连接到具有最高平均度的邻居的节点
n=100 #Final number of nodes
ncols = 10 #Number of columns of a 10x10 grid
pos = {i : (i // ncols, (n-i-1) % ncols) for i in G.nodes()} #G=graph
我的问题:我可以通过访问函数的源代码来做到这一点,但我不知道哪个是生长阶段,哪个是优先连接阶段,以及每个节点的度是在哪里计算的如果有人能给我指出正确的方向,我会很高兴。
nx.barabasi\u albert\u graph()函数的源代码:
def barabasi_albert_graph(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=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
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)
source += 1
return G
def barabasi\u albert\u图(n,m,seed=None):
如果m<1或m>=n:
提升nx.NetworkXError(\
“Barabási Albert网络必须具有m>=1和m我已经实现了一个,并且我认为该实现对于优先连接标准和节点位置是容易调整的
节点位置
您需要查看第39行(用于起始节点)和第69行(用于新添加的节点)中节点位置(我随机选择)的animate_BA
函数
成长阶段
至于增长阶段,这是在一个单独的函数中实现的,该函数调用该函数将一个新节点插入到图中。我的实现以概率选择要连接的节点:(deg(i)+1)/Sum(deg(i)+1)
,其中i
是图中的一个节点,而deg(i)
是该节点的阶数,和(阶数(i)+1)
是图+1中所有节点的度数总和。这是通过创建一个从0到1的浮点列表来实现的,该列表指定每个节点根据其度数选择的概率。您可以将其调整为邻居的平均度数。因此,您必须创建此列表,但不同,因为此函数调用选择_neights
函数,根据定义的概率随机选择邻居
其他功能主要与动画相关,因此您可能不需要查看它们。代码有文档记录,您可以在那里找到进一步的解释