微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在python中生成幂律度分布

如何解决在python中生成幂律度分布

n 为网络大小,t 为幂律指数。目标是生成具有 G 指定的幂律度分布的 n 个顶点的随机t

有几个现有的答案:(1) answer 1 和 (2) answer 2,它们都使用 random.paretovariate() 函数。但是,这并不能保证结果序列是有效的度数序列。

我还检查了 networkx 并没有找到任何产生幂律度数序列的函数。有没有办法在 Python 中做到这一点?

解决方法

您可以使用 networkx 执行此操作。 is_graphical 函数允许您确定 powerlaw_sequence 函数的结果是否是有效的度数序列。一旦我们生成了一个有效的序列,我们就可以从中创建一个随机图。


    from networkx.generators.degree_seq import random_degree_sequence_graph
    from networkx.algorithms.graphical import is_graphical
    from networkx.utils.random_sequence import powerlaw_sequence
    
    
    n,t = 10,2
    while True:  # Continue generating sequences until one of them is graphical
        seq = sorted([int(round(d)) for d in powerlaw_sequence(n,t)],reverse=True)  # Round to nearest integer to obtain DISCRETE degree sequence
        if is_graphical(seq):
            break
    G = random_degree_sequence_graph(seq,tries=100)  # Adjust number of tries as you see fit
    print(sorted(d for _,d in G.degree()))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。