Welcome to this comprehensive guide on Kruskal's Algorithm in C programming! We'll dive deep into this graph theory-based algorithm that helps you find the minimum spanning tree (MST) of a graph. Let's get started!
Kruskal's Algorithm is a popular technique used for finding the minimum spanning tree (MST) of a graph. It works well for both unweighted and weighted graphs. The algorithm sorts the edges of the graph in non-decreasing order of their weights and then builds the MST progressively by including the smallest edge that does not create a cycle with the already chosen edges.
Before we dive into the code, make sure you're familiar with the following:
Here's a step-by-step approach to implement Kruskal's Algorithm in C:
Initialize: Initialize an array parent[] to keep track of the set each vertex belongs to and a boolean array visited[] to mark the visited vertices.
Sort Edges: Sort the edges in non-decreasing order of their weights using a function like qsort().
Build MST: Start from the smallest edge and repeatedly add the smallest edge that does not create a cycle with the already chosen edges.
Merge Sets: If the two vertices of an edge belong to different sets, merge the sets using a Union function.
Here's a sample implementation of Kruskal's Algorithm in C:
#include <stdio.h>
#include <stdlib.h>
#define V 9
int minKey(int key[], int mstSet[]) {
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (mstSet[v] == 0 && key[v] < min)
min = key[v], min_index = v;
return min_index;
}
void printMST(int parent[], int edges[][2]) {
printf("Edge \tWeight\n");
for (int i = 1; i < V; ++i)
printf("%d - %d\t%d\n", parent[edges[i][0]], parent[edges[i][1]], edges[i][2]);
}
void initialize(int parent[], int mstSet[]) {
for (int i = 0; i < V; i++) {
parent[i] = i;
mstSet[i] = 1;
}
}
int find(int parent[], int x) {
if (parent[x] == x)
return x;
return parent[x] = find(parent, parent[x]);
}
void unionSet(int parent[], int rank[], int x, int y) {
int xRoot = find(parent, x);
int yRoot = find(parent, y);
if (xRoot != yRoot) {
if (rank[xRoot] < rank[yRoot]) {
parent[xRoot] = yRoot;
} else if (rank[xRoot] > rank[yRoot]) {
parent[yRoot] = xRoot;
} else {
parent[yRoot] = xRoot;
rank[xRoot]++;
}
}
}
void KruskalMST(int edges[][3]) {
int parent[V];
int mstSet[V];
int rank[V] = {0};
int minKey, edge_index, edges_count = 0;
initialize(parent, mstSet);
qsort(edges, V * (V - 1) / 2, sizeof(edges[0]), compare);
for (int i = 0; edges_count < V - 1; i++) {
int x = edges[i][0];
int y = edges[i][1];
int w = edges[i][2];
if (find(parent, x) != find(parent, y)) {
edges_count++;
unionSet(parent, rank, x, y);
printf("Edge %d and %d are added to the MST\n", x, y);
printf("Edge weight: %d\n", w);
}
}
printMST(parent, edges);
}
int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int main() {
int edges[][3] = { { 0, 1, 4 }, { 0, 7, 8 }, { 1, 2, 11 }, { 1, 7, 14 },
{ 2, 3, 7 }, { 2, 8, 2 }, { 2, 5, 4 }, { 3, 4, 9 },
{ 3, 5, 14 }, { 4, 5, 10 }, { 4, 8, 2 }, { 5, 6, 6 },
{ 6, 7, 1 }, { 6, 8, 7 } };
KruskalMST(edges);
return 0;
}š” Pro Tip: Make sure you understand the working of the qsort() function and the custom compare() function before diving into the code.
What is the main advantage of using Kruskal's Algorithm over Prim's Algorithm?
Keep exploring and happy learning! š