如何将幂律分布中的权重随机分配给具有大量节点的网络。

我写

import networkx as nx
import numpy as np
from networkx.utils import powerlaw_sequence

z=nx.utils.create_degree_sequence(200,nx.utils.powerlaw_sequence,exponent=1.9)
nx.is_valid_degree_sequence(z)
G=nx.configuration_model(z)
Gcc=nx.connected_component_subgraphs(G)[0]

edgelist=[nx.utils.powerlaw_sequence(nx.number_of_edges(Gcc),exponent=2.0)]

我知道我通过使用元组的字典(node1,node2,weight)为边缘分配权重:
nx.from_edgelist(edgelist,create_using=None)

但是,当我只想获得一个权重分布为幂律的加权网络时,还有另一种更短的方法吗?

最佳答案

例如,您可以直接使用G [u] [v] ['weight']分配权重

In [1]: import networkx as nx

In [2]: import random

In [3]: G = nx.path_graph(10)

In [4]: for u,v in G.edges():
   ...:     G[u][v]['weight'] = random.paretovariate(2)
   ...:
   ...:

In [5]: print G.edges(data=True)
[(0, 1, {'weight': 1.6988521989583232}), (1, 2, {'weight': 1.0749963615177736}), (2, 3, {'weight': 1.1503859779558812}), (3, 4, {'weight': 1.675436575683888}), (4, 5, {'weight': 1.1948608572552846}), (5, 6, {'weight': 1.080152340891444}), (6, 7, {'weight': 1.0296667672332183}), (7, 8, {'weight': 2.0014384064255446}), (8, 9, {'weight': 2.2691612212058447})]

我使用Python的random.paretovariate()选择权重,但是您当然可以在其中放置任何内容。

关于python - 具有随机幂律分布权重的网络,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8954177/

10-13 04:54