Below are the steps for finding MST using Kruskal’s algorithm
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.
We have discussed one implementation of Kruskal’s algorithm in previous post. In this post, a simpler implementation for adjacency matrix is discussed.
Edge 0:(0, 1) cost:2 Edge 1:(1, 2) cost:3 Edge 2:(1, 4) cost:5 Edge 3:(0, 3) cost:6 Minimum cost= 16
Note that the above solution is not efficient. The idea is to provide a simple implementation for adjacency matrix representations. Please see below for efficient implementations.
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Kruskal’s Minimum Spanning Tree using STL in C++
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
- Convert Adjacency Matrix to Adjacency List representation of Graph
- Comparison between Adjacency List and Adjacency Matrix representation of Graph
- Convert Adjacency List to Adjacency Matrix representation of a Graph
- Implementation of DFS using adjacency matrix
- Implementation of BFS using adjacency matrix
- Bellman Ford Algorithm (Simple Implementation)
- Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
- Add and Remove vertex in Adjacency Matrix representation of Graph
- C program to implement Adjacency Matrix of a given Graph
- Add and Remove Edge in Adjacency Matrix representation of a Graph
- Strassen’s Matrix Multiplication Algorithm | Implementation
- Find if a degree sequence can form a simple graph | Havel-Hakimi Algorithm
- Prim’s MST for Adjacency List Representation | Greedy Algo-6
- DFS for a n-ary tree (acyclic graph) represented as adjacency list
- Add and Remove vertex in Adjacency List representation of Graph
- Add and Remove Edge in Adjacency List representation of a Graph
- Karger's algorithm for Minimum Cut | Set 1 (Introduction and Implementation)
- Hopcroft–Karp Algorithm for Maximum Matching | Set 2 (Implementation)
- Push Relabel Algorithm | Set 2 (Implementation)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.