Krusshkal's Alogirithim
Krusshkal's Alogirithim
OPERATING SYSTEM
WRITE A PROGRAM FOR MINIMAL SPANNING TREE USING KRUSKAL’S ALOGIRITHIM USING
ADAJANCENCY LIST AND MATRIX
CODE :
#include <stdio.h>
#include <stdlib.h>
typedef struct {
} Edge;
typedef struct {
int parent;
int rank;
} Subset;
if (subsets[i].parent != i)
return subsets[i].parent;
}
subsets[rootX].parent = rootY;
subsets[rootY].parent = rootX;
else {
subsets[rootY].parent = rootX;
subsets[rootX].rank++;
int edgeCount = 0;
subsets[v].parent = v;
subsets[v].rank = 0;
int mstWeight = 0;
if (x != y) {
mstWeight += edges[i].weight;
Union(subsets, x, y);
e++;
}
printf("Total weight of MST: %d\n", mstWeight);
free(subsets);
int main() {
int graph[V][V] = {
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}
};
KruskalMSTMatrix(graph);
return 0;
OUTPUT :
CODE FOR ADAJACENCY MATRIX
#include <stdio.h>
#include <stdlib.h>
typedef struct {
} Edge;
typedef struct {
int parent;
int rank;
} Subset;
typedef struct {
Edge* edges;
int edgeCount;
} Graph;
graph->edgeCount = 0;
return graph;
if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
subsets[rootX].parent = rootY;
subsets[rootY].parent = rootX;
else {
subsets[rootY].parent = rootX;
subsets[rootX].rank++;
subsets[v].parent = v;
subsets[v].rank = 0;
}
int mstWeight = 0;
if (x != y) {
mstWeight += graph->edges[i].weight;
Union(subsets, x, y);
e++;
free(subsets);
int main() {
int edgeCount = 7;
addEdge(graph, 0, 1, 2);
addEdge(graph, 0, 3, 6);
addEdge(graph, 1, 2, 3);
addEdge(graph, 1, 3, 8);
addEdge(graph, 1, 4, 5);
addEdge(graph, 2, 4, 7);
addEdge(graph, 3, 4, 9);
KruskalMSTList(graph);
free(graph->edges);
free(graph);
return 0;
OUTPUT :