ó
 ,µ[c           @   sy   d  Z  d d l m Z d d l Z d d l m Z d d l m Z d d g Z d Z	 d	 „  Z
 e d
 ƒ d d „ ƒ Z d S(   s   Functions for generating trees.iÿÿÿÿ(   t   defaultdictN(   t   generate_unique_node(   t   py_random_statet   prefix_treet   random_treet   NILc            sd   ‡  f d †  ‰  t  j ƒ  } t ƒ  } | j | d d ƒ| j t d t ƒˆ  |  | | ƒ | | f S(   sì
  Creates a directed prefix tree from the given list of iterables.

    Parameters
    ----------
    paths: iterable of lists
        An iterable over "paths", which are themselves lists of
        nodes. Common prefixes among these paths are converted into
        common initial segments in the generated tree.

        Most commonly, this may be an iterable over lists of integers,
        or an iterable over Python strings.

    Returns
    -------
    DiGraph
        A directed graph representing an arborescence consisting of the
        prefix tree generated by `paths`. Nodes are directed "downward",
        from parent to child. A special "synthetic" root node is added
        to be the parent of the first node in each path. A special
        "synthetic" leaf node, the "nil" node, is added to be the child
        of all nodes representing the last element in a path. (The
        addition of this nil node technically makes this not an
        arborescence but a directed acyclic graph; removing the nil node
        makes it an arborescence.)

        Each node has an attribute 'source' whose value is the original
        element of the path to which this node corresponds. The 'source'
        of the root node is None, and the 'source' of the nil node is
        :data:`.NIL`.

        The root node is the only node of in-degree zero in the graph,
        and the nil node is the only node of out-degree zero.  For
        convenience, the nil node can be accessed via the :data:`.NIL`
        attribute; for example::

            >>> from networkx.generators.trees import NIL
            >>> paths = ['ab', 'abs', 'ad']
            >>> T = nx.prefix_tree(paths)
            >>> T.predecessors(NIL)  # doctest: +SKIP

    Notes
    -----
    The prefix tree is also known as a *trie*.

    Examples
    --------
    Create a prefix tree from a list of strings with some common
    prefixes::

        >>> strings = ['ab', 'abs', 'ad']
        >>> T, root = nx.prefix_tree(strings)

    Continuing the above example, to recover the original paths that
    generated the prefix tree, traverse up the tree from the
    :data:`.NIL` node to the root::

        >>> from networkx.generators.trees import NIL
        >>>
        >>> strings = ['ab', 'abs', 'ad']
        >>> T, root = nx.prefix_tree(strings)
        >>> recovered = []
        >>> for v in T.predecessors(NIL):
        ...     s = ''
        ...     while v != root:
        ...         # Prepend the character `v` to the accumulator `s`.
        ...         s = str(T.node[v]['source']) + s
        ...         # Each non-nil, non-root node has exactly one parent.
        ...         v = next(T.predecessors(v))
        ...     recovered.append(s)
        >>> sorted(recovered)
        ['ab', 'abs', 'ad']

    c   
         s¼   t  t ƒ } xP |  D]H } | s5 | j | t ƒ q n  | d | d } } | | j | ƒ q WxV | j ƒ  D]H \ } } t ƒ  }	 | j |	 d | ƒ| j | |	 ƒ ˆ  | |	 | ƒ ql Wd S(   sm  Recursively create a trie from the given list of paths.

        `paths` is a list of paths, each of which is itself a list of
        nodes, relative to the given `root` (but not including it). This
        list of paths will be interpreted as a tree-like structure, in
        which two paths that share a prefix represent two branches of
        the tree with the same initial segment.

        `root` is the parent of the node at index 0 in each path.

        `B` is the "accumulator", the :class:`networkx.DiGraph`
        representing the branching to which the new nodes and edges will
        be added.

        i    i   t   sourceN(   R    t   listt   add_edgeR   t   appendt   itemsR   t   add_node(
   t   pathst   roott   Bt   childrent   patht   childt   restt   headt   tailst   new_head(   t   _helper(    s8   lib/python2.7/site-packages/networkx/generators/trees.pyR   c   s    	R   N(   t   nxt   DiGraphR   R   t   NoneR   (   R   t   TR   (    (   R   s8   lib/python2.7/site-packages/networkx/generators/trees.pyR      s    J-	i   c         C   sv   |  d k r t  j d ƒ ‚ n  |  d k r7 t  j d ƒ Sg  t |  d ƒ D] } | j t |  ƒ ƒ ^ qH } t  j | ƒ S(   sÑ  Returns a uniformly random tree on `n` nodes.

    Parameters
    ----------
    n : int
        A positive integer representing the number of nodes in the tree.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    NetworkX graph
        A tree, given as an undirected graph, whose nodes are numbers in
        the set {0, â€¦, *n* - 1}.

    Raises
    ------
    NetworkXPointlessConcept
        If `n` is zero (because the null graph is not a tree).

    Notes
    -----
    The current implementation of this function generates a uniformly
    random PrÃ¼fer sequence then converts that to a tree via the
    :func:`~networkx.from_prufer_sequence` function. Since there is a
    bijection between PrÃ¼fer sequences of length *n* - 2 and trees on
    *n* nodes, the tree is chosen uniformly at random from the set of
    all trees on *n* nodes.

    i    s   the null graph is not a treei   i   (   R   t   NetworkXPointlessConceptt   empty_grapht   ranget   choicet   from_prufer_sequence(   t   nt   seedt   it   sequence(    (    s8   lib/python2.7/site-packages/networkx/generators/trees.pyR   Ÿ   s    !2(   t   __doc__t   collectionsR    t   networkxR   t   networkx.utilsR   R   t   __all__R   R   R   R   (    (    (    s8   lib/python2.7/site-packages/networkx/generators/trees.pyt   <module>	   s   	†	