度序列#

根据给定的度序列生成随机图。

plot degree sequence
True
Configuration model
Degree sequence [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
Degree histogram
degree #nodes
   5      1
   3      4
   2      3
   1      3

import matplotlib.pyplot as plt
import networkx as nx

# 指定种子以确保可重复性
seed = 668273

z = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
print(nx.is_graphical(z))

print("Configuration model")
G = nx.configuration_model(
    z, seed=seed
)  # 配置模型,用于可重复性的种子
degree_sequence = [d for n, d in G.degree()]  # 度序列
print(f"Degree sequence {degree_sequence}")
print("Degree histogram")
hist = {}
for d in degree_sequence:
    if d in hist:
        hist[d] += 1
    else:
        hist[d] = 1
print("degree #nodes")
for d in hist:
    print(f"{d:4} {hist[d]:6}")

pos = nx.spring_layout(G, seed=seed)  # 设定种子布局以确保可重复性
nx.draw(G, pos=pos)
plt.show()

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

Gallery generated by Sphinx-Gallery