The Wayback Machine - https://web.archive.org/web/20240707125308/https://www.geeksforgeeks.org/implementation-of-erdos-renyi-model-on-social-networks/
Open In App

Implementation of Erdos-Renyi Model on Social Networks

Last Updated : 01 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:

 



Similar Reads

Erdos Renyl Model (for generating Random Graphs)
In graph theory, the Erdos–RÃĐnyi model is either of two closely related models for generating random graphs. There are two closely related variants of the Erdos–RÃĐnyi (ER) random graph model. In the G(n, M) model, a graph is chosen uniformly at random from the collection of all graphs which have n nodes and M edges. For example, in the G(3, 2) mode
5 min read
Selection and Social Influence-Homophily in Social Networks
Homophily is the tendency in social groups of similar people connected together. We often hear similar voices interact with like-minded people. Homophily has a significant impact on social media. Example - Birds with feather flock together. Assume there are 1000 people in a party out of which 500 are of age ranges from 18-25 and the other 500 peopl
2 min read
Signed Networks in Social Networks
Prerequisite: Introduction to Social Networks In Social Networks, Network is of 2 types- Unsigned Network and Signed Network. In the unsigned network, there are no signs between any nodes, and in the signed network, there is always a sign between 2 nodes either + or -. The '+' sign indicates friendship between 2 nodes and the '-' sign indicates enm
4 min read
Fatman Evolutionary Model in Social Networks
Prerequisite: Introduction to Social Networks In the Fatman evolutionary model, there are 3 main concepts to make an evolutionary model i.e Homophily, Social influence, and closure. Homophily- People who are similar to each other, they tend to make friends with each other.Social Influence- People change their behavior and properties because of soci
4 min read
Introduction to Social Networks using NetworkX in Python
Prerequisite - Python Basics Ever wondered how the most popular social networking site Facebook works? How we are connected with friends using just Facebook? So, Facebook and other social networking sites work on a methodology called social networks. Social networking is used in mostly all social media sites such as Facebook, Instagram, and LinkedI
4 min read
Different dataset forms in Social Networks
Prerequisite: Python Basics For constructing any network we need a good dataset and every network has a different format for data of datasets. Basically a dataset is nothing but a huge collection of data that can be used further for any analysis. Data in dataset can be in many formats. Example of Network datasets: Ingredients network.Synonymy netwo
3 min read
Spatial Segregation in Social Networks
Prerequisite - Introduction to Social Networks Segregation refers to the act of separation of people from others or the main group. Spatial Segregation refers to the distribution of social groups or any other elements in space. In Spatial Segregation, people tend to migrate to other places where they have more neighbors who are like them. According
6 min read
Cascading Behavior in Social Networks
Prerequisite: Introduction to Social Networks, Python Basics When people are connected in networks to each other then they can influence each other's behavior and decisions. This is called Cascading Behavior in Networks. Let's consider an example, assume all the people in a society have adopted a trend X. Now there comes new trend Y and a small gro
7 min read
K-shell decomposition on Social Networks
Prerequisite: Introduction to Social Networks K-shell decomposition is the method in which we can divide nodes on the basis of the number of its degree like nodes with degree 1 in one bucket etc. Consider an example, assume there are n nodes and you apply k-shell decomposition in it. So nodes with degree 1 will be in bucket1 then we will see that a
4 min read
Differences Between Bayesian Networks and Neural Networks
Bayesian networks and neural networks are two distinct types of graphical models used in machine learning and artificial intelligence. While both models are designed to handle complex data and make predictions, they differ significantly in their theoretical foundations, operational mechanisms, and applications. This article will delve into the diff
9 min read
three90RightbarBannerImg