Kruskal’s Algorithm (Simple Implementation for Adjacency Matrix)
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.
C++
// Simple C++ implementation for Kruskal's // algorithm #include <bits/stdc++.h> using namespace std; #define V 5 int parent[V]; // Find set of vertex i int find(int i) { while (parent[i] != i) i = parent[i]; return i; } // Does union of i and j. It returns // false if i and j are already in same // set. void union1(int i, int j) { int a = find(i); int b = find(j); parent[a] = b; } // Finds MST using Kruskal's algorithm void kruskalMST(int cost[][V]) { int mincost = 0; // Cost of min MST. // Initialize sets of disjoint sets. for (int i = 0; i < V; i++) parent[i] = i; // Include minimum weight edges one by one int edge_count = 0; while (edge_count < V - 1) { int min = INT_MAX, a = -1, b = -1; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (find(i) != find(j) && cost[i][j] < min) { min = cost[i][j]; a = i; b = j; } } } union1(a, b); printf("Edge %d:(%d, %d) cost:%d \n", edge_count++, a, b, min); mincost += min; } printf("\n Minimum cost= %d \n", mincost); } // driver program to test above function int main() { /* Let us create the following graph 2 3 (0)--(1)--(2) | / \ | 6| 8/ \5 |7 | / \ | (3)-------(4) 9 */ int cost[][V] = { { INT_MAX, 2, INT_MAX, 6, INT_MAX }, { 2, INT_MAX, 3, 8, 5 }, { INT_MAX, 3, INT_MAX, INT_MAX, 7 }, { 6, 8, INT_MAX, INT_MAX, 9 }, { INT_MAX, 5, 7, 9, INT_MAX }, }; // Print the solution kruskalMST(cost); return 0; } |
Java
// Simple Java implementation for Kruskal's // algorithm import java.util.*; class GFG { static int V = 5; static int[] parent = new int[V]; static int INF = Integer.MAX_VALUE; // Find set of vertex i static int find(int i) { while (parent[i] != i) i = parent[i]; return i; } // Does union of i and j. It returns // false if i and j are already in same // set. static void union1(int i, int j) { int a = find(i); int b = find(j); parent[a] = b; } // Finds MST using Kruskal's algorithm static void kruskalMST(int cost[][]) { int mincost = 0; // Cost of min MST. // Initialize sets of disjoint sets. for (int i = 0; i < V; i++) parent[i] = i; // Include minimum weight edges one by one int edge_count = 0; while (edge_count < V - 1) { int min = INF, a = -1, b = -1; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (find(i) != find(j) && cost[i][j] < min) { min = cost[i][j]; a = i; b = j; } } } union1(a, b); System.out.printf("Edge %d:(%d, %d) cost:%d \n", edge_count++, a, b, min); mincost += min; } System.out.printf("\n Minimum cost= %d \n", mincost); } // Driver code public static void main(String[] args) { /* Let us create the following graph 2 3 (0)--(1)--(2) | / \ | 6| 8/ \5 |7 | / \ | (3)-------(4) 9 */ int cost[][] = { { INF, 2, INF, 6, INF }, { 2, INF, 3, 8, 5 }, { INF, 3, INF, INF, 7 }, { 6, 8, INF, INF, 9 }, { INF, 5, 7, 9, INF }, }; // Print the solution kruskalMST(cost); } } // This code contributed by Rajput-Ji |
C#
// Simple C# implementation for Kruskal's // algorithm using System; class GFG { static int V = 5; static int[] parent = new int[V]; static int INF = int.MaxValue; // Find set of vertex i static int find(int i) { while (parent[i] != i) i = parent[i]; return i; } // Does union of i and j. It returns // false if i and j are already in same // set. static void union1(int i, int j) { int a = find(i); int b = find(j); parent[a] = b; } // Finds MST using Kruskal's algorithm static void kruskalMST(int [,]cost) { int mincost = 0; // Cost of min MST. // Initialize sets of disjoint sets. for (int i = 0; i < V; i++) parent[i] = i; // Include minimum weight edges one by one int edge_count = 0; while (edge_count < V - 1) { int min = INF, a = -1, b = -1; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (find(i) != find(j) && cost[i, j] < min) { min = cost[i, j]; a = i; b = j; } } } union1(a, b); Console.Write("Edge {0}:({1}, {2}) cost:{3} \n", edge_count++, a, b, min); mincost += min; } Console.Write("\n Minimum cost= {0} \n", mincost); } // Driver code public static void Main(String[] args) { /* Let us create the following graph 2 3 (0)--(1)--(2) | / \ | 6| 8/ \5 |7 | / \ | (3)-------(4) 9 */ int [,]cost = { { INF, 2, INF, 6, INF }, { 2, INF, 3, 8, 5 }, { INF, 3, INF, INF, 7 }, { 6, 8, INF, INF, 9 }, { INF, 5, 7, 9, INF }, }; // Print the solution kruskalMST(cost); } } /* This code contributed by PrinciRaj1992 */ |
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++
Recommended Posts:
- Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
- Implementation of BFS using adjacency matrix
- Implementation of DFS using adjacency matrix
- Bellman Ford Algorithm (Simple Implementation)
- Strassen’s Matrix Multiplication Algorithm | Implementation
- Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
- Push Relabel Algorithm | Set 2 (Implementation)
- Find if a degree sequence can form a simple graph | Havel-Hakimi Algorithm
- Karger's algorithm for Minimum Cut | Set 1 (Introduction and Implementation)
- Hopcroft–Karp Algorithm for Maximum Matching | Set 2 (Implementation)
- Johnson’s algorithm for All-pairs shortest paths | Implementation
- Freivald’s Algorithm to check if a matrix is product of two
- Prim’s MST for Adjacency List Representation | Greedy Algo-6
- DFS for a n-ary tree (acyclic graph) represented as adjacency list
- C/C++ program to make a simple calculator
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



