
    sh                     L   S r SSKrSSKJrJr  SSKJrJr  SSKr	/ SQr
\	R                  SS j5       r\	R                  S 5       r\	R                  R                  S5      \	R                  S	 5       5       r\	R                  R                  S5      \	R                  S
 5       5       rg)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                 ^   [        U 5      nUc  [        R                  R                  U5      nX;  a  [        R                  " SU S35      eU1n[        X   5      nX$-
  U-
  nU(       aB  UR                  5       n[        X   5      U-
  nUR                  U5        XG-  nXW-  nU(       a  MB  U$ )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

z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           r/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/networkx/algorithms/dominating.pyr   r      s    J AIXX//	:
zl,?@@ \N!-(O1NBO
!qt9~5 	1++ /     c                    ^  U Vs1 s H  o"T ;   d  M
  UiM     nn[        [        R                  " U 4S jU 5       5      5      n[        [        T 5      U-
  U-
  5      S:H  $ s  snf )a  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              3   .   >#    U  H
  nTU   v   M     g 7fN ).0nr   s     r   	<genexpr>$is_dominating_set.<locals>.<genexpr>i   s     "9A1Q4s   r   )r   r   from_iterablelen)r   nbunchr!   testsetnbrss   `    r   r   r   K   s[    : !+&QFq&G+u"""9"99:Ds1v$&'1,, ,s
   	A!A!directedc                    [        U 5      S:X  a
  [        5       $ [        R                  " U 5      (       d  [        R                  " S5      e[        U 5      S:X  a  [        U 5      $ U R
                  n[        5       n[        U R                  5      n[        UR                  5       S S9u  pEX    H  nX6==   S-  ss'   M     [        U 5      U1-
  nU* [        U5      U4/n[        5       n	U(       a  [        U5      u  pnU
* X<   :  a  [        XU   * X45        M1  X    HM  nX;   d  M
  UR                  U5        X    H  nX6==   S-  ss'   M     [        XU   * [        U5      U45        MO     U	R                  U5        U(       a  M  U	$ )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                     U S   $ )Nr+   r   )xs    r   <lambda>*connected_dominating_set.<locals>.<lambda>   s    qtr   )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|   ^ 1v{u??1<==
1v{1vVVF 	A NM "-"5"5"7^L\#a $ V|n$F XtAw-.D"u #DMq8m&&T1--s67A{a !9C!&!+& %q!1 147A>?  	!$$Q' &  $#r   c                     [         R                  " X5      =(       a*    [         R                  " [         R                  " X5      5      $ )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   r1   subgraph)r   r&   s     r   r
   r
      s,    : *Vrr{{1?U/VVr   r   )__doc__mathheapqr   r   	itertoolsr   r   networkxr   __all___dispatchabler   r   r   not_implemented_forr	   r
   r   r   r   <module>rN      s    9  # "  6 6r - -B j)}$  *}$@ j)W  *Wr   