
    (ph                         S r  " S S5      rg)z
Disjoint set data structure
c                   ^    \ rS rSrSrSS jrS rS rS rS r	S	 r
S
 rS rS rS rS rSrg)DisjointSet   a  Disjoint set data structure for incremental connectivity queries.

.. versionadded:: 1.6.0

Attributes
----------
n_subsets : int
    The number of subsets.

Methods
-------
add
merge
connected
subset
subset_size
subsets
__getitem__

Notes
-----
This class implements the disjoint set [1]_, also known as the *union-find*
or *merge-find* data structure. The *find* operation (implemented in
`__getitem__`) implements the *path halving* variant. The *merge* method
implements the *merge by size* variant.

References
----------
.. [1] https://en.wikipedia.org/wiki/Disjoint-set_data_structure

Examples
--------
>>> from scipy.cluster.hierarchy import DisjointSet

Initialize a disjoint set:

>>> disjoint_set = DisjointSet([1, 2, 3, 'a', 'b'])

Merge some subsets:

>>> disjoint_set.merge(1, 2)
True
>>> disjoint_set.merge(3, 'a')
True
>>> disjoint_set.merge('a', 'b')
True
>>> disjoint_set.merge('b', 'b')
False

Find root elements:

>>> disjoint_set[2]
1
>>> disjoint_set['b']
3

Test connectivity:

>>> disjoint_set.connected(1, 2)
True
>>> disjoint_set.connected(1, 'b')
False

List elements in disjoint set:

>>> list(disjoint_set)
[1, 2, 3, 'a', 'b']

Get the subset containing 'a':

>>> disjoint_set.subset('a')
{'a', 3, 'b'}

Get the size of the subset containing 'a' (without actually instantiating
the subset):

>>> disjoint_set.subset_size('a')
3

Get all subsets in the disjoint set:

>>> disjoint_set.subsets()
[{1, 2}, {'a', 3, 'b'}]
Nc                     SU l         0 U l        0 U l        0 U l        0 U l        Ub  U H  nU R                  U5        M     g g )N    )	n_subsets_sizes_parents_nbrs_indicesadd)selfelementsxs      K/var/www/html/venv/lib/python3.13/site-packages/scipy/_lib/_disjoint_set.py__init__DisjointSet.__init__[   sE    
       c                 ,    [        U R                  5      $ )zcReturns an iterator of the elements in the disjoint set.

Elements are ordered by insertion order.
)iterr   r   s    r   __iter__DisjointSet.__iter__g   s    
 DMM""r   c                 ,    [        U R                  5      $ N)lenr   r   s    r   __len__DisjointSet.__len__n   s    4==!!r   c                     XR                   ;   $ r   r   r   r   s     r   __contains__DisjointSet.__contains__q   s    MM!!r   c                     XR                   ;  a  [        U5      eU R                  nU R                   U   U R                   X!      :w  a1  X"U      X!'   X!   nU R                   U   U R                   X!      :w  a  M1  U$ )zFind the root element of `x`.

Parameters
----------
x : hashable object
    Input element.

Returns
-------
root : hashable object
    Root element of `x`.
)r   KeyErrorr	   )r   r   parentss      r   __getitem__DisjointSet.__getitem__t   sy     MM!1+ --mmA$--
";; ,GJ
A mmA$--
";; r   c                     XR                   ;   a  gSU R                  U'   XR                  U'   XR                  U'   [	        U R                   5      U R                   U'   U =R
                  S-  sl        g)z(Add element `x` to disjoint set
        N   )r   r   r	   r
   r   r   r    s     r   r   DisjointSet.add   sZ     Aa

1t}}-a!r   c                    X   nX   nU R                   U   U R                   U   :X  a  gU R                  nXS   U R                   U   4XT   U R                   U   4:  a  XCpCX0R                  U'   U R                  U==   U R                  U   -  ss'   U R                  U   U R                  U   sU R                  U'   U R                  U'   U =R                  S-  sl        g)a  Merge the subsets of `x` and `y`.

The smaller subset (the child) is merged into the larger subset (the
parent). If the subsets are of equal size, the root element which was
first inserted into the disjoint set is selected as the parent.

Parameters
----------
x, y : hashable object
    Elements to merge.

Returns
-------
merged : bool
    True if `x` and `y` were in disjoint sets, False otherwise.
Fr)   T)r   r   r	   r
   r   )r   r   yxryrsizess         r   mergeDisjointSet.merge   s    " WW==b 11It}}R()UYb8I,JJbB4;;r?*)-BB&

2

2!r   c                 H    U R                   X      U R                   X      :H  $ )zTest whether `x` and `y` are in the same subset.

Parameters
----------
x, y : hashable object
    Elements to test.

Returns
-------
result : bool
    True if `x` and `y` are in the same set, False otherwise.
r   )r   r   r,   s      r   	connectedDisjointSet.connected   s%     }}TW%tw)???r   c                 4   XR                   ;  a  [        U5      eU/nU R                  U   nU R                   U   U R                   U   :w  aB  UR                  U5        U R                  U   nU R                   U   U R                   U   :w  a  MB  [	        U5      $ )zGet the subset containing `x`.

Parameters
----------
x : hashable object
    Input element.

Returns
-------
result : set
    Subset containing `x`.
)r   r$   r
   appendset)r   r   resultnxts       r   subsetDisjointSet.subset   s     MM!1+jjmmmC DMM!$44MM#**S/C mmC DMM!$44 6{r   c                 $    U R                   X      $ )aV  Get the size of the subset containing `x`.

Note that this method is faster than ``len(self.subset(x))`` because
the size is directly read off an internal field, without the need to
instantiate the full subset.

Parameters
----------
x : hashable object
    Input element.

Returns
-------
result : int
    Size of the subset containing `x`.
)r   r    s     r   subset_sizeDisjointSet.subset_size   s    " {{47##r   c                     / n[        5       nU  H=  nX2;  d  M
  U R                  U5      nUR                  U5        UR                  U5        M?     U$ )ziGet all the subsets in the disjoint set.

Returns
-------
result : list
    Subsets in the disjoint set.
)r7   r:   updater6   )r   r8   visitedr   xsets        r   subsetsDisjointSet.subsets   sL     %A{{1~t$d#	 
 r   )r   r
   r	   r   r   r   )__name__
__module____qualname____firstlineno____doc__r   r   r   r!   r&   r   r0   r3   r:   r=   rC   __static_attributes__ r   r   r   r      sA    Sh
#"".
>@.$&r   r   N)rI   r   rK   r   r   <module>rL      s   
x xr   