
    h
                     (    S r SSKJr   " S S5      rg)z
Union-find data structure.
    )groupsc                   :    \ rS rSrSrS
S jrS rS rS rS r	S	r
g)	UnionFind   aP  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                 v    Uc  Sn0 U l         0 U l        U H   nSU R                  U'   X R                   U'   M"     g)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      K/var/www/html/env/lib/python3.13/site-packages/networkx/utils/union_find.py__init__UnionFind.__init__   s?     HADLLOLLO     c                    XR                   ;  a  XR                   U'   SU R                  U'   U$ / nU R                   U   nX1:w  a)  UR                  U5        UnU R                   U   nX1:w  a  M)  U H  nX0R                   U'   M     U$ )z:Find and return the name of the set containing the object.r	   )r   r   append)r   objectpathrootancestors        r   __getitem__UnionFind.__getitem__.   s     %#)LL #$DLL M ||F#nKKF<<'D n H%)LL" r   c                 ,    [        U R                  5      $ )zBIterate through all items ever found or unioned by this structure.)iterr   )r   s    r   __iter__UnionFind.__iter__D   s    DLL!!r   c              #      #    U R                    H  nX   nM	     [        U R                   5      R                  5        Sh  vN   g N7f)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_setsUnionFind.to_setsH   s9      AA  $,,'..000s   >A AAc           	        ^  [        [        U Vs1 s H  nT U   iM
     snU 4S jSS95      n [        U5      nU H6  nT R                  U==   T R                  U   -  ss'   UT R
                  U'   M8     gs  snf ! [         a     gf = f)z8Find the sets containing the objects and merge them all.c                 "   > TR                   U    $ N)r   )rr   s    r   <lambda>!UnionFind.union.<locals>.<lambda>`   s    $,,q/r   T)keyreverseN)r   sortednextStopIterationr   r   )r   objectsr   rootsr   r(   s   `     r   unionUnionFind.union[   s     ")*'Qa'*0ISW

	;D ALL$,,q/1"DLLO  +
  		s   A7A< <
B	B	r
   r'   )__name__
__module____qualname____firstlineno____doc__r   r   r   r#   r2   __static_attributes__r   r   r   r   r      s     , ,"1&#r   r   N)r8   networkx.utilsr   r   r   r   r   <module>r;      s    "b# b#r   