Tutorialspoint
Problem
Solution
Submissions

Prim's Algorithm

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 15

Write a C++ program to implement Prim's algorithm to find the minimum spanning tree (MST) of a connected, undirected graph with weighted edges. The program should output the edges of the MST along with their weights.

Example 1
  • Input: Number of vertices: 4 Number of edges: 5 Edges and weights: 0 1 10 0 2 6 0 3 5 1 3 15 2 3 4
  • Output: Edges in the MST: 0 - 3 : 5 3 - 2 : 4 0 - 1 : 10
  • Explanation:
    • Step 1: Initialize MST as empty and a priority queue to store vertices.
    • Step 2: Start with vertex 0 (can start with any vertex).
    • Step 3: Add all edges from vertex 0 to the priority queue (edge weights: 10, 6, 5).
    • Step 4: Choose the smallest edge (0-3) with weight 5 and add it to MST.
    • Step 5: Add all edges from vertex 3 to the priority queue (edge weights: 15, 4).
    • Step 6: Choose the smallest edge (3-2) with weight 4 and add it to MST.
    • Step 7: Choose the smallest remaining edge (0-1) with weight 10 and add it to MST.
    • Step 8: Return the MST with total weight 5 + 4 + 10 = 19.
Example 2
  • Input: Number of vertices: 5 Number of edges: 7 Edges and weights: 0 1 2 0 3 6 1 2 3 1 3 8 1 4 5 2 4 7 3 4 9
  • Output: Edges in the MST: 0 - 1 : 2 1 - 2 : 3 1 - 4 : 5 0 - 3 : 6
  • Explanation:
    • Step 1: Initialize MST as empty and a priority queue to store vertices.
    • Step 2: Start with vertex 0 (can start with any vertex).
    • Step 3: Add all edges from vertex 0 to the priority queue (edge weights: 2, 6).
    • Step 4: Choose the smallest edge (0-1) with weight 2 and add it to MST.
    • Step 5: Add all edges from vertex 1 to the priority queue (edge weights: 3, 8, 5).
    • Step 6: Choose the smallest edge (1-2) with weight 3 and add it to MST.
    • Step 7: Choose the smallest edge (1-4) with weight 5 and add it to MST.
    • Step 8: Choose the smallest edge (0-3) with weight 6 and add it to MST.
    • Step 9: Return the MST with total weight 2 + 3 + 5 + 6 = 16.
Constraints
  • 1 ≤ Number of vertices ≤ 100
  • 1 ≤ Number of edges ≤ 1000
  • Time Complexity: O(V^2) or O(E log V)
  • Space Complexity: O(V + E)
Priority QueueFunctions / MethodsTech MahindraSwiggy
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use a priority queue (min-heap) to select the edge with the minimum weight.
  • Maintain a set of vertices included in the MST.
  • Start with any vertex and add the minimum weight edge connecting the MST set to the remaining vertices.
  • Repeat until all vertices are included in the MST.

Steps to solve by this approach:

 Step 1: Initialize key values (minimum edge weight) for all vertices to infinity.

 Step 2: Set key value of first vertex to 0 and add all vertices to a priority queue.
 Step 3: While the priority queue is not empty, extract vertex with minimum key value.
 Step 4: Mark the extracted vertex as processed (added to MST).
 Step 5: For all adjacent unprocessed vertices, if edge weight is less than current key, update key and parent.
 Step 6: After all vertices are processed, use parent array to construct MST.

Submitted Code :