Skip to content

Commit 073ef00

Browse files
committed
Added docstrings
1 parent 9f361c4 commit 073ef00

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/tdamapper/utils/unionfind.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
1+
"""
2+
This module implements a Union-Find data structure that supports union and
3+
find operations.
4+
"""
5+
16
from typing import Any, Iterable
27

38

49
class UnionFind:
10+
"""
11+
A Union-Find data structure that supports union and find operations.
12+
13+
This implementation uses path compression for efficient find operations
14+
and union by size to keep the tree flat. It allows for efficient
15+
determination of connected components in a set of elements.
16+
17+
:param X: An iterable of elements to initialize the Union-Find structure.
18+
"""
519

620
_parent: dict[Any, Any]
721
_size: dict[Any, int]
822

9-
def __init__(self, X: Iterable[Any]):
10-
self._parent = {x: x for x in X}
11-
self._size = {x: 1 for x in X}
23+
def __init__(self, items: Iterable[Any]):
24+
self._parent = {x: x for x in items}
25+
self._size = {x: 1 for x in items}
1226

1327
def find(self, x: Any) -> Any:
28+
"""
29+
Finds the class of an element, applying path compression.
30+
31+
:param x: The element to find the class of.
32+
:return: The representative of the class containing x.
33+
"""
1434
root = x
1535
while root != self._parent[root]:
1636
root = self._parent[root]
@@ -22,6 +42,13 @@ def find(self, x: Any) -> Any:
2242
return root
2343

2444
def union(self, x: Any, y: Any) -> Any:
45+
"""
46+
Unites the classes of two elements.
47+
48+
:param x: The first element.
49+
:param y: The second element.
50+
:return: The representative of the class after the union operation.
51+
"""
2552
x, y = self.find(x), self.find(y)
2653
if x != y:
2754
x_size, y_size = self._size[x], self._size[y]

0 commit comments

Comments
 (0)