Skip to content

Commit d2296a4

Browse files
faizan842cclausspre-commit-ci[bot]
authored
Add comprehensive doctests to disjoint_set.py (#13619)
* Add comprehensive doctests to disjoint_set.py - Add doctests for make_set() function with basic initialization tests - Add doctests for union_set() function with union operations and connected nodes - Add doctests for find_set() function with path compression verification - Add doctests for find_python_set() function with error handling - Add doctests for test_disjoint_set() function with comprehensive example - All doctests pass and follow project coding standards - Improves test coverage for fundamental disjoint set data structure Contributes to #9943 * from dataclasses import dataclass * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 6a840d3 commit d2296a4

1 file changed

Lines changed: 64 additions & 4 deletions

File tree

‎data_structures/disjoint_set/disjoint_set.py‎

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,28 @@
33
Reference: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
44
"""
55

6+
from dataclasses import dataclass
67

8+
9+
@dataclass
710
class Node:
8-
def __init__(self, data: int) -> None:
9-
self.data = data
10-
self.rank: int
11-
self.parent: Node
11+
data: int
12+
rank: int = 0
13+
parent: Node | None = None
1214

1315

1416
def make_set(x: Node) -> None:
1517
"""
1618
Make x as a set.
19+
20+
>>> node = Node(1)
21+
>>> make_set(node)
22+
>>> node.parent == node
23+
True
24+
>>> node.rank
25+
0
26+
>>> node.data
27+
1
1728
"""
1829
# rank is the distance from x to its' parent
1930
# root's rank is 0
@@ -26,6 +37,22 @@ def union_set(x: Node, y: Node) -> None:
2637
Union of two sets.
2738
set with bigger rank should be parent, so that the
2839
disjoint set tree will be more flat.
40+
41+
>>> node1 = Node(1)
42+
>>> node2 = Node(2)
43+
>>> make_set(node1)
44+
>>> make_set(node2)
45+
>>> union_set(node1, node2)
46+
>>> find_set(node1) == find_set(node2)
47+
True
48+
>>> # Test union of already connected nodes
49+
>>> node3 = Node(3)
50+
>>> make_set(node3)
51+
>>> union_set(node1, node3)
52+
>>> find_set(node1) == find_set(node3)
53+
True
54+
>>> find_set(node2) == find_set(node3)
55+
True
2956
"""
3057
x, y = find_set(x), find_set(y)
3158
if x == y:
@@ -42,6 +69,24 @@ def union_set(x: Node, y: Node) -> None:
4269
def find_set(x: Node) -> Node:
4370
"""
4471
Return the parent of x
72+
73+
>>> node = Node(1)
74+
>>> make_set(node)
75+
>>> find_set(node) == node
76+
True
77+
>>> node1 = Node(1)
78+
>>> node2 = Node(2)
79+
>>> make_set(node1)
80+
>>> make_set(node2)
81+
>>> union_set(node1, node2)
82+
>>> find_set(node1) == find_set(node2)
83+
True
84+
>>> # Test path compression
85+
>>> node3 = Node(3)
86+
>>> make_set(node3)
87+
>>> union_set(node1, node3)
88+
>>> find_set(node1) == find_set(node3)
89+
True
4590
"""
4691
if x != x.parent:
4792
x.parent = find_set(x.parent)
@@ -51,6 +96,18 @@ def find_set(x: Node) -> Node:
5196
def find_python_set(node: Node) -> set:
5297
"""
5398
Return a Python Standard Library set that contains i.
99+
100+
>>> node = Node(1)
101+
>>> find_python_set(node)
102+
{0, 1, 2}
103+
>>> node = Node(4)
104+
>>> find_python_set(node)
105+
{3, 4, 5}
106+
>>> node = Node(6)
107+
>>> find_python_set(node)
108+
Traceback (most recent call last):
109+
...
110+
ValueError: 6 is not in ({0, 1, 2}, {3, 4, 5})
54111
"""
55112
sets = ({0, 1, 2}, {3, 4, 5})
56113
for s in sets:
@@ -62,6 +119,9 @@ def find_python_set(node: Node) -> set:
62119

63120
def test_disjoint_set() -> None:
64121
"""
122+
Test the disjoint set operations with a comprehensive example.
123+
Creates two disjoint sets: {0, 1, 2} and {3, 4, 5}
124+
65125
>>> test_disjoint_set()
66126
"""
67127
vertex = [Node(i) for i in range(6)]

0 commit comments

Comments
 (0)