ADA Lab Manual 2024
ADA Lab Manual 2024
1.Find minimum cost spanning tree of a given undirected graph using Kruskal’s Graph
#include<stdio.h>
#include<conio.h>
int parent[10],min,ne=1,mincost=0,cost[10][10];
int i,j,a,b,u,v,n;
void main()
{ clrscr();
printf("enter the number of vertices in graph data\n");
scanf("%d",&n);
printf("enter the elements of matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]); //read elements of matrix
if(cost[i][j]==0)
cost[i][j]=999;
}
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
{
min=cost[i][j]; //initialize min value
a=u=i;
b=v=j;
}
while(parent[u])
u=parent[u];
while(parent[v])
v=parent[v];
if(u!=v)
{
printf("\n%d\t,edge\t(%d,%d)=%d",ne++,a,b,min);
mincost+=min;
parent[v]=u;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMINCOST=%d\n",mincost);
getch();
}
Page 1
ADA Lab,IV Semeseter,GMU
Output of Program:
enter the number of vertices in graph data
7
enter the elements of matrix
0 28 0 0 0 0 0
28 0 16 0 0 0 14
0 16 0 12 0 0 0
0 0 12 0 22 0 18
0 0 0 22 0 25 24
10 0 0 0 25 0 0
0 14 0 18 24 0 0
1.edge(6,1)=10 2.edge (3,4)=12 3.edge (2,7)=14, 4.edge (2,3)=16,5. edge (4,5)=22,6.edge (5,6)=25
MINCOST=99
2.Find Minimum Cost Spanning tree of a given undirected graph using Prim’s algorithm
#include<stdio.h>
#include<conio.h>
int a,b,u,v,ne=1,i,j,n;
int visited[10],min,mincost=0,cost[10][10];
void main()
{
clrscr();
printf("enter the number of vertices and graph data\n");
scanf("%d",&n); //read no of vertices
printf("enter the adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]); //read cost matrix
if(cost[i][j]==0)
cost[i][j]=999;
}
for(i=2;i<=n;i++)
visited[i]=0;
printf("\nthe edge of spanning tree are\n");
visited[1]=1;
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]==0)
continue;
else
{
min=cost[i][j]; //initialize min value
a=u=i;
b=v=j;
Page 2
ADA Lab,IV Semeseter,GMU
}
if(visited[u]==0||visited[v]==0)
{
printf("%d\t,edge\t(%d,%d)=%d\n",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tmincost=%d\n",mincost);
getch();
}
Output of Program:
enter the number of vertices and graph data
5
enter the adjacency matrix
0 11 9 7 8
11 0 15 14 13
9 15 0 12 14
7 14 12 0 6
8 13 14 6 0
the edge of spanning tree are
1.edge (1,4)=7 2.edge (4,5)=6
3.edge (1,3)=9 4.edge (1,2)=11
mincost=33
#include<stdio.h>
#include<conio.h>
#define true 1
#define max 10
#define infinity 999
void floyds(int[][max],int [][max],int);
int min(int ,int );
void main()
{
int cost[max][max];
int a[max][max];
int n,i,j;
clrscr();
printf("enter the no of vertices");
scanf("%d",&n);
printf("enter the adjacancy matrice");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
Page 3
ADA Lab,IV Semeseter,GMU
scanf("%d",&cost[i][j]); //read cost matrix
}
floyds(cost,a,n); //call floyd’s function
printf("all pair shortest path");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
getch();
}
void floyds(int cost[][max],int a[][max],int n) //floyd’s function
{
int s,i,j,k;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[i][j]=cost[i][j];
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[i][j]=min(a[i][j],a[i][k]+a[k][j]); //find min value
}
int min(int a,int b) //min function
{
return(a<b)?a:b;
}
Output of Program:
enter the no of vertices: 4
enter the adjacancy matrice
0 10 999 40
999 0 999 20
50 999 0 999
999 999 60 0
all pair shortest path
0 10 90 30
130 0 80 20
50 60 0 80
110 120 60 0
3 b) Compute the transitive closure of a given directed graph using Warshall’s algorithm.
#include<stdio.h>
#include<conio.h>
void main()
{
int p[10][10],i,j,n;
clrscr();
printf("enter no of vertices");
scanf("%d",&n); //read no of vertices
printf("enter adjency matrice:");
for(i=1;i<=n ;i++)
for(j=1;j<=n;j++)
scanf("%d",&p[i][j]); //read matrix elements
Output of Program:
enter no of vertices
4
enter adjency matrice:
0 1 1 0
0 0 0 1
0 0 0 1
0 1 1 0
the path matrice is
0 1 1 1
0 1 1 1
0 1 1 1
0 1 1 1
4.From a given vertex in a weigthed connected graph ,find shortest paths to other vertices using
Dijkstra’s algorithm.
#include<stdio.h>
#include<conio.h>
void dijikstra(int n,int v,int cost[10][10],int dist[]) //function definition
{
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
Page 5
ADA Lab,IV Semeseter,GMU
flag[i]=0,dist[i]=cost[v][i];
flag[v]=1,dist[v]=1;
count=2;
while(count<=n)
{
min=999;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
min=dist[w],u=w; //find min distance
flag[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("Enter the no of nodes:");
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]); //read matrix elements
if(cost[i][j]==0)
cost[i][j]=999;
}
printf("\nEnter the sources vertex:");
scanf("%d",&v);
dijikstra(n,v,cost,dist); //call function
printf("Shrotest path from");
for(j=1;j<=n;j++)
if(j!=v)
printf("%d->%d:::%d\n",v,j,dist[j]);
getch();
}
Output of Program:
Enter the no of nodes:4
Enter the cost matrix
0 4 12 10
0 0 6 0
0 0 0 0
0 0 15 0
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n,a[10][10],indeg[10],flag[10],count=0;
clrscr();
printf("enter the num of verticies\n");
scanf("%d",&n); //read n vertices
printf("enter the matrix form\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]); //read matrix elements
for(i=1;i<=n;i++)
indeg[i]=0,flag[i]=0; //initialize indeg and flag to 0
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
indeg[i]+=a[j][i];
printf("topological ordering\n");
for(k=1;k<=n;k++)
if( ( indeg[k]==0) && ( flag[k]==0 ) )
{
printf("%d\t",k);
flag[k]=1;
count++;
for(i=1;i<=n;i++)
if(a[k][i]==1)
indeg[i]--;
}
}
getch();
}
Page 7
ADA Lab,IV Semeseter,GMU
Output of Program:
enter the num of verticies
6
enter the matrix form
0 0 1 1 0 0
0 0 0 1 1 0
0 0 0 1 0 1
0 0 0 0 0 1
0 0 0 0 0 1
0 0 0 0 0 0
topological ordering
1 2 3 4 5 6
#include<stdio.h>
#include<conio.h>
#include<time.h>
int capacity,n,i,w[10],p[10],profit;
void main()
{
clock_t begin,end;
clrscr();
printf("enter the capacity of the knapsack\n");
scanf("%d",&capacity); //enter capacity
printf("enter the no of objects\n");
scanf("%d",&n); //no of objects
if(n==0)
{
printf("invalid input\n");
exit(0);
}
printf("enter the weight \n");
for(i=0;i<n;i++)
scanf("%d",&w[i]); //read weight of objects
printf("Enter the profit");
for(i=0;i<n;i++)
scanf("%d",&p[i]); //enter profit of each object
begin=clock();
profit=sack(0,capacity); //call sack function
end=clock();
printf("max profit=%d\n",profit);
printf("Begin Time=%d\n",begin);
printf("End time=%d\n",end);
printf("Total no of Clock ticks=%d\n",end-begin);
printf("Total time required by Knapsack=%f\n",(end-begin)/CLK_TCK);
getch();
Page 8
ADA Lab,IV Semeseter,GMU
}
Output of Program:
8.Design and implement C program to find a subset of a given set S={S1, S2,……Sn} of n positive
integers whose sum is equal to a given integer d.
#include<stdio.h>
#include<conio.h>
int w[10],d,n,count,x[10],i;
void sum(int s,int k,int r) //function for finding subset
{ //w.r.to sum value
x[k]=1;
if(s+w[k]==d)
{
printf("\n Subset %d=",++count);
for(i=0;i<=k;i++)
if(x[i])
printf("%3d",w[i]);
}
else if(s+w[k]+w[k+1]<=d)
Page 9
ADA Lab,IV Semeseter,GMU
sum(s+w[k],k+1,r-w[k]);
if((s+r-w[k]>=d) && (s+w[k+1]<=d))
{
x[k]=0;
sum(s,k+1,r-w[k]);
}
void main()
{
int s,r=0,k;
clrscr();
printf("\n Enter the No's of Elements:");
scanf("%d",&n);
printf("\n Enter the Elements:");
for(i=0;i<n;i++)
scanf("%d",&w[i]); //enter no of elements
printf("\n Enter the sum:");
scanf("%d",&d);
for(i=0;i<n;i++)
x[i]=0;
for(i=0;i<n;i++)
r+=w[i];
sum(0,0,r); //call sum function
getch();
}
Output of Program:
Subset 1= 1 2 6
Subset 2= 1 8
10. Sort a given set of elements using Quick sort method and determine the time required to sort
the elements. Repeat the experiment for different values of n, the number of element in the list to be
sorted and plot a graph of the time taken versus n.
#include<stdio.h>
#include<conio.h>
#include<time.h>
int a[20],n,i,j,temp;
}
}
void main()
{
int choice;
clock_t begin,end;
clrscr();
l1: printf("Enter the size of the array:");
scanf("%d",&n);
printf("Enter the elemenets of the array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Elements before sorting\n");
for(i=0;i<n;i++)
printf("a[%d]=%d\n",i,a[i]);
begin=clock();
quick_sort(0,n-1);
Page 11
ADA Lab,IV Semeseter,GMU
end=clock();
printf("The sorted elements are:");
for(i=0;i<n;i++)
printf("a[%d]=%d\n",i,a[i]);
printf("Total time required by Quick sort=%fSeconds\n",(end-begin)/CLK_TCK);
printf(“YOU WANT TO REPEAT AN EXPERIMENT FOR DIFFERENT VALUES OF
INPUT(YES-1,NO-0)”);
scanf(“%d”,&choice);
if(choice==1)
goto l1;
else
exit(0);
getch();
}
Output of Program:
Enter the size of the array:8
Enter the elemenets of the array:
25 10 72 18 40 11 32 9
Elements before sorting
a[0]=25
a[1]=10
a[2]=72
a[3]=18
a[4]=40
a[5]=11
a[6]=32
a[7]=9
The sorted elements are:
a[0]=9
a[1]=10
a[2]=11
a[3]=18
a[4]=25
a[5]=32
a[6]=40
a[7]=72
Total time required by Quick sort=2.472527Seconds
11. Sort a given set of elements using Merge sort method and determine the time required to sort
the elements. Repeat the experiment for different values of n, the number of element in the list to be
sorted and plot a graph of the time taken versus n.
#include<stdio.h>
#include<conio.h>
#include<time.h>
int a[10],b[10];
if(h>mid)
for(k=j;k<=high;k++)
b[i++]=a[k];
else
for(k=h;k<=mid;k++)
b[i++]=a[k];
for(k=low;k<=high;k++)
a[k]=b[k];
}
getch();
}
Output of Program:
Enter the size of the array:5
Enter the elements of array:
12
45
23
90
56
The sorted elements are
a[0]=12
a[1]=23
a[2]=45
a[3]=56
a[4]=90
Total time required by merge sort=1.263736seconds
#include<stdio.h>
#include<conio.h>
void main()
{
int m[25],n,k;
clrscr();
printf("Enter the no of queens:");
scanf("%d",&n); //read no of queens
printf(" The solution to problem is \n");
n--;
for(m[0]= 0,k=0; k >= 0; m[k]+=1)
{
while(m[k] <= n && !place(m,k))
m[k]+=1;
if(m[k]<= n )
if(k == n)
display(m,n+1); //call display function
else
k++,m[k]=-1;
else
k--;
}
printf("no Solution\n");
getch();
}
Output of Program:
Enter the no of queens:4
The solution to problem is
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0
Page 15