(Translated by https://www.hiragana.jp/)
Prim’s Algorithm for Minimum Spanning Tree (MST) - GeeksforGeeks
Open In App

Prim’s Algorithm for Minimum Spanning Tree (MST)

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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. 

A group of edges that connects two sets of vertices in a graph is called Articulation Points (or Cut Vertices) in graph theory. So, at every step of Prim’s algorithm, find a cut, pick the minimum weight edge from the cut, and include this vertex in MST Set (the set that contains already included vertices).

Working of the Prim's Algorithm

Step 1: Determine an arbitrary vertex as the starting vertex of the MST. We pick 0 in the below diagram.
Step 2: Follow steps 3 to 5 till there are vertices that are not included in the MST (known as fringe vertex).
Step 3: Find edges connecting any tree vertex with the fringe vertices.
Step 4: Find the minimum among these edges.
Step 5: Add the chosen edge to the MST. Since we consider only the edges that connect fringe vertices with the rest, we never get a cycle.
Step 6: Return the MST and exit


How to implement Prim's Algorithm?

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.

Below is the implementation of the approach:

C++
C Java Python C# JavaScript

Output
Edge 	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)

Optimized Implementation using Adjacency List Representation (of Graph) and Priority Queue

  1. We transform the adjacency matrix into adjacency list using ArrayList<ArrayList<Integer>>. in Java, list of list in Python
    and array of vectors in C++.
  2. Then we create a Pair class to store the vertex and its weight .
  3. We sort the list on the basis of lowest weight.
  4. We create priority queue and push the first vertex and its weight in the queue
  5. Then we just traverse through its edges and store the least weight in a variable called ans.
  6. At last after all the vertex we return the ans.
C++
Java Python C# JavaScript

Output
4

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

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.

Problems based on Minimum Spanning Tree


Prim's Algorithm/Minimum Spanning Tree
Visit Course explore course icon
Video Thumbnail

Prim's Algorithm/Minimum Spanning Tree

Video Thumbnail

Implementation of Prims Algorithm

Similar Reads