I want to show in a random graph the average distance of nodes increases by log N where N is the number of nodes. p here is the probability of having an edge between a pair of nodes. What I tried is this:
import matplotlib.pyplot as plt
import networkx as nx
%matplotlib inline
def run_experiment(ns, p=0.45, iters=30):
"""
p: probability of having an edge between a pair of nodes
ns: sequence of `number of nodes(n)` to try
iters: number of times to run for each `n`
"""
G = {}
shortestPath={}
res = []
for n in ns:
print(n)
for i in range(iters):
G[i] = nx.gnp_random_graph(n, p)
shortestPath[i] = nx.average_shortest_path_length(G[i], weight=None, method=None)
means = array([shortestPath[k] for k in shortestPath]).mean()
print(means)
res.append(means)
return np.array(res)
I tried it for some n values:
ns = [1,10, 100, 1000]
res = run_experiment(ns)
Then I plot it to show the logarithmic curve, but I've got this:
dictionary = dict(zip(ns, res))
plt.plot(list(dictionary.keys()),list(dictionary.values()))
plt.yscale('log')
plt.xscale('log')
What is the problem?