最大独立集#

独立集是图中的一个顶点集合,其中任意两个顶点都不相邻。最大独立集是给定图中可能的最大规模的独立集。

plot maximum independent set
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, seed=39299899)
nx.draw(
    G,
    pos=pos,
    with_labels=True,
    node_color=["tab:red" if n in I else "tab:blue" for n in G],
)

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

Gallery generated by Sphinx-Gallery