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