1"""
2Ramsey numbers.
3"""
4
5import networkx as nx
6from networkx.utils import not_implemented_for
7
8from ...utils import arbitrary_element
9
10__all__ = ["ramsey_R2"]
11
12
13@not_implemented_for("directed")
14@not_implemented_for("multigraph")
15@nx._dispatchable
16def ramsey_R2(G):
17 r"""Compute the largest clique and largest independent set in `G`.
18
19 This can be used to estimate bounds for the 2-color
20 Ramsey number `R(2;s,t)` for `G`.
21
22 This is a recursive implementation which could run into trouble
23 for large recursions. Note that self-loop edges are ignored.
24
25 Parameters
26 ----------
27 G : NetworkX graph
28 Undirected graph
29
30 Returns
31 -------
32 max_pair : (set, set) tuple
33 Maximum clique, Maximum independent set.
34
35 Raises
36 ------
37 NetworkXNotImplemented
38 If the graph is directed or is a multigraph.
39 """
40 if not G:
41 return set(), set()
42
43 node = arbitrary_element(G)
44 nbrs = (nbr for nbr in nx.all_neighbors(G, node) if nbr != node)
45 nnbrs = nx.non_neighbors(G, node)
46 c_1, i_1 = ramsey_R2(G.subgraph(nbrs).copy())
47 c_2, i_2 = ramsey_R2(G.subgraph(nnbrs).copy())
48
49 c_1.add(node)
50 i_2.add(node)
51 # Choose the larger of the two cliques and the larger of the two
52 # independent sets, according to cardinality.
53 return max(c_1, c_2, key=len), max(i_1, i_2, key=len)