ó žÃÒYc@sÝdZddlZddlmZdddddd d gZed ƒd „ƒZed ƒdd „ƒZed ƒd„ƒZ ed ƒe d„ƒZ ed ƒd„ƒZ ed ƒd„ƒZ ed ƒdd„ƒZdS(sStrongly connected components.iÿÿÿÿN(tnot_implemented_fort$number_strongly_connected_componentststrongly_connected_componentst&strongly_connected_component_subgraphstis_strongly_connectedt'strongly_connected_components_recursivet&kosaraju_strongly_connected_componentst condensationt undirectedccsçi}i}i}g}d}xÂ|D]º}||kr%|g}xœ|rÛ|d}||krv|d}|||>> G = nx.cycle_graph(4, create_using=nx.DiGraph()) >>> nx.add_cycle(G, [10, 11, 12]) >>> [len(c) for c in sorted(nx.strongly_connected_components(G), ... key=len, reverse=True)] [4, 3] If you only want the largest component, it's more efficient to use max instead of sort. >>> largest = max(nx.strongly_connected_components(G), key=len) See Also -------- connected_components weakly_connected_components kosaraju_strongly_connected_components Notes ----- Uses Tarjan's algorithm[1]_ with Nuutila's modifications[2]_. Nonrecursive version of algorithm. References ---------- .. [1] Depth-first search and linear graph algorithms, R. Tarjan SIAM Journal of Computing 1(2):146-160, (1972). .. [2] On finding the strongly connected components in a directed graph. E. Nuutila and E. Soisalon-Soinen Information Processing Letters 49(1): 9-14, (1994).. iiÿÿÿÿiN(tappendtmintpoptTruetadd(tGtpreordertlowlinkt scc_foundt scc_queuetitsourcetqueuetvtdonetv_nbrstwtscctk((s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/components/strongly_connected.pyRsJ8               !%   !  c #s¦tjj|ƒ ttj|d|ƒƒ}WdQXtƒ‰xb|r¡|jƒ}|ˆkrdq@ntj||ƒ}‡fd†|Dƒ}|Vˆj|ƒq@WdS(séGenerate nodes in strongly connected components of graph. Parameters ---------- G : NetworkX Graph A directed graph. Returns ------- comp : generator of sets A genrator of sets of nodes, one for each strongly connected component of G. Raises ------ NetworkXNotImplemented: If G is undirected. Examples -------- Generate a sorted list of strongly connected components, largest first. >>> G = nx.cycle_graph(4, create_using=nx.DiGraph()) >>> nx.add_cycle(G, [10, 11, 12]) >>> [len(c) for c in sorted(nx.kosaraju_strongly_connected_components(G), ... key=len, reverse=True)] [4, 3] If you only want the largest component, it's more efficient to use max instead of sort. >>> largest = max(nx.kosaraju_strongly_connected_components(G), key=len) See Also -------- strongly_connected_components Notes ----- Uses Kosaraju's algorithm. RNcs"h|]}|ˆkr|’qS(((t.0R(tseen(s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/components/strongly_connected.pys °s ( tnxtutilstreversedtlisttdfs_postorder_nodestsetR tdfs_preorder_nodestupdate(RRtposttrtctnew((Rs‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/components/strongly_connected.pyR{s,!    c#s‡‡‡‡‡‡fd†‰i‰i‰i‰d}g‰x<ˆD]4}|ˆkrCxˆ||ƒD] }|VqeWqCqCWdS(szGenerate nodes in strongly connected components of graph. Recursive version of algorithm. Parameters ---------- G : NetworkX Graph A directed graph. Returns ------- comp : generator of sets A generator of sets of nodes, one for each strongly connected component of G. Raises ------ NetworkXNotImplemented : If G is undirected. Examples -------- Generate a sorted list of strongly connected components, largest first. >>> G = nx.cycle_graph(4, create_using=nx.DiGraph()) >>> nx.add_cycle(G, [10, 11, 12]) >>> [len(c) for c in sorted(nx.strongly_connected_components_recursive(G), ... key=len, reverse=True)] [4, 3] If you only want the largest component, it's more efficient to use max instead of sort. >>> largest = max(nx.strongly_connected_components_recursive(G), key=len) See Also -------- connected_components Notes ----- Uses Tarjan's algorithm[1]_ with Nuutila's modifications[2]_. References ---------- .. [1] Depth-first search and linear graph algorithms, R. Tarjan SIAM Journal of Computing 1(2):146-160, (1972). .. [2] On finding the strongly connected components in a directed graph. E. Nuutila and E. Soisalon-Soinen Information Processing Letters 49(1): 9-14, (1994).. c3s|ˆ|<|ˆ|<|d7}ˆj|ƒxjˆ|D]^}|ˆkrjxˆ||ƒD] }|VqXWn|ˆkr6tˆ|ˆ|ƒˆ|>> G = nx.cycle_graph(4, create_using=nx.DiGraph()) >>> nx.add_cycle(G, [10, 11, 12]) >>> [len(Gc) for Gc in sorted(nx.strongly_connected_component_subgraphs(G), ... key=len, reverse=True)] [4, 3] If you only want the largest component, it's more efficient to use max instead of sort. >>> Gc = max(nx.strongly_connected_component_subgraphs(G), key=len) See Also -------- strongly_connected_components connected_component_subgraphs weakly_connected_component_subgraphs N(Rtsubgraphtcopy(RR3tcomp((s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/components/strongly_connected.pyR s-cCsttt|ƒƒƒS(sêReturn number of strongly connected components in graph. Parameters ---------- G : NetworkX graph A directed graph. Returns ------- n : integer Number of strongly connected components Raises ------ NetworkXNotImplemented: If G is undirected. See Also -------- strongly_connected_components number_connected_components number_weakly_connected_components Notes ----- For directed graphs only. (tlenR!R(R((s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/components/strongly_connected.pyR@scCsJt|ƒdkr$tjdƒ‚nttt|ƒƒdƒt|ƒkS(sþTest directed graph for strong connectivity. Parameters ---------- G : NetworkX Graph A directed graph. Returns ------- connected : bool True if the graph is strongly connected, False otherwise. Raises ------ NetworkXNotImplemented: If G is undirected. See Also -------- is_weakly_connected is_semiconnected is_connected is_biconnected strongly_connected_components Notes ----- For directed graphs only. is-Connectivity is undefined for the null graph.(R5RtNetworkXPointlessConceptR!R(R((s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/components/strongly_connected.pyR`s csä|dkrtj|ƒ}ni‰i}tjƒ}d‰xAt|ƒD]3\‰}||ˆ<ˆj‡fd†|DƒƒqIWˆd}|jt|ƒƒ|j‡fd†|j ƒDƒƒtj ||dƒˆ|j d<|S(sÙReturns the condensation of G. The condensation of G is the graph with each of the strongly connected components contracted into a single node. Parameters ---------- G : NetworkX DiGraph A directed graph. scc: list or generator (optional, default=None) Strongly connected components. If provided, the elements in `scc` must partition the nodes in `G`. If not provided, it will be calculated as scc=nx.strongly_connected_components(G). Returns ------- C : NetworkX DiGraph The condensation graph C of G. The node labels are integers corresponding to the index of the component in the list of strongly connected components of G. C has a graph attribute named 'mapping' with a dictionary mapping the original nodes to the nodes in C to which they belong. Each node in C also has a node attribute 'members' with the set of original nodes in G that form the SCC that the node in C represents. Raises ------ NetworkXNotImplemented: If G is undirected. Notes ----- After contracting all strongly connected components to a single node, the resulting graph is a directed acyclic graph. ic3s|]}|ˆfVqdS(N((Rtn(R(s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/components/strongly_connected.pys µsic3s=|]3\}}ˆ|ˆ|krˆ|ˆ|fVqdS(N((RtuR(tmapping(s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/components/strongly_connected.pys ¸s tmembersR9N( tNoneRRtDiGrapht enumerateR%tadd_nodes_fromtrangetadd_edges_fromtedgestset_node_attributestgraph(RRR:tCR-tnumber_of_components((RR9s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/components/strongly_connected.pyR†s'   ! # (t__doc__tnetworkxRtnetworkx.utils.decoratorsRt__all__RR;RRR RRRR(((s‡/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/components/strongly_connected.pyt s&  a 9W 3 &