ó
|£*^c        
   @   sü   d  d l  m Z d  d l Z d  d l Td j d d d g ƒ Z d d	 d
 d d d d d d d g
 Z d „  Z d „  Z	 d „  Z
 d e d „ Z d e d „ Z d „  Z e d ƒ d „  ƒ Z e d ƒ d „  ƒ Z e d ƒ d „  ƒ Z e d ƒ d „  ƒ Z d S(   iÿÿÿÿ(   t   gcdN(   t   *s   
s%   Aric Hagberg <aric.hagberg@gmail.com>s    Dan Schult (dschult@colgate.edu)s!   Ben Edwards (bedwards@cs.unm.edu)t   descendantst	   ancestorst   topological_sortt   topological_sort_recursivet   is_directed_acyclic_grapht   is_aperiodict   transitive_closuret
   antichainst   dag_longest_patht   dag_longest_path_lengthc         C   sW   |  j  | ƒ s% t j d | ƒ ‚ n  t t j |  d | ƒj ƒ  ƒ t | g ƒ } | S(   sÒ   Return all nodes reachable from `source` in G.

    Parameters
    ----------
    G : NetworkX DiGraph
    source : node in G

    Returns
    -------
    des : set()
        The descendants of source in G
    s    The node %s is not in the graph.t   source(   t   has_nodet   nxt   NetworkXErrort   sett   shortest_path_lengtht   keys(   t   GR   t   des(    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/dag.pyR      s    .c         C   sW   |  j  | ƒ s% t j d | ƒ ‚ n  t t j |  d | ƒj ƒ  ƒ t | g ƒ } | S(   sØ   Return all nodes having a path to `source` in G.

    Parameters
    ----------
    G : NetworkX DiGraph
    source : node in G

    Returns
    -------
    ancestors : set()
        The ancestors of source in G
    s    The node %s is not in the graph.t   target(   R   R   R   R   R   R   (   R   R   t   anc(    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/dag.pyR   .   s    .c         C   sD   |  j  ƒ  s t Sy t |  d t ƒt SWn t j k
 r? t SXd S(   sô   Return True if the graph G is a directed acyclic graph (DAG) or 
    False if not.

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

    Returns
    -------
    is_dag : bool
        True if G is a DAG, false otherwise
    t   reverseN(   t   is_directedt   FalseR   t   TrueR   t   NetworkXUnfeasible(   R   (    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/dag.pyR   A   s    c         C   sj  |  j  ƒ  s t j d ƒ ‚ n  t ƒ  } g  } t ƒ  } | d k rQ |  j ƒ  } n  xø | D]ð } | | k rp qX n  | g } xÌ | rG| d } | | k r¨ | j ƒ  q| n  | j | ƒ g  }	 xL |  | D]@ }
 |
 | k rÆ |
 | k rö t j d ƒ ‚ n  |	 j	 |
 ƒ qÆ qÆ W|	 r | j
 |	 ƒ q| | j | ƒ | j	 | ƒ | j ƒ  q| WqX W| rV| St t | ƒ ƒ Sd S(   sÉ  Return a list of nodes in topological sort 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 graph

    nbunch : container of nodes (optional)
        Explore graph in specified order given in nbunch

    reverse : bool, optional
        Return postorder instead of preorder if True.
        Reverse mode is a bit more efficient.

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

    NetworkXUnfeasible
        If G is not a directed acyclic graph (DAG) no topological sort
        exists and a NetworkXUnfeasible exception is raised.

    Notes
    -----
    This algorithm is based on a description and proof in
    The Algorithm Design Manual [1]_ .

    See also
    --------
    is_directed_acyclic_graph

    References
    ----------
    .. [1] Skiena, S. S. The Algorithm Design Manual  (Springer-Verlag, 1998). 
        http://www.amazon.com/exec/obidos/ASIN/0387948600/ref=ase_thealgorithmrepo/
    s2   Topological sort not defined on undirected graphs.iÿÿÿÿs   Graph contains a cycle.N(   R   R   R   R   t   Nonet
   nodes_itert   popt   addR   t   appendt   extendt   listt   reversed(   R   t   nbunchR   t   seent   ordert   exploredt   vt   fringet   wt	   new_nodest   n(    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/dag.pyR   X   s@    +				

c            s´   ˆ  j  ƒ  s t j d ƒ ‚ n  ‡  ‡ ‡ ‡ ‡ f d †  ‰ t ƒ  ‰ t ƒ  ‰ g  ‰ | d k rl ˆ  j ƒ  } n  x' | D] } | ˆ k rs ˆ | ƒ qs qs W| r  ˆ St t ˆ ƒ ƒ Sd S(   s×  Return a list of nodes in topological sort 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

    nbunch : container of nodes (optional)
        Explore graph in specified order given in nbunch

    reverse : bool, optional
        Return postorder instead of preorder if True.
        Reverse mode is a bit more efficient.

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

    NetworkXUnfeasible
        If G is not a directed acyclic graph (DAG) no topological sort
        exists and a NetworkXUnfeasible exception is raised.

    Notes
    -----
    This is a recursive version of topological sort.

    See also
    --------
    topological_sort
    is_directed_acyclic_graph

    s2   Topological sort not defined on undirected graphs.c            s„   ˆ j  |  ƒ xI ˆ  |  D]= } | ˆ k r< t j d ƒ ‚ n  | ˆ k r ˆ | ƒ q q Wˆ j |  ƒ ˆ j  |  ƒ ˆ j |  ƒ d  S(   Ns   Graph contains a cycle.(   R   R   R   t   removeR    (   R(   R*   (   R   t   _dfsR   R'   R&   (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/dag.pyR.   Õ   s    N(   R   R   R   R   R   R   R"   R#   (   R   R$   R   R(   (    (   R   R.   R   R'   R&   sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/dag.pyR   «   s    &		c   	      C   s8  |  j  ƒ  s t j d ƒ ‚ n  t |  j ƒ  ƒ } i d | 6} | g } d } d } x‹ | rß g  } xh | D]` } xW |  | D]K } | | k r­ t | | | | | d ƒ } qy | j | ƒ | | | <qy Wqh W| } | d 7} qU Wt | ƒ t |  ƒ k r| d k S| d k o3t j |  j	 t
 |  ƒ t
 | ƒ ƒ ƒ Sd S(   sk  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
        Graph

    Returns
    -------
    aperiodic : boolean
        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   t   nextR   R    R    t   lenR   t   subgraphR   (	   R   t   st   levelst
   this_levelt   gt   lt
   next_levelt   uR(   (    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/dag.pyR   ô   s*    "		"
t
   undirectedc            ss   t  j ƒ  } | j |  j ƒ  ƒ | j |  j ƒ  ƒ x: |  D]2 ‰  | j ‡  f d †  t  j |  d ˆ  ƒDƒ ƒ q9 W| S(   s%   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
        Graph

    Returns
    -------
    TC : NetworkX DiGraph
        Graph

    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(    (   t   .0R8   (   R(   (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/dag.pys	   <genexpr>P  s    R   (   R   t   DiGrapht   add_nodes_fromR   t   add_edges_fromt
   edges_itert   dfs_preorder_nodes(   R   t   TC(    (   R(   sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/dag.pyR   0  s    0c   	      c   sÇ   t  j |  ƒ } g  t  j |  d t ƒf g } x“ | rÂ | j ƒ  \ } } | Vxo | r¾ | j ƒ  } | | g } g  | D], } | | | k p™ | | | k sv | ^ qv } | j | | f ƒ qP Wq0 Wd S(   sN  Generates antichains from a 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
        Graph

    Returns
    -------
    antichain : 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://sage.informatik.uni-goettingen.de/src/combinat/posets/hasse_diagram.py

    References
    ----------
    .. [1] Free Lattices, by R. Freese, J. Jezek and J. B. Nation,
       AMS, Vol 42, 1995, p. 226.
    R   N(   R   R   R   R   R   R    (	   R   R@   t   antichains_stackst	   antichaint   stackt   xt   new_antichaint   tt	   new_stack(    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/dag.pyR	   U  s    %		9c         C   sè   i  } xr t  j |  ƒ D]a } g  |  j | D] } | | d d | f ^ q* } | rg t | ƒ | | <q d | f | | <q Wt | j ƒ  d d „  ƒ\ } \ } } g  } x- | d k r× | j | ƒ | | \ } } q« Wt t | ƒ ƒ S(   s0  Returns the longest path in a DAG

    Parameters
    ----------
    G : NetworkX DiGraph
        Graph

    Returns
    -------
    path : list
        Longest path

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

    See also
    --------
    dag_longest_path_length
    i    i   t   keyc         S   s   |  d S(   Ni   (    (   RD   (    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/dag.pyt   <lambda>©  s    (   R   R   t   predt   maxt   itemsR    R"   R#   (   R   t   distt   nodeR(   t   pairst   lengtht   _t   path(    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/dag.pyR
   Š  s    2'c         C   s   t  t j |  ƒ ƒ d } | S(   s=  Returns the longest path length in a DAG

    Parameters
    ----------
    G : NetworkX DiGraph
        Graph

    Returns
    -------
    path_length : int
        Longest path length

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

    See also
    --------
    dag_longest_path
    i   (   R0   R   R
   (   R   t   path_length(    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/dag.pyR   ±  s    (   t	   fractionsR    t   networkxR   t   networkx.utils.decoratorst   joint
   __author__t   __all__R   R   R   R   R   R   R   R   t   not_implemented_forR   R	   R
   R   (    (    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/dag.pyt   <module>   s2   
					SI	<%5'