【发布时间】:2018-03-21 15:33:50
【问题描述】:
我正在使用 igraph 使用 K_Regular() 函数生成节点具有相同度数的随机网络。我想知道是否有一个类似的功能来拥有一个节点共享相同程度的小世界网络。
我编写了自己的函数,在
我首先生成一个规则格子,然后以小概率重新连接边缘:
g = igraph.Graph.Lattice([1,5000], nei=8, directed=False, mutual=True, circular=True)
for e in g.es:
# if we randomize this link
if random.uniform(0,1) < 0.05:
# pick the nodes
end1 = e.tuple[0]
# pool
pool = [n for n in range(0, g.vcount())]
#
end2 = random.choice([i for i in pool if i != end1 and i not in g.neighbors(end1)])
# create link end1-end2
if end1 < end2:
g.add_edge(end1, end2)
else:
g.add_edge(end2, end1)
# rewire the other end of this link
end3 = e.tuple[1]
#
end4 = random.choice([i for i in pool if i != end3\
and i in g.neighbors(end2)\
and i not in g.neighbors(end3)])
# create link end3-end4
if end3 < end4:
g.add_edge(end3, end4)
else:
g.add_edge(end4, end3)
# delete old edge
g.delete_edges((e))
g.delete_edges((end2, end4))
我看到了this,但老实说我不明白如何指定重新布线的概率......
所以我猜它可能是:
g = igraph.Graph.Lattice([1,5000], nei=8, directed=False, mutual=True, circular=True)
g.rewire(n=int(g.ecount()*0.05), mode="simple") # say you want a 0.05 prob
【问题讨论】: