
    \Mh                     D   d Z ddlZddlmZmZ ddlmZmZ ddlZ	g dZ
e	j        dd            Ze	j        d             Ze	j                            d          e	j        d	                         Ze	j                            d          e	j        d
                         ZdS )z3Functions for computing dominating sets in a graph.    N)heappopheappush)chaincount)dominating_setis_dominating_setconnected_dominating_setis_connected_dominating_setc                 |   t          |           }|t          j                            |          }|| vrt          j        d| d          |h}t          | |                   }||z
  |z
  }|rM|                                }t          | |                   |z
  }|                    |           ||z  }||z  }|M|S )a\  Finds a dominating set for the graph G.

    A *dominating set* for a graph with node set *V* is a subset *D* of
    *V* such that every node not in *D* is adjacent to at least one
    member of *D* [1]_.

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

    start_with : node (default=None)
        Node to use as a starting point for the algorithm.

    Returns
    -------
    D : set
        A dominating set for G.

    Notes
    -----
    This function is an implementation of algorithm 7 in [2]_ which
    finds some dominating set, not necessarily the smallest one.

    See also
    --------
    is_dominating_set

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Dominating_set

    .. [2] Abdol-Hossein Esfahanian. Connectivity Algorithms.
        http://www.cse.msu.edu/~cse835/Papers/Graph_connectivity_revised.pdf

    Nznode z is not in G)setnxutilsarbitrary_elementNetworkXErrorpopadd)G
start_with	all_nodesr   dominated_nodesremaining_nodesvundominated_nbrss           ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/networkx/algorithms/dominating.pyr   r      s    J AIX//	::
?z???@@@ \N!J-((O/1NBO
 	,!!qt99~5 	1++++  	,     c                       fd|D             }t          t          j         fd|D                                 }t          t                     |z
  |z
            dk    S )aP  Checks if `nbunch` is a dominating set for `G`.

    A *dominating set* for a graph with node set *V* is a subset *D* of
    *V* such that every node not in *D* is adjacent to at least one
    member of *D* [1]_.

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

    nbunch : iterable
        An iterable of nodes in the graph `G`.

    Returns
    -------
    dominating : bool
        True if `nbunch` is a dominating set of `G`, false otherwise.

    See also
    --------
    dominating_set

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Dominating_set

    c                     h | ]}|v |	S  r   .0nr   s     r   	<setcomp>z$is_dominating_set.<locals>.<setcomp>h   s    +++QAFFqFFFr   c              3   (   K   | ]}|         V  d S Nr   r   s     r   	<genexpr>z$is_dominating_set.<locals>.<genexpr>i   s'      "9"9A1Q4"9"9"9"9"9"9r   r   )r   r   from_iterablelen)r   nbunchtestsetnbrss   `   r   r   r   K   sn    : ,+++&+++Gu""9"9"9"9"9"9"999::Ds1vv$&''1,,r   directedc                    t          |           dk    rt                      S t          j        |           st          j        d          t          |           dk    rt          |           S | j        }t                      }t          | j                  }t          |
                                d           \  }}||         D ]}||xx         dz  cc<   t          |           |hz
  }| t          |          |fg}t                      }	|rt          |          \  }
}}|
 ||         k    rt          |||          ||f           =||         D ]]}||v rW|                    |           ||         D ]}||xx         dz  cc<   t          |||          t          |          |f           ^|	                    |           ||	S )a"
  Returns a connected dominating set.

    A *dominating set* for a graph *G* with node set *V* is a subset *D* of *V*
    such that every node not in *D* is adjacent to at least one member of *D*
    [1]_. A *connected dominating set* is a dominating set *C* that induces a
    connected subgraph of *G* [2]_.
    Note that connected dominating sets are not unique in general and that there
    may be other connected dominating sets.

    Parameters
    ----------
    G : NewtorkX graph
        Undirected connected graph.

    Returns
    -------
    connected_dominating_set : set
        A dominating set of nodes which induces a connected subgraph of G.

    Raises
    ------
    NetworkXNotImplemented
        If G is directed.

    NetworkXError
        If G is disconnected.

    Examples
    ________
    >>> G = nx.Graph(
    ...     [
    ...         (1, 2),
    ...         (1, 3),
    ...         (1, 4),
    ...         (1, 5),
    ...         (1, 6),
    ...         (2, 7),
    ...         (3, 8),
    ...         (4, 9),
    ...         (5, 10),
    ...         (6, 11),
    ...         (7, 12),
    ...         (8, 12),
    ...         (9, 12),
    ...         (10, 12),
    ...         (11, 12),
    ...     ]
    ... )
    >>> nx.connected_dominating_set(G)
    {1, 2, 3, 4, 5, 6, 7}

    Notes
    -----
    This function implements Algorithm I in its basic version as described
    in [3]_. The idea behind the algorithm is the following: grow a tree *T*,
    starting from a node with maximum degree. Throughout the growing process,
    nonleaf nodes in *T* are our connected dominating set (CDS), leaf nodes in
    *T* are marked as "seen" and nodes in G that are not yet in *T* are marked as
    "unseen". We maintain a max-heap of all "seen" nodes, and track the number
    of "unseen" neighbors for each node. At each step we pop the heap top -- a
    "seen" (leaf) node with maximal number of "unseen" neighbors, add it to the
    CDS and mark all its "unseen" neighbors as "seen". For each one of the newly
    created "seen" nodes, we also decrement the number of "unseen" neighbors for
    all its neighbors. The algorithm terminates when there are no more "unseen"
    nodes.
    Runtime complexity of this implementation is $O(|E|*log|V|)$ (amortized).

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Dominating_set
    .. [2] https://en.wikipedia.org/wiki/Connected_dominating_set
    .. [3] Guha, S. and Khuller, S.
           *Approximation Algorithms for Connected Dominating Sets*,
           Algorithmica, 20, 374-387, 1998.

    r   zG must be a connected graph   c                     | d         S )Nr-   r   )xs    r   <lambda>z*connected_dominating_set.<locals>.<lambda>   s
    qt r   )key)r'   r   r   is_connectedr   _adjr   dictdegreemaxitemsnextr   r   remover   )r   G_succcunseen_degreemax_deg_nodemax_degnbrunseenseenr	   neg_degcntur   s                 r   r	   r	   m   s   ^ 1vv{{uu?1 ><===
1vv{{1vvVF 	A NNM "-"5"5"7"7^^LLL\7l#    ca VV|n$F XtAww-.D"uu  (#DMM#q8mA&&&T]1--sA6777 	@ 	@AF{{a   !!9 , ,C!#&&&!+&&&&q!1 1477A>??? $$Q'''  (  $#r   c                 z    t          j        | |          o&t          j        t          j        | |                    S )a  Checks if `nbunch` is a connected dominating set for `G`.

    A *dominating set* for a graph *G* with node set *V* is a subset *D* of
    *V* such that every node not in *D* is adjacent to at least one
    member of *D* [1]_. A *connected dominating set* is a dominating
    set *C* that induces a connected subgraph of *G* [2]_.

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

    nbunch : iterable
        An iterable of nodes in the graph `G`.

    Returns
    -------
    connected_dominating : bool
        True if `nbunch` is connected dominating set of `G`, false otherwise.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Dominating_set
    .. [2] https://en.wikipedia.org/wiki/Connected_dominating_set

    )r   r   r2   subgraph)r   r(   s     r   r
   r
      s2    : 6**Vrr{1f?U?U/V/VVr   r$   )__doc__mathheapqr   r   	itertoolsr   r   networkxr   __all___dispatchabler   r   r   not_implemented_forr	   r
   r   r   r   <module>rO      s-   9 9  # # # # # # # # " " " " " " " "       6 6 6 6r - - -B j))}$ }$  *)}$@ j))W W  *)W W Wr   