Kruskal's algorithm in C
Kruskal's algorithm is a widely used algorithm for finding the minimum spanning tree of a connected, weighted graph. Here is an example implementation of Kruskal's algorithm in C, along with explanations for each step:
#include <stdio.h>
#include <stdlib.h>
// Structure to represent an edge in the graph
struct Edge {
int src, dest, weight;
};
// Structure to represent a subset for union-find operation
struct Subset {
int parent;
int rank;
};
// Function to create a graph with V vertices and E edges
struct Graph* createGraph(int V, int E) {
struct Graph graph = (struct Graph) malloc(sizeof(struct Graph));
graph->V = V;
graph->E = E;
graph->edge = (struct Edge) malloc(graph->E sizeof(struct Edge));
return graph;
}
// Function to find the parent of a node using path compression technique
int find(struct Subset subsets[], int i) {
if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}
// Function to perform union of two subsets using union by rank
void Union(struct Subset subsets[], int x, int y) {
int xroot = find(subsets, x);
int yroot = find(subsets, y);
if (subsets[xroot].rank < subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
// Function to implement Kruskal's algorithm
void KruskalMST(struct Graph* graph) {
int V = graph->V;
struct Edge result[V];
int e = 0;
int i = 0;
// Sort all the edges in non-decreasing order of their weight
qsort(graph->edge, graph->E, sizeof(graph->edge[0]), compare);
// Create V subsets with single elements
struct Subset subsets = (struct Subset) malloc(V * sizeof(struct Subset));
for (int v = 0; v < V; ++v) {
subsets[v].parent = v;
subsets[v].rank = 0;
}
// Process V-1 edges
while (e < V - 1 && i < graph->E) {
// Pick the smallest edge and increment the index for next iteration
struct Edge next_edge = graph->edge[i++];
int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);
// If including this edge doesn't form a cycle, add it to the result and increment the index
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
}
}
// Print the edges of the minimum spanning tree
printf("Edges of the minimum spanning tree:\n");
for (i = 0; i < e; ++i)
printf("%d -- %d Weight: %d\n", result[i].src, result[i].dest, result[i].weight);
}
// Driver program to test above functions
int main() {
int V = 4; // Number of vertices in the graph
int E = 5; // Number of edges in the graph
struct Graph* graph = createGraph(V, E);
// Add edge 0-1
graph->edge[0].src = 0;
graph->edge[0].dest = 1;
graph->edge[0].weight = 10;
// Add edge 0-2
graph->edge[1].src = 0;
graph->edge[1].dest = 2;
graph->edge[1].weight = 6;
// Add edge 0-3
graph->edge[2].src = 0;
graph->edge[2].dest = 3;
graph->edge[2].weight = 5;
// Add edge 1-3
graph->edge[3].src = 1;
graph->edge[3].dest = 3;
graph->edge[3].weight = 15;
// Add edge 2-3
graph->edge[4].src = 2;
graph->edge[4].dest = 3;
graph->edge[4].weight = 4;
KruskalMST(graph);
return 0;
}
Explanation of Kruskal's Algorithm:
The algorithm starts by sorting all the edges of the graph in non-decreasing order of their weights. This can be done using any sorting algorithm, such as the
qsort
function in C.Next, the algorithm creates a disjoint set data structure to keep track of the subsets of the vertices. Each subset initially contains a single vertex.
The algorithm then processes the edges in the sorted order. For each edge, it checks if including that edge in the minimum spanning tree would create a cycle. This check is done by finding the parent of the source and destination vertices using the
find
function.If including the edge does not create a cycle, it is added to the minimum spanning tree result, and the subsets of the source and destination vertices are merged using the
Union
function.The algorithm continues this process until
V-1
edges have been added to the result, whereV
is the number of vertices in the graph. This ensures that the resulting tree is a spanning tree of the graph.Finally, the algorithm prints the edges of the minimum spanning tree.
The main function in the provided code is a driver program that creates a sample graph and tests the KruskalMST
function by calling it with the graph as an argument.