
    4[g                         d Z  G d d      Zy)z
Disjoint set data structure
c                   T    e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zy)DisjointSeta   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                     d| _         i | _        i | _        i | _        i | _        ||D ]  }| j                  |        y y )N    )	n_subsets_sizes_parents_nbrs_indicesadd)selfelementsxs      S/var/www/html/bid-api/venv/lib/python3.12/site-packages/scipy/_lib/_disjoint_set.py__init__zDisjointSet.__init__[   sE    
       c                 ,    t        | j                        S )zsReturns an iterator of the elements in the disjoint set.

        Elements are ordered by insertion order.
        )iterr
   r   s    r   __iter__zDisjointSet.__iter__g   s    
 DMM""r   c                 ,    t        | j                        S N)lenr
   r   s    r   __len__zDisjointSet.__len__n   s    4==!!r   c                     || j                   v S r   r
   r   r   s     r   __contains__zDisjointSet.__contains__q   s    DMM!!r   c                     || j                   vrt        |      | j                  }| j                   |   | j                   ||      k7  r3|||      ||<   ||   }| j                   |   | j                   ||      k7  r3|S )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__zDisjointSet.__getitem__t   s     DMM!1+ --mmA$--
";; ,GAJ
A mmA$--
";; r   c                     || j                   v ryd| j                  |<   || j                  |<   || j                  |<   t	        | j                         | j                   |<   | xj
                  dz  c_        y)z(Add element `x` to disjoint set
        N   )r
   r   r   r	   r   r   r   s     r   r   zDisjointSet.add   s`     Aa

1t}}-a!r   c                    | |   }| |   }| j                   |   | j                   |   k(  ry| j                  }||   | j                   |   f||   | j                   |   fk  r||}}|| j                  |<   | j                  |xx   | j                  |   z  cc<   | j                  |   | j                  |   c| j                  |<   | j                  |<   | xj                  dz  c_        y)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   mergezDisjointSet.merge   s    " !W!W==b 11"It}}R()U2Yb8I,JJBbB4;;r?*)-BB&

2

2!r   c                 L    | j                   | |      | j                   | |      k(  S )a  Test 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   	connectedzDisjointSet.connected   s)     }}T!W%tAw)???r   c                 ,   || j                   vrt        |      |g}| j                  |   }| j                   |   | j                   |   k7  r@|j                  |       | j                  |   }| j                   |   | j                   |   k7  r@t	        |      S )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   subsetzDisjointSet.subset   s     DMM!1+jjmmmC DMM!$44MM#**S/C mmC DMM!$44 6{r   c                 &    | j                   | |      S )a  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_sizezDisjointSet.subset_size   s    " {{47##r   c                     g }t               }| D ]:  }||vs| j                  |      }|j                  |       |j                  |       < |S )zGet all the subsets in the disjoint set.

        Returns
        -------
        result : list
            Subsets in the disjoint set.
        )r.   r1   updater-   )r   r/   visitedr   xsets        r   subsetszDisjointSet.subsets   sN     %A{{1~t$d#	 
 r   r   )__name__
__module____qualname____doc__r   r   r   r   r!   r   r)   r+   r1   r3   r8    r   r   r   r      sA    Sh
#"".
>@.$&r   r   N)r<   r   r=   r   r   <module>r>      s   
x xr   