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

ADA Lab Program 1&2

The document outlines two programming assignments for the Analysis and Design of Algorithms Lab (BCSL404) for the academic year 2024-25. The first program implements Kruskal's algorithm to find the Minimum Cost Spanning Tree of a connected undirected graph, while the second program uses Prim's algorithm for the same purpose. Both programs include code snippets and instructions for inputting the cost matrix and displaying the results.

Uploaded by

Nivetha
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)
20 views6 pages

ADA Lab Program 1&2

The document outlines two programming assignments for the Analysis and Design of Algorithms Lab (BCSL404) for the academic year 2024-25. The first program implements Kruskal's algorithm to find the Minimum Cost Spanning Tree of a connected undirected graph, while the second program uses Prim's algorithm for the same purpose. Both programs include code snippets and instructions for inputting the cost matrix and displaying the results.

Uploaded by

Nivetha
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 and Design of Algorithms Lab (BCSL404) 2024-25

Program 1:
Design and implement C/C++ program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Kruskal’s algorithm.

Algorithm:

Code:
#include<stdio.h>
int ne=1, min_cost=0;
void main()
{
int n,i,j,min,a,u,b,v,cost[20][20],parent[20];
printf("Enter the number of vertices: ");
scanf("%d", &n);
printf("\nEnter the cost matrix: \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d", &cost[i][j]);
for(i=1;i<=n;i++)
parent[i]=0;

Dept. of CE, BTI, Bangalore-35 Page No. 1


Analysis and Design of Algorithms Lab (BCSL404) 2024-25

printf("\n The edges of spanning tree are\n");


while(ne<n)
{
min=999;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
while(parent[u])
u=parent[u];
while(parent[v])
v=parent[v];
if(u!=v)
{
printf("Edge %d\t(%d->%d)=%d\n",ne++,a,b,min);
min_cost=min_cost+min;
parent[v]=u;
}
cost[a][b]=cost[a][b]=999;
}
printf("\n Minimum cost=%d\n",min_cost);
}

Dept. of CE, BTI, Bangalore-35 Page No. 2


Analysis and Design of Algorithms Lab (BCSL404) 2024-25

Output:

Dept. of CE, BTI, Bangalore-35 Page No. 3


Analysis and Design of Algorithms Lab (BCSL404) 2024-25

Program 2:
Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Prim’s algorithm.

Algorithm:

Code:
#include<stdio.h>
int ne=1,min_cost=0;
void main()
{
int n,i,j,min,cost[20][20],a,u,b,v,source,visited[20];
printf("Enter the number of nodes: ");
scanf("%d",&n);
printf("Enter the cost matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);

Dept. of CE, BTI, Bangalore-35 Page No. 4


Analysis and Design of Algorithms Lab (BCSL404) 2024-25

}
}
for(i=1;i<=n;i++)
visited[i]=0;
printf("Enter the root node: ");
scanf("%d",&source);
visited[source]=1;
printf("\n Minimum cost spanning tree is\n");
while(ne<n)
{
min=999;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
if(visited[i]==0)
continue;
else
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
if(visited[u]==0||visited[v]==0)
{
printf("\nEdge %d\t(%d->%d)=%d\n",ne++,a,b,min);
min_cost=min_cost+min;

Dept. of CE, BTI, Bangalore-35 Page No. 5


Analysis and Design of Algorithms Lab (BCSL404) 2024-25

visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\nMinimum cost=%d\n",min_cost);
}

Output:

Dept. of CE, BTI, Bangalore-35 Page No. 6

You might also like