<jupyter_start><jupyter_text>$\newcommand{\xv}{\mathbf{x}} \newcommand{\wv}{\mathbf{w}} \newcommand{\Chi}{\mathcal{X}} \newcommand{\R}{\rm I\!R} \newcommand{\sign}{\text{sign}} \newcommand{\Tm}{\mathbf{T}} \newcommand{\Xm}{\mathbf{X}} \newcommand{\Im}{\mathbf{I}}  \newcommand{\Ym}{\mathbf{Y}}$ ITCS8010 G_np Simulation ExperimentIn this experiment I like to replicate the behaviour of `Fraction of node in largest CC` and `Fraction of isolated nodes` over the `p*log(n)` in `Erdös-Renyi random graph model`.<jupyter_code>import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import collections as collec
%matplotlib inline
# Fraction of node in largest CC Vs. {p*log(n)}
n = 100000
x1 = []
y1 = []

for kave in np.arange(0.5, 3.0, 0.1):
    G = nx.fast_gnp_random_graph(n, kave / (n - 1))
    largest_cc = max(nx.connected_components(G), key=len)
    x1.append(kave)
    y1.append(len(largest_cc)/n)
    # print(kave)
    # print(len(largest_cc)/n)

fig, ax = plt.subplots()
ax.plot(x1, y1)

ax.set(xlabel='p*(n-1)', ylabel='Fraction of node in largest CC',
       title='Fraction of node in largest CC Vs. p*(n-1)')
ax.grid()

# fig.savefig("test.png")
plt.show()
# Fraction of isolated nodes Vs. {p*log(n)}
x2 = []
y2 = []

for kave in np.arange(0.3, 1.5, 0.1):
    p = kave / (n - 1)
    G = nx.fast_gnp_random_graph(n, p)
    isolates = len(list(nx.isolates(G)))
    x2.append(p * np.log10(n))
    y2.append(isolates/n)
    # print(kave)
    # print(isolates/n)

fig, ax = plt.subplots()
ax.plot(x2, y2)

ax.set(xlabel='p*log(n)', ylabel='Fraction of isolated nodes',
       title='Fraction of isolated nodes Vs. p*log(n)')
ax.grid()

# fig.savefig("test.png")
plt.show()
# Fraction of isolated nodes Vs. {p*log(n)}
x2 = []
y2 = []

for kave in np.arange(0.3, 10, 0.1):
    p = kave / (n - 1)
    G = nx.fast_gnp_random_graph(n, p)
    isolates = len(list(nx.isolates(G)))
    x2.append(p * np.log10(n))
    y2.append(isolates/n)
    # print(kave)
    # print(isolates/n)

fig, ax = plt.subplots()
ax.plot(x2, y2)

ax.set(xlabel='p*log(n)', ylabel='Fraction of isolated nodes',
       title='Fraction of isolated nodes Vs. p*log(n)')
ax.grid()

# fig.savefig("test.png")
plt.show()<jupyter_output><empty_output><jupyter_text>Observation:1. The result of the first experiment (i.e. `fraction of node in largest CC` varying `p*(n-1)`) gives somewhat similar behaviour we observed in the class slide.2. In the second experiment (i.e. plotting `fraction of isolated nodes` on varying `p*log(n)`) gives somewhat different result comparing to the one we found in the class slide. When we plot the graph for p*(n-1) within the range of 0.3 to 1.5 we don't get the long tail; which we can get when we increase the range of p*(n-1) from 0.3 to 10. Just to inform, in this experiment we do run the loop on the different values of `p*(n-1)`, but plot it on `p*log(n)` scale. I am not sure the reason behind type of behaviour.  Key Network PropertiesNow we like to use the networkx [[2]](https://networkx.github.io/documentation/stable/) library support to ovserve the values of the key network properties in Erdös-Renyi random graph.<jupyter_code># plotting degree distribution

n1 = 180
p1 = 0.11

G = nx.fast_gnp_random_graph(n1, p1)

degree_sequence = sorted([d for n, d in G.degree()], reverse=True)  # degree sequence
degreeCount = collec.Counter(degree_sequence)
deg, cnt = zip(*degreeCount.items())

fig, ax = plt.subplots()
plt.bar(deg, cnt, width=0.80, color="b")

plt.title("Degree Histogram")
plt.ylabel("Count")
plt.xlabel("Degree")
ax.set_xticks([d + 0.4 for d in deg])
ax.set_xticklabels(deg)

# draw graph in inset
plt.axes([0.4, 0.4, 0.5, 0.5])
Gcc = G.subgraph(sorted(nx.connected_components(G), key=len, reverse=True)[0])
pos = nx.spring_layout(G)
plt.axis("off")
nx.draw_networkx_nodes(G, pos, node_size=20)
nx.draw_networkx_edges(G, pos, alpha=0.4)
plt.show()

# diameter and path length
dia = nx.diameter(G)
print(dia)
avg_path_len = nx.average_shortest_path_length(G)
print(avg_path_len)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15621915103652517, 'fraction_numerical': 0.02418558736426456, 'mean_word_length': 4.3258869908015765, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '4230', 'license': 'MIT', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1540, 'n_words': 426, 'path': 'assignments/assignment_0/gnp_simulation_experiment.ipynb', 'repo_name': 'biqar/Fall-2020-ITCS-8010-MLGraph', 'size': '4052'}