Implementation of Erdos-Renyi Model on Social Networks

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.
Step 1) Import necessary modules like networkx, matplotlib.pyplot, and random module.
Python3
# Import Required modulesimport networkx as nximport matplotlib.pyplot as pltimport random
 
Step 2) Create a distribution graph for the model.
Python3
# Distribution graph for Erdos_Renyi modeldef 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 inputprint("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 edgesprint("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 objectg = nx.Graph()  # Adding nodesg.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)
Python3
# Implementation of Erdos-Renyi Model on a Social Network    # Import Required modulesimport networkx as nximport matplotlib.pyplot as pltimport random    # Distribution graph for Erdos_Renyi modeldef 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 userprint("Enter number of nodes")N = int(input())    # Take P probability value for edgesprint("Enter value of probability of every node")P = float(input())    # Create an empty graph objectg = nx.Graph()    # Adding nodesg.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 nodes10Enter value of probability of every node0.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:
 
Advanced Data Structure
Graph
Python
Graph
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Agents in Artificial Intelligence
Decision Tree Introduction with example
Segment Tree | Set 1 (Sum of given range)
Binary Indexed Tree or Fenwick Tree
AVL Tree | Set 2 (Deletion)
Breadth First Search or BFS for a Graph
Depth First Search or DFS for a Graph
Find if there is a path between two vertices in a directed graph
Graph and its representations