Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
19 views9 pages

BCLDL404 Lab Programs

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)
19 views9 pages

BCLDL404 Lab Programs

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/ 9

Analysis & Design of Algorithms Lab Manual BCDL404

1. Design and implement C/C++ program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Krusal’s algorithm.
#include<stdio.h>
#include<conio.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;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]);
}
void main()

Dr Shiva Prasad KM Associate Professor Department of CSE RYMEC Ballari Page 1


Analysis & Design of Algorithms Lab Manual BCDL404

{
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);
getch();
}

Dr Shiva Prasad KM Associate Professor Department of CSE RYMEC Ballari Page 2


Analysis & Design of Algorithms Lab Manual BCDL404

2. 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<conio.h>
#define INF 999
int prim(int c[10][10],int n,int s)
{
int v[10],i,j,sum=0,ver[10],d[10],min,u;
for(i=1;i<=n;i++)
{
ver[i]=s;
d[i]=c[s][i];
v[i]=0;
}
v[s]=1;
for(i=1;i<=n-1;i++)
{
min=INF;
for(j=1;j<=n;j++)
if(v[j]==0 && d[j]<min)
{
min=d[j];
u=j;
}
v[u]=1;
sum=sum+d[u];
printf("\n%d->%d sum=%d",ver[u],u,sum);
for(j=1;j<=n;j++)
if(v[j]==0 && c[u][j]<d[j])
{

Dr Shiva Prasad KM Associate Professor Department of CSE RYMEC Ballari Page 3


Analysis & Design of Algorithms Lab Manual BCDL404

d[j]=c[u][j];
ver[j]=u;
}
}
return sum;
}
void main()
{
int c[10][10],i,j,res,s,n;
clrscr();
printf("\n enter n value:");
scanf("%d",&n);
printf("\nenter the graph data:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&c[i][j]);
printf("\n enter the source node:");
scanf("%d",&s);
res=prim(c,n,s);
printf("\n cost=%d",res);
getch();
}

Dr Shiva Prasad KM Associate Professor Department of CSE RYMEC Ballari Page 4


Analysis & Design of Algorithms Lab Manual BCDL404

3. a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem
using Floyd’s algorithm.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int iN, i, j, k;
int iaFloyd[10][10], iaCost[10][10];
printf("nEnter the number of verticesn");
scanf("%d",&iN);
printf("nEnter the Cost adjacency Matrixn");
for(i=0;i<iN;i++)
for(j=0;j<iN;j++)
scanf("%d",&iaCost[i][j]);
printf("nInput Graphn");
for(i=0;i<iN;i++)
{
for(j=0;j<iN;j++)
{
printf("%dt",iaCost[i][j]);
}
printf("n");
}
printf("n");
for(i=0;i<iN;i++)
{
for(j=0;j<iN;j++)
{
iaFloyd[i][j] = iaCost[i][j];
}

Dr Shiva Prasad KM Associate Professor Department of CSE RYMEC Ballari Page 5


Analysis & Design of Algorithms Lab Manual BCDL404

}
for(k=0;k<iN;k++)
{
for(i=0;i<iN;i++)
{
for(j=0;j<iN;j++)
{
if(iaFloyd[i][j] > (iaFloyd[i][k] + iaFloyd[k][j]))
iaFloyd[i][j] = (iaFloyd[i][k] + iaFloyd[k][j]);
}
}
}
printf("nAll Pair Shortest Path Matrixn");
for(i=0;i<iN;i++)
{
for(j=0;j<iN;j++)
{
printf("%dt",iaFloyd[i][j]);
}
Output:
Enter the number of vertices
printf("n"); 5

} Enter the Cost adjacency Matrix


0 3 9999 7 9
printf("n"); 3 0 4 2 9999
9999 4 0 5 6
return 0; 7 2 5 0 4
9 9999 6 4 0
}
Input Graph
0 3 9999 7 9
3 0 4 2 9999
9999 4 0 5 6
7 2 5 0 4
9 9999 6 4 0

All Pair Shortest Path Matrix


0 3 7 5 9
3 0 4 2 6
7 4 0 5 6
5 2 5 0 4
9 6 6 4 0

Dr Shiva Prasad KM Associate Professor Department of CSE RYMEC Ballari Page 6


Analysis & Design of Algorithms Lab Manual BCDL404

3.b. Design and implement C/C++ Program to find the transitive closure using
Warshal’s algorithm.

#include <stdio.h>

const int MAX = 100;

void WarshallTransitiveClosure(int graph[MAX][MAX], int numVert);

int main(void)

int i, j, numVert;

int graph[MAX][MAX];

printf("Warshall's Transitive Closuren");

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

scanf("%d",&numVert);

printf("Enter the adjacency matrix :-n");

for (i=0; i<numVert; i++)

for (j=0; j<numVert; j++)

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

WarshallTransitiveClosure(graph, numVert);

printf("nThe transitive closure for the given graph is :-n");

for (i=0; i<numVert; i++)

for (j=0; j<numVert; j++)

printf("%dt",graph[i][j]);

printf("n");

return 0;

Dr Shiva Prasad KM Associate Professor Department of CSE RYMEC Ballari Page 7


Analysis & Design of Algorithms Lab Manual BCDL404

void WarshallTransitiveClosure(int graph[MAX][MAX], int numVert)

int i,j,k;

for (k=0; k<numVert; k++)

for (i=0; i<numVert; i++)

for (j=0; j<numVert; j++)

if (graph[i][j] || (graph[i][k] && graph[k][j]))

graph[i][j] = 1;

Output:

Enter the number of vertices : 4


Enter the adjacency matrix :-
0 0 1 0
0 0 0 1
1 0 0 0
0 1 0 0

The transitive closure for the given graph is


:-
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1

Dr Shiva Prasad KM Associate Professor Department of CSE RYMEC Ballari Page 8


Analysis & Design of Algorithms Lab Manual BCDL404

4. Design and implement C/C++ Program to find shortest paths from a given vertex in a
weighted connected graph to other vertices using Dijkstra’s algorithm.

Dr Shiva Prasad KM Associate Professor Department of CSE RYMEC Ballari Page 9

You might also like