Repository navigation
Expand file tree
/
Copy pathconnect_the_dots.py
More file actions
53 lines (46 loc) · 1.81 KB
/
Copy pathconnect_the_dots.py
File metadata and controls
53 lines (46 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from typing import List
class UnionFind:
def __init__(self, size):
self.parent = [i for i in range(size)]
self.size = [1] * size
def union(self, x, y) -> bool:
rep_x, rep_y = self.find(x), self.find(y)
if rep_x != rep_y:
if self.size[rep_x] > self.size[rep_y]:
self.parent[rep_y] = rep_x
self.size[rep_x] += self.size[rep_y]
else:
self.parent[rep_x] = rep_y
self.size[rep_y] += self.size[rep_x]
# Return True if both groups were merged.
return True
# Return False if the points belong to the same group.
return False
def find(self, x) -> int:
if x == self.parent[x]:
return x
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def connect_the_dots(points: List[List[int]]) -> int:
n = len(points)
# Create and populate a list of all possible edges.
edges = []
for i in range(n):
for j in range(i + 1, n):
# Manhattan distance.
cost = (abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1]))
edges.append((cost, i, j))
# Sort the edges by their cost in ascending order.
edges.sort()
uf = UnionFind(n)
total_cost = edges_added = 0
# Use Kruskal's algorithm to create the MST and identify its minimum cost.
for cost, p1, p2 in edges:
# If the points are not already connected (i.e. their representatives are
# not the same), connect them, and add the cost to the total cost.
if uf.union(p1, p2):
total_cost += cost
edges_added += 1
# If n - 1 edges have been added to the MST, the MST is complete.
if edges_added == n - 1:
return total_cost