view ch4ex4.py @ 38:d876711b88b5

Comment out the calculation of L as it may take a long time.
author Brian Neal <bgneal@gmail.com>
date Thu, 10 Jan 2013 20:24:56 -0600
parents 15ff31ecec7a
children
line wrap: on
line source
"""This program performs item 4 in 4.4 exercise 4.

"Make a graph that replicates the line marked C(p)/C(0) in Figure 2 of the
paper. In other words, confirm that the clustering coefficient drops off slowly
for small values of p."

"""
import matplotlib.pyplot as pyplot

from Graph import Vertex
from SmallWorldGraph import SmallWorldGraph

# Use Dijkstra or Floyd-Warshall to compute L
DIJKSTRA = True

# compute C(0)
n = 1000
k = 10
vs = [Vertex(str(i)) for i in range(n)]
g = SmallWorldGraph(vs, k, 0.0)
c0 = g.clustering_coefficient()
l0 = g.big_l3() if DIJKSTRA else g.big_l2()
print 'c0 =', c0, 'l0 =', l0

# compute data
p_vals = [0.0001, 0.0002, 0.0004, # 0.0006, 0.0008,
          0.001, 0.002, 0.004, # 0.006, 0.008,
          0.01, 0.02, 0.04, # 0.06, 0.08,
          0.1, 0.2, 0.4, # 0.6, 0.8,
          1.0]

c_vals = []
l_vals = []
for p in p_vals:
    g = SmallWorldGraph(vs, k, p)
    c_vals.append(g.clustering_coefficient() / c0)
    l = g.big_l3() if DIJKSTRA else g.big_l2()
    l_vals.append(l / l0)

p_vals.insert(0, 0.0)
c_vals.insert(0, 1.0)
l_vals.insert(0, 1.0)

# plot graph
pyplot.clf()
pyplot.xscale('log')
pyplot.yscale('linear')
pyplot.title('')
pyplot.xlabel('p')
pyplot.ylabel('C(p)/C(0)')
pyplot.plot(p_vals, c_vals, label='C(p)/C(0)', color='green', linewidth=3)
pyplot.plot(p_vals, l_vals, label='L(p)/L(0)', color='blue', linewidth=3)
pyplot.legend(loc='lower left')
pyplot.show()