ó
 ,µ[c           @   s  d  Z  d d l m Z m Z d d l m Z d d l m Z d d l m	 Z	 d d l m
 Z
 d d l m Z d d l Z d d l Z d d	 l m Z d d
 l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d d d d d d d d d d d d g Z e	 j Z d „  Z d „  Z d „  Z d „  Z d  „  Z d d! „ Z  e d" ƒ d# „  ƒ Z! d$ „  Z" e d" ƒ d% „  ƒ Z# e d" ƒ d& „  ƒ Z$ e d" ƒ d' „  ƒ Z% e d" ƒ d( d) d* „ ƒ Z& e d" ƒ d( d) d+ „ ƒ Z' d, „  Z( e d- ƒ e d" ƒ d. „  ƒ ƒ Z) d S(/   sÞ   Algorithms for directed acyclic graphs (DAGs).

Note that most of these functions are only guaranteed to work for DAGs.
In general, these functions do not check for acyclic-ness, so it is up
to the user to check for that.
iÿÿÿÿ(   t   defaultdictt   deque(   t   gcd(   t   partial(   t   chain(   t   product(   t   starmapN(   t   NIL(   t   arbitrary_element(   t   consume(   t   pairwise(   t   generate_unique_node(   t   not_implemented_fort   descendantst	   ancestorst   topological_sortt    lexicographical_topological_sortt   all_topological_sortst   is_directed_acyclic_grapht   is_aperiodict   transitive_closuret   transitive_reductiont
   antichainst   dag_longest_patht   dag_longest_path_lengtht   dag_to_branchingc         C   s[   |  j  | ƒ s% t j d | ƒ ‚ n  t d „  t j |  d | ƒj ƒ  Dƒ ƒ } | | h S(   sû   Return all nodes reachable from `source` in `G`.

    Parameters
    ----------
    G : NetworkX DiGraph
        A directed acyclic graph (DAG)
    source : node in `G`

    Returns
    -------
    set()
        The descendants of `source` in `G`
    s    The node %s is not in the graph.c         s   s   |  ] \ } } | Vq d  S(   N(    (   t   .0t   nt   d(    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pys	   <genexpr>G   s    t   source(   t   has_nodet   nxt   NetworkXErrort   sett   shortest_path_lengtht   items(   t   GR   t   des(    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyR   7   s    +c         C   s[   |  j  | ƒ s% t j d | ƒ ‚ n  t d „  t j |  d | ƒj ƒ  Dƒ ƒ } | | h S(   s÷   Return all nodes having a path to `source` in `G`.

    Parameters
    ----------
    G : NetworkX DiGraph
        A directed acyclic graph (DAG)
    source : node in `G`

    Returns
    -------
    set()
        The ancestors of source in G
    s    The node %s is not in the graph.c         s   s   |  ] \ } } | Vq d  S(   N(    (   R   R   R   (    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pys	   <genexpr>[   s    t   target(   R   R   R    R!   R"   R#   (   R$   R   t   anc(    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyR   K   s    +c         C   s4   y t  t |  ƒ ƒ Wn t j k
 r+ t SXt Sd S(   s/   Decides whether the directed graph has a cycle.N(   R	   R   R   t   NetworkXUnfeasiblet   Truet   False(   R$   (    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyt	   has_cycle_   s
    c         C   s   |  j  ƒ  o t |  ƒ S(   sÞ   Return True if the graph `G` is a directed acyclic graph (DAG) or
    False if not.

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

    Returns
    -------
    bool
        True if `G` is a DAG, False otherwise
    (   t   is_directedR+   (   R$   (    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyR   i   s    c         c   s5  |  j  ƒ  s t j d ƒ ‚ n  d „  |  j ƒ  Dƒ } g  |  j ƒ  D] \ } } | d k rA | ^ qA } x± | r| j ƒ  } | |  k r• t d ƒ ‚ n  xx |  j | ƒ D]g \ } } y | | c d 8<Wn t k
 rä t d ƒ ‚ n X| | d k r¥ | j | ƒ | | =q¥ q¥ W| Vqh W| r1t j	 d ƒ ‚ n  d S(   sÄ  Return a generator of nodes in topologically sorted order.

    A topological sort is a nonunique permutation of the nodes such that an
    edge from u to v implies that u appears before v in the topological sort
    order.

    Parameters
    ----------
    G : NetworkX digraph
        A directed acyclic graph (DAG)

    Returns
    -------
    iterable
        An iterable of node names in topological sorted order.

    Raises
    ------
    NetworkXError
        Topological sort is defined for directed graphs only. If the graph `G`
        is undirected, a :exc:`NetworkXError` is raised.

    NetworkXUnfeasible
        If `G` is not a directed acyclic graph (DAG) no topological sort exists
        and a :exc:`NetworkXUnfeasible` exception is raised.  This can also be
        raised if `G` is changed while the returned iterator is being processed

    RuntimeError
        If `G` is changed while the returned iterator is being processed.

    Examples
    --------
    To get the reverse order of the topological sort:

    >>> DG = nx.DiGraph([(1, 2), (2, 3)])
    >>> list(reversed(list(nx.topological_sort(DG))))
    [3, 2, 1]

    If your DiGraph naturally has the edges representing tasks/inputs
    and nodes representing people/processes that initiate tasks, then
    topological_sort is not quite what you need. You will have to change
    the tasks to nodes with dependence reflected by edges. The result is
    a kind of topological sort of the edges. This can be done
    with :func:`networkx.line_graph` as follows:

    >>> list(nx.topological_sort(nx.line_graph(DG)))
    [(1, 2), (2, 3)]

    Notes
    -----
    This algorithm is based on a description and proof in
    "Introduction to Algorithms: A Creative Approach" [1]_ .

    See also
    --------
    is_directed_acyclic_graph, lexicographical_topological_sort

    References
    ----------
    .. [1] Manber, U. (1989).
       *Introduction to Algorithms - A Creative Approach.* Addison-Wesley.
    s2   Topological sort not defined on undirected graphs.c         S   s+   i  |  ]! \ } } | d  k r | | “ q S(   i    (    (   R   t   vR   (    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pys
   <dictcomp>¼   s   	 i    s   Graph changed during iterationi   s8   Graph contains a cycle or graph changed during iterationN(
   R,   R   R    t	   in_degreet   popt   RuntimeErrort   edgest   KeyErrort   appendR(   (   R$   t   indegree_mapR-   R   t   zero_indegreet   nodet   _t   child(    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyR   y   s(    ?1		c   
      #   s  |  j  ƒ  s t j d ƒ ‚ n  ˆ  d	 k r6 d „  ‰  n  ‡  f d †  } d „  |  j ƒ  Dƒ } g  |  j ƒ  D]$ \ } } | d k rh | | ƒ ^ qh } t j | ƒ xÃ | rdt j | ƒ \ } } | |  k rØ t d ƒ ‚ n  x |  j	 | ƒ D]p \ } }	 y | |	 c d 8<Wn t
 k
 r't d ƒ ‚ n X| |	 d k rè t j | | |	 ƒ ƒ | |	 =qè qè W| Vq¢ W| r}t j d ƒ ‚ n  d	 S(
   sÌ  Return a generator of nodes in lexicographically topologically sorted
    order.

    A topological sort is a nonunique permutation of the nodes such that an
    edge from u to v implies that u appears before v in the topological sort
    order.

    Parameters
    ----------
    G : NetworkX digraph
        A directed acyclic graph (DAG)

    key : function, optional
        This function maps nodes to keys with which to resolve ambiguities in
        the sort order.  Defaults to the identity function.

    Returns
    -------
    iterable
        An iterable of node names in lexicographical topological sort order.

    Raises
    ------
    NetworkXError
        Topological sort is defined for directed graphs only. If the graph `G`
        is undirected, a :exc:`NetworkXError` is raised.

    NetworkXUnfeasible
        If `G` is not a directed acyclic graph (DAG) no topological sort exists
        and a :exc:`NetworkXUnfeasible` exception is raised.  This can also be
        raised if `G` is changed while the returned iterator is being processed

    RuntimeError
        If `G` is changed while the returned iterator is being processed.

    Notes
    -----
    This algorithm is based on a description and proof in
    "Introduction to Algorithms: A Creative Approach" [1]_ .

    See also
    --------
    topological_sort

    References
    ----------
    .. [1] Manber, U. (1989).
       *Introduction to Algorithms - A Creative Approach.* Addison-Wesley.
    s2   Topological sort not defined on undirected graphs.c         S   s   |  S(   N(    (   t   x(    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyt   key  s    c            s   ˆ  |  ƒ |  f S(   N(    (   R6   (   R:   (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyt   create_tuple  s    c         S   s+   i  |  ]! \ } } | d  k r | | “ q S(   i    (    (   R   R-   R   (    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pys
   <dictcomp>  s   	 i    s   Graph changed during iterationi   s8   Graph contains a cycle or graph changed during iterationN(   R,   R   R    t   NoneR.   t   heapqt   heapifyt   heappopR0   R1   R2   t   heappushR(   (
   R$   R:   R;   R4   R-   R   R5   R7   R6   R8   (    (   R:   s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyR   Ô   s0    27		t
   undirectedc   
      c   s©  |  j  ƒ  s t j d ƒ ‚ n  t |  j ƒ  ƒ } t g  |  j ƒ  D] \ } } | d k r@ | ^ q@ ƒ } g  } g  } x/t r¤t g  | D] } | | d k ^ q† ƒ s« t ‚ t	 | ƒ t	 |  ƒ k rÀt
 | ƒ Vxºt	 | ƒ d k r¼t	 | ƒ t	 | ƒ k st ‚ | j ƒ  } xC |  j | ƒ D]2 \ } }	 | |	 c d 7<| |	 d k st ‚ qWx4 t	 | ƒ d k r‰| | d d k r‰| j ƒ  qVW| j | ƒ | d | d k r¸| j ƒ  qÑ PqÑ WnË t	 | ƒ d k rät j d ƒ ‚ n  | j ƒ  } xc |  j | ƒ D]R \ } }	 | |	 c d 8<| |	 d k s2t ‚ | |	 d k r | j |	 ƒ q q W| j | ƒ t	 | ƒ t	 | ƒ k  r‹| j | ƒ n  t	 | ƒ d k rv Pqv qv Wd S(   s¹  Returns a generator of _all_ topological sorts of the directed graph G.

    A topological sort is a nonunique permutation of the nodes such that an
    edge from u to v implies that u appears before v in the topological sort
    order.

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

    Returns
    -------
    generator
        All topological sorts of the digraph G

    Raises
    ------
    NetworkXNotImplemented
        If `G` is not directed
    NetworkXUnfeasible
        If `G` is not acyclic

    Examples
    --------
    To enumerate all topological sorts of directed graph:

    >>> DG = nx.DiGraph([(1, 2), (2, 3), (2, 4)])
    >>> list(nx.all_topological_sorts(DG))
    [[1, 2, 4, 3], [1, 2, 3, 4]]

    Notes
    -----
    Implements an iterative version of the algorithm given in [1].

    References
    ----------
    .. [1] Knuth, Donald E., Szwarcfiter, Jayme L. (1974).
       "A Structured Program to Generate All Topological Sorting Arrangements"
       Information Processing Letters, Volume 2, Issue 6, 1974, Pages 153-157,
       ISSN 0020-0190,
       https://doi.org/10.1016/0020-0190(74)90001-5.
       Elsevier (North-Holland), Amsterdam
    s2   Topological sort not defined on undirected graphs.i    i   iÿÿÿÿs   Graph contains a cycle.N(   R,   R   R    t   dictR.   R   R)   t   allt   AssertionErrort   lent   listR/   t	   out_edgest
   appendleftR(   R3   (
   R$   t   countR-   R   t   Dt   basest   current_sortt   qR7   t   j(    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyR   *  sH    .7	/)c   	      C   s2  |  j  ƒ  s t j d ƒ ‚ n  t |  ƒ } i d | 6} | g } d } d } x‹ | rÙ g  } xh | D]` } xW |  | D]K } | | k r§ t | | | | | d ƒ } qs | j | ƒ | | | <qs Wqb W| } | d 7} qO Wt | ƒ t |  ƒ k rü | d k S| d k o-t j |  j t	 |  ƒ t	 | ƒ ƒ ƒ Sd S(   sr  Return True if `G` is aperiodic.

    A directed graph is aperiodic if there is no integer k > 1 that
    divides the length of every cycle in the graph.

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

    Returns
    -------
    bool
        True if the graph is aperiodic False otherwise

    Raises
    ------
    NetworkXError
        If `G` is not directed

    Notes
    -----
    This uses the method outlined in [1]_, which runs in $O(m)$ time
    given $m$ edges in `G`. Note that a graph is not aperiodic if it is
    acyclic as every integer trivial divides length 0 cycles.

    References
    ----------
    .. [1] Jarvis, J. P.; Shier, D. R. (1996),
       "Graph-theoretic analysis of finite Markov chains,"
       in Shier, D. R.; Wallenius, K. T., Applied Mathematical Modeling:
       A Multidisciplinary Approach, CRC Press.
    s.   is_aperiodic not defined for undirected graphsi    i   N(
   R,   R   R    R   R   R3   RE   R   t   subgraphR!   (	   R$   t   st   levelst
   this_levelt   gt   lt
   next_levelt   uR-   (    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyR   ¢  s*    "		"
c            sM   |  j  ƒ  } x: |  D]2 ‰  | j ‡  f d †  t j |  d ˆ  ƒDƒ ƒ q W| S(   sE   Returns transitive closure of a directed graph

    The transitive closure of G = (V,E) is a graph G+ = (V,E+) such that
    for all v,w in V there is an edge (v,w) in E+ if and only if there
    is a non-null path from v to w in G.

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

    Returns
    -------
    NetworkX DiGraph
        The transitive closure of `G`

    Raises
    ------
    NetworkXNotImplemented
        If `G` is not directed

    References
    ----------
    .. [1] http://www.ics.uci.edu/~eppstein/PADS/PartialOrder.py

    c         3   s'   |  ] } ˆ  | k r ˆ  | f Vq d  S(   N(    (   R   RV   (   R-   (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pys	   <genexpr>ü  s    R   (   t   copyt   add_edges_fromR   t   dfs_preorder_nodes(   R$   t   TC(    (   R-   s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyR   Þ  s    0c            s%  t  |  ƒ s$ d } t j | ƒ ‚ n  t j ƒ  } | j |  j ƒ  ƒ i  } t |  j ƒ } xÆ |  D]¾ ‰  t |  ˆ  ƒ } xˆ |  ˆ  D]| } | | k rÒ | | k rÁ d „  t j	 |  | ƒ Dƒ | | <n  | | | 8} n  | | c d 8<| | d k r€ | | =q€ q€ W| j
 ‡  f d †  | Dƒ ƒ q_ W| S(   sæ   Returns transitive reduction of a directed graph

    The transitive reduction of G = (V,E) is a graph G- = (V,E-) such that
    for all v,w in V there is an edge (v,w) in E- if and only if (v,w) is
    in E and there is no path from v to w in G with length greater than 1.

    Parameters
    ----------
    G : NetworkX DiGraph
        A directed acyclic graph (DAG)

    Returns
    -------
    NetworkX DiGraph
        The transitive reduction of `G`

    Raises
    ------
    NetworkXError
        If `G` is not a directed acyclic graph (DAG) transitive reduction is
        not uniquely defined and a :exc:`NetworkXError` exception is raised.

    References
    ----------
    https://en.wikipedia.org/wiki/Transitive_reduction

    s8   Directed Acyclic Graph required for transitive_reductionc         S   s   h  |  ] \ } } | ’ q S(    (    (   R   R9   t   y(    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pys	   <setcomp>+  s   	 i   i    c         3   s   |  ] } ˆ  | f Vq d  S(   N(    (   R   R-   (   RV   (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pys	   <genexpr>0  s    (   R   R   R    t   DiGrapht   add_nodes_fromt   nodesRB   R.   R!   t	   dfs_edgesRX   (   R$   t   msgt   TRR   t   check_countt   u_nbrsR-   (    (   RV   s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyR     s&    #!c   	      c   sÓ   t  j |  ƒ } g  t t t t  j |  ƒ ƒ ƒ ƒ f g } x“ | rÎ | j ƒ  \ } } | Vxo | rÊ | j ƒ  } | | g } g  | D], } | | | k p¥ | | | k s‚ | ^ q‚ } | j | | f ƒ q\ Wq< Wd S(   s€  Generates antichains from a directed acyclic graph (DAG).

    An antichain is a subset of a partially ordered set such that any
    two elements in the subset are incomparable.

    Parameters
    ----------
    G : NetworkX DiGraph
        A directed acyclic graph (DAG)

    Returns
    -------
    generator object

    Raises
    ------
    NetworkXNotImplemented
        If `G` is not directed

    NetworkXUnfeasible
        If `G` contains a cycle

    Notes
    -----
    This function was originally developed by Peter Jipsen and Franco Saliola
    for the SAGE project. It's included in NetworkX with permission from the
    authors. Original SAGE code at:

    https://github.com/sagemath/sage/blob/master/src/sage/combinat/posets/hasse_diagram.py

    References
    ----------
    .. [1] Free Lattices, by R. Freese, J. Jezek and J. B. Nation,
       AMS, Vol 42, 1995, p. 226.
    N(   R   R   RF   t   reversedR   R/   R3   (	   R$   RZ   t   antichains_stackst	   antichaint   stackR9   t   new_antichaint   tt	   new_stack(    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyR   4  s    %*		9t   weighti   c   	         s*  |  s
 g  Si  ‰  x® t  j |  ƒ D] } g  |  j | j ƒ  D]0 \ } } ˆ  | d | j | | ƒ | f ^ q: } | r‹ t | d d „  ƒn	 d | f } | d d k r­ | n	 d | f ˆ  | <q  Wd } t ˆ  d ‡  f d †  ƒ} g  } x1 | | k r| j | ƒ | } ˆ  | d } që W| j ƒ  | S(   sb  Returns the longest path in a directed acyclic graph (DAG).

    If `G` has edges with `weight` attribute the edge data are used as
    weight values.

    Parameters
    ----------
    G : NetworkX DiGraph
        A directed acyclic graph (DAG)

    weight : str, optional
        Edge data key to use for weight

    default_weight : int, optional
        The weight of edges that do not have a weight attribute

    Returns
    -------
    list
        Longest path

    Raises
    ------
    NetworkXNotImplemented
        If `G` is not directed

    See also
    --------
    dag_longest_path_length

    i    R:   c         S   s   |  d S(   Ni    (    (   R9   (    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyt   <lambda>’  s    c            s   ˆ  |  d S(   Ni    (    (   R9   (   t   dist(    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyRl   •  s    i   N(	   R   R   t   predR#   t   gett   maxR<   R3   t   reverse(	   R$   Rk   t   default_weightR-   RV   t   datat   ust   maxut   path(    (   Rm   s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyR   i  s"    !G'*
c         C   sZ   t  j |  | | ƒ } d } x8 t | ƒ D]* \ } } | |  | | j | | ƒ 7} q( W| S(   sõ  Returns the longest path length in a DAG

    Parameters
    ----------
    G : NetworkX DiGraph
        A directed acyclic graph (DAG)

    weight : string, optional
        Edge data key to use for weight

    default_weight : int, optional
        The weight of edges that do not have a weight attribute

    Returns
    -------
    int
        Longest path length

    Raises
    ------
    NetworkXNotImplemented
        If `G` is not directed

    See also
    --------
    dag_longest_path
    i    (   R   R   R
   Ro   (   R$   Rk   Rr   Rv   t   path_lengthRV   R-   (    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyR   Ÿ  s
    "c         C   sZ   d „  |  j  ƒ  Dƒ } d „  |  j ƒ  Dƒ } t t j |  ƒ } t t | t | | ƒ ƒ ƒ S(   sq  Yields root-to-leaf paths in a directed acyclic graph.

    `G` must be a directed acyclic graph. If not, the behavior of this
    function is undefined. A "root" in this graph is a node of in-degree
    zero and a "leaf" a node of out-degree zero.

    When invoked, this function iterates over each path from any root to
    any leaf. A path is a list of nodes.

    c         s   s'   |  ] \ } } | d  k r | Vq d S(   i    N(    (   R   R-   R   (    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pys	   <genexpr>Ï  s    c         s   s'   |  ] \ } } | d  k r | Vq d S(   i    N(    (   R   R-   R   (    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pys	   <genexpr>Ð  s    (   R.   t
   out_degreeR   R   t   all_simple_pathst   chainiR   R   (   R$   t   rootst   leavest	   all_paths(    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyt   root_to_leaf_pathsÄ  s    t
   multigraphc         C   sc   t  |  ƒ r$ d } t j | ƒ ‚ n  t |  ƒ } t j | ƒ \ } } | j | ƒ | j t ƒ | S(   s(  Returns a branching representing all (overlapping) paths from
    root nodes to leaf nodes in the given directed acyclic graph.

    As described in :mod:`networkx.algorithms.tree.recognition`, a
    *branching* is a directed forest in which each node has at most one
    parent. In other words, a branching is a disjoint union of
    *arborescences*. For this function, each node of in-degree zero in
    `G` becomes a root of one of the arborescences, and there will be
    one leaf node for each distinct path from that root to a leaf node
    in `G`.

    Each node `v` in `G` with *k* parents becomes *k* distinct nodes in
    the returned branching, one for each parent, and the sub-DAG rooted
    at `v` is duplicated for each copy. The algorithm then recurses on
    the children of each copy of `v`.

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

    Returns
    -------
    DiGraph
        The branching in which there is a bijection between root-to-leaf
        paths in `G` (in which multiple paths may share the same leaf)
        and root-to-leaf paths in the branching (in which there is a
        unique path from a root to a leaf).

        Each node has an attribute 'source' whose value is the original
        node to which this node corresponds. No other graph, node, or
        edge attributes are copied into this new graph.

    Raises
    ------
    NetworkXNotImplemented
        If `G` is not directed, or if `G` is a multigraph.

    HasACycle
        If `G` is not acyclic.

    Examples
    --------
    To examine which nodes in the returned branching were produced by
    which original node in the directed acyclic graph, we can collect
    the mapping from source node to new nodes into a dictionary. For
    example, consider the directed diamond graph::

        >>> from collections import defaultdict
        >>> from operator import itemgetter
        >>>
        >>> G = nx.DiGraph(nx.utils.pairwise('abd'))
        >>> G.add_edges_from(nx.utils.pairwise('acd'))
        >>> B = nx.dag_to_branching(G)
        >>>
        >>> sources = defaultdict(set)
        >>> for v, source in B.nodes(data='source'):
        ...     sources[source].add(v)
        >>> len(sources['a'])
        1
        >>> len(sources['d'])
        2

    To copy node attributes from the original graph to the new graph,
    you can use a dictionary like the one constructed in the above
    example::

        >>> for source, nodes in sources.items():
        ...     for v in nodes:
        ...         B.node[v].update(G.node[source])

    Notes
    -----
    This function is not idempotent in the sense that the node labels in
    the returned branching may be uniquely generated each time the
    function is invoked. In fact, the node labels may not be integers;
    in order to relabel the nodes to be more readable, you can use the
    :func:`networkx.convert_node_labels_to_integers` function.

    The current implementation of this function uses
    :func:`networkx.prefix_tree`, so it is subject to the limitations of
    that function.

    s3   dag_to_branching is only defined for acyclic graphs(   R+   R   t	   HasACycleR~   t   prefix_treet   remove_nodeR   (   R$   R`   t   pathst   Bt   root(    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyR   Ö  s    W(*   t   __doc__t   collectionsR    R   t	   fractionsR   t	   functoolsR   t	   itertoolsR   R   R   R=   t   networkxR   t   networkx.generators.treesR   t   networkx.utilsR   R	   R
   R   R   t   __all__t   from_iterableRz   R   R   R+   R   R   R<   R   R   R   R   R   R   R   R   R~   R   (    (    (    s6   lib/python2.7/site-packages/networkx/algorithms/dag.pyt   <module>   s\   					
		[Vx	<#35	5	$		