python - Generating a graph with certain degree distribution? -
i trying generate random graph has small-world properties (exhibits power law distribution). started using networkx package , discovered offers variety of random graph generation. can tell me if possible generate graph given node's degree follows gamma distribution (either in r or using python's networkx package)?
if want use configuration model should work in networkx:
import random import networkx nx z=[int(random.gammavariate(alpha=9.0,beta=2.0)) in range(100)] g=nx.configuration_model(z)
you might need adjust mean of sequence z depending on parameters in gamma distribution. z doesn't need graphical (you'll multigraph), need sum might have try few random sequences (or add 1)...
the networkx documentation notes configuration_model give example, reference , how remove parallel edges , self loops:
notes ----- described newman [1]_. non-graphical degree sequence (not realizable simple graph) allowed since function returns graphs self loops , parallel edges. exception raised if degree sequence not have sum. configuration model construction process can lead duplicate edges , loops. can remove self-loops , parallel edges (see below) result in graph doesn't have exact degree sequence specified. "finite-size effect" decreases size of graph increases. references ---------- .. [1] m.e.j. newman, "the structure , function of complex networks", siam review 45-2, pp 167-256, 2003. examples -------- >>> networkx.utils import powerlaw_sequence >>> z=nx.create_degree_sequence(100,powerlaw_sequence) >>> g=nx.configuration_model(z) remove parallel edges: >>> g=nx.graph(g) remove self loops: >>> g.remove_edges_from(g.selfloop_edges())
here example similar 1 @ http://networkx.lanl.gov/examples/drawing/degree_histogram.html makes drawing including graph layout of largest connected component:
#!/usr/bin/env python import random import matplotlib.pyplot plt import networkx nx def seq(n): return [random.gammavariate(alpha=2.0,beta=1.0) 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()
Comments
Post a Comment