ó
|£*^c           @   sõ   d  Z  d d l Z d d l m Z d j d d d g ƒ Z d d	 d
 d d d d g Z e d ƒ d „  ƒ Z e d ƒ d d „ ƒ Z
 e d ƒ d „  ƒ Z e d ƒ e d „ ƒ Z e d ƒ d „  ƒ Z e d ƒ d „  ƒ Z e d ƒ d d „ ƒ Z d S(   s   Strongly connected components.
iÿÿÿÿN(   t   not_implemented_fors   
s
   Eben Kenahs2   Aric Hagberg (hagberg@lanl.gov)Christopher Ellisons!   Ben Edwards (bedwards@cs.unm.edu)t$   number_strongly_connected_componentst   strongly_connected_componentst&   strongly_connected_component_subgraphst   is_strongly_connectedt'   strongly_connected_components_recursivet&   kosaraju_strongly_connected_componentst   condensationt
   undirectedc         c   sç  i  } i  } i  } g  } d } xÂ|  D]º} | | k r% | g } xœ| rÛ| d } | | k rv | d } | | | <n  d }	 |  | }
 x1 |
 D]) } | | k r� | j  | ƒ d }	 Pq� q� W|	 d k rC | | | | <xp |
 D]h } | | k rÛ | | | | k r"t | | | | g ƒ | | <qCt | | | | g ƒ | | <qÛ qÛ W| j ƒ  | | | | k rÈt | | <| h } xE | r¿| | d | | k r¿| j ƒ  } t | | <| j | ƒ q{W| VqØ| j  | ƒ qC qC Wq% q% Wd S(   s}  Generate nodes in strongly connected components of graph.

    Parameters
    ----------
    G : NetworkX Graph
        An 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())
    >>> G.add_cycle([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

    Notes
    -----
    Uses Tarjan's algorithm with Nuutila's modifications.
    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)..

    i    iÿÿÿÿi   N(   t   appendt   mint   popt   Truet   add(   t   Gt   preordert   lowlinkt	   scc_foundt	   scc_queuet   it   sourcet   queuet   vt   donet   v_nbrst   wt   scct   k(    (    s…   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/components/strongly_connected.pyR      sJ    7		


!%

	!
c      	   #   s¦   t  j j |  ƒ �  t t  j |  d | ƒƒ } Wd QXt ƒ  ‰  xb | r¡ | j ƒ  } | ˆ  k rd q@ n  t  j |  | ƒ } ‡  f d †  | Dƒ } | Vˆ  j | ƒ q@ Wd S(   sý  Generate nodes in strongly connected components of graph.

    Parameters
    ----------
    G : NetworkX Graph
        An 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())
    >>> G.add_cycle([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
    --------
    connected_components
    weakly_connected_components

    Notes
    -----
    Uses Kosaraju's algorithm.

    R   Nc            s"   h  |  ] } | ˆ  k r | ’ q S(    (    (   t   .0R   (   t   seen(    s…   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/components/strongly_connected.pys	   <setcomp>±   s   	 (	   t   nxt   utilst   reversedt   listt   dfs_postorder_nodest   setR   t   dfs_preorder_nodest   update(   R   R   t   postt   rt   ct   new(    (   R   s…   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/components/strongly_connected.pyR   {   s    -!		c         #   s   ‡  ‡ ‡ ‡ ‡ ‡ f d †  ‰ i  ‰ i  ‰ i  ‰ d } g  ‰ x< ˆ  D]4 } | ˆ k rC x ˆ | | ƒ D] } | Vqe WqC qC Wd S(   sm  Generate nodes in strongly connected components of graph.

    Recursive version of algorithm.

    Parameters
    ----------
    G : NetworkX Graph
        An 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())
    >>> G.add_cycle([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 with Nuutila's modifications.

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

    c         3   s  | ˆ |  <| ˆ |  <| d 7} ˆ j  |  ƒ xj ˆ  |  D]^ } | ˆ k rj x ˆ | | ƒ D] } | VqX Wn  | ˆ k r6 t ˆ |  ˆ | ƒ ˆ |  <q6 q6 Wˆ |  ˆ |  k rˆ |  ˆ |  <|  h } x; ˆ d |  k r ˆ j ƒ  } ˆ |  ˆ | <| j | ƒ qÆ Wˆ j |  ƒ | Vn  d  S(   Ni   iÿÿÿÿ(   R	   R
   R   R   t   remove(   R   t   cntR   R(   t   tmpc(   R   t	   componentt   roott   stackt   visitt   visited(    s…   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/components/strongly_connected.pyR0   í   s&    


"	i    N(    (   R   R+   R   R(   (    (   R   R-   R.   R/   R0   R1   s…   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/components/strongly_connected.pyR   ¶   s    7c         c   sF   x? t  |  ƒ D]1 } | r0 |  j | ƒ j ƒ  Vq |  j | ƒ Vq Wd S(   sñ  Generate strongly connected components as subgraphs.

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

    copy : boolean, optional
        if copy is True, Graph, node, and edge attributes are copied to
        the subgraphs.

    Returns
    -------
    comp : generator of graphs
      A generator of graphs, one for each strongly connected component of G.

    Examples
    --------
    Generate a sorted list of strongly connected components, largest first.

    >>> G = nx.cycle_graph(4, create_using=nx.DiGraph())
    >>> G.add_cycle([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
    --------
    connected_component_subgraphs
    weakly_connected_component_subgraphs

    N(   R   t   subgrapht   copy(   R   R3   t   comp(    (    s…   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/components/strongly_connected.pyR     s    'c         C   s   t  t t |  ƒ ƒ ƒ S(   sK  Return number of strongly connected components in graph.

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

    Returns
    -------
    n : integer
       Number of strongly connected components

    See Also
    --------
    connected_components

    Notes
    -----
    For directed graphs only.
    (   t   lenR!   R   (   R   (    (    s…   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/components/strongly_connected.pyR   ;  s    c         C   sJ   t  |  ƒ d k r$ t j d ƒ ‚ n  t  t t |  ƒ ƒ d ƒ t  |  ƒ k S(   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.

    See Also
    --------
    strongly_connected_components

    Notes
    -----
    For directed graphs only.
    i    s-   Connectivity is undefined for the null graph.(   R5   R   t   NetworkXPointlessConceptR!   R   (   R   (    (    s…   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/components/strongly_connected.pyR   T  s    c            sÞ   | d k r t j |  ƒ } n  i  ‰ i  } t j ƒ  } xA t | ƒ D]3 \ ‰  } | | ˆ  <ˆ j ‡  f d †  | Dƒ ƒ qC Wˆ  d } | j t | ƒ ƒ | j ‡ f d †  |  j	 ƒ  Dƒ ƒ t j
 | 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 not directed

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

    c         3   s   |  ] } | ˆ  f Vq d  S(   N(    (   R   t   n(   R   (    s…   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/components/strongly_connected.pys	   <genexpr>Ÿ  s    i   c         3   s=   |  ]3 \ } } ˆ  | ˆ  | k r ˆ  | ˆ  | f Vq d  S(   N(    (   R   t   uR   (   t   mapping(    s…   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/components/strongly_connected.pys	   <genexpr>¢  s    	t   membersR9   N(   t   NoneR   R   t   DiGrapht	   enumerateR%   t   add_nodes_fromt   ranget   add_edges_fromt
   edges_itert   set_node_attributest   graph(   R   R   R:   t   CR-   t   number_of_components(    (   R   R9   s…   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/components/strongly_connected.pyR   q  s    '
!
#(   t   __doc__t   networkxR   t   networkx.utils.decoratorsR    t   joint   __authors__t   __all__R   R;   R   R   R   R   R   R   R   (    (    (    s…   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/components/strongly_connected.pyt   <module>   s,   		`	:W	-	