Mercurial > public > think_complexity
comparison ch4ex4.py @ 23:74c9d126bd05
Working on the SmallWorldGraph exercises.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 02 Jan 2013 16:50:55 -0600 |
parents | |
children | 5c2c4ce095ef |
comparison
equal
deleted
inserted
replaced
22:84e183b40c63 | 23:74c9d126bd05 |
---|---|
1 """This program performs item 4 in 4.4 exercise 4. | |
2 | |
3 "Make a graph that replicates the line marked C(p)/C(0) in Figure 2 of the | |
4 paper. In other words, confirm that the clustering coefficient drops off slowly | |
5 for small values of p." | |
6 | |
7 """ | |
8 import random | |
9 import matplotlib.pyplot as pyplot | |
10 | |
11 from Graph import Vertex | |
12 from SmallWorldGraph import SmallWorldGraph | |
13 | |
14 | |
15 title = 'C(p)/C(0)' | |
16 | |
17 # compute C(0) | |
18 n = 1000 | |
19 k = 10 | |
20 vs = [Vertex(str(i)) for i in range(n)] | |
21 g = SmallWorldGraph(vs, k, 0.0) | |
22 c0 = g.clustering_coefficient() | |
23 print 'c0 =', c0 | |
24 | |
25 # compute data | |
26 p_vals = [0, | |
27 0.0001, 0.0002, 0.0004, 0.0006, 0.0008, | |
28 0.001, 0.002, 0.004, 0.006, 0.008, | |
29 0.01, 0.02, 0.04, 0.06, 0.08, | |
30 0.1, 0.2, 0.4, 0.6, 0.8, | |
31 1.0] | |
32 | |
33 c_vals = [] | |
34 for p in p_vals: | |
35 g = SmallWorldGraph(vs, k, p) | |
36 c_vals.append(g.clustering_coefficient() / c0) | |
37 | |
38 # plot graph | |
39 pyplot.clf() | |
40 pyplot.xscale('log') | |
41 pyplot.yscale('log') | |
42 pyplot.title('') | |
43 pyplot.xlabel('p') | |
44 pyplot.ylabel('C(p)/C(0)') | |
45 pyplot.plot(p_vals, c_vals, label=title, color='green', linewidth=3) | |
46 pyplot.legend(loc=4) | |
47 pyplot.show() |