15.08.2015 Views

Introduction to the Modeling and Analysis of Complex Systems

introduction-to-the-modeling-and-analysis-of-complex-systems-sayama-pdf

introduction-to-the-modeling-and-analysis-of-complex-systems-sayama-pdf

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

15.6. GENERATING RANDOM GRAPHS 321sions where you want <strong>to</strong> have a r<strong>and</strong>omly generated network. Here are some examples<strong>of</strong> NetworkX’s built-in functions that can generate r<strong>and</strong>om graph samples:Code 15.21: r<strong>and</strong>om-graphs.pyfrom pylab import *import networkx as nxsubplot(2, 2, 1)nx.draw(nx.gnm_r<strong>and</strong>om_graph(10, 20))title(’r<strong>and</strong>om graph with\n10 nodes, 20 edges’)subplot(2, 2, 2)nx.draw(nx.gnp_r<strong>and</strong>om_graph(20, 0.1))title(’r<strong>and</strong>om graph with\n 20 nodes, 10% edge probability’)subplot(2, 2, 3)nx.draw(nx.r<strong>and</strong>om_regular_graph(3, 10))title(’r<strong>and</strong>om regular graph with\n10 nodes <strong>of</strong> degree 3’)subplot(2, 2, 4)nx.draw(nx.r<strong>and</strong>om_degree_sequence_graph([3,3,3,3,4,4,4,4,5,5]))title(’r<strong>and</strong>om graph with\n degree sequence\n[3,3,3,3,4,4,4,4,5,5]’)show()The output is shown in Fig. 15.10. The first example, gnm_r<strong>and</strong>om_graph(n, m), simplygenerates a r<strong>and</strong>om graph made <strong>of</strong> n nodes <strong>and</strong> m edges. The second example,gnp_r<strong>and</strong>om_graph(n, p), generates a r<strong>and</strong>om graph made <strong>of</strong> n nodes, where eachnode pair is connected <strong>to</strong> each o<strong>the</strong>r with probability p. These two types <strong>of</strong> r<strong>and</strong>omgraphs are called Erdős-Rényi r<strong>and</strong>om graphs after two Hungarian ma<strong>the</strong>maticians PaulErdős <strong>and</strong> Alfréd Rényi who studied r<strong>and</strong>om graphs in <strong>the</strong> 1950s [63]. The third example,r<strong>and</strong>om_regular_graph(k, n), generates a r<strong>and</strong>om regular graph made <strong>of</strong> n nodes thatall have <strong>the</strong> same degree k. The fourth example, r<strong>and</strong>om_degree_sequence_graph(list),generates a r<strong>and</strong>om graph whose nodes have degrees specified in list. In this particularexample, four nodes have degree 3, four nodes have degree 4, <strong>and</strong> two nodes have degree5. Note that this function may not be able <strong>to</strong> generate a graph within a given number<strong>of</strong> trials (which is set <strong>to</strong> 10 by default). You can increase <strong>the</strong> number <strong>of</strong> trials by specifying<strong>the</strong> tries option in <strong>the</strong> function, e.g., tries = 100.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!