Skip to content

Commit f45aa86

Browse files
committed
Fixed 1135
1 parent 982f484 commit f45aa86

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# 1135. Connecting Cities With Minimum Cost
22

33
## Prim's Algorithm Solution
4-
- Run-time: O((V + E) log(V))
5-
- Space: O(E)
6-
- E - Number of Edges
4+
- Run-time: O(N^2)
5+
- Space: O(N)
6+
- N = Number of cities
77

8-
This is question requires a minimum span tree(MST) algothrim.
9-
I choose to use Prim's algothrim because it is similar to Dijkstra's algorithm over Kruskal's algorithm.
8+
This is question requires a minimum spanning tree (MST) algorithm.
9+
I choose to use Prim's algorithm because it is similar to Dijkstra's algorithm over Kruskal's algorithm, both are greedy algorithms.
1010
Knowing one will help you in learning the other.
1111

1212
The idea with Prim's is to utilize a heap of vertices and an adjacent list.
1313
The resulting MST cannot produce a cycle due to the method of selecting an edge of an unvisited vertex.
1414
To select an edge, we use the heap sorted by the cost or weight of the edge.
15-
Each time we pop from the heap, we mark that vetrex as visited and add it's neighboring vertices' edges to the heap.
15+
Each time we pop from the heap, we mark that vertex as visited and add it's neighboring vertices' edges to the heap.
1616

1717
The pseudocode could be represented as such:
1818
1. Select an arbitrary vertex to start with and add its neighboring vertices to the heap.
@@ -22,48 +22,44 @@ The pseudocode could be represented as such:
2222
- Add the selected vertex to the visited vertices.
2323
- Add selected vertex's neighboring edges of unvisited vertices into the heap.
2424

25-
Since we are sorting based on vertices, the sorting will take log(V) run-time.
26-
However, we have to sort this based on number of vertices or edges because we can be given a graph of islands or nodes that have edges to all nodes.
27-
That is why its O((V+E) log(V)) run-time.
25+
As of the time of writing this, Python 3.7 doesn't have an adaptable priority queue implementation.
26+
So this is why this version of Prim's runs at O(N^2) time.
27+
This example is more of a lazy heap implementation.
28+
If we were to use an adaptable priority queue, we would see O(MlogN) run-time.
29+
This is because we will only have at most N cities in the heap, no duplicates.
30+
There would be a feature to update a specific city's cost in the heap and bubble up and down the heap to maintain the heap property.
31+
With an adaptable priority queue, there would be no need to use a visited set.
32+
33+
To read more about adaptable priority queues, check out [Data Structures and Algorithms in Python 1st Edition](https://www.amazon.com/Structures-Algorithms-Python-Michael-Goodrich/dp/1118290275).
2834

2935
```
3036
from collections import defaultdict
3137
3238
class Solution:
3339
def minimumCost(self, N: int, connections: List[List[int]]) -> int:
3440
35-
def create_adj_list(connections):
41+
def create_adj_list():
3642
adj_list = defaultdict(list)
43+
for n in range(1, N + 1): # incase there are cities with no connections
44+
adj_list[n]
3745
for city1, city2, cost in connections:
38-
adj_list[city1].append(Vertex(city2, cost))
39-
adj_list[city2].append(Vertex(city1, cost))
46+
adj_list[city1].append((city2, cost))
47+
adj_list[city2].append((city1, cost))
4048
return adj_list
4149
42-
adj_list = create_adj_list(connections)
43-
visited = set([N])
44-
min_heap = list(adj_list[N] if N in adj_list else [])
45-
heapq.heapify(min_heap)
50+
adj_list = create_adj_list()
51+
min_heap = list([(0, 1)]) # start at city #1
52+
visited = set()
4653
min_cost = 0
4754
while min_heap and len(visited) != N:
48-
vertex = heapq.heappop(min_heap)
49-
if vertex.to in visited:
55+
cost, city = heapq.heappop(min_heap)
56+
if city in visited: # skip this city
5057
continue
51-
visited.add(vertex.to)
52-
min_cost += vertex.cost
53-
for vertex in adj_list[vertex.to]:
54-
if vertex.to not in visited:
55-
heapq.heappush(min_heap, vertex)
58+
visited.add(city)
59+
min_cost += cost
60+
for next_city, next_cost in adj_list[city]:
61+
if next_city in visited:
62+
continue
63+
heapq.heappush(min_heap, (next_cost, next_city))
5664
return min_cost if len(visited) == N else -1
57-
58-
class Vertex(object):
59-
60-
def __init__(self, to, cost):
61-
self.to = to
62-
self.cost = cost
63-
64-
def __lt__(self, other):
65-
return self.cost < other.cost
66-
67-
def __repr__(self):
68-
return 'To: {}, Cost: {}'.format(self.to, self.cost)
6965
```

0 commit comments

Comments
 (0)