【问题标题】:k-regular small world network with igraph带有 igraph 的 k 正则小世界网络
【发布时间】: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

【问题讨论】:

    标签: python igraph


    【解决方案1】:

    “小世界”不是一个严格定义的量。所以从某种意义上说,你的问题是无法回答的。话虽如此,平均距离与重新布线概率的关系图通常可以很好地说明导致平均距离较短的图表的最小概率是多少。使用p=0.05,与常规图相比,您肯定处于平均距离非常小的状态(~5 对~160 跳),并且增加重新布线的概率会产生强烈的收益递减。

    import numpy as np
    import matplotlib.pyplot as plt
    import igraph
    
    rewiring_probability = np.logspace(-3., -1., 10.)
    closeness_centrality = np.zeros_like(rewiring_probability)
    g = igraph.Graph.Lattice([1,5000], nei=8, directed=False, mutual=True, circular=True)
    E = g.ecount()
    
    for ii, p in enumerate(rewiring_probability):
        print("Iteration {}, p = {}".format(ii, p))
        h = g.copy() # creates a deepcopy
        n = int(p * E)
        h.rewire(n=n, mode="simple")
        closeness_centrality[ii] = np.mean(h.closeness())
    
    fig, ax = plt.subplots(1,1)
    ax.plot(rewiring_probability, 1./closeness_centrality)
    ax.set_xlabel('Rewiring probability')
    ax.set_ylabel('Mean distance')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2022-07-12
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 2013-03-17
      • 2014-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多