
    \Mh
                     0    d Z ddlmZ  G d d          ZdS )z
Union-find data structure.
    )groupsc                   2    e Zd ZdZddZd Zd Zd Zd ZdS )		UnionFinda  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,
      https://code.activestate.com/recipes/215912/
      with significant additional changes by D. Eppstein.
      http://www.ics.uci.edu/~eppstein/PADS/UnionFind.py

    Nc                 \    |d}i | _         i | _        |D ]}d| j        |<   || j         |<   dS )zCreate 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.

        N    )parentsweights)selfelementsxs      Y/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/networkx/utils/union_find.py__init__zUnionFind.__init__   sN     H 	  	 ADLODLOO	  	     c                     || j         vr|| j         |<   d| j        |<   |S g }| j         |         }||k    r*|                    |           |}| j         |         }||k    *|D ]}|| j         |<   |S )z:Find and return the name of the set containing the object.r   )r	   r
   append)r   objectpathrootancestors        r   __getitem__zUnionFind.__getitem__.   s     %%#)DL #$DL M |F#fnnKKF<'D fnn  	* 	*H%)DL""r   c                 *    t          | j                  S )zBIterate through all items ever found or unioned by this structure.)iterr	   )r   s    r   __iter__zUnionFind.__iter__D   s    DL!!!r   c              #      K   | j         D ]
}| |         }t          | j                                                   E d{V  dS )a]  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   values)r   r   _s      r   to_setszUnionFind.to_setsH   sX        	 	AQAA$,''..00000000000r   c                     t          t           fd|D              fdd                    }	 t          |          }n# t          $ r Y dS w xY w|D ],} j        |xx          j        |         z  cc<   | j        |<   -dS )z8Find the sets containing the objects and merge them all.c                      h | ]
}|         S r   r   ).0r   r   s     r   	<setcomp>z"UnionFind.union.<locals>.<setcomp>`   s    ***Qa***r   c                     j         |          S N)r
   )rr   s    r   <lambda>z!UnionFind.union.<locals>.<lambda>`   s    $,q/ r   T)keyreverseN)r   sortednextStopIterationr
   r	   )r   objectsrootsr   r%   s   `    r   unionzUnionFind.union[   s     ****'***0I0I0I0ISW  
 

	;;DD 	 	 	FF	  	# 	#AL$,q/1"DLOO	# 	#s   A 
AAr$   )	__name__
__module____qualname____doc__r   r   r   r   r.   r   r   r   r   r      sn         ,         ," " "1 1 1&# # # # #r   r   N)r2   networkx.utilsr   r   r   r   r   <module>r4      s_     " ! ! ! ! !b# b# b# b# b# b# b# b# b# b#r   