ó
úŁŐ\c           @   s§   d  Z  d j d g  Z d d d d d d d	 g Z d
 d l Z d
 d l m Z d d  Z	 d 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.

Based on http://www.ics.uci.edu/~eppstein/PADS/DFS.py
by D. Eppstein, July 2004.
s   
s   Aric Hagberg <hagberg@lanl.gov>t	   dfs_edgest   dfs_treet   dfs_predecessorst   dfs_successorst   dfs_preorder_nodest   dfs_postorder_nodest   dfs_labeled_edgesi˙˙˙˙N(   t   defaultdictc   	      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(   s9   Produce edges in a depth-first-search starting at source.i˙˙˙˙N(   t   Nonet   sett   addt   itert   nextt   appendt   StopIterationt   pop(	   t   Gt   sourcet   nodest   visitedt   startt   stackt   parentt   childrent   child(    (    sh   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/traversal/depth_first_search.pyR       s&    				$c         C   s   t  j t |  d |  S(   s7   Return directed tree of depth-first-search from source.R   (   t   nxt   DiGraphR    (   R   R   (    (    sh   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/traversal/depth_first_search.pyR   1   s    c         C   s    t  d   t |  d | D  S(   sD   Return dictionary of predecessors in depth-first-search from source.c         s   s!   |  ] \ } } | | f Vq d  S(   N(    (   t   .0t   st   t(    (    sh   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/traversal/depth_first_search.pys	   <genexpr>8   s    R   (   t   dictR    (   R   R   (    (    sh   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/traversal/depth_first_search.pyR   6   s    c         C   sJ   t  t  } x1 t |  d | D] \ } } | | j |  q Wt |  S(   sB   Return dictionary of successors in depth-first-search from source.R   (   R   t   listR    R   R   (   R   R   t   dR   R   (    (    sh   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/traversal/depth_first_search.pyR   ;   s    c         C   s#   d   t  j |  d | D } | S(   sS   Produce nodes in a depth-first-search post-ordering starting 
    from source.
    c         s   s.   |  ]$ \ } } } | d  d k r | Vq d S(   t   dirt   reverseN(    (   R   t   ut   vR    (    (    sh   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/traversal/depth_first_search.pys	   <genexpr>G   s    R   (   R   R   (   R   R   t   post(    (    sh   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/traversal/depth_first_search.pyR   C   s    c         C   s#   d   t  j |  d | D } | S(   sF   Produce nodes in a depth-first-search pre-ordering starting at source.c         s   s.   |  ]$ \ } } } | d  d k r | Vq d S(   R!   t   forwardN(    (   R   R#   R$   R    (    (    sh   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/traversal/depth_first_search.pys	   <genexpr>P   s    R   (   R   R   (   R   R   t   pre(    (    sh   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/traversal/depth_first_search.pyR   N   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 starting at source and
    labeled by direction type (forward, reverse, nontree).
    R&   R!   i˙˙˙˙t   nontreei    R"   N(   R   R	   R
   R   R   R   R   R   (	   R   R   R   R   R   R   R   R   R   (    (    sh   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/traversal/depth_first_search.pyR   W   s0    				!
((   t   __doc__t   joint
   __author__t   __all__t   networkxR   t   collectionsR   R   R    R   R   R   R   R   R   (    (    (    sh   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/traversal/depth_first_search.pyt   <module>
   s   		