Source code for networkx.algorithms.approximation.ramsey

"""
拉姆齐数。
"""

import networkx as nx
from networkx.utils import not_implemented_for

from ...utils import arbitrary_element

__all__ = ["ramsey_R2"]


[docs] @not_implemented_for("directed") @not_implemented_for("multigraph") @nx._dispatchable def ramsey_R2(G): r"""计算图 `G` 中的最大团和最大独立集。 这可以用于估计图 `G` 的2-染色Ramsey数 `R(2;s,t)` 的界限。 这是一个递归实现,对于大量递归可能会遇到问题。注意,自环边被忽略。 Parameters ---------- G : NetworkX 图 无向图 Returns ------- max_pair : (集合, 集合) 元组 最大团, 最大独立集。 Raises ------ NetworkXNotImplemented 如果图是有向的或是一个多重图。 """ if not G: return set(), set() node = arbitrary_element(G) nbrs = (nbr for nbr in nx.all_neighbors(G, node) if nbr != node) nnbrs = nx.non_neighbors(G, node) c_1, i_1 = ramsey_R2(G.subgraph(nbrs).copy()) c_2, i_2 = ramsey_R2(G.subgraph(nnbrs).copy()) c_1.add(node) i_2.add(node) # Choose the larger of the two cliques and the larger of the two # independent sets, according to cardinality. return max(c_1, c_2, key=len), max(i_1, i_2, key=len)