Tutorialspoint
Problem
Solution
Submissions

Kruskal's Algorithm

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

Write a C++ program to implement Kruskal'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: 2 - 3 : 4 0 - 3 : 5 0 - 1 : 10
  • Explanation:
    • Step 1: Sort all edges in non-decreasing order of their weight.
    • Step 2: Initialize an empty MST and a disjoint set data structure.
    • Step 3: Pick edges in increasing order of weight:
      • Edge (2-3) with weight 4: Include in MST
      • Edge (0-3) with weight 5: Include in MST
      • Edge (0-2) with weight 6: Skip (would create a cycle)
      • Edge (0-1) with weight 10: Include in MST
      • Edge (1-3) with weight 15: Skip (would create a cycle)
    • Step 4: Return the edges included in the MST with total weight 4 + 5 + 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: Sort all edges in non-decreasing order of their weight.
    • Step 2: Initialize an empty MST and a disjoint set data structure.
    • Step 3: Pick edges in increasing order of weight:
      • Edge (0-1) with weight 2: Include in MST
      • Edge (1-2) with weight 3: Include in MST
      • Edge (1-4) with weight 5: Include in MST
      • Edge (0-3) with weight 6: Include in MST
      • Edge (2-4) with weight 7: Skip (would create a cycle)
      • Edge (1-3) with weight 8: Skip (would create a cycle)
      • Edge (3-4) with weight 9: Skip (would create a cycle)
    • Step 4: Return the edges included in the MST with total weight 2 + 3 + 5 + 6 = 16.
Constraints
  • 1 ≤ Number of vertices ≤ 100
  • 1 ≤ Number of edges ≤ 1000
  • Time Complexity: O(E log E) or O(E log V)
  • Space Complexity: O(V + E)
SetTreePwCSamsung
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 disjoint-set (Union-Find) data structure to manage the vertices and detect cycles.
  • Sort all the edges in non-decreasing order of their weight.
  • Iterate through the sorted edges and add them to the MST if they do not form a cycle.
  • Continue until there are (V-1) edges in the MST.

Steps to solve by this approach:

 Step 1: Sort all edges in non-decreasing order of their weight.

 Step 2: Initialize result set as empty and create disjoint sets for each vertex.
 Step 3: For each edge in sorted order, check if including it creates a cycle.
 Step 4: Use union-find algorithm to detect if both endpoints are in same set (would create cycle).
 Step 5: If no cycle is formed, add the edge to result and merge sets of the two vertices.
 Step 6: Repeat until we have V-1 edges in the result (where V is number of vertices).

Submitted Code :