Open In App

Erdos Renyl Model (for generating Random Graphs)

Last Updated : 25 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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) model, each of the three possible graphs on three vertices and two edges are included with probability 1/3. 

In the G(n, p) model, a graph is constructed by connecting nodes randomly. Each edge is included in the graph with probability p independent from every other edge. Equivalently, all graphs with n nodes and M edges have equal probability of p^M (1-p)^\binom{n}{2}-M

Erdos Renyl Model for generating Random Graphs 1

 A graph generated by the binomial model of Erdos and Rényi (p = 0.01) 

The parameter p in this model can be thought of as a weighting function; as p increases from 0 to 1, the model becomes more and more likely to include graphs with more edges and less and less likely to include graphs with fewer edges. In particular, the case p = 0.5 corresponds to the case where all 2^{\binom{n}{2}}   graphs on n vertices are chosen with equal probability. 

The article will basically deal with the G (n,p) model where n is the no of nodes to be created and p defines the probability of joining of each node to the other. 

Properties of G(n, p)

With the notation above, a graph in G(n, p) has on average {\binom{n}{2}}p   edges. The distribution of the degree of any particular vertex is binomial:

 P(deg(v)=k)=\binom{n-1}{k}{p^k}{1-p^{n-1-k}}

Where n is the total number of vertices in the graph. 

Since P(deg(v)=k)\rightarrow\frac{{np^k}{e^{-np}}}{k!}   as n\rightarrow infinity   and np= constant This distribution is Poisson for large n and np = const. In a 1960 paper, Erdos and Rényi described the behaviour of G(n, p) very precisely for various values of p. Their results included that:

  • If np < 1, then a graph in G(n, p) will almost surely have no connected components of size larger than O(log(n)).
  • If np = 1, then a graph in G(n, p) will almost surely have a largest component whose size is of order n^{2/3}   .
  • If np \rightarrow   c > 1, where c is a constant, then a graph in G(n, p) will almost surely have a unique giant component containing a positive fraction of the vertices. No other component will contain more than O(log(n)) vertices.
  • If p<\frac{(1-\varepsilon )ln n}{n}   , then a graph in G(n, p) will almost surely contain isolated vertices, and thus be disconnected.
  • If p>\frac{(1+\varepsilon )ln n}{n}   , then a graph in G(n, p) will almost surely be connected.

Thus \frac{ln n}{n}   is a sharp threshold for the connectedness of G(n, p). Further properties of the graph can be described almost precisely as n tends to infinity. For example, there is a k(n) (approximately equal to 2log2(n)) such that the largest clique in G(n, 0.5) has almost surely either size k(n) or k(n) + 1. Thus, even though finding the size of the largest clique in a graph is NP-complete, the size of the largest clique in a “typical” graph (according to this model) is very well understood. Interestingly, edge-dual graphs of Erdos-Renyi graphs are graphs with nearly the same degree distribution, but with degree correlations and a significantly higher clustering coefficient. 

Next I’ll describe the code to be used for making the ER graph. For implementation of the code below, you’ll need to install the netwrokx library as well you’ll need to install the matplotlib library. Following you’ll see the exact code of the graph which has been used as a function of the networkx library lately in this article. 

Erdos_renyi_graph(n, p, seed=None, directed=False) 

Returns a G(n,p) random graph, also known as an Erdos-Rényi graph or a binomial graph. 
The G(n,p) model chooses each of the possible edges with probability p. The functions binomial_graph() and erdos_renyi_graph() are aliases of this function. 

Parameters: n (int) – The number of nodes. 
p (float) – Probability for edge creation. 
seed (int, optional) – Seed for random number generator (default=None). 
directed (bool, optional (default=False)) – If True, this function returns a directed graph. 

Python

#importing the networkx library
>>> import networkx as nx
 
#importing the matplotlib library for plotting the graph
>>> import matplotlib.pyplot as plt
 
>>> G= nx.erdos_renyi_graph(50,0.5)
>>> nx.draw(G, with_labels=True)
>>> plt.show()

                    
Erdos Renyl Model for generating Random Graphs 2

Figure 1: For n=50, p=0.5

The above example is for 50 nodes and is thus a bit unclear. 

When considering the case for lesser no of nodes (for example 10), you can clearly see the difference. 

Using the codes for various probabilities, we can see the difference easily: 

Python

>>>H= nx.erdos_renyi_graph(10,0.5)
>>> nx.draw(H, with_labels=True)
>>> plt.show()

                    
Erdos Renyl Model for generating Random Graphs 3

Figure 2: For n=10, p=0

Python

>>> K=nx.erdos_renyi_graph(10,0.25)
>>> nx.draw(K, with_labels=True)
>>> plt.show()

                    
Erdos Renyl Model for generating Random Graphs 4

Figure 3: For n=10, p=0.25
 

Python

>>>H= nx.erdos_renyi_graph(10,0.5)
>>> nx.draw(H, with_labels=True)
>>> plt.show()

                    
Erdos Renyl Model for generating Random Graphs 5

Figure 4: For n=10, p=0.5

This algorithm runs in O(n^2   ) time. For sparse graphs (that is, for small values of p), fast_gnp_random_graph() is a faster algorithm. Thus the above examples clearly define the use of erdos renyi model to make random graphs and how to use the foresaid using the networkx library of python. Next we will discuss the ego graph and various other types of graphs in python using the library networkx. 

.  



Previous Article
Next Article

Similar Reads

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) Imp
3 min read
Linear Congruence method for generating Pseudo Random Numbers
Linear Congruential Method is a class of Pseudo Random Number Generator (PRNG) algorithms used for generating sequences of random-like numbers in a specific range. This method can be defined as: [Tex]X_{i + 1} = aX_{i} + c \hspace{0.2cm} mod \hspace{0.2cm} m [/Tex] where, X, is the sequence of pseudo-random numbersm, ( &gt; 0) the modulusa, (0, m)
6 min read
Multiplicative Congruence method for generating Pseudo Random Numbers
Multiplicative Congruential Method (Lehmer Method) is a type of linear congruential generator for generating pseudorandom numbers in a specific range. This method can be defined as: [Tex]\[X_{i + 1} = aX_{i} \hspace{0.2cm} mod \hspace{0.2cm} m\] [/Tex] where, X, the sequence of pseudo-random numbersm ( &gt; 0), the modulusa (0, m), the multiplierX0
6 min read
Additive Congruence method for generating Pseudo Random Numbers
Additive Congruential Method is a type of linear congruential generator for generating pseudorandom numbers in a specific range. This method can be defined as: [Tex]X_{i + 1} = X_{i} + c \hspace{0.2cm} mod \hspace{0.2cm} m [/Tex] where, X, the sequence of pseudo-random numbersm ( &gt; 0), the modulusc [0, m), the incrementX0 [0, m), initial value o
6 min read
Test Case Generation | Set 5 (Generating random Sorted Arrays and Palindromes)
Generating Random Sorted Arrays We store the random array elements in an array and then sort it and print it. [GFGTABS] CPP // A C++ Program to generate test cases for // array filled with random numbers #include&amp;lt;bits/stdc++.h&amp;gt; using namespace std; // Define the number of runs for the test data // generated #define RUN 5 // Define the
10 min read
Generating Random String Using PHP
Generate a random, unique, alpha-numeric string using PHP. Examples: EA070aBX32gTfTable of Content Brute Force Using Hashing Functions Using uniqid() functionUsing random_bytes() function. (Cryptographically Secure) Using random_int() in a Custom FunctionApproach 1: Brute Force The first approach is the simplest one to understand and thus brute for
3 min read
Test Case Generation | Set 4 (Random directed / undirected weighted and unweighted Graphs)
Generating Random Directed Unweighted Graphs Since this is a graph, the test data generation plan doesn't guarantee that a cycle gets formed or not.The number of edges - NUMEDGE is greater than zero and less than NUM*(NUM-1)/2, where NUM = Number of VerticesFor each RUN we first print the number of vertices - NUM first in a new separate line and th
15+ min read
Implement random-0-6-Generator using the given random-0-1-Generator
Given a function random01Generator() that gives you randomly either 0 or 1, implement a function that utilizes this function and generate numbers between 0 and 6(both inclusive). All numbers should have same probabilities of occurrence. Examples: on multiple runs, it gives 3 2 3 6 0 Approach : The idea here is to find the range of the smallest numb
5 min read
Test Case Generation | Set 2 ( Random Characters, Strings and Arrays of Random Strings)
Set 1 (Random Numbers, Arrays and Matrices) Generating Random Characters C/C++ Code // A C++ Program to generate test cases for // random characters #include&lt;bits/stdc++.h&gt; using namespace std; // Define the number of runs for the test data // generated #define RUN 5 // Define the range of the test data generated // Here it is 'a' to 'z' #def
11 min read
Program to Change RGB color model to HSV color model
Given RGB color range, our task is to convert RGB color to HSV color.RGB Color Model : The RGB color model is an additive color model in which red, green and blue light are added together in various ways to reproduce a broad array of colors. The name of the model comes from the initials of the three additive primary colors, red, green, and blue. HS
9 min read
Article Tags :
Practice Tags :