Implementation of Erdos-Renyi Model on Social Networks
Prerequisite: Introduction to Social Networks, Erdos-Renyi Model
Erdos Renyi model is used to create random networks or graphs on social networking. In the Erdos Reny model, each edge has a fixed probability of being present and being absent independent of the edges in a network.
Implementing a Social Network using the Erdos-Renyi model:
Step 1) Import necessary modules like networkx, matplotlib.pyplot, and random module.
Python3
import networkx as nx
import matplotlib.pyplot as plt
import random
|
Step 2) Create a distribution graph for the model.
Python3
def distribution_graph(g):
print (nx.degree(g))
all_node_degree = list ( dict ((nx.degree(g))).values())
unique_degree = list ( set (all_node_degree))
unique_degree.sort()
nodes_with_degree = []
for i in unique_degree:
nodes_with_degree.append(all_node_degree.count(i))
plt.plot(unique_degree, nodes_with_degree)
plt.xlabel( "Degrees" )
plt.ylabel( "No. of nodes" )
plt.title( "Degree distribution" )
plt.show()
|
Step 3) Take N i.e number of nodes from the user.
Python3
print ( "Enter number of nodes" )
N = int ( input ())
|
Step 4) Now take P i.e the probability from the user for edges.
Python3
print ( "Enter value of probability of every node" )
P = float ( input ())
|
Step 5) Create a graph with N nodes without any edges.
Python3
g = nx.Graph()
g.add_nodes_from( range ( 1 , N + 1 ))
|
Step 6) Add the edges to the graph randomly, take a pair of nodes, and get a random number R. If R<P (probability), add an edge. Repeat steps 5 and 6 for all possible pairs of nodes and then display the whole social network (graph) formed.
Python3
for i in g.nodes():
for j in g.nodes():
if (i < j):
R = random.random()
if (R < P):
g.add_edge(i, j)
pos = nx.circular_layout(g)
nx.draw(g, pos, with_labels = 1 )
plt.show()
|
Step 7) Display the connecting nodes.
Below is the complete program of the above step-wise approach:
Python3
import networkx as nx
import matplotlib.pyplot as plt
import random
def distribution_graph(g):
print (nx.degree(g))
all_node_degree = list ( dict ((nx.degree(g))).values())
unique_degree = list ( set (all_node_degree))
unique_degree.sort()
nodes_with_degree = []
for i in unique_degree:
nodes_with_degree.append(all_node_degree.count(i))
plt.plot(unique_degree, nodes_with_degree)
plt.xlabel( "Degrees" )
plt.ylabel( "No. of nodes" )
plt.title( "Degree distribution" )
plt.show()
print ( "Enter number of nodes" )
N = int ( input ())
print ( "Enter value of probability of every node" )
P = float ( input ())
g = nx.Graph()
g.add_nodes_from( range ( 1 , N + 1 ))
for i in g.nodes():
for j in g.nodes():
if (i < j):
R = random.random()
if (R < P):
g.add_edge(i, j)
pos = nx.circular_layout(g)
nx.draw(g, pos, with_labels = 1 )
plt.show()
distribution_graph(g)
|
Output:
Enter number of nodes
10
Enter value of probability of every node
0.4
[(1, 5), (2, 3), (3, 4), (4, 2), (5, 3), (6, 5), (7, 4), (8, 2), (9, 2), (10, 2)]
Adding edges randomly in a graph
Degree Distribution Graph of the implementation of Erdos-Renyi model on the above program:
Last Updated :
01 Oct, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...