【问题标题】:Measuring network properties at intermediate time steps在中间时间步测量网络属性
【发布时间】:2018-02-20 17:25:29
【问题描述】:

我必须在网络增长的某些步骤(即 N=100、N=1000、N=10000 等)测量 Barabasi Albert 图的某些属性,例如度数分布。我知道如何使用 networkx 生成这样的图表,但我真的不清楚如何在增长过程中访问这些属性。

没有代码给你看,我需要算法本身的提示,一些例子将不胜感激。我使用 Python 2.7,但如有必要,我也对 R 感到满意。

【问题讨论】:

  • 如果这是作业的一部分,我怀疑讲师分配中间细节要求的部分原因是强迫您自己编写算法,而不仅仅是使用内置命令.

标签: r python-2.7 graph-theory networkx


【解决方案1】:

基于@Mohammed Kashif 的回答,这里是 Barabasi Albert 图的修改后的源代码,它返回一个以节点数和值数组为键的字典:

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])}

【讨论】:

    【解决方案2】:

    我认为您正在寻找的功能是Barbabasi Albert Graph function in netwrokx。在查看上述函数on this link(基于networkx 文档)的源代码时,cmets 中给出了这些步骤。您可以在while source&lt;n: 循环的每次迭代中打印Graph 的属性,以获得您想要的属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2010-09-10
      相关资源
      最近更新 更多