VTU ADA Lab Programs
VTU ADA Lab Programs
VTU ADA Lab Programs
1. Implement Recursive Binary search and Linear search and determine the time
required to search an element. Repeat the experiment for different values of n,
the number of elements in the list to be searched and plot a graph of the time
taken versus n.
2. Sort a given set of elements using the Heapsort method and determine the time
required to sort the elements. Repeat the experiment for different values of n,
the number of elements in the list to be sorted and plot a graph of the time
taken versus n.
3. 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 elements in the list to be sorted and plot a graph of the time
taken versus n.
4. Sort a given set of elements using Selection sort and determine the time
required to sort elements. Repeat the experiment for different values of n, the
number of elements in the list to be sorted and plot a graph of the time taken
versus n.
7. From a given vertex in a weighted connected graph, find shortest paths to other
vertices using Dijkstra's algorithm.
8. Sort a given set of elements using Quick sort method and determine the time
required sort the elements. Repeat the experiment for different values of n, the
number of elements in the list to be sorted and plot a graph of the time taken
versus n.
11. Find a subset of a given set S = {sl,s2,.....,sn} of n positive integers whose sum is
equal to a given positive integer d. For example, if S= {1, 2, 5, 6, 8} and d = 9
there are two solutions{1,2,6}and{1,8}.A suitable message is to be displayed if
the given problem instance doesn't have a solution.
13. Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s
algorithm.
#include<stdio.h>
#include<conio.h>
#include<time.h>
void main()
{
int a[20],n,key,i,pos,choice;
clock_t start,end;
float duration;
for(;;)
{
clrscr();
printf("1.Binary search\n 2.Linear search\n 3.exit\n");
printf("\n Enter your choice :");
scanf("%d",&choice);
if(choice >= 3)
exit(0);
else
{
printf("\n Enter the number of elements : ");
scanf("%d",&n);
printf("\n Enter the elements \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter the key to be searched : ");
scanf("%d",&key);
switch(choice)
{
case 1:
start=clock();
pos=binsearch(a,key,0,n-1);
delay(100);
end=clock();
duration=(end-start)/CLK_TCK;
printf("\n Time taken is %f\n",duration);
if(pos==-1)
printf("\n key is not found\n");
else
printf("\n key is found\n");
break;
case 2:
start=clock();
pos=linsearch(a,0,n,key);
delay(100);
end=clock();
duration=(end-start)/CLK_TCK;
printf("\n Time taken is %f",duration);
if(pos==-1)
printf("\n key not found\n");
else
printf("\n Key is found\n");
break;
}
getch();
}
}
}
Output :
1.Binary search
2.Linear search
3.exit
key is found
1.Binary search
2.Linear search
3.exit
1.Binary search
2.Linear search
3.exit
1.Binary search
2.Linear search
3.exit
Output :
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[15];
clrscr();
printf("\n Enter the number of elements : ");
scanf("%d",&n);
printf("\n Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\n The sorted elements are\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
Output :
#include<stdio.h>
#include<conio.h>
int visited[10];
void main()
{
int i,j,source;
int n,a[10][10];
int count=0;
clrscr();
printf("\n Enter the number of nodes : ");
scanf("%d",&n);
printf("\n Enter the paths (0 if edge is not present,1 if present\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the source vertex :");
scanf("%d",&source);
for(i=1;i<=n;i++)
visited[i]=0;
dfs(n,a,source);
for(i=1;i<=n;i++)
if(visited[i]) count=count+1;
if(count==n)
printf("\n The graph is connected \n");
else
printf("\n The graph is not connected \n");
getch();
}
Output :
Output :
Output :
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[20],n;
clrscr();
printf("\n\n Enter the number of elements : ");
scanf("%d",&n);
printf("\n\n Enter the elments : \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insertsort(a,n);
printf("\n The sorted elements are \n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
Output :
void main()
{
int m,i,max_profit;
clrscr();
printf ("\n Enter the number of objects : ");
scanf ("%d",&n);
printf ("\n Enter the knapsack capacity:");
scanf("%d",&m);
printf ("\n Enter profit followed by weight:\n");
for (i=1;i<=n;i++)
scanf("%d%d",&p[i],&w[i]);
max_profit=knap(1,m);
printf("\n Max profit = %d",max_profit);
getch();
}
Output :
#include<stdio.h>
#include<conio.h>
#define INFINITY 999
void main()
{
int n,cost[10][10],distance[10];
int i,j,source,sum;
clrscr();
printf("\n Enter how many nodes :");
scanf("%d",&n);
printf("\n Cost Matrix\n Enter 999 for no edge\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
printf("Enter the source node :");
scanf("%d",&source);
dijikstra(cost,n,source,distance);
for(i=1;i<=n;i++)
printf("\n SHORTEST DISTANCE FROM %d TO %d IS %d\n",source,i,
distance[i]);
getch();
}
Output :
void main()
{
int i,n,a[20];
clrscr();
printf("\n Enter the number of elements : ");
scanf("%d",&n);
printf("\n Enter the elements \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
printf("\n The sorted elements are \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
Output :
#include<stdio.h>
#include<conio.h>
#define INFINITY 999
#define MAX 100
int parent[MAX],cost[MAX][MAX],t[MAX][2];
int find(int v)
{
while(parent[v])
{
v=parent[v];
}
return v;
}
void kruskal(int n)
{
int i,j,k,u,v,mincost,res1,res2,sum=0;
for(k=1;k<n;k++)
{
mincost=INFINITY;
for(i=1;i<n-1;i++)
{
for(j=1;j<=n;j++)
{
if(i==j) continue;
if(cost[i][j] < mincost)
{
u=find(i);
v=find(j);
if(u!=v)
{
res1=i;
res2=j;
mincost=cost[i][j];
}
}
}
}
union1(res1,find(res2));
t[k][1]=res1;
t[k][2]=res2;
sum=sum+mincost;
}
printf("\n cost of spanning tree is %d\n",sum);
printf("\n Edges of spanning tree are \n");
for(i=1;i<n;i++)
printf(" %d->%d\n",t[i][1],t[i][2]);
}
void main()
{
int i,j,n;
clrscr();
printf("\n Enter the number of vertices : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
parent[i]=0;
printf("\n Enter the cost adjaceny matrix 0- for self edge and 999-if
no edge \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
kruskal(n);
getch();
}
Output :
Enter the cost adjaceny matrix 0- for self edge and 999-if no edge
0 20 2 999
20 0 15 5
2 15 0 25
999 5 25 999
Enter the cost adjaceny matrix 0 - for self egde and 999 – if no edge
0 28 999 999 999 10 999
28 0 16 999 999 999 14
999 16 999 12 999 999 999
999 999 12 0 22 999 18
999 999 999 22 0 25 24
10 999 999 999 25 0 999
999 14 999 18 24 999 0
#include<stdio.h>
#include<conio.h>
int visited[10];
u=q[front];
front=front+1;
for(i=1;i<=n;i++)
if(a[u][i]==1 && visited[i]==0)
{
rear=rear+1;
q[rear]=i;
visited[i]=1;
}
}
}
void main()
{
int n,a[10][10],i,j,source;
clrscr();
printf("Enter the number of nodes : ");
scanf("%d",&n);
printf("\n Enter the adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the source :");
scanf("%d",&source);
for(i=1;i<=n;i++)
visited[i]=0;
bfs(n,a,source);
for(i=1;i<=n;i++)
{
if(visited[i]==0)
printf("\n The node %d is not reacable",i);
else
printf("\n The node %d is reachable",i);
}
getch();
}
Output :
Enter the number of nodes : 4
void main()
{
int a[10][10],n,i,j;
clrscr();
printf("\n Enter the number of vertices: ");
scanf("%d",&n);
printf("\n Enter the cost matrix 0 -self loop & 999 –for no
edge\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
floyd(a,n);
printf("\n Shortest path matrix\n");
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
getch();
}
output :
Enter the number of vertices: 4
Enter the cost matrix 0 - self loop and 999 – for no edge
0 999 3 999
2 0 999 999
999 7 0 1
6 999 999 0
Output :
#include<stdio.h>
#include<conio.h>
int binomial(int n,int k)
{
int i,j,a[10][10];
for(i=0;i<=n;i++)
{
a[i][0]=1;
a[i][i]=1;
}
for(i=2;i<=n;i++)
{
for(j=1;j<=i-1;j++)
{
a[i][j] = a[i-1][j] + a[i-1][j-1];
}
}
return a[n][k];
}
void main()
{
int n,k,res;
clrscr();
printf("\n Enter the value for n : ");
scanf("%d",&n);
printf("\n Enter the value for k : ");
scanf("%d",&k);
res=binomial(n,k);
printf("\n The binomial co-efficient is %d\n",res);
getch();
}
output :
void main()
{
int a[10][10],n,i,j,m,source;
clrscr();
printf("\nEnter the number of vertices :");
scanf("%d",&n);
printf("\nEnter the cost matrix: 0 –self loop & 999 - no
edge\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the source :");
scanf("%d",&source);
m=prim(a,source,n);
printf("\n\n Cost = %d",m);
getch();
}
Output :
1 - > 3 sum = 10
1 - > 2 sum = 30
3 - > 4 sum = 70
Cost = 70
1 - > 4 sum=7
4 - > 5 sum=13
1 - > 3 sum=22
1 - > 2 sum=33
Cost=33
void main()
{
int i,j,source;
int n,a[10][10];
clrscr();
printf("\n Enter the number of nodes : ");
scanf("%d",&n);
printf("\n Enter the paths (0 - no edge and 1 - if present \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the source vertex :");
scanf("%d",&source);
for(i=1;i<=n;i++)
visited[i]=0;
dfs(n,a,source);
for(i=1;i<=n;i++)
{
if(visited[i]==0)
printf("\n The node %d is not reacable",i);
else
printf("\n The node %d is reachable",i);
}
getch();
}
output :
#include<stdio.h>
#include<conio.h>
for (k=1;k<=n;k++)
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
p[i][j]=p[i][j] || (p[i][k] && p[k][j]);
}
void main()
{
int a[10][10],i,j,n;
clrscr();
printf("\n Enter the number of vertices : ");
scanf("%d",&n);
printf("\n Enter the adjacency matrix \n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
warsh(a,n);
printf("\n The resultant Path matrix is \n");
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
getch();
}
output :
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 50
return 1;
}
void main()
{
int n; /* Number of queens. */
clrscr();
printf ("\n Enter the number of queens :");
scanf("%d",&n);
n_queens(n);
getch();
}
output :
- Q - -
- - - Q
Q - - -
- - Q -
- - Q -
Q - - -
- - - Q
- Q - -