在Python中,我需要创建一个指数网络,它不同于指数随机图。
2005年,刘唐推出了指数网络。,并起源于一个轻微的变化巴拉巴西-阿尔伯特模型,用来创建无标度网络.这个新算法仍然使用增长和优先附加,但其方式是:
因此,现在驱动附件的不是现有节点的程度,而是节点邻域的平均程度。这意味着生成Barabasi-Albert模型的算法需要修改,这就是我的目标。
我想用简单的一步一步的方式编写一个代码,使用嵌套的for循环来模拟生长和优先依附。另外,我希望为节点分配特定的位置,如下所示:
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
My的问题:可以通过访问图()函数的源代码来实现这一点,但是我不知道哪个是增长阶段,哪个是优先附加阶段,以及每个节点的程度是在哪里计算的。,如果有人能给我指路,我会很高兴的。
nx.barabasi_albert_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
发布于 2016-06-27 19:50:13
我已经实现了一个Barabasi-Albert图增长动画,并且我认为该实现对于优先附加条件和节点位置来说是很容易调整的。
节点定位
您需要查看
animate_BA
函数的节点位置(正如我随机选择的)在第39行(用于启动节点)和第69行(用于新添加的节点)。
生长阶段
至于增长阶段,这是在一个单独的函数
choose_neighbors
中实现的,该函数调用该函数将一个新节点插入到图中。我的实现选择了一个概率连接的节点:(deg(i)+1)/Sum(deg(i)+1)
,其中i
是图中的一个节点,deg(i)
是该节点的程度,Sum(deg(i)+1)
是图+ 1中所有节点的度之和。这是通过创建一个从0到1的浮点数列表,指定每个节点根据其degree.You选择的概率来实现的。因此,您必须创建这个列表,但不同的是,这个函数调用select_neighbors
函数来根据定义的概率随机选择邻居。
其他功能主要与动画相关,因此您可能不需要查看它们。代码有文档化,您可以在那里找到进一步的解释。
https://stackoverflow.com/questions/38008748
复制相似问题