0% found this document useful (0 votes)
13 views5 pages

Program 9 (b)

Uploaded by

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

Program 9 (b)

Uploaded by

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

Program 9 (b)

Aim: Write a program to implement Kruskal’s Algorithm to find the MST.


Complexity:
 Worst-case complexity: O(Elog⁡E+Elog⁡V)O(E \log E + E \log V)O(ElogE+ElogV), where EEE is
the number of edges, and VVV is the number of vertices.

o O(Elog⁡E)O(E \log E)O(ElogE) for sorting edges.

o O(Elog⁡V)O(E \log V)O(ElogV) for union-find operations.

Algorithm:
1. Sort all edges in ascending order of their weights.

2. Initialize the MST as empty.

3. Iterate through the sorted edges and add an edge to the MST if it does not form a cycle
(using union-find to check).

4. Stop when the MST contains V−1V-1V−1 edges, where VVV is the number of vertices.

Source Code:

#include <stdio.h>

#include <stdlib.h>

struct Edge {

int src, dest, weight;

};

struct Graph {

int V, E;

struct Edge* edges;

};

struct Subset {

int parent;

int rank;

};
struct Graph* createGraph(int V, int E) {

struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));

graph->V = V;

graph->E = E;

graph->edges = (struct Edge*)malloc(E * sizeof(struct Edge));

return graph;

int find(struct Subset subsets[], int i) {

if (subsets[i].parent != i) {

subsets[i].parent = find(subsets, subsets[i].parent);

return subsets[i].parent;

void unionSets(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++;

}
int compareEdges(const void* a, const void* b) {

struct Edge* edge1 = (struct Edge*)a;

struct Edge* edge2 = (struct Edge*)b;

return edge1->weight - edge2->weight;

void kruskalMST(struct Graph* graph) {

int V = graph->V;

struct Edge result[V]; // Store the resultant MST

int e = 0;

int i = 0;

qsort(graph->edges, graph->E, sizeof(graph->edges[0]), compareEdges);

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;

while (e < V - 1 && i < graph->E) {

struct Edge nextEdge = graph->edges[i++];

int x = find(subsets, nextEdge.src);

int y = find(subsets, nextEdge.dest);

if (x != y) {

result[e++] = nextEdge;

unionSets(subsets, x, y);

}printf("Edge \tWeight\n");

for (i = 0; i < e; ++i) {

printf("%d - %d \t%d\n", result[i].src, result[i].dest, result[i].weight);

}
free(subsets);

int main() {

int V, E;

printf("Enter the number of vertices: ");

scanf("%d", &V);

printf("Enter the number of edges: ");

scanf("%d", &E);

struct Graph* graph = createGraph(V, E);

printf("Enter the edges (source, destination, weight):\n");

for (int i = 0; i < E; i++) {

printf("Edge %d: ", i + 1);

scanf("%d %d %d", &graph->edges[i].src, &graph->edges[i].dest, &graph->edges[i].weight);

printf("\nRunning Kruskal's Algorithm to find MST...\n");

kruskalMST(graph);

free(graph->edges);

free(graph);

return 0;

}
Sample Input and Output
Input:
Enter the number of vertices: 4

Enter the number of edges: 5

Enter the edges (source, destination, weight):

Edge 1: 0 1 10

Edge 2: 0 2 6

Edge 3: 0 3 5

Edge 4: 1 3 15

Edge 5: 2 3 4

Output:
Running Kruskal's Algorithm to find MST...

Edge Weight

2-3 4

0-3 5

0 - 1 10

You might also like