Kruskal’s algorithm is a minimum spanning tree algorithm that takes a graph as input and finds the subset of the edges of that graph which
- form a tree that includes every vertex
- has the minimum sum of weights among all the trees that can be formed from the graph
How Kruskal’s algorithm works
Kruskal’s algorithm is a type of greedy algorithm that finds the local optimum in the hopes of finding a global optimum.
We start from the edges with the lowest weight and keep adding edges until we reach our goal.
The steps for implementing Kruskal’s algorithm are as follows:
- Sort all the edges from low weight to high
- Take the edge with the lowest weight and add it to the spanning tree. If adding the edge created a cycle, then reject this edge.
- Keep adding edges until we reach all vertices.
Implementation of Kruskal’s Algorithm:
#include <iostream>
#define I 32767 // Infinity
#define V 7 // # of vertices in Graph
#define E 9 // # of edges in Graph
using namespace std;
void PrintMCST(int T[][V-1], int A[][E]){
cout << "\nMinimum Cost Spanning Tree Edges\n" << endl;
for (int i {0}; i<V-1; i++){
cout << "[" << T[0][i] << "]-----[" << T[1][i] << "]" << endl;
}
cout << endl;
}
// Set operations: Union and Find
void Union(int u, int v, int s[]){
if (s[u] < s[v]){
s[u] += s[v];
s[v] = u;
} else {
s[v] += s[u];
s[u] = v;
}
}
int Find(int u, int s[]){
int x = u;
int v = 0;
while (s[x] > 0){
x = s[x];
}
while (u != x){
v = s[u];
s[u] = x;
u = v;
}
return x;
}
void KruskalsMCST(int A[3][9]){
int T[2][V-1]; // Solution array
int track[E] {0}; // Track edges that are included in solution
int set[V+1] = {-1, -1, -1, -1, -1, -1, -1, -1}; // Array for finding cycle
int i {0};
while (i < V-1){
int min = I;
int u {0};
int v {0};
int k {0};
// Find a minimum cost edge
for (int j {0}; j<E; j++){
if (track[j] == 0 && A[2][j] < min){
min = A[2][j];
u = A[0][j];
v = A[1][j];
k = j;
}
}
// Check if the selected min cost edge (u, v) forming a cycle or not
if (Find(u, set) != Find(v, set)){
T[0][i] = u;
T[1][i] = v;
// Perform union
Union(Find(u, set), Find(v, set), set);
i++;
}
track[k] = 1;
}
PrintMCST(T, A);
}
int main() {
int edges[3][9] = {{ 1, 1, 2, 2, 3, 4, 4, 5, 5},
{ 2, 6, 3, 7, 4, 5, 7, 6, 7},
{25, 5, 12, 10, 8, 16, 14, 20, 18}};
KruskalsMCST(edges);
return 0;
}
Leave a comment