Degree distribution of Barabasi-Albert model

Viewed 1601

I have a been able to run the run this:

import numpy as np
import networkx as nx
import matplotlib.pyplot as plt

n = 20
m = 3

G_barabasi = nx.barabasi_albert_graph(n,m)
plt.figure(figsize=(12,8))
nx.draw(G_barabasi, node_size=4)
plt.show()

The above code was able to plot the nodes and edges.

However, I need to obtain the degree of distribution of the Barabasi-Albert Model or rather the power-law degree distribution.

1 Answers

We could make use of nx.degree_histogram, which returns a list of frequencies of the degrees in the network, where the degree values are the corresponding indices in the list.

Normally the logarithm of both x and y axes is taken when plotting the degree distribution, this helps seing if a networkx is scale-free (a network whose degree distribution follows a power law) which is the case with the Barabási–Albert model, we can use plt.loglog for that:

import networkx as nx
import matplotlib.pyplot as plt

n = 2000
m = 3
G_barabasi = nx.barabasi_albert_graph(n,m)

degree_freq = nx.degree_histogram(G_barabasi)
degrees = range(len(degree_freq))
plt.figure(figsize=(12, 8)) 
plt.loglog(degrees[m:], degree_freq[m:],'go-') 
plt.xlabel('Degree')
plt.ylabel('Frequency')

enter image description here

Related