aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakiyamn2021-04-19 18:18:52 +1000
committerakiyamn2021-04-19 18:18:52 +1000
commit6a7732a192f9f6c3ebc16f86875e9fb627ce7af4 (patch)
treea4ceb2fcf8e0dc0e9ebf239581086260e81e0a90
parent6fc4e20b4aadb28d6f5bfc15146ab83cc47e626b (diff)
downloadfit3155-6a7732a192f9f6c3ebc16f86875e9fb627ce7af4.tar.gz
fit3155-6a7732a192f9f6c3ebc16f86875e9fb627ce7af4.zip
Ass 2 Q1 union-by-height -> rank
-rw-r--r--ass2/q1/kruskals.py11
1 files changed, 7 insertions, 4 deletions
diff --git a/ass2/q1/kruskals.py b/ass2/q1/kruskals.py
index f3c0703..6b73019 100644
--- a/ass2/q1/kruskals.py
+++ b/ass2/q1/kruskals.py
@@ -53,15 +53,18 @@ def sort_edges(edges):
def find(groups, vertex):
"""
Finds the root of the vertex's group according to a union-find data structure
+ Implements the find operation of union-by-rank
"""
- while groups[vertex] >= 0:
- vertex = groups[vertex]
- return vertex
+ if groups[vertex] < 0:
+ return vertex
+ else:
+ groups[vertex] = find(groups, groups[vertex])
+ return find(groups, groups[vertex])
def union(groups, u, v):
"""
- Unions two vertices together according to a union-find data structure
+ Unions two vertices together (by height/rank) according to a union-find data structure
"""
root_u, root_v = find(groups, u), find(groups, v)
height_u, height_v = -groups[root_u], -groups[root_v]