ó
 ,ĩ[c           @   s-   d  Z  d d l m Z d d d     YZ d S(   s   
Union-find data structure.
iĸĸĸĸ(   t   groupst	   UnionFindc           B   s>   e  Z d  Z d d  Z d   Z d   Z d   Z d   Z RS(   s  Union-find data structure.

    Each unionFind instance X maintains a family of disjoint sets of
    hashable objects, supporting the following two methods:

    - X[item] returns a name for the set containing the given item.
      Each set is named by an arbitrarily-chosen one of its members; as
      long as the set remains unchanged it will keep the same name. If
      the item is not yet part of a set in X, a new singleton set is
      created for it.

    - X.union(item1, item2, ...) merges the sets containing each item
      into a single larger set.  If any item is not yet part of a set
      in X, it is added to X as one of the members of the merged set.

      Union-find data structure. Based on Josiah Carlson's code,
      http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/215912
      with significant additional changes by D. Eppstein.
      http://www.ics.uci.edu/~eppstein/PADS/UnionFind.py

    c         C   sV   | d k r d } n  i  |  _ i  |  _ x( | D]  } d |  j | <| |  j | <q. Wd S(   sū   Create a new empty union-find structure.

        If *elements* is an iterable, this structure will be initialized
        with the discrete partition on the given set of elements.

        i   N(    (   t   Nonet   parentst   weights(   t   selft   elementst   x(    (    s8   lib/python2.7/site-packages/networkx/utils/union_find.pyt   __init__&   s    			c         C   s   | |  j  k r- | |  j  | <d |  j | <| S| g } |  j  | } x. | | d k rs | j |  |  j  | } qF Wx | D] } | |  j  | <q{ W| S(   s:   Find and return the name of the set containing the object.i   iĸĸĸĸ(   R   R   t   append(   R   t   objectt   patht   roott   ancestor(    (    s8   lib/python2.7/site-packages/networkx/utils/union_find.pyt   __getitem__5   s    	c         C   s   t  |  j  S(   sL   Iterate through all items ever found or unioned by this structure.

        (   t   iterR   (   R   (    (    s8   lib/python2.7/site-packages/networkx/utils/union_find.pyt   __iter__J   s    c         c   s)   x" t  |  j  j   D] } | Vq Wd S(   s]  Iterates over the sets stored in this structure.

        For example::

            >>> partition = UnionFind('xyz')
            >>> sorted(map(sorted, partition.to_sets()))
            [['x'], ['y'], ['z']]
            >>> partition.union('x', 'y')
            >>> sorted(map(sorted, partition.to_sets()))
            [['x', 'y'], ['z']]

        N(   R    R   t   values(   R   t   block(    (    s8   lib/python2.7/site-packages/networkx/utils/union_find.pyt   to_setsP   s    c            s   g  | D] }   | ^ q } t  | d   f d   } xD | D]< } | | k r?   j | c   j | 7<|   j | <q? q? Wd S(   s8   Find the sets containing the objects and merge them all.t   keyc            s     j  |  S(   N(   R   (   t   r(   R   (    s8   lib/python2.7/site-packages/networkx/utils/union_find.pyt   <lambda>e   s    N(   t   maxR   R   (   R   t   objectsR   t   rootst   heaviestR   (    (   R   s8   lib/python2.7/site-packages/networkx/utils/union_find.pyt   uniona   s    N(	   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   (    (    (    s8   lib/python2.7/site-packages/networkx/utils/union_find.pyR      s   			N(    (   R   t   networkx.utilsR    R   (    (    (    s8   lib/python2.7/site-packages/networkx/utils/union_find.pyt   <module>
   s   