Skip to content

Commit

Permalink
Merge pull request keon#464 from travishen/master
Browse files Browse the repository at this point in the history
refactor function find_set
  • Loading branch information
goswami-rahul authored Dec 13, 2018
2 parents 1cfe245 + 6354b68 commit 141bcc6
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions algorithms/graph/minimum_spanning_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def merge_set(self, a, b):

# Get the set of nodes at position <a> and <b>
# If <a> and <b> are the roots, this will be constant O(1)
a = self.findSet(a)
b = self.findSet(b)
a = self.find_set(a)
b = self.find_set(b)

# Join the shortest node to the longest, minimizing tree size (faster find)
if self.size[a] < self.size[b]:
Expand Down Expand Up @@ -73,8 +73,8 @@ def kruskal(n, edges, ds):
mst = [] # List of edges taken, minimum spanning tree

for edge in edges:
set_u = ds.findSet(edge.u) # Set of the node <u>
set_v = ds.findSet(edge.v) # Set of the node <v>
set_u = ds.find_set(edge.u) # Set of the node <u>
set_v = ds.find_set(edge.v) # Set of the node <v>
if set_u != set_v:
ds.merge_set(set_u, set_v)
mst.append(edge)
Expand Down Expand Up @@ -127,4 +127,4 @@ def kruskal(n, edges, ds):
edges[i] = Edge(u, v, weight)

# After finish input and graph creation, use Kruskal algorithm for MST:
print("MST weights sum:", kruskal(n, edges, ds))
print("MST weights sum:", kruskal(n, edges, ds))

0 comments on commit 141bcc6

Please sign in to comment.