Skip to content

Commit 59ab6e3

Browse files
committed
Added Kruskals MST
1 parent 1bba71c commit 59ab6e3

File tree

6 files changed

+41
-43
lines changed

6 files changed

+41
-43
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ Algos in Python
22
======
33

44
Implementation for the algorithms and datastructures taught in [CS161](http://openclassroom.stanford.edu/MainFolder/CoursePage.php?course=IntroToAlgorithms), in Python
5-
Includes test code - doctests for few and full unittests for others
65

76
Completed
87
---

graphs/graph_algorithms.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from collections import deque
2-
from graph import graph
3-
from digraph import digraph
42
from copy import deepcopy
53
import heapq
4+
from union_find.unionfind import UnionFind
65

76
def BFS(gr, s):
87
""" Breadth first search
@@ -219,3 +218,17 @@ def compute_key(gr, n, nodes_explored):
219218
w = gr.get_edge_weight((n, v))
220219
if w < min: min = w
221220
return min
221+
222+
def kruskal_MST(gr):
223+
""" computes minimum cost spanning tree in a undirected,
224+
connected graph using Kruskal's MST. Uses union-find data structure
225+
for running times of O(mlogn) """
226+
sorted_edges = sorted(gr.get_edge_weights())
227+
uf = UnionFind()
228+
min_cost = 0
229+
for (w, (u, v)) in sorted_edges:
230+
if (not uf.get_leader(u) and not uf.get_leader(v)) \
231+
or (uf.get_leader(u) != uf.get_leader(v)):
232+
uf.insert(u, v)
233+
min_cost += w
234+
return min_cost

tests/assign.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import os, sys
22
sys.path.append(os.path.join(os.getcwd(), os.path.pardir))
3-
4-
from graphs import digraph
5-
from heaps import minheap
3+
from graphs.graph import graph
4+
from union_find.unionfind import UnionFind
65

76
lines = [l for l in open("edges.txt").readlines()]
87
lines = lines[1:]
@@ -16,10 +15,15 @@
1615
gr.add_node(v)
1716
gr.add_edge((u, v), int(w))
1817

19-
2018
def kruskal_MST(gr):
2119
sorted_edges = sorted(gr.get_edge_weights())
22-
temp_gr = {}
20+
uf = UnionFind()
2321
min_cost = 0
2422
for (w, (u, v)) in sorted_edges:
25-
temp_gr[u] = v
23+
if (not uf.get_leader(u) and not uf.get_leader(v)) \
24+
or (uf.get_leader(u) != uf.get_leader(v)):
25+
uf.insert(u, v)
26+
min_cost += w
27+
return min_cost
28+
29+
print kruskal_MST(gr)

tests/graph_algorithms_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,20 @@ def test_prims_minimum_spanning_tree(self):
115115
min_cost = minimum_spanning_tree(gr)
116116
self.assertEqual(min_cost, 39)
117117

118+
def test_kruskals_minimum_spanning_tree(self):
119+
lines = [l for l in open("tests/edges.txt")]
120+
lines = lines[1:]
121+
edges = (l.split() for l in lines)
122+
gr = graph()
123+
for (u, v, w) in edges:
124+
if u not in gr.nodes():
125+
gr.add_node(u)
126+
if v not in gr.nodes():
127+
gr.add_node(v)
128+
gr.add_edge( (u, v), int(w) )
129+
130+
min_cost = kruskal_MST(gr)
131+
self.assertEqual(min_cost, 39)
132+
118133
if __name__ == "__main__":
119134
unittest.main()

union_find/unionfind.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def make_union(self, leadera, leaderb):
5151
groupb = self.group[leaderb]
5252
if len(groupa) < len(groupb):
5353
# swap a and b if a is a smaller set
54-
a, leadera, groupa, b, leaderb, groupb = b, leaderb, groupb, a, leadera, groupa
54+
leadera, groupa, leaderb, groupb = leaderb, groupb, leadera, groupa
5555
groupa |= groupb # taking union of a with b
5656
del self.group[leaderb] # delete b
5757
for k in groupb:

union_find/unionfind_test.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy