0% found this document useful (0 votes)
17 views2 pages

Ada

The document describes an algorithm to find the minimum spanning tree of a graph using Kruskal's algorithm. It defines edge and subset structures, includes function prototypes for find, union, compare and kruskalMST functions, and provides implementations for these functions to perform Kruskal's algorithm.

Uploaded by

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

Ada

The document describes an algorithm to find the minimum spanning tree of a graph using Kruskal's algorithm. It defines edge and subset structures, includes function prototypes for find, union, compare and kruskalMST functions, and provides implementations for these functions to perform Kruskal's algorithm.

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>

#define MAX_NODES 20

// Structure to represent an edge in the graph


struct Edge {
int source, destination, weight;
};

// Structure to represent a subset for union-find


struct Subset {
int parent;
int rank;
};

// Global variables
int parent[MAX_NODES];
int rank[MAX_NODES];

// Function prototypes
int find(int node);
void unionSets(int x, int y);
int compare(const void *a, const void *b);
void kruskalMST(struct Edge edges[], int numEdges, int numNodes);

// Find operation of union-find algorithm


int find(int node) {
if (parent[node] != node)
parent[node] = find(parent[node]);
return parent[node];
}

// Union operation of union-find algorithm


void unionSets(int x, int y) {
int xRoot = find(x);
int yRoot = find(y);

if (rank[xRoot] < rank[yRoot])


parent[xRoot] = yRoot;
else if (rank[xRoot] > rank[yRoot])
parent[yRoot] = xRoot;
else {
parent[yRoot] = xRoot;
rank[xRoot]++;
}
}

// Compare function for qsort() to sort edges based on weight


int compare(const void *a, const void *b) {
struct Edge *edge1 = (struct Edge *)a;
struct Edge *edge2 = (struct Edge *)b;
return edge1->weight - edge2->weight;
}

// Kruskal's algorithm for finding minimum spanning tree


void kruskalMST(struct Edge edges[], int numEdges, int numNodes) {
qsort(edges, numEdges, sizeof(edges[0]), compare);
struct Edge result[numNodes]; // To store the resultant MST
int resultIndex = 0; // Index used for result[]

// Initialize sets
for (int i = 0; i < numNodes; i++) {
parent[i] = i;
rank[i] = 0;
}

int edgeIndex = 0; // Index used for picked edges

// Number of edges to be taken is equal to numNodes-1


while (resultIndex < numNodes - 1 && edgeIndex < numEdges) {
// Pick the smallest edge
struct Edge nextEdge = edges[edgeIndex++];

int x = find(nextEdge.source);
int y = find(nextEdge.destination);

// If including this edge doesn't cause cycle, include it


if (x != y) {
result[resultIndex++] = nextEdge;
unionSets(x, y);
}
}

// Print the edges of MST


printf("Edges in the MST:\n");
for (int i = 0; i < resultIndex; i++)
printf("%d -- %d == %d\n", result[i].source, result[i].destination,
result[i].weight);
}

int main() {
int numNodes, numEdges;
printf("Enter the number of nodes and edges in the graph: ");
scanf("%d %d", &numNodes, &numEdges);

struct Edge edges[numEdges];


printf("Enter source, destination, and weight of each edge:\n");
for (int i = 0; i < numEdges; i++)
scanf("%d %d %d", &edges[i].source, &edges[i].destination,
&edges[i].weight);

kruskalMST(edges, numEdges, numNodes);

return 0;
}

You might also like