import numpy as np
import matplotlib.pyplot as plt
plt.xkcd()
import networkx as nx
%matplotlib inline
During the lecture we have understood, that the clustering coefficient of a random graph is equal to the probability $p$: $$\text{Clustering coefficient} = \frac{\langle k \rangle}{n} = p $$
In this task you have to check it on generated data. Please, generate $100$ Random Graphs with $n = 1000$ and $p = 0.002$ (for saving computational time) and plot the box-plot of your computations.
ccoef = []
n = 1000
p = 2.0/(n)
for i in xrange(100):
G = nx.erdos_renyi_graph(n, p)
ccoef.append(nx.transitivity(G))
plt.plot(ccoef, '*')
m = np.mean(ccoef)
plt.plot(np.arange(len(ccoef)), np.repeat(m,len(ccoef)), 'r')
In this task you are asked to calculate the average size of small components (small component = not a giant one) with regard to average degree of the network. To see the effect clearly, plot average size around $\langle k \rangle = 1$
n = 100
p = np.arange(1.0/(10*n),5.0/n, 1.0/(10*n))
av_sizes = []
for prob in p:
p_sizes = []
for i in xrange(50):
G = nx.erdos_renyi_graph(n, prob)
CC = nx.connected_components(G)
p_sizes.extend(map(len, CC[1:]))
av_sizes.append(np.mean(p_sizes))
plt.plot(p*n, av_sizes)
[<matplotlib.lines.Line2D at 0xb2c2c50>]
s = np.arange(0.001, 1, 0.001)
c = -np.log(1-s)/s
plt.plot(c,s)
[<matplotlib.lines.Line2D at 0xcd79390>]