K i|dZddlZddlmZddgZededej dZededej d Zededej d Z y) ar Algorithms for asteroidal triples and asteroidal numbers in graphs. An asteroidal triple in a graph G is a set of three non-adjacent vertices u, v and w such that there exist a path between any two of them that avoids closed neighborhood of the third. More formally, v_j, v_k belongs to the same connected component of G - N[v_i], where N[v_i] denotes the closed neighborhood of v_i. A graph which does not contain any asteroidal triples is called an AT-free graph. The class of AT-free graphs is a graph class for which many NP-complete problems are solvable in polynomial time. Amongst them, independent set and coloring. N)not_implemented_for is_at_freefind_asteroidal_tripledirected multigraphct|j}t|dkryt|}t j |D]\}}t||j |g}t||j |g}|j |}||z D]@}||||||k(s||||||k(s'||||||k(s9|||gccSy)uFind an asteroidal triple in the given graph. An asteroidal triple is a triple of non-adjacent vertices such that there exists a path between any two of them which avoids the closed neighborhood of the third. It checks all independent triples of vertices and whether they are an asteroidal triple or not. This is done with the help of a data structure called a component structure. A component structure encodes information about which vertices belongs to the same connected component when the closed neighborhood of a given vertex is removed from the graph. The algorithm used to check is the trivial one, outlined in [1]_, which has a runtime of :math:`O(|V||\overline{E} + |V||E|)`, where the second term is the creation of the component structure. Parameters ---------- G : NetworkX Graph The graph to check whether is AT-free or not Returns ------- list or None An asteroidal triple is returned as a list of nodes. If no asteroidal triple exists, i.e. the graph is AT-free, then None is returned. Notes ----- The component structure and the algorithm is described in [1]_. The current implementation implements the trivial algorithm for simple graphs. References ---------- .. [1] Ekkehard Köhler, "Recognizing Graphs without asteroidal triples", Journal of Discrete Algorithms 2, pages 439-452, 2004. https://www.sciencedirect.com/science/article/pii/S157086670400019X N)setnodeslencreate_component_structurenx non_edgesunion) GVcomponent_structureuvu_neighborhoodv_neighborhoodunion_of_neighborhoodsws d/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/algorithms/asteroidal.pyrrsR AGG A 1vz4Q7 Q !1QqT!-QqT!-!/!5!5n!E++ !A $A&q)-@-CA-FF'*1-1DQ1G1JJ'*1-1DQ1G1JJ1ay  ! ! ct|duS)aCheck if a graph is AT-free. The method uses the `find_asteroidal_triple` method to recognize an AT-free graph. If no asteroidal triple is found the graph is AT-free and True is returned. If at least one asteroidal triple is found the graph is not AT-free and False is returned. Parameters ---------- G : NetworkX Graph The graph to check whether is AT-free or not. Returns ------- bool True if G is AT-free and False otherwise. Examples -------- >>> G = nx.Graph([(0, 1), (0, 2), (1, 2), (1, 3), (1, 4), (4, 5)]) >>> nx.is_at_free(G) True >>> G = nx.cycle_graph(6) >>> nx.is_at_free(G) False N)r)rs rrrVs> "! $ ,,rcLt|j}i}|D]}d}t||j|h}i}|D]}d||< |jt|j|z }t j |D]} |dz }| D]}|||< |||<|S)aCreate component structure for G. A *component structure* is an `nxn` array, denoted `c`, where `n` is the number of vertices, where each row and column corresponds to a vertex. .. math:: c_{uv} = \begin{cases} 0, if v \in N[u] \\ k, if v \in component k of G \setminus N[u] \end{cases} Where `k` is an arbitrary label for each component. The structure is used to simplify the detection of asteroidal triples. Parameters ---------- G : NetworkX Graph Undirected, simple graph. Returns ------- component_structure : dictionary A dictionary of dictionaries, keyed by pairs of vertices. r)r r rsubgraphrconnected_components) rrrrlabelclosed_neighborhoodrow_dictr G_reducedccs rr r xs6 AGG A  *!!A$iooqc2$ AHQK JJs177|.AAB )))4 $B QJE $#  $ $ "*A * r) __doc__networkxrnetworkx.utilsr__all__ _dispatchablerrr rrr,s . 1 2Z \"<#!<~Z \"-#!->Z \")#!)r