K iJdZddlZddlmZmZddlmZmZddlZ gdZ e jd dZ e jdZ e jjde jd Ze jjde jd Zy) z3Functions for computing dominating sets in a graph.N)heappopheappush)chaincount)dominating_setis_dominating_setconnected_dominating_setis_connected_dominating_setcLt|}|tjj|}||vrtjd|d|h}t||}||z |z }|r?|j }t|||z }|j |||z}||z}|r?|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 znode z is not in G)setnxutilsarbitrary_element NetworkXErrorpopadd)G start_with all_nodesrdominated_nodesremaining_nodesvundominated_nbrss d/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/algorithms/dominating.pyrrsJAIXX// : zl,?@@ \N!J-(O/1NBO     !qt9~5 1++++  c|Dchc] }|vs| }}ttjfd|D}tt|z |z dk(Scc}w)aPChecks 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 c3(K|] }| ywN).0nrs r z$is_dominating_set..is"9A1Q4"9sr)r r from_iterablelen)rnbunchr!testsetnbrss` rrrKs\:!+QAFq+G+ u"""9"99 :D s1v$& '1 ,,,s AAdirectedct|dk(r tStj|stjdt|dk(r t|S|j }t }t|j}t|jd\}}||D]}||xxdzcc<t||hz }| t||fg}t} |rt|\} } } | || kDrt|||  | | f.|| D]K} | |vs|j| || D]}||xxdzcc<t|||  t|| fM| j| |r| 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. rzG must be a connected graphc |dS)Nr*r)xs rz*connected_dominating_set..s qtr)key)r$r r is_connectedr_adjrdictdegreemaxitemsnextrrremover)rG_succc unseen_degree max_deg_nodemax_degnbrunseenseenr neg_degcnturs rr r ms^ 1v{u ??1 <== 1v{1v VVF ANM"-"5"5"7^L\7l# ca V|n $FXtAw - .D"u #DM#q 8mA& & T]1--sA6 7  @AF{ a !!9,C!#&!+&,q!1 147A>?  @ !$$Q'  $#rctj||xr)tjtj||S)aChecks 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 rr/subgraph)rr%s rr r s0:   6 * Vrr{{1f?U/VVrr)__doc__mathheapqrr itertoolsrrnetworkxr __all__ _dispatchablerrrnot_implemented_forr r rrrrLs9 #" 66r--Bj)}$*}$@j)W*Wr