Example03 / 13
鲁棒性——随机失效与蓄意攻击(双曲线对照图)
在 500 个节点的 BA 网络上依次删除 0%–50% 的节点,比较两种策略下最大连通分支占原网络的比例: 随机删除 vs 按初始度从高到低删除。图中把两条曲线画在同一坐标系,标题直接写结论。 解读:随机删除时巨分支缓慢缩小,攻击枢纽时迅速崩解——无标度网络“耐随机、怕攻击”。
Output输出图

Output文字输出
删除 20% 节点后,巨分支比例:随机删除 = 0.79,攻击枢纽 = 0.03
Code代码
# 在 500 个节点的 BA 网络上依次删除 0%–50% 的节点,比较两种策略下最大连通分支占原网络的比例:
# 随机删除 vs 按初始度从高到低删除。图中把两条曲线画在同一坐标系,标题直接写结论。
# 解读:随机删除时巨分支缓慢缩小,攻击枢纽时迅速崩解——无标度网络“耐随机、怕攻击”。
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
G0 = nx.barabasi_albert_graph(500, 2, seed=16)
N = G0.number_of_nodes()
fracs = np.linspace(0, 0.5, 11)
rng = np.random.default_rng(0)
order_random = list(rng.permutation(N))
order_attack = [v for v, _ in sorted(G0.degree(), key=lambda x: -x[1])]
def giant_fraction(G0, order, f):
G = G0.copy()
G.remove_nodes_from(order[:int(f * N)])
return max((len(c) for c in nx.connected_components(G)), default=0) / N
gr = [giant_fraction(G0, order_random, f) for f in fracs]
ga = [giant_fraction(G0, order_attack, f) for f in fracs]
print(f"删除 20% 节点后,巨分支比例:随机删除 = {gr[4]:.2f},攻击枢纽 = {ga[4]:.2f}")
plt.figure(figsize=(5, 3))
plt.plot(fracs, gr, "o-", label="random failure")
plt.plot(fracs, ga, "s-", label="targeted attack (by degree)")
plt.xlabel("fraction of nodes removed"); plt.ylabel("giant component fraction")
plt.title("Hub removal fragments the BA network far faster")
plt.legend(); plt.tight_layout(); plt.show()Run在浏览器中运行
首次加载 Python 环境约需 20–40 秒;可以修改参数后重新运行。