ó
|Ł*^c           @   s¤   d  Z  d d l Z d d l m Z d j d g  Z d d d d	 d
 d d g Z d d  Z	 d   Z
 d d  Z d d  Z d d  Z d d  Z d d  Z d S(   sÎ   
==================
Depth-first search
==================

Basic algorithms for depth-first searching the nodes of a graph.

Based on http://www.ics.uci.edu/~eppstein/PADS/DFS.py
by D. Eppstein, July 2004.
i˙˙˙˙N(   t   defaultdicts   
s%   Aric Hagberg <aric.hagberg@gmail.com>t	   dfs_edgest   dfs_treet   dfs_predecessorst   dfs_successorst   dfs_preorder_nodest   dfs_postorder_nodest   dfs_labeled_edgesc   	      c   s  | d k r |  } n	 | g } t   } xŐ | D]Í } | | k rF q. n  | j |  | t |  |  f g } x | rú | d \ } } yT t |  } | | k rŘ | | f V| j |  | j | t |  |  f  n  Wqo t k
 rö | j   qo Xqo Wq. Wd S(   sŢ  Produce edges in a depth-first-search (DFS).

    Parameters
    ----------
    G : NetworkX graph

    source : node, optional
       Specify starting node for depth-first search and return edges in
       the component reachable from source.

    Returns
    -------
    edges: generator
       A generator of edges in the depth-first-search.

    Examples
    --------
    >>> G = nx.Graph()
    >>> G.add_path([0,1,2])
    >>> print(list(nx.dfs_edges(G,0)))
    [(0, 1), (1, 2)]

    Notes
    -----
    Based on http://www.ics.uci.edu/~eppstein/PADS/DFS.py
    by D. Eppstein, July 2004.

    If a source is not specified then a source is chosen arbitrarily and
    repeatedly until all components in the graph are searched.
    i˙˙˙˙N(   t   Nonet   sett   addt   itert   nextt   appendt   StopIterationt   pop(	   t   Gt   sourcet   nodest   visitedt   startt   stackt   parentt   childrent   child(    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/traversal/depth_first_search.pyR      s&    				$c         C   sO   t  j   } | d k r( | j |   n | j |  | j t |  |   | S(   sˇ  Return oriented tree constructed from a depth-first-search from source.

    Parameters
    ----------
    G : NetworkX graph

    source : node, optional
       Specify starting node for depth-first search.

    Returns
    -------
    T : NetworkX DiGraph
       An oriented tree

    Examples
    --------
    >>> G = nx.Graph()
    >>> G.add_path([0,1,2])
    >>> T = nx.dfs_tree(G,0)
    >>> print(T.edges())
    [(0, 1), (1, 2)]
    N(   t   nxt   DiGraphR   t   add_nodes_fromt   add_nodet   add_edges_fromR   (   R   R   t   T(    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/traversal/depth_first_search.pyR   I   s    c         C   s    t  d   t |  d | D  S(   sţ  Return dictionary of predecessors in depth-first-search from source.

    Parameters
    ----------
    G : NetworkX graph

    source : node, optional
       Specify starting node for depth-first search and return edges in
       the component reachable from source.

    Returns
    -------
    pred: dict
       A dictionary with nodes as keys and predecessor nodes as values.

    Examples
    --------
    >>> G = nx.Graph()
    >>> G.add_path([0,1,2])
    >>> print(nx.dfs_predecessors(G,0))
    {1: 0, 2: 1}

    Notes
    -----
    Based on http://www.ics.uci.edu/~eppstein/PADS/DFS.py
    by D. Eppstein, July 2004.

    If a source is not specified then a source is chosen arbitrarily and
    repeatedly until all components in the graph are searched.
    c         s   s!   |  ] \ } } | | f Vq d  S(   N(    (   t   .0t   st   t(    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/traversal/depth_first_search.pys	   <genexpr>   s    R   (   t   dictR   (   R   R   (    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/traversal/depth_first_search.pyR   h   s    c         C   sJ   t  t  } x1 t |  d | D] \ } } | | j |  q Wt |  S(   s  Return dictionary of successors in depth-first-search from source.

    Parameters
    ----------
    G : NetworkX graph

    source : node, optional
       Specify starting node for depth-first search and return edges in
       the component reachable from source.

    Returns
    -------
    succ: dict
       A dictionary with nodes as keys and list of successor nodes as values.

    Examples
    --------
    >>> G = nx.Graph()
    >>> G.add_path([0,1,2])
    >>> print(nx.dfs_successors(G,0))
    {0: [1], 1: [2]}

    Notes
    -----
    Based on http://www.ics.uci.edu/~eppstein/PADS/DFS.py
    by D. Eppstein, July 2004.

    If a source is not specified then a source is chosen arbitrarily and
    repeatedly until all components in the graph are searched.
    R   (   R    t   listR   R   R"   (   R   R   t   dR    R!   (    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/traversal/depth_first_search.pyR      s    c         C   s#   d   t  j |  d | D } | S(   s  Produce nodes in a depth-first-search post-ordering starting
    from source.

    Parameters
    ----------
    G : NetworkX graph

    source : node, optional
       Specify starting node for depth-first search and return edges in
       the component reachable from source.

    Returns
    -------
    nodes: generator
       A generator of nodes in a depth-first-search post-ordering.

    Examples
    --------
    >>> G = nx.Graph()
    >>> G.add_path([0,1,2])
    >>> print(list(nx.dfs_postorder_nodes(G,0)))
    [2, 1, 0]

    Notes
    -----
    Based on http://www.ics.uci.edu/~eppstein/PADS/DFS.py
    by D. Eppstein, July 2004.

    If a source is not specified then a source is chosen arbitrarily and
    repeatedly until all components in the graph are searched.
    c         s   s.   |  ]$ \ } } } | d  d k r | Vq d S(   t   dirt   reverseN(    (   R   t   ut   vR$   (    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/traversal/depth_first_search.pys	   <genexpr>Ď   s    R   (   R   R   (   R   R   t   post(    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/traversal/depth_first_search.pyR   Ż   s     c         C   s#   d   t  j |  d | D } | S(   s  Produce nodes in a depth-first-search pre-ordering starting
    from source.

    Parameters
    ----------
    G : NetworkX graph

    source : node, optional
       Specify starting node for depth-first search and return edges in
       the component reachable from source.

    Returns
    -------
    nodes: generator
       A generator of nodes in a depth-first-search pre-ordering.

    Examples
    --------
    >>> G = nx.Graph()
    >>> G.add_path([0,1,2])
    >>> print(list(nx.dfs_preorder_nodes(G,0)))
    [0, 1, 2]

    Notes
    -----
    Based on http://www.ics.uci.edu/~eppstein/PADS/DFS.py
    by D. Eppstein, July 2004.

    If a source is not specified then a source is chosen arbitrarily and
    repeatedly until all components in the graph are searched.
    c         s   s.   |  ]$ \ } } } | d  d k r | Vq d S(   R%   t   forwardN(    (   R   R'   R(   R$   (    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/traversal/depth_first_search.pys	   <genexpr>ö   s    R   (   R   R   (   R   R   t   pre(    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/traversal/depth_first_search.pyR   Ö   s     c   	      c   sr  | d k r |  } n	 | g } t   } xD| D]<} | | k rF q. n  | | i d d 6f V| j |  | t |  |  f g } xŃ | rT| d \ } } ys t |  } | | k rÍ | | i d d 6f Vn? | | i d d 6f V| j |  | j | t |  |  f  Wq t k
 rP| j   | rQ| d d | i d d 6f VqQq Xq W| | i d d 6f Vq. Wd S(   s  Produce edges in a depth-first-search (DFS) labeled by type.

    Parameters
    ----------
    G : NetworkX graph

    source : node, optional
       Specify starting node for depth-first search and return edges in
       the component reachable from source.

    Returns
    -------
    edges: generator
       A generator of edges in the depth-first-search labeled with 'forward',
       'nontree', and 'reverse'.

    Examples
    --------
    >>> G = nx.Graph()
    >>> G.add_path([0,1,2])
    >>> edges = (list(nx.dfs_labeled_edges(G,0)))

    Notes
    -----
    Based on http://www.ics.uci.edu/~eppstein/PADS/DFS.py
    by D. Eppstein, July 2004.

    If a source is not specified then a source is chosen arbitrarily and
    repeatedly until all components in the graph are searched.
    R*   R%   i˙˙˙˙t   nontreei    R&   N(   R   R	   R
   R   R   R   R   R   (	   R   R   R   R   R   R   R   R   R   (    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/traversal/depth_first_search.pyR   ý   s0    !				!
((   t   __doc__t   networkxR   t   collectionsR    t   joint
   __author__t   __all__R   R   R   R   R   R   R   R   (    (    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/traversal/depth_first_search.pyt   <module>
   s   	6	"%''