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

Lab1 Kruskal

The document provides a C/C++ program that implements Kruskal's algorithm to find the Minimum Cost Spanning Tree (MST) of a connected undirected graph. It includes functions for finding and uniting sets, as well as the main logic for constructing the MST based on edge weights. An example input and output demonstrate the program's functionality, resulting in a total cost of 13 for the MST.

Uploaded by

ananya.r.amcec
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)
11 views2 pages

Lab1 Kruskal

The document provides a C/C++ program that implements Kruskal's algorithm to find the Minimum Cost Spanning Tree (MST) of a connected undirected graph. It includes functions for finding and uniting sets, as well as the main logic for constructing the MST based on edge weights. An example input and output demonstrate the program's functionality, resulting in a total cost of 13 for the MST.

Uploaded by

ananya.r.amcec
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
You are on page 1/ 2

Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given connected undirected graph using

Kruskal's algorithm

Aim: To find MST using Kruskal’s algorithm

#include <stdio.h>
#define INF 999
#define MAX 100
int p[MAX], c[MAX][MAX], t[MAX][2];
int find(int v)
{
while (p[v])
v = p[v];
return v;
}
void union1(int i, int j)
{
p[j] = i;
}
void kruskal(int n)
{
int i, j, k, u, v, min, res1, res2, sum = 0;
for (k = 1; k < n; k++)
{
min = INF;
for (i = 1; i < n - 1; i++)
{
for (j = 1; j <= n; j++)
{
if (i == j) continue;
if (c[i][j] < min)
{
u = find(i);
v = find(j);
if (u != v)
{
res1 = i;
res2 = j;
min = c[i][j];
}
}
}
}
union1(res1, find(res2));
t[k][1] = res1;
t[k][2] = res2;
sum = sum + min;
}
printf("\nCost of spanning tree is=%d", sum);
printf("\nEdgesof spanning tree are:\n");
for (i = 1; i < n; i++)
printf("%d -> %d\n", t[i][1], t[i][2]);
}
int main()
{
int i, j, n;
printf("\nEnter the n value:");
scanf("%d", & n);
for (i = 1; i <= n; i++)
p[i] = 0;
printf("\nEnter the graph data:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", & c[i][j]);
kruskal(n);
return 0;
}
Input: Undirected weighted graph

Graph Data:
Enter the n value: 5
Enter the graph data:
99 5 99 1 99
5 99 3 99 8
99 3 99 7 4
1 99 7 99 99
99 8 4 99 99
Output: MST
Cost of spanning tree is=13
Edgesof spanning tree are:
1 -> 4
2 -> 3
3 -> 5
1 -> 2

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