|
| 1 | +# Prim's Algorithm |
| 2 | + |
| 3 | +## Background |
| 4 | + |
| 5 | +Prim's Algorithm is a greedy algorithm that finds the minimum spanning tree of a graph by starting from an |
| 6 | +arbitrary node (vertex) and adding the edge, with the minimum weight that connects the current tree to an unexplored |
| 7 | +node, and the unexplored node to the current tree, until all nodes are included in the tree. |
| 8 | + |
| 9 | +### Implementation Details |
| 10 | + |
| 11 | +A priority queue (binary heap) is utilised to keep track of the minimum weight edge that connects the current tree to an |
| 12 | +unexplored node. In an ideal scenario, the minimum weight edge to each node in the priority queue should be updated each |
| 13 | +time a lighter edge is found to maintain a single unique node in the priority queue. This means that a decrease key |
| 14 | +operation is required. However, we know that the decrease key operation of a binary heap implementation of a priority |
| 15 | +queue will take O(V) time, which will result in a larger time complexity for the entire algorithm compared to using only |
| 16 | +O(log V) operations for each edge. |
| 17 | + |
| 18 | +Hence, in our implementation, to avoid the use of a decrease key operation, we will simply insert duplicate nodes with |
| 19 | +their new minimum weight edge, which will take O(log E) = O(log V) given an upper bound of E = V^2, into the queue, |
| 20 | +while leaving the old node in the queue. Additionally, we will track if a node has already been added into the MST to |
| 21 | +avoid adding duplicate nodes. |
| 22 | + |
| 23 | +Note that a priority queue is an abstract data type that can be implemented using different data structures. In this |
| 24 | +implementation, the default Java `PriorityQueue` is used, which is a binary heap. By implementing the priority queue |
| 25 | +with an AVL tree, a decrease key operation that has a time complexity of O(log V) can also be achieved. |
| 26 | + |
| 27 | +## Complexity Analysis |
| 28 | + |
| 29 | +**Time Complexity:** |
| 30 | +- O(V^2 log V) for the basic version with an adjacency matrix, where V is the number of vertices. |
| 31 | +- O(E log V) with a binary heap and adjacency list, where V and E is the number of vertices and edges |
| 32 | +respectively. |
| 33 | + |
| 34 | +**Space Complexity:** |
| 35 | +- O(V^2) for the adjacency matrix representation. |
| 36 | +- O(V + E) for the adjacency list representation. |
| 37 | + |
| 38 | +## Notes |
| 39 | + |
| 40 | +### Difference between Prim's Algorithm and Dijkstra's Algorithm |
| 41 | + |
| 42 | +| | Prim's Algorithm | Dijkstra's Algorithm | |
| 43 | +|-------------------------------------|---------------------------------------------------------------------------------|----------------------------------------------------------| |
| 44 | +| Purpose | Finds MST - minimum sum of edge weights that includes all vertices in the graph | Finds shortest path from a single source to all vertices | |
| 45 | +| Property Compared in Priority Queue | Minimum weight of incoming edge to a vertex | Minimum distance from source vertex to current vertex | |
| 46 | + |
0 commit comments