Prim’s algorithm is a Greedy algorithm like Kruskal's algorithm. This algorithm always starts with a single node and moves through several adjacent nodes, in order to explore all of the connected edges along the way.
- The algorithm starts with an empty spanning tree.
- The idea is to maintain two sets of vertices. The first set contains the vertices already included in the MST, and the other set contains the vertices not yet included.
- At every step, it considers all the edges that connect the two sets and picks the minimum weight edge from these edges. After picking the edge, it moves the other endpoint of the edge to the set containing MST.
Simple Implementation for Adjacency Matrix Representation
Follow the given steps to utilize the Prim's Algorithm mentioned above for finding MST of a graph:
- Create a set mstSet that keeps track of vertices already included in MST.
- Assign a key value to all vertices in the input graph. Initialize all key values as INFINITE.
- Assign the key value as 0 for the first vertex so that it is picked first.
- While mstSet doesn't include all vertices
=> Pick a vertex u that is not there in mstSet and has a minimum key value.
=> Include u in the mstSet.
=> Update the key value of all adjacent vertices of u. To update the key values, iterate through all adjacent vertices. For every adjacent vertex v, if the weight of edge u-v is less than the previous key value of v, update the key value as the weight of u-v.
The idea of using key values is to pick the minimum weight edge from the cut. The key values are used only for vertices that are not yet included in MST, the key value for these vertices indicates the minimum weight edges connecting them to the set of vertices included in MST.
OutputEdge Weight
0 - 1 2
1 - 2 3
0 - 3 6
1 - 4 5
Time Complexity: O(V2), As, we are using adjacency matrix, if the input graph is represented using an adjacency list, then the time complexity of Prim's algorithm can be reduced to O((E+V) * logV) with the help of a binary heap.
Auxiliary Space: O(V)
Efficient Implementation using Priority Queue and Adjacency List
For adjacency list representation, we can achieve O((E+V)*log(V)) because we can find all adjacent of every vertex in O(V + E) time and we can get minimum using priority queue in O(Log V) time.
- We use a priority queue (min-heap) to always select the edge with the smallest weight.
- Push the first vertex and its weight into the queue.
- While the queue is not empty, extract the minimum-weight edge.
- If the vertex is unvisited, add its weight to a variable (res) and mark it as visited.
- Push all unvisited adjacent vertices of this vertex into the queue.
- After all vertices are processed, return the total weight stored in res.
Time Complexity: O((E+V)*log(V)) where V is the number of vertex and E is the number of edges
Auxiliary Space: O(E+V) where V is the number of vertex and E is the number of edges
How Does it Work?
The core is based on a fundamental property of MSTs called the cut property (If we partition the vertices into two groups (a cut), then the lightest edge that crosses that cut must be part of some MST of the graph. Since, we always consider cut vertices, a cycle is never formed.
Advantages and Disadvantages of Prim's algorithm
Advantages:
- Prim's algorithm is guaranteed to find the MST in a connected, weighted graph.
- It has a time complexity of O((E+V)*log(V)) using a binary heap or Fibonacci heap, where E is the number of edges and V is the number of vertices.
- It is a relatively simple algorithm to understand and implement compared to some other MST algorithms.
Disadvantages:
- Like Kruskal's algorithm, Prim's algorithm can be slow on dense graphs with many edges, as it requires iterating over all edges at least once.
- Prim's algorithm relies on a priority queue, which can take up extra memory and slow down the algorithm on very large graphs.
- The choice of starting node can affect the MST output, which may not be desirable in some applications.
Also Check: