class UnionFind(Generic[
Constructor: UnionFind(elements)
Union-find disjoint sets datastructure.
Union-find is a data structure that maintains disjoint set (called connected components or components in short) membership, and makes it easier to merge (union) two components, and to find if two elements are connected (i.e., belong to the same component).
This implements the "weighted-quick-union-with-path-compression" union-find algorithm. Only works if elements are immutable objects.
Worst case for union and find: (N + Mlog*N), with N elements and M unions. The function log* is the number of times needed to take log of a number until reaching 1. In practice, the amortized cost of each operation is nearly linear [1].
Terms
- Component
- Elements belonging to the same disjoint set
- Connected
- Two elements are connected if they belong to the same component.
- Union
- The operation where two components are merged into one.
- Root
- An internal representative of a disjoint set.
- Find
- The operation to find the root of a disjoint set.
Parameters
- elements : NoneType or container, optional, default: None
- The initial list of elements.
Attributes
- n_elts : int
- Number of elements.
- n_comps : int
- Number of distjoint sets or components.
| [1] | http://algs4.cs.princeton.edu/lectures/ |
| Method | __contains__ |
Undocumented |
| Method | __getitem__ |
Undocumented |
| Method | __init__ |
Undocumented |
| Method | __iter__ |
Undocumented |
| Method | __len__ |
Undocumented |
| Method | __repr__ |
Undocumented |
| Method | __str__ |
Undocumented |
| Method | add |
Add a single disjoint element. |
| Method | component |
Find the connected component containing the given element. |
| Method | components |
Return the list of connected components. |
| Method | connected |
Return whether the two given elements belong to the same component. |
| Method | copy |
Copy this union-find into a fresh one. |
| Method | remove |
Remove an item from the collection. |
| Method | union |
Merge the components of the two given elements into one. Initialize elements if they are not already in the collection. |
| Class Variable | free |
Undocumented |
| Instance Variable | n |
Undocumented |
| Instance Variable | n |
Undocumented |
| Method | _find |
Find the index of root of the disjoint set containing the given element. |
| Instance Variable | _elts |
Undocumented |
| Instance Variable | _free |
Undocumented |
| Instance Variable | _indx |
Undocumented |
| Instance Variable | _nb |
Undocumented |
| Instance Variable | _next |
Undocumented |
| Instance Variable | _par |
Undocumented |
| Instance Variable | _removed |
Undocumented |
| Instance Variable | _siz |
Undocumented |
Find the connected component containing the given element.
Parameters
x : immutable object
Returns
Insertion ordered set, first element is the root.
Raises
- ValueError
- If the given element is not found.
Return the list of connected components.
Returns
- list
- A list of insertion ordered set, first element is the root.
Return whether the two given elements belong to the same component.
Parameters
x : immutable object y : immutable object
Returns
- bool
- True if x and y are connected, false otherwise.
Merge the components of the two given elements into one. Initialize elements if they are not already in the collection.
Parameters
x : immutable object y : immutable object
Find the index of root of the disjoint set containing the given element.
Parameters
x : immutable object
Returns
- int
- The (index of the) root.
Raises
- ValueError
- If the given element is not found.
Note
Despite beeing one of the core method of a 'Union-Find' structure,
_find() is marked as private. This is because the implementation
of the removal feature implies that _find might return the index
of a removed element. So the returned number shoud not be used to access
a element of the union, simply to act like a representant object for it's set.