Skip to content

Commit 4b2e30a

Browse files
meysam81cclausspoyeagithub-actions
authored andcommitted
graphs/kruskal: add a test case to verify the correctness, fix styles (TheAlgorithms#2443)
* test/graphs/kruskal: adding a test case to verify the correctness of the algorithm Fixes TheAlgorithms#2128 * grahps/kruskal: running psf/black * graphs/kruskal: read edges in a friendlier fashion Co-authored-by: Christian Clauss <[email protected]> * Update minimum_spanning_tree_kruskal.py * fixup! Format Python code with psf/black push * Update test_min_spanning_tree_kruskal.py * updating DIRECTORY.md Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: John Law <[email protected]> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 805b599 commit 4b2e30a

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

Diff for: DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@
293293
* [Scc Kosaraju](https://github.com/TheAlgorithms/Python/blob/master/graphs/scc_kosaraju.py)
294294
* [Strongly Connected Components](https://github.com/TheAlgorithms/Python/blob/master/graphs/strongly_connected_components.py)
295295
* [Tarjans Scc](https://github.com/TheAlgorithms/Python/blob/master/graphs/tarjans_scc.py)
296+
* Tests
297+
* [Test Min Spanning Tree Kruskal](https://github.com/TheAlgorithms/Python/blob/master/graphs/tests/test_min_spanning_tree_kruskal.py)
296298

297299
## Greedy Method
298300
* [Greedy Knapsack](https://github.com/TheAlgorithms/Python/blob/master/greedy_method/greedy_knapsack.py)

Diff for: graphs/minimum_spanning_tree_kruskal.py

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
if __name__ == "__main__":
2-
num_nodes, num_edges = list(map(int, input().strip().split()))
3-
4-
edges = []
5-
6-
for i in range(num_edges):
7-
node1, node2, cost = list(map(int, input().strip().split()))
8-
edges.append((i, node1, node2, cost))
9-
10-
edges = sorted(edges, key=lambda edge: edge[3])
1+
def kruskal(num_nodes, num_edges, edges):
2+
edges = sorted(edges, key=lambda edge: edge[2])
113

124
parent = list(range(num_nodes))
135

@@ -20,13 +12,22 @@ def find_parent(i):
2012
minimum_spanning_tree = []
2113

2214
for edge in edges:
23-
parent_a = find_parent(edge[1])
24-
parent_b = find_parent(edge[2])
15+
parent_a = find_parent(edge[0])
16+
parent_b = find_parent(edge[1])
2517
if parent_a != parent_b:
26-
minimum_spanning_tree_cost += edge[3]
18+
minimum_spanning_tree_cost += edge[2]
2719
minimum_spanning_tree.append(edge)
2820
parent[parent_a] = parent_b
2921

30-
print(minimum_spanning_tree_cost)
31-
for edge in minimum_spanning_tree:
32-
print(edge)
22+
return minimum_spanning_tree
23+
24+
25+
if __name__ == "__main__": # pragma: no cover
26+
num_nodes, num_edges = list(map(int, input().strip().split()))
27+
edges = []
28+
29+
for _ in range(num_edges):
30+
node1, node2, cost = [int(x) for x in input().strip().split()]
31+
edges.append((node1, node2, cost))
32+
33+
kruskal(num_nodes, num_edges, edges)

Diff for: graphs/tests/test_min_spanning_tree_kruskal.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from graphs.minimum_spanning_tree_kruskal import kruskal
2+
3+
4+
def test_kruskal_successful_result():
5+
num_nodes, num_edges = 9, 14
6+
edges = [
7+
[0, 1, 4],
8+
[0, 7, 8],
9+
[1, 2, 8],
10+
[7, 8, 7],
11+
[7, 6, 1],
12+
[2, 8, 2],
13+
[8, 6, 6],
14+
[2, 3, 7],
15+
[2, 5, 4],
16+
[6, 5, 2],
17+
[3, 5, 14],
18+
[3, 4, 9],
19+
[5, 4, 10],
20+
[1, 7, 11],
21+
]
22+
23+
result = kruskal(num_nodes, num_edges, edges)
24+
25+
expected = [
26+
[7, 6, 1],
27+
[2, 8, 2],
28+
[6, 5, 2],
29+
[0, 1, 4],
30+
[2, 5, 4],
31+
[2, 3, 7],
32+
[0, 7, 8],
33+
[3, 4, 9],
34+
]
35+
36+
assert sorted(expected) == sorted(result)

0 commit comments

Comments
 (0)