DAA Manual 2025
DAA Manual 2025
#include<stdio.h>
#include<time.h>
void bfs(int a[10][10],int n,int source)
{
int s[10],q[10],f=0,r=-1,t,v;
for(v=0;v<n;v++)
{
s[v]=0;
}
q[++r]= source;
s[source]=1;
while(f<=r)
{
t=q[f++];
for(v=0; v<n; v++)
if(a[t][v]==1 && s[v]==0)
{
q[++r]=v;
printf("The BFS traversal is:\n %d %d", t, v);
s[v]=1;
}
}
}
main()
{
int a[10][10],n,i,j,s;
double clk;
clock_t starttime,endtime;
printf("\n Enter the number of cities\n");
scanf("%d",&n);
printf("\nEnter the matrix represention:");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the source city");
scanf("%d",&s);
starttime=clock();
bfs(a,n,s);
endtime=clock();
clk=(double)(endtime-starttime)/CLOCKS_PER_SEC;
printf("\nThe run time is %f\n",clk);
}
Expected output:
Enter the number of cities: 5
Enter the matrix representation:
01110
00001
00000
00000
00000
#include<stdio.h>
#include<time.h>
int n,a[50][50],i,j,count=0,reach[50],pos[50];
int main()
{
int v;
double clk;
clock_t starttime,endtime;
printf("\n\t\t\t DEPTH FIRST SEARCH \n");
printf("\n Enter number of Lands to be surveyed:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
pos[i]=0;
}
read_matrix();
printf("\n Enter the starting Land number:");
scanf("%d",&v);
starttime=clock();
dfs(v);
endtime=clock();
clk=(double)(endtime-starttime)/CLOCKS_PER_SEC;
printf("\n Vertices reached from the given vertex are...\n");
for(i=1;i<=count;i++)
{
printf("%d\t",reach[i]);
}
printf("\nThe run time is %f\n",clk);
}
read_matrix()
{
printf("\n Enter the adjacency matrix (Enter 0/1)\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i!=j)
{
printf("(%d,%d):",i,j);
scanf("%d",&a[i][j]);
}
}
dfs(int v)
{
int u;
reach[++count]=v;
u=adjacent(v);
while(u)
{
if(checkreach(u)==0) dfs(u);
u=adjacent(v);
}
}
adjacent(int i)
{
for(j=pos[i]+1;j<=n;j++)
if(a[i][j]==1)
{
pos[i]=j;
return j;
}
pos[i]=n+1;
return 0;
}
checkreach(int u)
{
for(i=1;i<=count;i++)
if(reach[i]==u)
return 1;
return 0;
}
Expected output:
DEPTH FIRST SEARCH
Enter number of Lands to be surveyed: 5
Enter the starting Land number: 1
Enter the adjacency matrix:
01110
00001
00000
00000
00000
Vertices reached from the given vertex are...
12
25
1 3
14
PROGRAM 3: BINARY SEARCH
//binary search
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define size 1000000
int binarysearch(int a[], int low , int high , int key)
{
if(high>=low)
{
int mid=(low+high)/2;
if(a[mid]==key)
return mid;
else if(a[mid]>key)
return binarysearch(a,low,mid-1,key);
else
return binarysearch(a,mid+1,high,key);
}
return -1;
}
int main()
{
int a[size],key,i,n,result;
printf("enter the n value: \n");
scanf("%d",&n);
printf("considering the elements in the sorted order\n");
for(i=0;i<n;i++)
{
a[i]=i+4;
}
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\nenter the key element:");
scanf("%d",&key);
double timespent;
clock_t begin=clock();
result=binarysearch(a,0,n-1,key);
(result==-1)?printf("the element is not present in the array\n"):printf("the element is present
at index %d",result);
clock_t end=clock();
timespent=(double)(end-begin)/CLOCKS_PER_SEC;
printf("\n the time taken is %f sec \n",timespent);
return 0;
}
OUTPUT1:
found elements:
>>>n=10
enter the key element:11
the element is present at index 7
the time taken is 0.000007 sec
>>>n=100
enter the key element:89
the element is present at index 85
the time taken is 0.000008 sec
>>>n=1000
enter the key element:916
the element is present at index 912
the time taken is 0.000007 sec
>>>n=10000
enter the key element:9864
the element is present at index 9860
the time taken is 0.000007 sec
>>>n=100000
enter the key element:99716
the element is present at index 99712
the time taken is 0.000008 sec 8
>>>n=1000000
enter the key element:999925
the element is present at index 999921
the time taken is 0.000008 sec
OUTPUT2:
>>>n=10
enter the key element:56
the element is not present in the array
TABLE:
GRAPH:
UNITS:
x axis= found lin(brown)
y axis= not found line(green)
PROGRAM 4: INSERTION SORT
SOLUTION:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
double count=0;
void insertionsort(int *num , int n)
{
int i,t,j,flag=0;
for(i=0;i<n;i++)
{
t=*(num+i);
else
break;
}
if(flag)
{
*(num+j+1)=t;
}
}
}
int main()
{
int n,*a,i,t;
clock_t start ,end;
double time;
printf("enter the size of array\t");
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));
printf("enter elements\n");
for(i=0;i<n;i++)
{
*(a+i)=rand()%100+1;
}
start=clock();
insertionsort(a,n);
end=clock();
printf("sorted array is\n");
for(i=0;i<n;i++)
{
printf("%d\t",*(a+i));
}
time+=(double)(end-start)/CLOCKS_PER_SEC;
printf("time taken is %lf",time);
printf("no of comparisons made is %lf\t",count);
return 0;
}
OUTPUT:
graph:
30000000000
25000000000
20000000000
15000000000 Column F
Column G
10000000000
5000000000
0
n 10 100 1000 10000 1000001000000
PROGRAM 5 : MERGE SORT
SOLUTION:
//MERGE sort
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int a[10000];
void merge(int,int,int);
void merge_sort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
void merge(int low,int mid,int high)
{
int h,i,j,b[10000],k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid)
{for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++) a[k]=b[k];
}
int main()
{
int n,i;
double clk;
clock_t starttime,endtime;
printf("MERGE SORT\n");
printf("Enter the number of employee records:\n ");
scanf("%d",&n);
for(i=0;i<n;i++)
a[i]=rand()%100; //to get 2 digit number
printf("The Employee IDs are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
starttime=clock();
merge_sort(0,n-1);
endtime=clock();
clk=(double)(endtime-starttime)/CLOCKS_PER_SEC;
printf("\nEmployee IDs in sorted order:\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\nThe run time is %f seconds. \n",clk);
}
OUTPUT:
GRAPH:
0.14
0.12
0.1
0.08
0.06
Column E
0.04
0.02
0
n 10 0 00 0 0 0
10 10 000 000 000
1 10 0
10
PROGRAM 6: QUICK SORT
SOLUTION:
//quick sort
#include<stdio.h>
#include<time.h>
int partition(int a[],int low,int high)
{
int i,j,temp,pivot;
pivot=a[low];
i=low+1;
j=high;
while(1)
{
while(i<high&&a[i]<=pivot)
i++;
while(a[j]>pivot)
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
temp=a[j];
a[j]=a[low];
a[low]=temp;
return j;
}
}
}
main()
{
int i, n,a[200000];
double clk;
clock_t starttime,endtime;
printf("Enter the number of students records: \n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
a[i]=rand()%100;
printf("%d\t",a[i]);
}
starttime=clock();
quick_sort(a,0,n-1);
endtime=clock();
clk=(double)(endtime-starttime)/CLOCKS_PER_SEC;
printf("\nSorted roll numbers are: \n");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
printf("\nThe run time is %f\n",clk);
}
OUTPUT:
GRAPH:
12
10
6 Column I
0
n 10 100 1000 10000 100000 1000000
PROGRAM 7 : HEAP SORT
SOLUTION:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<stdbool.h>
bool TRUE=1 ;
bool FALSE=0 ;
void heapbottomup(int h[],int n)
{
int i,heap,v,j,k;
for(i=(n/2);i>0;i--)
{
k=i;
v=h[k];
heap=FALSE;
while(!heap&&(2*k)<=n)
{
j=2*k;
if(j<n)
if(h[j]<h[j+1])
j=j+1;
if(v>=h[j])
heap=TRUE;
else
{
h[k]=h[j];
k=j;
}
}
h[k]=v;
}
}
void heapsort(int h[],int n)
{
int i,temp,last=n;
if(n<=1)
return;
else
{
heapbottomup(h,n);
temp=h[last];
h[last]=h[1];
h[1]=temp;
last--;
heapsort(h,n-1);
}
}
int main()
{
int h[1000000],n,i;
double clk;
clock_t starttime,endtime;
printf("enter the no:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
h[i]=rand()%100;
}
printf("the numbers are:\n");
for(i=1;i<=n;i++)
{
printf("%d\t",h[i]);
}
starttime=clock();
heapsort(h,n);
endtime=clock();
clk=(double)(endtime-starttime)/CLOCKS_PER_SEC;
printf("\nthe sorted order is:\n");
for(i=1;i<=n;i++)
printf("\t%d",h[i]);
printf("\nthe time taken is %f \n",clk);
}
OUTPUT:
>>>enter the no:
10
the numbers are:
83 86 77 15 93 35 86 92 49 21
the sorted order is:
15 21 35 49 77 83 86 86 92 93
the time taken is 0.000003
n time
10 3E-06
100 5.3E-05
1000 0.003573
10000 0.306069
100000 29.4561
GRAPH:
35
30
25
20
time
15
10
5
0
10 100 1000 10000 100000
SOLUTION:
//linear_search
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int main()
{
int i,*a,key;
int n;
double clk;
clock_t starttime , endtime;
printf("enter the number of medicine types:\n");
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
a[i]=rand()%100;
printf("the medicine IDs are:\n");
for(i=0;i<n;i++)
printf("%d \t",a[i]);
printf("\nenter the medicine ID to be searched:\n");
scanf("%d",&key);
starttime=clock();
for(i=0;i<n;i++)
{
if(a[i]==key)
{
endtime=clock();
clk=(double)(endtime-starttime)/CLOCKS_PER_SEC;
printf("time taken to search is %f sec\n",clk);
printf("medicine ID %d is present at location %d\n",key,i+1);
return 0;
}
}
printf("not found\n");
endtime=clock();
clk=(double)(endtime-starttime)/CLOCKS_PER_SEC;
printf("time taken to search is %f sec\n",clk);
}
OUTPUT1:
found elements:
>>> n=10;
enter the medicine ID to be searched:
77
time taken to search is 0.000002 sec
medicine ID 77 is present at location 3
>>>n=100;
enter the medicine ID to be searched:
15
time taken to search is 0.000003 sec
medicine ID 15 is present at location 4
>>>n=1000;
enter the medicine ID to be searched:
45
time taken to search is 0.000003 sec
medicine ID 45 is present at location 74
>>>n=10000;
enter the medicine ID to be searched:
3
time taken to search is 0.000003 sec
medicine ID 3 is present at location 87
>>>>n=100000;
enter the medicine ID to be searched:
12
time taken to search is 0.000003 sec
medicine ID 12 is present at location 96
>>>n=1000000;
enter the medicine ID to be searched:
84
time taken to search is 0.000003 sec
medicine ID 84 is present at location 45
>>>n=10000000;
enter the medicine ID to be searched:
86
time taken to search is 0.000003 sec
medicine ID 84 is present at location 45
OUTPUT2:
not found elements:
>>>n=10;
enter the medicine ID to be searched:
123
not found
time taken to search is 0.000013 sec
>>>n=100;
enter the medicine ID to be searched:
4567
not found
time taken to search is 0.000018 sec
>>>n=1000;
enter the medicine ID to be searched:
4566
not found
time taken to search is 0.00076 sec
>>>n=10000;
enter the medicine ID to be searched:
43567
not found
time taken to search is 0.000082 sec
>>>n=100000;
enter the medicine ID to be searched:
67889
not found
time taken to search is 0.000508 sec
>>>n=1000000;
enter the medicine ID to be searched:
67889
not found
time taken to search is 0.000508 sec
GRAPH:
1200000
1000000
Column C
Column D
800000
Column E
Column F
time
600000 Column G
Column H
400000 Column I
Column J
200000
0
1 2 3 4 5 6
instructions
TABLE:
SOLTUION:
#include<stdio.h>
#include<time.h>
double clk;
clock_t starttime,endtime;
int min(int a,int b)
{
if(a<b)
return a;
else
return b;
}
void floyd(int n,int W[10][10],int D[10][10])
{
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
D[i][j]=W[i][j];
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
D[i][j]=min(D[i][j],D[i][k]+D[k][j]);
}
}
}
}
void main()
{
int i,j,n,D[10][10],W[10][10];
printf("Enter no.of vertices: \n");
scanf("%d",&n);
printf("Enter the cost matrix: \n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&W[i][j]);
starttime=clock();
floyd(n,W,D);
endtime=clock();
clk=(double)(endtime-starttime)/CLOCKS_PER_SEC;
printf("All pair shortest path matrix is\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",D[i][j]);
}
}
printf("\nThe run time is %f\n",clk);
}
OUTPUT:
>>>Enter no.of vertices:
4
Enter the cost matrix:
0 999 3 999
2 0 999 999
999 7 0 1
6 999 999 0
All pair shortest path matrix is
0 10 3 4 2 0 5 6 77 0 1 6 16 9 0
The run time is 0.000003
SOLTUION:
#include<stdio.h>
#include<time.h>
int max(int x,int y)
{
return((x>y)?x:y);
}
int knap(int n,int w[10],int value[10],int m,int v[10][10])
{
int i,j;
for(i=0;i<=n;i++)
for(j=0;j<=m;j++)
{
if(i==0||j==0)
v[i][j]=0;
else if(j<w[i])
v[i][j]=v[i-1][j];
else
v[i][j]=max(v[i-1][j],value[i]+v[i-1][j-w[i]]);
}
printf("\n The table for solving knapsack problem using dynamic programming is:\n");
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
printf("%d\t",v[i][j]);
}
printf("\n");
}
}
int main()
{
double clk;
clock_t starttime,endtime;
int v[10][10],n,i,j,w[10],value[10],m,result;
printf("Enter the number of items:");
scanf("%d",&n);
printf("Enter the weights of %d items:/n",n);
for(i=1;i<=n;i++)
{
scanf("%d",&w[i]);
}
printf("Enter the value of %d items:",n);
for(i=1;i<=n;i++)
{
scanf("%d",&value[i]);
}
printf("Enter the capacity of the knapsack:");
scanf("%d",&m);
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
v[i][j]=0;
}}
starttime=clock();
result=knap(n,w,value,m,v);
endtime=clock();
clk=(double)(endtime-starttime)/CLOCKS_PER_SEC;
printf("Optimal solution for the knapsack problem is %d\n",v[n][m]);
printf("%f\n",clk);
}
OUTPUT:
>>>Enter the number of items:4
Enter the weights of 4 items:/n4 7 5 3
Enter the value of 4 items:40 42 25 12
Enter the capacity of the knapsack:10
The table for solving knapsack problem using dynamic programming is:
0 0 0 0 0 0 0 0 00 0
0 0 0 0 40 40 40 40 40 40 0
0 0 0 0 40 40 40 42 42 42 0
0 0 0 0 40 40 40 42 42 65 0
0 0 0 12 40 40 40 52 52 65 54
Optimal solution for the knapsack problem is 54
0.000114
SOLTUION:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include<time.h>
int i,j,k,a,b,u,v,n,ne=1;
clock_t starttime,endtime;
double clk;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
{
printf("\n\tImplementation of Kruskal's Algorithm\n");
printf("\nEnter the no. of vertices:");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
starttime=clock();
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("The edges of Minimum Cost Spanning Tree are\n");
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];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("%d edge (%d,%d) =%d\n",ne++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
endtime=clock();
printf("\n\tMinimum cost = %d\n",mincost);
clk=(double)(endtime-starttime)/CLOCKS_PER_SEC;
printf("the time taken is %lf",clk);
getch();
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}
OUTPUT:
>>> Implementation of Kruskal's Algorithm
Minimum cost = 8
the time taken is 0.000036
SOLUTION:
#include<stdio.h>
#include<time.h>
#define MAX 10
int choose(int dist[],int s[],int n)
{
int j=1,min=100,w;
for(w=1;w<=n;w++)
if((dist[w]<min) && (s[w]==0))
{
min=dist[w];
j=w;
}
return j;
}
void spath(int v,int cost[][MAX],int dist[],int n)
{
int w,u,i,num,s[MAX];
for(i=1;i<=n;i++)
{
s[i]=0;
dist[i]=cost[v][i];
}
s[v]=0;
dist[i]=999;
for(num=2;num<=n;num++)
{
u=choose(dist,s,n);
s[u]=1;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w])<dist[w] && !s[w])
dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int i,j,cost[MAX][MAX],dist[MAX],n,v;
double clk;
clock_t starttime,endtime;
printf("\nEnter number of vertices:");
scanf("%d",&n);
printf("\nEnter the cost of adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
printf("\nEnter the source vertex");
scanf("%d",&v);
starttime=clock();
spath(v,cost,dist,n);
endtime=clock();
printf("\nShortest distance\n");
for(i=1;i<=n;i++)
printf("\n%d to %d = %d",v,i,dist[i]);
clk=(double)(endtime-starttime)/CLOCKS_PER_SEC;
printf("The time taken is %f\n",clk);
}
OUTPUT:
>>>
Enter number of vertices:5
Shortest distance
1 to 1 = 999
1 to 2 = 4
1 to 3 = 2
1 to 4 = 3
1 to 5 = 6The time taken is 0.000003
SOLUTION:
#include<stdio.h>
#include<math.h>
#define FALSE 0
#define TRUE 1
int x[20];
int place(int k,int i)
{
int j;
for(j=1;j<=k;j++)
{
if((x[j]==i)||(abs(x[j]-i)==abs(j-k)))
return FALSE;
}
return TRUE;
}
void nqueens(int k,int n)
{
int i,a;
for(i=1;i<=n;i++)
{
if(place(k,i))
{
x[k]=i;
if(k==n)
{
for(a=1;a<=n;a++)
printf("%d\t",x[a]);
printf("\n");
}
else
nqueens(k+1,n);
}
}
}
void main()
{
int n;
printf("\nEnter the number of queens:");
scanf("%d",&n);
printf("\n The solution to N Queens problem is: \n");
nqueens(1,n);
}
OUTPUT:
>>>Enter the number of queens:4
SOLUTION:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
int s[20],x[20],n,sum,i,tot,soln=0;
long int a;
printf("\nEnter the value of n ");
scanf("%d",&n);
printf("\nEnter the values ");
for(i=0;i<n;i++)
{
scanf("%d",&s[i]);
}
printf("\nEnter the total sum ");
scanf("%d",&tot);
printf("\nSolution to the problem is ");
for(a=0;a<pow(2,n);a=a+1)
{
sum=0;
for(i=0;i<n;i++)
{
if((a&(int)pow(2,i))!=0)
{
x[i]=1;
sum=sum+s[i];
}
else
{
x[i]=0;
}
}
if(sum==tot)
{
soln=soln+1;
printf("\n");
for(i=0;i<n;i++)
{
if(x[i]==1)
{
printf("%d ",s[i]);}
}
}
}
if(soln==0)
{
printf("is not possible!!");
}
}
OUTPUT:
>>>
Enter the value of n 5