Degree Sequence#

Random graph from given degree sequence.

plot degree sequence
Sequence [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1] is a valid degree sequence: True
Degree sequence of G identical to z: True

Degree histogram
degree: #nodes
{0: 0,
 1: 3,
 2: 3,
 3: 4,
 4: 0,
 5: 1}

from pprint import pprint
import matplotlib.pyplot as plt
import networkx as nx

# Specify seed for reproducibility
seed = 668273

z = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
print(f"Sequence {z} is a valid degree sequence: {nx.is_graphical(z)}")

G = nx.configuration_model(z, seed=seed)
degree_sequence = [d for _, d in G.degree()]

# The degree sequence for the random graph computed using configuration_model
# should match the degree sequence used to generate it
print(f"Degree sequence of G identical to z: {degree_sequence == z}")

print("\nDegree histogram")
deg_hist = dict(enumerate(nx.degree_histogram(G)))
print("degree: #nodes")
pprint(deg_hist, width=10)

pos = nx.spring_layout(G, seed=seed)  # Seed layout for reproducibility
nx.draw(G, pos=pos)
plt.show()

Total running time of the script: (0 minutes 0.051 seconds)

Gallery generated by Sphinx-Gallery