0% found this document useful (0 votes)
15 views6 pages

DAA LAB Program 4 and 5

The document contains two C/C++ programs for finding the Minimum Cost Spanning Tree (MST) of a connected undirected graph using Kruskal's and Prim's algorithms. The first program implements Kruskal's algorithm, utilizing a union-find structure to manage edges, while the second program implements Prim's algorithm using an adjacency matrix and a priority mechanism to select edges. Both programs output the edges included in the MST and the total cost associated with it.

Uploaded by

hansikaldr
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)
15 views6 pages

DAA LAB Program 4 and 5

The document contains two C/C++ programs for finding the Minimum Cost Spanning Tree (MST) of a connected undirected graph using Kruskal's and Prim's algorithms. The first program implements Kruskal's algorithm, utilizing a union-find structure to manage edges, while the second program implements Prim's algorithm using an adjacency matrix and a priority mechanism to select edges. Both programs output the edges included in the MST and the total cost associated with it.

Uploaded by

hansikaldr
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

Program-4

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>

#define MAX 100

#define INF 9999

typedef struct {

int u, v, cost;

} Edge;

int parent[MAX];

// Find function for union-find

int find(int i) {

while (parent[i])

i = parent[i];

return i;

// Union function

int unionSet(int i, int j) {

int a = find(i);

int b = find(j);

if (a != b) {

parent[a] = b;

return 1;

return 0;

int main() {
int n, e, i, j, u, v, cost;

Edge edges[MAX];

int totalCost = 0;

printf("Enter number of vertices: ");

scanf("%d", &n);

printf("Enter number of edges: ");

scanf("%d", &e);

printf("Enter edges (u v cost):\n");

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

scanf("%d%d%d", &edges[i].u, &edges[i].v, &edges[i].cost);

// Sort edges using Bubble Sort (simple for small graphs)

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

for (j = 0; j < e - i - 1; j++) {

if (edges[j].cost > edges[j + 1].cost) {

Edge temp = edges[j];

edges[j] = edges[j + 1];

edges[j + 1] = temp;

printf("Edges in Minimum Cost Spanning Tree:\n");

for (i = 0, j = 0; i < e && j < n - 1; i++) {

u = edges[i].u;

v = edges[i].v;

cost = edges[i].cost;

if (unionSet(u, v)) {

printf("%d - %d : %d\n", u, v, cost);


totalCost += cost;

j++;

printf("Total cost of Minimum Spanning Tree: %d\n", totalCost);

return 0;

OUTPUT:

Enter number of vertices: 4

Enter number of edges: 5

Enter edges (u v cost):

121

133

231

246

342

Edges in Minimum Cost Spanning Tree:

1-2:1

2-3:1

3-4:2

Total cost of Minimum Spanning Tree: 4


Program-5
Design and implement C/C++ Program to find Minimum Cost Spanning Tree of
a given connected undirected graph using Prim's algorithm.
#include <stdio.h>

#include <limits.h>

#define MAX 100

#define INF 9999

int minKey(int key[], int mstSet[], int n) {

int min = INF, min_index;

for (int v = 0; v < n; v++)

if (mstSet[v] == 0 && key[v] < min)

min = key[v], min_index = v;

return min_index;

void printMST(int parent[], int graph[MAX][MAX], int n) {

int cost = 0;

printf("Edge \tWeight\n");

for (int i = 1; i < n; i++) {

printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);

cost += graph[i][parent[i]];

printf("Total cost of Minimum Spanning Tree: %d\n", cost);

void primMST(int graph[MAX][MAX], int n) {

int parent[MAX];

int key[MAX];

int mstSet[MAX];

for (int i = 0; i < n; i++)


key[i] = INF, mstSet[i] = 0;

key[0] = 0;

parent[0] = -1;

for (int count = 0; count < n - 1; count++) {

int u = minKey(key, mstSet, n);

mstSet[u] = 1;

for (int v = 0; v < n; v++)

if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v])

parent[v] = u, key[v] = graph[u][v];

printMST(parent, graph, n);

int main() {

int n, graph[MAX][MAX];

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

scanf("%d", &n);

printf("Enter the adjacency matrix (0 for no edge):\n");

for (int i = 0; i < n; i++)

for (int j = 0; j < n; j++) {

scanf("%d", &graph[i][j]);

if (graph[i][j] == 0)

graph[i][j] = INF;

printf("\nMinimum Cost Spanning Tree using Prim's Algorithm:\n");

primMST(graph, n);

return 0;

}
OUTPUT:

Enter the number of vertices: 5

Enter the adjacency matrix (0 for no edge):

02060

20385

03007

68009

05790

Minimum Cost Spanning Tree using Prim's Algorithm:

Edge Weight

0-1 2

1-2 3

0-3 6

1-4 5

Total cost of Minimum Spanning Tree: 16

You might also like