注意
跳转到末尾以下载完整的示例代码。
最大独立集#
独立集是指图中一组顶点,其中任意两个顶点都不相邻。最大独立集是指给定图中尺寸最大的独立集。

Maximum independent set of G: {8, 1, 10, 3}
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
from networkx.algorithms import approximation as approx
G = nx.Graph(
[
(1, 2),
(7, 2),
(3, 9),
(3, 2),
(7, 6),
(5, 2),
(1, 5),
(2, 8),
(10, 2),
(1, 7),
(6, 1),
(6, 9),
(8, 4),
(9, 4),
]
)
I = approx.maximum_independent_set(G)
print(f"Maximum independent set of G: {I}")
pos = nx.spring_layout(G, iterations=100, seed=42)
nx.draw(
G,
pos=pos,
with_labels=True,
node_color=["tab:red" if n in I else "tab:blue" for n in G],
)
脚本总运行时间: (0 分钟 0.061 秒)