Skip to content

Commit 6d3346d

Browse files
committed
Add problem 1584 - Min Cost To Connect All Points
1 parent a29db4b commit 6d3346d

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import heapq
2+
from typing import List
3+
4+
5+
class MinCostToConnectAllPoints:
6+
@staticmethod
7+
def minCostConnectPoints(points: List[List[int]]) -> int:
8+
# Special case
9+
if not points:
10+
return 0
11+
# Total number of points
12+
n = len(points)
13+
# List to store visited vertices
14+
visited = [False] * n
15+
# We only want to visit n - 1 vertices as we don't want to have
16+
# cycle in the graph
17+
edges_left_to_visit = n - 1
18+
# Total cost of connecting all points
19+
cost = 0
20+
# Since we always want nearest vertex to be chosen first, we will
21+
# choose min heap for that purpose
22+
edges = []
23+
heapq.heapify(edges)
24+
# We will start from vertex 0 to reach to all other points from here
25+
for i in range(1, n):
26+
distance = abs(points[i][0] - points[0][0]) + abs(points[i][1] - points[0][1])
27+
heapq.heappush(edges, Edge(0, i, distance))
28+
# Mark 0 as visited
29+
visited[0] = True
30+
# Process edges until edgesLeftToVisit becomes 0
31+
while edges and edges_left_to_visit > 0:
32+
# Get the current edge
33+
edge = heapq.heappop(edges)
34+
v, weight = edge.v, edge.weight
35+
if not visited[v]:
36+
# Add the cost and mark as visited
37+
cost += weight
38+
visited[v] = True
39+
# Now traverse all possible points from v
40+
for i in range(n):
41+
if not visited[i]:
42+
distance = abs(points[v][0] - points[i][0]) + abs(points[v][1] - points[i][1])
43+
heapq.heappush(edges, Edge(v, i, distance))
44+
edges_left_to_visit -= 1
45+
return cost
46+
47+
48+
class Edge:
49+
def __init__(self, u, v, weight):
50+
self.u = u
51+
self.v = v
52+
self.weight = weight
53+
54+
# For comparisons if needed, otherwise the heap only uses the weight
55+
def __lt__(self, other):
56+
return self.weight < other.weight
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import unittest
2+
3+
from problems.graph.min_cost_to_connect_all_points import MinCostToConnectAllPoints
4+
5+
6+
class TestMinCostToConnectAllPoints(unittest.TestCase):
7+
8+
def setUp(self):
9+
self.min_cost = MinCostToConnectAllPoints()
10+
11+
def test_min_cost_connect_points_empty(self):
12+
self.assertEqual(self.min_cost.minCostConnectPoints([]), 0)
13+
14+
def test_min_cost_connect_points_single_point(self):
15+
self.assertEqual(self.min_cost.minCostConnectPoints([[0, 0]]), 0)
16+
17+
def test_min_cost_connect_points_two_points(self):
18+
self.assertEqual(self.min_cost.minCostConnectPoints([[0, 0], [1, 1]]), 2)
19+
20+
def test_min_cost_connect_points_multiple_points(self):
21+
self.assertEqual(self.min_cost.minCostConnectPoints([[0, 0], [2, 2], [3, 10], [5, 2], [7, 0]]), 20)
22+
23+
def test_min_cost_connect_points_negative_points(self):
24+
self.assertEqual(self.min_cost.minCostConnectPoints([[-1, -2], [1, 3], [4, 5]]), 12)
25+
26+
27+
if __name__ == '__main__':
28+
unittest.main()

0 commit comments

Comments
 (0)