In: Computer Science
PYTHON
First, create an ER random graph with N=100 and p=0.1 and name it G. Then use the histogram() function to draw a histogram of the node degrees for the graph G. Don't worry about filling in "missing" values.
Also print the mean, median, and mode of the node degree distribution. Execute this a few times.
Note: while histogram(G.degree()) does draw a plot, that plot is not a histogram. Doing so will result in zero credit for this exercise.
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import statistics
G= nx.erdos_renyi_graph(100,0.1)
nodes = nx.nodes(G)
degrees = []
for v in nodes:
degrees.append(nx.degree(G, v))
plt.hist(degrees,color = 'green')
plt.show()
print("Mean: ",np.mean(degrees))
print("Median: ",np.median(degrees))
print("Mode: ",statistics.mode(degrees))