0% found this document useful (0 votes)
4 views8 pages

Krusshkal's Alogirithim

The document provides a program for finding the Minimal Spanning Tree (MST) using Kruskal's algorithm, implemented in C with both adjacency matrix and adjacency list representations. It includes the necessary data structures, functions for union-find operations, and the main function that initializes the graph and calls the MST function. The output displays the edges included in the MST and the total weight of the MST.

Uploaded by

nikhilyt93
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)
4 views8 pages

Krusshkal's Alogirithim

The document provides a program for finding the Minimal Spanning Tree (MST) using Kruskal's algorithm, implemented in C with both adjacency matrix and adjacency list representations. It includes the necessary data structures, functions for union-find operations, and the main function that initializes the graph and calls the MST function. The output displays the edges included in the MST and the total weight of the MST.

Uploaded by

nikhilyt93
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/ 8

Vishwakarma Institute of Technology, Pune

Department of Computer Engineering (A.Y 2024–2025)

OPERATING SYSTEM

Name : rahul pandita Div : C Roll no. : 71 PRN : 12311998

WRITE A PROGRAM FOR MINIMAL SPANNING TREE USING KRUSKAL’S ALOGIRITHIM USING
ADAJANCENCY LIST AND MATRIX

CODE :

CODE FOR ADAJENCENCY MATRIX

#include <stdio.h>

#include <stdlib.h>

#define V 5 // Number of vertices in the graph

// Structure to represent an edge

typedef struct {

int src, dest, weight;

} Edge;

// Structure to represent a subset for Union-Find

typedef struct {

int parent;

int rank;

} Subset;

// Find function for Union-Find

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

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

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

return subsets[i].parent;
}

// Union function for Union-Find

void Union(Subset subsets[], int x, int y) {

int rootX = find(subsets, x);

int rootY = find(subsets, y);

if (subsets[rootX].rank < subsets[rootY].rank)

subsets[rootX].parent = rootY;

else if (subsets[rootX].rank > subsets[rootY].rank)

subsets[rootY].parent = rootX;

else {

subsets[rootY].parent = rootX;

subsets[rootX].rank++;

// Comparator function for sorting edges by weight

int compare(const void* a, const void* b) {

return ((Edge*)a)->weight - ((Edge*)b)->weight;

void KruskalMSTMatrix(int graph[V][V]) {

Edge edges[V * V];

int edgeCount = 0;

// Extract edges from the adjacency matrix

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

for (int j = i + 1; j < V; j++) {


if (graph[i][j]) {

edges[edgeCount++] = (Edge){i, j, graph[i][j]};

// Sort edges by weight

qsort(edges, edgeCount, sizeof(edges[0]), compare);

Subset* subsets = (Subset*)malloc(V * sizeof(Subset));

for (int v = 0; v < V; ++v) {

subsets[v].parent = v;

subsets[v].rank = 0;

printf("Edges in the MST:\n");

int mstWeight = 0;

for (int i = 0, e = 0; e < V - 1 && i < edgeCount; i++) {

int x = find(subsets, edges[i].src);

int y = find(subsets, edges[i].dest);

if (x != y) {

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

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>

#define V 5 // Number of vertices

typedef struct {

int src, dest, weight;

} Edge;

typedef struct {

int parent;

int rank;

} Subset;

typedef struct {

Edge* edges;

int edgeCount;

} Graph;

Graph* createGraph(int edgeCount) {

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

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

graph->edgeCount = 0;

return graph;

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

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

return subsets[i].parent;

void Union(Subset subsets[], int x, int y) {

int rootX = find(subsets, x);

int rootY = find(subsets, y);

if (subsets[rootX].rank < subsets[rootY].rank)

subsets[rootX].parent = rootY;

else if (subsets[rootX].rank > subsets[rootY].rank)

subsets[rootY].parent = rootX;

else {

subsets[rootY].parent = rootX;

subsets[rootX].rank++;

int compare(const void* a, const void* b) {

return ((Edge*)a)->weight - ((Edge*)b)->weight;

void KruskalMSTList(Graph* graph) {

qsort(graph->edges, graph->edgeCount, sizeof(Edge), compare);

Subset* subsets = (Subset*)malloc(V * sizeof(Subset));

for (int v = 0; v < V; ++v) {

subsets[v].parent = v;

subsets[v].rank = 0;
}

printf("Edges in the MST:\n");

int mstWeight = 0;

for (int i = 0, e = 0; e < V - 1 && i < graph->edgeCount; i++) {

int x = find(subsets, graph->edges[i].src);

int y = find(subsets, graph->edges[i].dest);

if (x != y) {

printf("%d - %d: %d\n", graph->edges[i].src, graph->edges[i].dest, graph->edges[i].weight);

mstWeight += graph->edges[i].weight;

Union(subsets, x, y);

e++;

printf("Total weight of MST: %d\n", mstWeight);

free(subsets);

void addEdge(Graph* graph, int src, int dest, int weight) {

graph->edges[graph->edgeCount++] = (Edge){src, dest, weight};

int main() {

int edgeCount = 7;

Graph* graph = createGraph(edgeCount);

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 :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy