Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating graphs for given degree sequence in Python or R

I am trying to learn if in Python or R, there exist within the graph-theory related modules features that would enable one to start from a degree distribution (or expressed as a sequence once we set the number of vertices), and generate (random) graphs that satisfy the prescribed degree sequence.

As an example, we might be given the following distribution: p=(0.179,0.49,0.34) which are the probabilities of degree values 1,2 and 3 respectively. So we can set the number of vertices, n=500, map p to a degree sequence deseq list: filled with 0.179*n times of 1, and so on for the rest.

Any pointers towards previously discussed cases for such problems or library suggestions would be very helpful.

like image 359
user929304 Avatar asked Feb 16 '26 20:02

user929304


2 Answers

Here's an attempt to answer my own question after having learned how to use igraph in R and Python for generating the desired type of graphs.

In R:

For the purposes of this example, let's suppose the following degree sequence: total nodes n=20, 5,10 and 5 nodes with degrees 1,2, and 3 respectively. We create the degree sequence using c() and rep(). Then we'll use sample_degseq() from igraph to generate a graph corresponding to the above degree sequence. Then we'll draw its degree histogram to sanity check.

First install and call the igraph module in the R console using:

install.packages("igraph")
library(igraph)

Now we can proceed as described:

degreels <- c(rep(1,5),rep(2,10),rep(3,5))
graph <- sample_degseq(degreels, method="simple")
degreehist <- hist(degree(graph))
is.connected(graph)

enter image description here

In Python:

Now let's do the exact same thing with the igraph module in Python:

To install see here.

import igraph as ig
import matplotlib.pyplot as plt

degcounts = [5,10,5]
degreels = []
for i in range(len(degcounts)):
    degreels += degcounts[i]*[i+1]

graph = ig.GraphBase.Degree_Sequence(degreels,method="simple")
plt.hist(graph.degree())
plt.show()

and the obtained histogram:

enter image description here

like image 186
user929304 Avatar answered Feb 19 '26 08:02

user929304


I don't know how much have you already researched on the topic but there is a pretty wellknown package in R and module in Python called igraph. It might have what you seek for.

like image 25
Pedro Henrique Avatar answered Feb 19 '26 09:02

Pedro Henrique



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!