
    hF%                     V   S r SSKrSSKJr  / SQr\" S5      \R                  S 5       5       r\" S5      \R                  SS j5       5       r\" S5      \R                  S 5       5       r	\" S5      \R                  S	 5       5       r
\" S5      \R                  " S
S9SS j5       5       rg)zStrongly connected components.    N)not_implemented_for)$number_strongly_connected_componentsstrongly_connected_componentsis_strongly_connected&kosaraju_strongly_connected_componentscondensation
undirectedc              #     #    0 n0 n[        5       n/ nSnU  Vs0 s H  of[        X   5      _M     nnU  GH;  nX;  d  M  U/n	U	(       d  M  U	S   nXa;  a	  US-   nXQU'   Sn
Xv    H  nX;  d  M
  U	R                  U5        Sn
  O   U
(       a  X   X&'   X    H<  nX;  d  M
  X   X   :  a  [        X&   X+   /5      X&'   M)  [        X&   X   /5      X&'   M>     U	R	                  5         X&   X   :X  ad  U1nU(       aD  XS      X   :  a7  UR	                  5       nUR                  U5        U(       a  XS      X   :  a  M7  UR                  U5        Uv   OUR                  U5        U	(       a  GM$  GM>     gs  snf 7f)a  Generate nodes in strongly connected components of graph.

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(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)..

r      TFN)setiterappendminpopaddupdate)Gpreorderlowlink	scc_found	scc_queueiv	neighborssourcequeuedonewsccks                 c/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/components/strongly_connected.pyr   r      s    v HGII	A()*1DJI*"HE%"I$AA"#QK"A(Q$	 &
 !)GJT-'{X[8-0'*gj1I-J
-0'*hk1J-K
 " IIKzX[0 c'Hr],Chk,Q )AGGAJ (Hr],Chk,Q "((-!	!((+9 %  +s3   E:E5E:E:E:..E: BE:>.E:/E:c              #   X  #    [        [        R                  " U R                  SS9US95      n[	        5       nU(       ae  UR                  5       nXC;   a  M  [        R                  " X5      nU Vs1 s H  ofU;  d  M
  UiM     nnUR                  U5        Uv   U(       a  Md  ggs  snf 7f)a}  Generate nodes in strongly connected components of graph.

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.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.

F)copy)r   N)listnxdfs_postorder_nodesreverser   r   dfs_preorder_nodesr   )r   r   postseenrcr   news           r"   r   r   r   s     b &&qyyey'<VLMD5D
HHJ9!!!'-!Q}q!-C	 $
 .s   A.B*0	B%=B%B*#B*c                 8    [        S [        U 5       5       5      $ )aE  Returns 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.

Examples
--------
>>> G = nx.DiGraph(
...     [(0, 1), (1, 2), (2, 0), (2, 3), (4, 5), (3, 4), (5, 6), (6, 3), (6, 7)]
... )
>>> nx.number_strongly_connected_components(G)
3

See Also
--------
strongly_connected_components
number_connected_components
number_weakly_connected_components

Notes
-----
For directed graphs only.
c              3   &   #    U  H  nS v   M	     g7f)r   N ).0r    s     r"   	<genexpr>7number_strongly_connected_components.<locals>.<genexpr>   s     =<Sq<s   )sumr   r   s    r"   r   r      s    L =9!<===    c                     [        U 5      S:X  a  [        R                  " S5      e[        [        [	        U 5      5      5      [        U 5      :H  $ )a  Test directed graph for strong connectivity.

A directed graph is strongly connected if and only if every vertex in
the graph is reachable from every other vertex.

Parameters
----------
G : NetworkX Graph
   A directed graph.

Returns
-------
connected : bool
  True if the graph is strongly connected, False otherwise.

Examples
--------
>>> G = nx.DiGraph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 2)])
>>> nx.is_strongly_connected(G)
True
>>> G.remove_edge(2, 3)
>>> nx.is_strongly_connected(G)
False

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.
r   z-Connectivity is undefined for the null graph.)lenr&   NetworkXPointlessConceptnextr   r6   s    r"   r   r      sG    X 1v{))?
 	
 t1!456#a&@@r7   T)returns_graphc                   ^^ Uc  [         R                  " U 5      n0 m0 n[         R                  " 5       nTUR                  S'   [	        U 5      S:X  a  U$ [        U5       H%  u  mnXBT'   TR                  U4S jU 5       5        M'     TS-   nUR                  [        U5      5        UR                  U4S jU R                  5        5       5        [         R                  " X2S5        U$ )a  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.

Examples
--------
Contracting two sets of strongly connected nodes into two distinct SCC
using the barbell graph.

>>> G = nx.barbell_graph(4, 0)
>>> G.remove_edge(3, 4)
>>> G = nx.DiGraph(G)
>>> H = nx.condensation(G)
>>> H.nodes.data()
NodeDataView({0: {'members': {0, 1, 2, 3}}, 1: {'members': {4, 5, 6, 7}}})
>>> H.graph["mapping"]
{0: 0, 1: 0, 2: 0, 3: 0, 4: 1, 5: 1, 6: 1, 7: 1}

Contracting a complete graph into one single SCC.

>>> G = nx.complete_graph(7, create_using=nx.DiGraph)
>>> H = nx.condensation(G)
>>> H.nodes
NodeView((0,))
>>> H.nodes.data()
NodeDataView({0: {'members': {0, 1, 2, 3, 4, 5, 6}}})

Notes
-----
After contracting all strongly connected components to a single node,
the resulting graph is a directed acyclic graph.

mappingr   c              3   *   >#    U  H  oT4v   M
     g 7fNr1   )r2   nr   s     r"   r3   condensation.<locals>.<genexpr>W  s     1y!1vys   r   c              3   X   >#    U  H  u  pTU   TU   :w  d  M  TU   TU   4v   M!     g 7fr@   r1   )r2   ur   r>   s      r"   r3   rB   Z  s6      -6TQ'!*PQ
:R WQZ Ys   **members)r&   r   DiGraphgraphr9   	enumerater   add_nodes_fromrangeadd_edges_fromedgesset_node_attributes)r   r    rE   C	componentnumber_of_componentsr   r>   s         @@r"   r   r     s    ~ {..q1GG


A AGGI
1v{!#9
1y11 ' q5U/01 -.WWY  1y1Hr7   r@   )__doc__networkxr&   networkx.utils.decoratorsr   __all___dispatchabler   r   r   r   r   r1   r7   r"   <module>rV      s    $  9 \"^,  #^,B \"9  #9x \"$>  #$>N \"/A  #/Ad \"%P & #Pr7   