0% found this document useful (0 votes)
5 views

L1

The document provides a C/C++ program that implements Kruskal's algorithm to find the Minimum Cost Spanning Tree (MST) of a connected undirected graph. It includes structures for edges and a Disjoint Set Union (DSU) for managing connected components, along with functions for initializing the DSU, finding components, and uniting them. The program reads the number of vertices and edges, processes the edges, and outputs the edges in the MST along with the total cost.

Uploaded by

ananya.r.amcec
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

L1

The document provides a C/C++ program that implements Kruskal's algorithm to find the Minimum Cost Spanning Tree (MST) of a connected undirected graph. It includes structures for edges and a Disjoint Set Union (DSU) for managing connected components, along with functions for initializing the DSU, finding components, and uniting them. The program reads the number of vertices and edges, processes the edges, and outputs the edges in the MST along with the total cost.

Uploaded by

ananya.r.amcec
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given connected undirected graph using

Kruskal's algorithm.

#include <stdio.h>
#include <stdlib.h>
// Structure to represent an edge
struct Edge {
int src, dest, weight;
};
// Comparator function for sorting edges by weight
int compareEdges(const void* a, const void* b) {
return ((struct Edge*)a)->weight - ((struct Edge*)b)->weight;
}
// Disjoint Set Union (DSU) with path compression and union by rank
struct DSU {
int* parent;
int* rank;
};
void initializeDSU(struct DSU* dsu, int n) {
dsu->parent = (int*)malloc(n * sizeof(int));
dsu->rank = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
dsu->parent[i] = i;
dsu->rank[i] = 0;
}
}
int find(struct DSU* dsu, int v) {
if (v == dsu->parent[v])
return v;
return dsu->parent[v] = find(dsu, dsu->parent[v]); // Path compression
}
void unite(struct DSU* dsu, int a, int b) {
a = find(dsu, a);
b = find(dsu, b);
if (a != b) {
if (dsu->rank[a] < dsu->rank[b]) {
int temp = a;
a = b;
b = temp;
}
dsu->parent[b] = a;
if (dsu->rank[a] == dsu->rank[b])
dsu->rank[a]++;
}
}
// Kruskal's algorithm to find MST
void kruskalMST(int V, struct Edge edges[], int E) {
struct Edge* result = (struct Edge*)malloc((V - 1) * sizeof(struct Edge));
qsort(edges, E, sizeof(struct Edge), compareEdges);
struct DSU dsu;
initializeDSU(&dsu, V);
int edgeCount = 0;
int totalCost = 0;
for (int i = 0; i < E && edgeCount < V - 1; i++) {
struct Edge edge = edges[i];
if (find(&dsu, edge.src) != find(&dsu, edge.dest)) {
result[edgeCount++] = edge;
unite(&dsu, edge.src, edge.dest);
totalCost += edge.weight;
}
}
printf("Minimum Cost Spanning Tree:\n");
for (int i = 0; i < edgeCount; i++) {
printf("%d - %d : %d\n", result[i].src, result[i].dest, result[i].weight);
}
printf("Total Cost: %d\n", totalCost);
free(dsu.parent);
free(dsu.rank);
free(result);
}
int main() {
int V, E;
printf("Enter number of vertices and edges: ");
scanf("%d %d", &V, &E);
struct Edge* edges = (struct Edge*)malloc(E * sizeof(struct Edge));
printf("Enter edges (source destination weight):\n");
for (int i = 0; i < E; i++) {
scanf("%d %d %d", &edges[i].src, &edges[i].dest, &edges[i].weight);
}
kruskalMST(V, edges, E);
free(edges);
return 0;
}
OUTPUT
Enter number of vertices and edges: 5 6
Enter edges (source destination weight):
125
141
258
233
437
354
Minimum Cost Spanning Tree:
1-4:1
2-3:3
3-5:4
1-2:5
Total Cost: 13

You might also like