0% found this document useful (0 votes)
1 views

ADA_Lab_Program 1 & 2

The document outlines the implementation of two algorithms, Kruskal's and Prim's, for finding the Minimum Cost Spanning Tree (MST) of a connected undirected graph using C/C++. It includes code snippets for both algorithms, detailing the necessary functions and input/output processes. Sample outputs demonstrate the algorithms' functionality with example graphs.
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)
1 views

ADA_Lab_Program 1 & 2

The document outlines the implementation of two algorithms, Kruskal's and Prim's, for finding the Minimum Cost Spanning Tree (MST) of a connected undirected graph using C/C++. It includes code snippets for both algorithms, detailing the necessary functions and input/output processes. Sample outputs demonstrate the algorithms' functionality with example graphs.
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/ 6

Analysis & Design of Algorithms Lab-BCSL404 IV Semester

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 INF 999 // Define a large value to represent infinity (no direct edge)
#define MAX 100 // Maximum number of nodes

int p[MAX], c[MAX][MAX], t[MAX][2]; // Parent array, Cost matrix, MST edges array

// Function to find the root of a set


int find(int v)
{
while (p[v]) // Traverse up to the root parent
v = p[v];
return v;
}

// Function to perform union of two sets


void union1(int i, int j)
{
p[j] = i; // Set parent of `j` as `i`
}

// Function to implement Kruskal's algorithm


void kruskal(int n)
{
int i, j, k, u, v, min, res1, res2, sum = 0;

// Loop to find `n-1` edges for the MST


for (k = 1; k < n; k++)
{
min = INF;

// Find the minimum edge


for (i = 1; i < n - 1; i++)
{
for (j = 1; j <= n; j++)
{
if (i == j) continue; // Skip self-loops

if (c[i][j] < min) // Check if new minimum edge found


{
u = find(i);
v = find(j);

Dept. of CSE, SECAB. I. E. T., Vijayapur


Analysis & Design of Algorithms Lab-BCSL404 IV Semester

if (u != v) // Ensure it doesn't form a cycle


{
res1 = i;
res2 = j;
min = c[i][j];
}
}
}
}

union1(res1, find(res2)); // Perform union of sets


t[k][1] = res1; // Store MST edge
t[k][2] = res2;
sum = sum + min; // Add edge weight to MST cost
}

printf("\nCost of spanning tree is = %d", sum);


printf("\nEdges of spanning tree are:\n");

for (i = 1; i < n; i++) // Print MST edges


printf("%d -> %d\n", t[i][1], t[i][2]);
}

// Main function to read input and call Kruskal's algorithm


int main()
{
int i, j, n;

printf("\nEnter the number of nodes: ");


scanf("%d", &n);

// Initialize parent array


for (i = 1; i <= n; i++)
p[i] = 0;

printf("\nEnter the graph data (use %d for INF):\n", INF);

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


for (j = 1; j <= n; j++)
scanf("%d", &c[i][j]);

kruskal(n); // Call Kruskal's algorithm

Dept. of CSE, SECAB. I. E. T., Vijayapur


Analysis & Design of Algorithms Lab-BCSL404 IV Semester

return 0;
}
OUTPUT

Enter the n value:5

Enter the graph data:


13462
17693
5 2 8 99 45
1 44 66 33 6
12 4 3 2 0

Cost of spanning tree is=11


Edgesof spanning tree are:
2 -> 1
1 -> 5
3 -> 2
1 -> 4

Dept. of CSE, SECAB. I. E. T., Vijayapur


Analysis & Design of Algorithms Lab-BCSL404 IV Semester

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>
#define INF 999 // Define a large value to represent infinity (no direct edge)

// Function to implement Prim's algorithm


int prim(int c[10][10], int n, int s) {
int v[10] = {0}; // Visited nodes array (initialized to 0)
int ver[10], d[10]; // `ver[]` stores the MST edges, `d[]` stores the minimum cost
int i, j, sum = 0, min, u;

// Initialize the distance array and parent node array


for (i = 0; i < n; i++) {
ver[i] = s; // Initially, all vertices are connected to source
d[i] = c[s][i]; // Distance from source node `s` to all others
}
v[s] = 1; // Mark source as visited

// Loop to select `n-1` edges for the MST


for (i = 0; i < n - 1; i++) {
min = INF;
u = -1;

// Find the unvisited vertex with the smallest edge weight


for (j = 0; j < n; j++) {
if (!v[j] && d[j] < min) { // If vertex `j` is not visited and has a smaller cost
min = d[j];
u = j;
}
}

// If no valid `u` found, break (prevents accessing uninitialized `u`)


if (u == -1) break;

v[u] = 1; // Mark the selected vertex as visited


sum += d[u]; // Add edge weight to MST cost
printf("\n%d -> %d sum = %d", ver[u], u, sum); // Print the selected edge

// Update distance array for adjacent vertices of `u`


for (j = 0; j < n; j++) {
if (!v[j] && c[u][j] < d[j] && c[u][j] != INF) { // Update if new edge is smaller
d[j] = c[u][j];
ver[j] = u; // Store parent node

Dept. of CSE, SECAB. I. E. T., Vijayapur


Analysis & Design of Algorithms Lab-BCSL404 IV Semester

}
}
}

return sum; // Return total MST cost


}

// Main function to read input and call Prim's algorithm


int main() {
int c[10][10], i, j, res, s, n;

printf("\nEnter the number of nodes: ");


scanf("%d", &n);

printf("\nEnter the adjacency matrix (use %d for INF):\n", INF);


for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &c[i][j]);
}
}

printf("\nEnter the source node (0 to %d): ", n - 1);


scanf("%d", &s);

// Validate source node


if (s < 0 || s >= n) {
printf("\nInvalid source node!\n");
return 1;
}

res = prim(c, n, s); // Call Prim's algorithm


printf("\nTotal Cost of Minimum Spanning Tree = %d\n", res);

return 0;
}
OUTPUT

Enter n value: 4
Enter the graph data (use 999 for INF):
4521
7592
1769
0285

Dept. of CSE, SECAB. I. E. T., Vijayapur


Analysis & Design of Algorithms Lab-BCSL404 IV Semester

Enter the source node (0 to 3): 3


3 -> 0 sum = 0
3 -> 1 sum = 2
0 -> 2 sum = 4
Total Cost = 4

Dept. of CSE, SECAB. I. E. T., Vijayapur

You might also like