
    h                     p   S r SSKrSSKJr  SSKJr  / SQr\" S5      \" S5      \R                  S 5       5       5       r	\" S5      \" S5      \R                  S	 5       5       5       r
\" S5      \" S5      \R                  S
 5       5       5       r\" S5      \" S5      \R                  S 5       5       5       rg)zCFunctions for computing large cliques and maximum independent sets.    N)ramsey)not_implemented_for)clique_removal
max_cliquelarge_clique_sizemaximum_independent_setdirected
multigraphc                      [        U 5      u  pU$ )u  Returns an approximate maximum independent set.

Independent set or stable set is a set of vertices in a graph, no two of
which are adjacent. That is, it is a set I of vertices such that for every
two vertices in I, there is no edge connecting the two. Equivalently, each
edge in the graph has at most one endpoint in I. The size of an independent
set is the number of vertices it contains [1]_.

A maximum independent set is a largest independent set for a given graph G
and its size is denoted $\alpha(G)$. The problem of finding such a set is called
the maximum independent set problem and is an NP-hard optimization problem.
As such, it is unlikely that there exists an efficient algorithm for finding
a maximum independent set of a graph.

The Independent Set algorithm is based on [2]_.

Parameters
----------
G : NetworkX graph
    Undirected graph

Returns
-------
iset : Set
    The apx-maximum independent set

Examples
--------
>>> G = nx.path_graph(10)
>>> nx.approximation.maximum_independent_set(G)
{0, 2, 4, 6, 9}

Raises
------
NetworkXNotImplemented
    If the graph is directed or is a multigraph.

Notes
-----
Finds the $O(|V|/(log|V|)^2)$ apx of independent set in the worst case.

References
----------
.. [1] `Wikipedia: Independent set
    <https://en.wikipedia.org/wiki/Independent_set_(graph_theory)>`_
.. [2] Boppana, R., & Halldórsson, M. M. (1992).
   Approximating maximum independent sets by excluding subgraphs.
   BIT Numerical Mathematics, 32(2), 180–196. Springer.
)r   )Giset_s      Z/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/approximation/clique.pyr   r      s    j QGDK    c                 L    [         R                  " U 5      n[        U5      u  p#U$ )u   Find the Maximum Clique

Finds the $O(|V|/(log|V|)^2)$ apx of maximum clique/independent set
in the worst case.

Parameters
----------
G : NetworkX graph
    Undirected graph

Returns
-------
clique : set
    The apx-maximum clique of the graph

Examples
--------
>>> G = nx.path_graph(10)
>>> nx.approximation.max_clique(G)
{8, 9}

Raises
------
NetworkXNotImplemented
    If the graph is directed or is a multigraph.

Notes
-----
A clique in an undirected graph G = (V, E) is a subset of the vertex set
`C \subseteq V` such that for every two vertices in C there exists an edge
connecting the two. This is equivalent to saying that the subgraph
induced by C is complete (in some cases, the term clique may also refer
to the subgraph).

A maximum clique is a clique of the largest possible size in a given graph.
The clique number `\omega(G)` of a graph G is the number of
vertices in a maximum clique in G. The intersection number of
G is the smallest number of cliques that together cover all edges of G.

https://en.wikipedia.org/wiki/Maximum_clique

References
----------
.. [1] Boppana, R., & Halldórsson, M. M. (1992).
    Approximating maximum independent sets by excluding subgraphs.
    BIT Numerical Mathematics, 32(2), 180–196. Springer.
    doi:10.1007/BF01994876
)nx
complementr   )r   cgraphr   r   s       r   r   r   H   s$    l ]]1FV$GDKr   c                 R   U R                  5       n[        R                  " U5      u  p#U/nU/nU(       ab  UR                  U5        [        R                  " U5      u  p#U(       a  UR	                  U5        U(       a  UR	                  U5        U(       a  Mb  [        U[        S9nXd4$ )u=  Repeatedly remove cliques from the graph.

Results in a $O(|V|/(\log |V|)^2)$ approximation of maximum clique
and independent set. Returns the largest independent set found, along
with found maximal cliques.

Parameters
----------
G : NetworkX graph
    Undirected graph

Returns
-------
max_ind_cliques : (set, list) tuple
    2-tuple of Maximal Independent Set and list of maximal cliques (sets).

Examples
--------
>>> G = nx.path_graph(10)
>>> nx.approximation.clique_removal(G)
({0, 2, 4, 6, 9}, [{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}])

Raises
------
NetworkXNotImplemented
    If the graph is directed or is a multigraph.

References
----------
.. [1] Boppana, R., & Halldórsson, M. M. (1992).
    Approximating maximum independent sets by excluding subgraphs.
    BIT Numerical Mathematics, 32(2), 180–196. Springer.
key)copyr   	ramsey_R2remove_nodes_fromappendmaxlen)r   graphc_ii_icliquesisetsmaxisets          r   r   r      s    J FFHE&HCeGEE
$##E*NN3LL % %S!Gr   c                    ^^^ U R                   mUU4S jmSmUU4S jU  5       nU H,  nX    Vs1 s H  nTU   T:  d  M  UiM     nnT" XST5      mM.     T$ s  snf )a  Find the size of a large clique in a graph.

