|
1 | 1 | import pytest |
2 | 2 |
|
3 | | -from boruvkas_algorithm.boruvka import ( |
4 | | - Graph, |
5 | | - UnionFind, |
6 | | - find_mst_with_boruvkas_algorithm, |
7 | | -) |
| 3 | +from boruvkas_algorithm.boruvka import Graph, find_mst_with_boruvkas_algorithm |
| 4 | +from boruvkas_algorithm.union_find import UnionFind |
8 | 5 |
|
9 | 6 |
|
10 | 7 | @pytest.fixture |
@@ -52,6 +49,131 @@ def test_add_edge_invalid_vertices(setup_graph: Graph): |
52 | 49 | graph.add_edge(10, 11, 5) |
53 | 50 |
|
54 | 51 |
|
| 52 | +# ============================================================================= |
| 53 | +# UnionFind Tests |
| 54 | +# ============================================================================= |
| 55 | + |
| 56 | + |
| 57 | +def test_union_find_initialization(): |
| 58 | + """Tests that UnionFind initialises with correct parent and rank arrays.""" |
| 59 | + uf = UnionFind(5) |
| 60 | + assert uf.parent == [0, 1, 2, 3, 4], "Each node should be its own parent" |
| 61 | + assert uf.rank == [1, 1, 1, 1, 1], "Each node should have rank 1" |
| 62 | + |
| 63 | + |
| 64 | +def test_union_find_find_single_node(): |
| 65 | + """Tests that find returns the node itself when it's its own parent.""" |
| 66 | + uf = UnionFind(5) |
| 67 | + assert uf.find(0) == 0 |
| 68 | + assert uf.find(4) == 4 |
| 69 | + |
| 70 | + |
| 71 | +def test_union_find_union_two_nodes(): |
| 72 | + """Tests that union correctly combines two nodes.""" |
| 73 | + uf = UnionFind(5) |
| 74 | + result = uf.union(0, 1) |
| 75 | + assert result is True, "Union should return True when nodes are combined" |
| 76 | + assert uf.find(0) == uf.find(1), "Nodes should have the same root after union" |
| 77 | + |
| 78 | + |
| 79 | +def test_union_find_union_already_connected(): |
| 80 | + """Tests that union returns False when nodes are already connected.""" |
| 81 | + uf = UnionFind(5) |
| 82 | + uf.union(0, 1) |
| 83 | + result = uf.union(0, 1) |
| 84 | + assert result is False, "Union should return False when already connected" |
| 85 | + |
| 86 | + |
| 87 | +def test_union_find_union_by_size(): |
| 88 | + """Tests that smaller trees are merged into larger trees.""" |
| 89 | + uf = UnionFind(5) |
| 90 | + # Create a larger tree: 0 <- 1, 0 <- 2 |
| 91 | + uf.union(0, 1) |
| 92 | + uf.union(0, 2) |
| 93 | + # Now union with node 3 - node 3 should be merged into the larger tree. |
| 94 | + uf.union(3, 0) |
| 95 | + # The root of the larger tree should remain the root. |
| 96 | + root = uf.find(0) |
| 97 | + assert uf.find(3) == root, "Smaller tree should be merged into larger tree" |
| 98 | + |
| 99 | + |
| 100 | +def test_union_find_path_compression(): |
| 101 | + """Tests that path compression flattens the tree structure.""" |
| 102 | + uf = UnionFind(5) |
| 103 | + # Create a chain: 0 <- 1 <- 2 <- 3 |
| 104 | + uf.parent = [0, 0, 1, 2, 4] |
| 105 | + uf.rank = [4, 1, 1, 1, 1] |
| 106 | + # Find on node 3 should compress the path. |
| 107 | + root = uf.find(3) |
| 108 | + assert root == 0, "Root should be 0" |
| 109 | + # After path compression, intermediate nodes should point closer to root. |
| 110 | + assert uf.parent[2] in (0, 1), "Path compression should shorten the path" |
| 111 | + |
| 112 | + |
| 113 | +def test_union_find_multiple_components(): |
| 114 | + """Tests UnionFind with multiple separate components.""" |
| 115 | + uf = UnionFind(6) |
| 116 | + # Create two components: {0, 1, 2} and {3, 4, 5} |
| 117 | + uf.union(0, 1) |
| 118 | + uf.union(1, 2) |
| 119 | + uf.union(3, 4) |
| 120 | + uf.union(4, 5) |
| 121 | + |
| 122 | + # Check components are separate. |
| 123 | + assert uf.find(0) == uf.find(1) == uf.find(2) |
| 124 | + assert uf.find(3) == uf.find(4) == uf.find(5) |
| 125 | + assert uf.find(0) != uf.find(3), "Components should be separate" |
| 126 | + |
| 127 | + # Merge the two components. |
| 128 | + uf.union(2, 3) |
| 129 | + assert uf.find(0) == uf.find(5), "Components should be merged" |
| 130 | + |
| 131 | + |
| 132 | +def test_union_find_is_connected(): |
| 133 | + """Tests the is_connected convenience method.""" |
| 134 | + uf = UnionFind(5) |
| 135 | + assert not uf.is_connected(0, 1), "Nodes should not be connected initially" |
| 136 | + |
| 137 | + uf.union(0, 1) |
| 138 | + assert uf.is_connected(0, 1), "Nodes should be connected after union" |
| 139 | + assert not uf.is_connected(0, 2), "Unconnected nodes should return False" |
| 140 | + |
| 141 | + uf.union(1, 2) |
| 142 | + assert uf.is_connected(0, 2), "Transitively connected nodes should return True" |
| 143 | + |
| 144 | + |
| 145 | +# ============================================================================= |
| 146 | +# MST Algorithm Tests |
| 147 | +# ============================================================================= |
| 148 | + |
| 149 | + |
| 150 | +def test_mst_with_injected_union_find(setup_graph: Graph): |
| 151 | + """Tests that the algorithm works with an injected UnionFind instance.""" |
| 152 | + graph = setup_graph |
| 153 | + graph.add_edge(0, 1, 4) |
| 154 | + graph.add_edge(0, 6, 7) |
| 155 | + graph.add_edge(1, 6, 11) |
| 156 | + graph.add_edge(1, 7, 20) |
| 157 | + graph.add_edge(1, 2, 9) |
| 158 | + graph.add_edge(2, 3, 6) |
| 159 | + graph.add_edge(2, 4, 2) |
| 160 | + graph.add_edge(3, 4, 10) |
| 161 | + graph.add_edge(3, 5, 5) |
| 162 | + graph.add_edge(4, 5, 15) |
| 163 | + graph.add_edge(4, 7, 1) |
| 164 | + graph.add_edge(4, 8, 5) |
| 165 | + graph.add_edge(5, 8, 12) |
| 166 | + graph.add_edge(6, 7, 1) |
| 167 | + graph.add_edge(7, 8, 3) |
| 168 | + |
| 169 | + # Inject a custom UnionFind instance. |
| 170 | + union_find = UnionFind(len(graph.vertices)) |
| 171 | + mst_weight, mst_edges = find_mst_with_boruvkas_algorithm(graph, union_find) |
| 172 | + |
| 173 | + assert mst_weight == 29, "MST weight should be 29" |
| 174 | + assert len(mst_edges) == 8, "MST should have 8 edges for 9 vertices" |
| 175 | + |
| 176 | + |
55 | 177 | def test_mst(setup_graph: Graph): |
56 | 178 | """ |
57 | 179 | Tests that the MST has the correct total weight and structure by comparing |
@@ -94,33 +216,6 @@ def test_mst(setup_graph: Graph): |
94 | 216 | ) |
95 | 217 |
|
96 | 218 |
|
97 | | -def test_mst_with_injected_union_find(setup_graph: Graph): |
98 | | - """Tests that the algorithm works with an injected UnionFind instance.""" |
99 | | - graph = setup_graph |
100 | | - graph.add_edge(0, 1, 4) |
101 | | - graph.add_edge(0, 6, 7) |
102 | | - graph.add_edge(1, 6, 11) |
103 | | - graph.add_edge(1, 7, 20) |
104 | | - graph.add_edge(1, 2, 9) |
105 | | - graph.add_edge(2, 3, 6) |
106 | | - graph.add_edge(2, 4, 2) |
107 | | - graph.add_edge(3, 4, 10) |
108 | | - graph.add_edge(3, 5, 5) |
109 | | - graph.add_edge(4, 5, 15) |
110 | | - graph.add_edge(4, 7, 1) |
111 | | - graph.add_edge(4, 8, 5) |
112 | | - graph.add_edge(5, 8, 12) |
113 | | - graph.add_edge(6, 7, 1) |
114 | | - graph.add_edge(7, 8, 3) |
115 | | - |
116 | | - # Inject a custom UnionFind instance. |
117 | | - union_find = UnionFind(len(graph.vertices)) |
118 | | - mst_weight, mst_edges = find_mst_with_boruvkas_algorithm(graph, union_find) |
119 | | - |
120 | | - assert mst_weight == 29, "MST weight should be 29" |
121 | | - assert len(mst_edges) == 8, "MST should have 8 edges for 9 vertices" |
122 | | - |
123 | | - |
124 | 219 | def test_mst_simple_triangle(): |
125 | 220 | """Tests MST on a simple triangle graph.""" |
126 | 221 | graph = Graph(3) |
|
0 commit comments