Skip to content

Commit f6a3dbe

Browse files
committed
Added type-hints and docs
1 parent 54066de commit f6a3dbe

2 files changed

Lines changed: 103 additions & 8 deletions

File tree

src/tdamapper/utils/heap.py

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,47 @@
1-
def _left(i):
1+
"""
2+
This module implements a max-heap data structure.
3+
It provides methods to add elements, pop the maximum element,
4+
and iterate over the elements in the heap.
5+
"""
6+
7+
8+
def _left(i: int) -> int:
29
return 2 * i + 1
310

411

5-
def _right(i):
12+
def _right(i: int) -> int:
613
return 2 * i + 2
714

815

9-
def _parent(i):
16+
def _parent(i: int) -> int:
1017
return max(0, (i - 1) // 2)
1118

1219

1320
class _HeapNode:
21+
"""
22+
A node in the max-heap, storing a key and a value.
23+
24+
It provides methods to get the key and value, and to compare nodes.
25+
The comparison is based on the key, allowing the heap to maintain
26+
the max-heap property. It is important to note that the key must be
27+
comparable with other keys in the heap for the heap to function correctly.
28+
29+
:param key: The key used for ordering in the heap.
30+
:param value: The value associated with the key.
31+
:type key: Any
32+
:type value: Any
33+
"""
1434

1535
def __init__(self, key, value):
1636
self._key = key
1737
self._value = value
1838

1939
def get(self):
40+
"""
41+
Returns the key and value of the node.
42+
:return: A tuple containing the key and value.
43+
:rtype: tuple
44+
"""
2045
return self._key, self._value
2146

2247
def __lt__(self, other):
@@ -33,6 +58,12 @@ def __ge__(self, other):
3358

3459

3560
class MaxHeap:
61+
"""
62+
A max-heap data structure that allows for efficient retrieval of the maximum element.
63+
64+
It supports adding elements, popping the maximum element, and iterating over the elements.
65+
It is important to note that the keys used in the heap must be comparable with each other.
66+
"""
3667

3768
def __init__(self):
3869
self._heap = []
@@ -50,11 +81,23 @@ def __len__(self):
5081
return len(self._heap)
5182

5283
def top(self):
84+
"""
85+
Returns the maximum element in the heap without removing it.
86+
87+
:return: A tuple containing the key and value of the maximum element, or (None, None) if the heap is empty.
88+
:rtype: tuple
89+
"""
5390
if not self._heap:
5491
return (None, None)
5592
return self._heap[0].get()
5693

5794
def pop(self):
95+
"""
96+
Removes and returns the maximum element from the heap.
97+
98+
:return: A tuple containing the key and value of the maximum element, or None if the heap is empty.
99+
:rtype: tuple
100+
"""
58101
if not self._heap:
59102
return
60103
max_val = self._heap[0]
@@ -64,6 +107,16 @@ def pop(self):
64107
return max_val.get()
65108

66109
def add(self, key, val):
110+
"""
111+
Adds a new element to the heap with the specified key and value.
112+
113+
:param key: The key used for ordering in the heap.
114+
:param val: The value associated with the key.
115+
:type key: Any
116+
:type val: Any
117+
:return: None
118+
:rtype: None
119+
"""
67120
self._heap.append(_HeapNode(key, val))
68121
self._bubble_up()
69122

src/tdamapper/utils/unionfind.py

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
1-
class UnionFind:
1+
"""
2+
This module implements a Union-Find (Disjoint Set Union) data structure.
3+
It provides methods to find the root of an element and to union two elements.
4+
It is useful for efficiently managing and merging disjoint sets.
5+
"""
26

3-
def __init__(self, X):
4-
self._parent = {x: x for x in X}
7+
from typing import Dict, Generic, Iterable, TypeVar
8+
9+
T = TypeVar("T")
10+
11+
12+
class UnionFind(Generic[T]):
13+
"""
14+
A Union-Find data structure for managing disjoint sets.
15+
It supports efficient union and find operations, allowing for the
16+
merging of sets and finding the root representative of an element.
17+
It is particularly useful in algorithms that require dynamic connectivity,
18+
such as Kruskal's algorithm for minimum spanning trees or in network connectivity problems.
19+
20+
:param X: An iterable containing the initial elements to be managed by the Union-Find structure.
21+
:type X: iterable
22+
"""
23+
24+
def __init__(self, X: Iterable[T]):
25+
self._parent: Dict[T, T] = {x: x for x in X}
526
self._size = {x: 1 for x in X}
627

7-
def find(self, x):
28+
def find(self, x: T) -> T:
29+
"""
30+
Finds the root representative of the set containing element x.
31+
This method implements path compression to flatten the structure,
32+
making future queries faster by ensuring that all elements point directly to the root.
33+
:param x: The element whose root representative is to be found.
34+
:type x: Any
35+
:return: The root representative of the set containing x.
36+
:rtype: Any
37+
"""
838
root = x
939
while root != self._parent[root]:
1040
root = self._parent[root]
@@ -15,7 +45,19 @@ def find(self, x):
1545
tmp = parent
1646
return root
1747

18-
def union(self, x, y):
48+
def union(self, x: T, y: T) -> T:
49+
"""
50+
Unites the sets containing elements x and y.
51+
If the elements are already in the same set, no action is taken.
52+
If they are in different sets, the smaller set is merged into the larger set,
53+
ensuring that the root of the larger set becomes the new root representative.
54+
:param x: The first element to be united.
55+
:param y: The second element to be united.
56+
:type x: Any
57+
:type y: Any
58+
:return: The root representative of the united set.
59+
:rtype: Any
60+
"""
1961
x, y = self.find(x), self.find(y)
2062
if x != y:
2163
x_size, y_size = self._size[x], self._size[y]

0 commit comments

Comments
 (0)