我正在尝试生成一个具有小世界属性的随机图(显示幂律分布)。我刚开始使用networkx软件包,发现它提供了各种随机图生成。有人能告诉我是否有可能生成一个图形,其中给定节点的度数遵循伽马分布(在R中或使用python的networkx包)?
答案 0 :(得分:7)
如果你想使用配置模型,这样的东西应该在NetworkX中起作用:
import random
import networkx as nx
z=[int(random.gammavariate(alpha=9.0,beta=2.0)) for i in range(100)]
G=nx.configuration_model(z)
您可能需要根据伽马分布中的参数调整序列z的平均值。 z也不需要是图形的(你会得到一个多图),但它确实需要一个偶数,所以你可能需要尝试一些随机序列(或加1)......
关于configuration_model的NetworkX文档说明给出了另一个示例,一个参考以及如何删除并行边和自循环:
Notes
-----
As described by Newman [1]_.
A non-graphical degree sequence (not realizable by some simple
graph) is allowed since this function returns graphs with self
loops and parallel edges. An exception is raised if the degree
sequence does not have an even sum.
This configuration model construction process can lead to
duplicate edges and loops. You can remove the self-loops and
parallel edges (see below) which will likely result in a graph
that doesn't have the exact degree sequence specified. This
"finite-size effect" decreases as the size of the graph increases.
References
----------
.. [1] M.E.J. Newman, "The structure and function
of complex networks", SIAM REVIEW 45-2, pp 167-256, 2003.
Examples
--------
>>> from networkx.utils import powerlaw_sequence
>>> z=nx.create_degree_sequence(100,powerlaw_sequence)
>>> G=nx.configuration_model(z)
To remove parallel edges:
>>> G=nx.Graph(G)
To remove self loops:
>>> G.remove_edges_from(G.selfloop_edges())
这是一个类似于http://networkx.lanl.gov/examples/drawing/degree_histogram.html的示例,它使图形包含最大连通分量的图形布局:
#!/usr/bin/env python
import random
import matplotlib.pyplot as plt
import networkx as nx
def seq(n):
return [random.gammavariate(alpha=2.0,beta=1.0) for i in range(100)]
z=nx.create_degree_sequence(100,seq)
nx.is_valid_degree_sequence(z)
G=nx.configuration_model(z) # configuration model
degree_sequence=sorted(nx.degree(G).values(),reverse=True) # degree sequence
print "Degree sequence", degree_sequence
dmax=max(degree_sequence)
plt.hist(degree_sequence,bins=dmax)
plt.title("Degree histogram")
plt.ylabel("count")
plt.xlabel("degree")
# draw graph in inset
plt.axes([0.45,0.45,0.45,0.45])
Gcc=nx.connected_component_subgraphs(G)[0]
pos=nx.spring_layout(Gcc)
plt.axis('off')
nx.draw_networkx_nodes(Gcc,pos,node_size=20)
nx.draw_networkx_edges(Gcc,pos,alpha=0.4)
plt.savefig("degree_histogram.png")
plt.show()
答案 1 :(得分:2)
我之前在基础Python中做过这个... IIRC,我使用了以下方法。从记忆中,所以这可能不完全准确,但希望它是值得的:
N.B。这通常不会创建连接图。
答案 2 :(得分:1)
我知道这已经很晚了,但你可以用mathematica做同样的事情,虽然有点简单。
RandomGraph [DegreeGraphDistribution [{3,3,3,3,3,3,3,3}],4]
这将生成4个随机图,每个节点具有规定的度数。
答案 3 :(得分:0)
包括上面提到的,networkx
提供了4个接收degree_distribution作为输入的算法:
trials
参数,因为无法保证找到合适的配置。 完整列表(包括有向图的算法的某些版本)是here。
我还发现了几篇论文: