Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / approximation / ramsey.py @ 5cef0f13

History | View | Annotate | Download (1.03 KB)

1
# -*- coding: utf-8 -*-
2
"""
3
Ramsey numbers.
4
"""
5
#   Copyright (C) 2011 by
6
#   Nicholas Mancuso <nick.mancuso@gmail.com>
7
#   All rights reserved.
8
#   BSD license.
9
import networkx as nx
10
from ...utils import arbitrary_element
11

    
12
__all__ = ["ramsey_R2"]
13
__author__ = """Nicholas Mancuso (nick.mancuso@gmail.com)"""
14

    
15

    
16
def ramsey_R2(G):
17
    r"""Approximately computes the Ramsey number `R(2;s,t)` for graph.
18

19
    Parameters
20
    ----------
21
    G : NetworkX graph
22
        Undirected graph
23

24
    Returns
25
    -------
26
    max_pair : (set, set) tuple
27
        Maximum clique, Maximum independent set.
28
    """
29
    if not G:
30
        return set(), set()
31

    
32
    node = arbitrary_element(G)
33
    nbrs = nx.all_neighbors(G, node)
34
    nnbrs = nx.non_neighbors(G, node)
35
    c_1, i_1 = ramsey_R2(G.subgraph(nbrs).copy())
36
    c_2, i_2 = ramsey_R2(G.subgraph(nnbrs).copy())
37

    
38
    c_1.add(node)
39
    i_2.add(node)
40
    # Choose the larger of the two cliques and the larger of the two
41
    # independent sets, according to cardinality.
42
    return max(c_1, c_2, key=len), max(i_1, i_2, key=len)