A *clique* is a subset of nodes in which each pair of nodes is
adjacent. This function is a heuristic for finding the size of a
large clique in the graph.

Parameters
----------
G : NetworkX graph

Returns
-------
k: integer
   The size of a large clique in the graph.

Examples
--------
>>> G = nx.path_graph(10)
>>> nx.approximation.large_clique_size(G)
2

Raises
------
NetworkXNotImplemented
    If the graph is directed or is a multigraph.

Notes
-----
This implementation is from [1]_. Its worst case time complexity is
:math:`O(n d^2)`, where *n* is the number of nodes in the graph and
*d* is the maximum degree.

This function is a heuristic, which means it may work well in
practice, but there is no rigorous mathematical guarantee on the
ratio between the returned number and the actual largest clique size
in the graph.

References
----------
.. [1] Pattabiraman, Bharath, et al.
   "Fast Algorithms for the Maximum Clique Problem on Massive Graphs
   with Applications to Overlapping Community Detection."
   *Internet Mathematics* 11.4-5 (2015): 421--448.
   <https://doi.org/10.1080/15427951.2014.986778>

See also
--------

:func:`networkx.algorithms.approximation.clique.max_clique`
    A function that returns an approximate maximum clique with a
    guarantee on the approximation ratio.

:mod:`networkx.algorithms.clique`
    Functions for finding the exact maximum clique in a graph.

c                    > U(       d  [        X25      $ [        UTS9nUR                  U5        X    Vs1 s H  nTU   U:  d  M  UiM     nnT" XU-  US-   U5      $ s  snf )Nr      )r   remove)	r   Usize	best_sizeuvN_prime_clique_heuristicdegreess	          r   r.   ,large_clique_size.<locals>._clique_heuristic   se    y''w	d>dgajI&=1d> K9EE ?s   AAr   c              3   >   >#    U  H  nTU   T:  d  M  Uv   M     g 7f)N ).0r+   r*   r/   s     r   	<genexpr>$large_clique_size.<locals>.<genexpr>   s     51WQZ94QQs   	r&   )degree)r   nodesr+   r,   	neighborsr.   r*   r/   s        @@@r   r   r      sm    x hhGF I55E !@1
i(?Q	@%aAyA	   As   AA)__doc__networkxr   !networkx.algorithms.approximationr   networkx.utilsr   __all___dispatchabler   r   r   r   r2   r   r   <module>r?      s    I  4 . Z \"3  # !3l Z \"5  # !5p Z \"/  # !/d Z \"H  # !Hr   