我想生成一个具有小世界特性(呈幂律分布)的随机图。我刚开始使用networkx包,并发现它提供了各种随机图生成方法。请问是否可以生成一个给定节点度数遵循伽马分布的图形(无论是在R中还是使用Python的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)
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()
我之前用基本的Python完成了这个...如果我没记错,我使用了以下方法。我是凭记忆写的,所以可能不完全准确,但希望对你有所帮助:
请注意,这通常不会创建一个连通的图。
networkx
提供了4种算法,可以将 degree_distribution 作为输入参数:
trials
参数,因为没有保证找到一个合适的配置。完整列表(包括一些有向图算法的版本)在这里。
我还找到了几篇论文:
z = [3,2,2,1]
以生成这些度数的简单图,但实际上我得到了一个度数为1 2 2 1
的图。 - Legendhttp://networkx.lanl.gov/examples/drawing/degree_histogram.html
,看起来你是它的作者(?)你能告诉我如何绘制类似源文件中显示的图形吗?再次感谢。 - Legend