You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
10
-
Knowing one will help you in learning the other.
11
-
12
-
The idea with Prim's is to utilize a heap of vertices and an adjacent list.
13
-
The resulting MST cannot produce a cycle due to the method of selecting an edge of an unvisited vertex.
14
-
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 vertex as visited and add it's neighboring vertices' edges to the heap.
9
+
This question requires a minimum spanning tree (MST) algorithm.
10
+
I choose to use Prim's algorithm over Kruskal's algorithm because it is similar to Dijkstra's algorithm, both are greedy algorithms.
11
+
Knowing one will help you learn the other.
16
12
17
13
The pseudocode could be represented as such:
18
-
1. Select an arbitrary vertex to start with and add its neighboring vertices to the heap.
19
-
2. Mark that arbitrary vertex as visited.
20
-
3. While there are unvisited vertices:
21
-
- Select the edge with the min weight of an unvisited vertex from top of heap.
14
+
1. Select an arbitrary vertex to start with and add it to the heap.
15
+
2. While the heap isn't empty:
16
+
- Select the min edge/weight of an unvisited vertex from top of heap.
22
17
- Add the selected vertex to the visited vertices.
23
-
- Add selected vertex's neighboring edges of unvisited vertices into the heap.
18
+
- Take note of the cost or path taken.
19
+
- Add/Update selected vertex's neighboring edges/weights of unvisited vertices into the heap.
24
20
25
21
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
-
22
+
If there was a real adaptable priority queue, 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.
33
23
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).
34
24
25
+
This example is more of a lazy heap implementation, looking at the heapq documentation, they show an implementation of a lazy adaptable priority queue.
26
+
This will help us to get O((V + E)logV) run-time.
27
+
Without performing the lazy delete technique with a heaq, we would get O(V^2(V + E)logV) run-time.
28
+
That is because in a undirected dense graph, we would be adding duplicate entries of the same nodes into the heap.
29
+
There are a lot of incorrect implementations of Prim's Algorithm using heapq that do not perform lazy deletes out there, so be warned.
0 commit comments