← Back
Editing: _disjoint_set.cpython-311.pyc
� d�ck � �$ � d Z G d� d� � ZdS )z Disjoint set data structure c �P � e Zd ZdZd d�Zd� Zd� Zd� Zd� Zd� Z d � Z d � Zd� Zd� Z dS )�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 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 all subsets in the disjoint set: >>> disjoint_set.subsets() [{1, 2}, {'a', 3, 'b'}] Nc � � d| _ i | _ i | _ i | _ i | _ |�|D ]}| � |� � �d S d S )N� )� n_subsets�_sizes�_parents�_nbrs�_indices�add)�self�elements�xs �:/usr/lib/python3/dist-packages/scipy/_lib/_disjoint_set.py�__init__zDisjointSet.__init__T s] � ��������� ��� ��� ���� � ���������� �� � � 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__zDisjointSet.__iter__` s � � �D�M�"�"�"r c �* � t | j � � S �N)�lenr r s r �__len__zDisjointSet.__len__g s � ��4�=�!�!�!r c � � || j v S r �r �r r s r �__contains__zDisjointSet.__contains__j s � ��D�M�!�!r c � � || j vrt |� � �| j }| j | | j || k r;||| ||<