The Wayback Machine - https://web.archive.org/web/20210428151327/https://www.geeksforgeeks.org/implementation-of-erdos-renyi-model-on-social-networks/
Skip to content
Related Articles

Related Articles

Implementation of Erdos-Renyi Model on Social Networks
  • Last Updated : 01 Oct, 2020

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 Required modules
import networkx as nx
import matplotlib.pyplot as plt
import random

 



Step 2) Create a distribution graph for the model.

Python3




# Distribution graph for Erdos_Renyi model
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




# Take N number of nodes as input
print("Enter number of nodes")
N = int(input())

 

Step 4) Now take P i.e the probability from the user for edges.

Python3






# Take P probability value for edges
print("Enter value of probability of every node")
P = float(input())

 

Step 5) Create a graph with N nodes without any edges.

Python3




# Create an empty graph object
g = nx.Graph()
  
# Adding nodes
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




# Add edges to the graph randomly.
for i in g.nodes():
    for j in g.nodes():
        if (i < j):
              
            # Take random number R.
            R = random.random()
              
            # Check if R<P add the edge 
            # to the graph else ignore.
            if (R < P):
                g.add_edge(i, j)
    pos = nx.circular_layout(g)
      
    # Display the social network 
    nx.draw(g, pos, with_labels=1)
    plt.show()

 

Step 7) Display the connecting nodes.

Python3




# Display connection between nodes    
distribution_graph(g)

Below is the complete program of the above step-wise approach:

Python3




# Implementation of Erdos-Renyi Model on a Social Network
  
  
# Import Required modules
import networkx as nx
import matplotlib.pyplot as plt
import random
  
  
# Distribution graph for Erdos_Renyi model
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()
  
          
# Take N number of nodes from user
print("Enter number of nodes")
N = int(input())
  
  
# Take P probability value for edges
print("Enter value of probability of every node")
P = float(input())
  
  
# Create an empty graph object
g = nx.Graph()
  
  
# Adding nodes
g.add_nodes_from(range(1, N + 1))
  
  
# Add edges to the graph randomly.
for i in g.nodes():
    for j in g.nodes():
        if (i < j):
              
            # Take random number R.
            R = random.random()
              
            # Check if R<P add the edge to the graph else ignore.
            if (R < P):
                g.add_edge(i, j)
    pos = nx.circular_layout(g)
      
    # Display the social network 
    nx.draw(g, pos, with_labels=1)
    plt.show()
  
# Display connection between nodes    
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:

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA

My Personal Notes arrow_drop_up
Recommended Articles
Page :