ADA Lab Manual
ADA Lab Manual
Sort a given set of elements using the quick 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. the elements
can be read from a file or can be generated using the random number generator
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
void quicksort(int[],int,int);
int partition(int[],int,int);
void plotgraph(double time[],int ele[],int);
void main()
{
int i,n,a[20],ch=1;
double time[20],t;
int ele[20],k=0;
clock_t start,end;
clrscr();
while(ch)
{
start=clock();
printf("%d\t", a[i]);
}
quicksort(a,0,n-1);
end=clock();
t = (double)(end-start)/CLOCKS_PER_SEC;
time[k]=t;
ele[k]=n;
k++;
clrscr();
printf("\n Plot a graph");
printf("\nTime took ");
for(i=0;i<k;i++)
printf(" \n Elements = %d and Time = %lf",ele[i],time[i]);
getch();
clrscr();
plotgraph(time,ele,k);
}
int i,x1,x2,y1,y2;
int s=100;
char str[100];
clrscr();
printf("\t\t\tBAR CHART\n");
setfillstyle(SOLID_FILL,RED);
/* Display the data */
for(i=0;i<n;i++)
{
printf("\nElements = %d Time : %lf",ele[i],time[i]);
y2=y2 - int(time[i]*10);
x2=x2+50;
x1=x2+10;
bar(x1,y1,x2,y2);
y2 = 350;
}
/* Close the Graph */
getch();
closegraph();
}
if(low<high)
{
mid=partition(a,low,high);
quicksort(a,low,mid-1);
quicksort(a,mid+1,high);
}
}
key=a[low];
i=low+1;
j=high;
while(i<=j)
{
while(i<=high && key>=a[i])
i=i+1;
while(key<a[j])
j=j-1;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
k=a[j];
a[j]=a[low];
a[low]=k;
}
}
return j;
}
2. Implement merge sort algorithm to sort a given set of elements and determine the time
required to sort the elements. Repeat the experiment for the different values of n, the
number of elements in the list to be sorted. The elements can be read from a file or can be
generated using random generator.
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
void Merge(int a[], int low, int mid, int high)
{
int i,j,k,b[20];
i=low;
j=mid+1;
k=low;
while (i<=mid)
b[k++] = a[i++];
while (j<=high)
b[k++] = a[j++];
mid = (low+high)/2;
void main()
{
int n, a[2000],k;
clock_t st,et;
double ts;
int ch=1;
clrscr();
while(ch)
{
st=clock();
MergeSort(a, 1, n);
et=clock();
ts=(double)(et-st)/CLOCKS_PER_SEC;
5. Write a program to implement Dynamic programming algorithm for the 0/1 Knapsack
Problem
#include<stdio.h>
#include<conio.h>
int w[10],p[10],v[10][10],n,i,j,cap,x[10]={0};
int max(int i,int j)
{
return ((i>j)?i:j);
}
int knap(int i,int j)
{
int value;
if(v[i][j]<0)
{
if(j<w[i])
value=knap(i-1,j);
else
value=max(knap(i-1,j),p[i]+knap(i-1,j-w[i]));
v[i][j]=value;
}
return(v[i][j]);
}
void main()
{
int profit,count=0;
clrscr();
for(i=0;i<=n;i++)
for(j=0;j<=cap;j++)
if((i==0)||(j==0))
v[i][j]=0;
else
v[i][j]=-1;
profit=knap(n,cap);
i=n;
j=cap;
while(j!=0&&i!=0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1;
j=j-w[i];
i--;
}
else
i--;
}
if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
void main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
#include<stdio.h>
#include<conio.h>
int q[20],front=-1,rear=-1,a[20][20],vis[20];
void DFS(int,int);
int qdelete();
void add(int item);
void bfs(int s,int n);
void main()
{
int n,i,s,j;
clrscr();
for(i=1;i<=n;i++)
vis[i]=0;
printf("\n D F S\n");
DFS(s,n);
for(i=1;i<=n;i++)
vis[i]=0;
printf("\n B F S\n");
bfs(s,n);
getch();
}
add(s);
vis[s]=1;
p=qdelete();
if(p!=0)
printf("\t %d",p);
while(p!=0)
{
for(i=1;i<=n;i++)
if((a[p][i]!=0)&&(vis[i]==0))
{
add(i);
vis[i]=1;
}
p=qdelete();
if(p!=0)
printf("\t %d ",p);
}
for(i=1;i<=n;i++)
if(vis[i]==0)
bfs(i,n);
}
q[++rear]=item;
}
int qdelete()
{
int k;
if((front>rear)||(front==-1))
return(0);
else
{
k=q[front++];
return(k);
}
}
printf("\t%d",i);
vis[i]=1;
for(j=1;j<=n;j++)
if(!vis[j]&&a[i][j]==1)
DFS(j,n);
}
8. Write a program to find minimum and maximum value in an array using divide and
conquer.
#include<stdio.h>
#include<conio.h>
int max, min;
int a[100];
void maxmin(int i, int j)
{
int max1, min1, mid;
if(i==j)
{
max = min = a[i];
}
else
{
if(i == j-1)
{
if(a[i] <a[j])
{
max = a[j];
min = a[i];
}
else
{
max = a[i];
min = a[j];
}
}
else
{
mid = (i+j)/2;
maxmin(i, mid);
max1 = max;
min1 = min;
maxmin(mid+1, j);
if(max <max1)
max = max1;
if(min > min1)
min = min1;
}
}
}
void main ()
{
int i, num;
clrscr();
printf ("\nEnter the total number of numbers : ");
scanf ("%d",&num);
printf ("Enter the numbers : \n");
for (i=1;i<=num;i++)
scanf ("%d",&a[i]);
max = a[0];
min = a[0];
maxmin(1, num);
printf ("Minimum element in an array : %d\n", min);
printf ("Maximum element in an array : %d\n", max);
getch();
}
9. Write a test program to implement Divide and Conquer Strategy. Ex: Quick Sort
algorithm for sorting list of integers in ascending order.
#include<stdio.h>
#include<conio.h>
void quicksort(int[],int,int);
int partition(int[],int,int);
void main()
{
int i,n,a[20];
clrscr();
quicksort(a,0,n-1);
getch();
}
if(low<high)
{
mid=partition(a,low,high);
quicksort(a,low,mid-1);
quicksort(a,mid+1,high);
}
}
while(i<=j)
{
while(i<=high && key>=a[i])
i=i+1;
while(key<a[j])
j=j-1;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
k=a[j];
a[j]=a[low];
a[low]=k;
}
}
return j;
}
10. Write a program to implement Merge Sort Algorithm for sorting a list of integers in
ascending order.
#include<stdio.h>
#include<conio.h>
while (i<=mid)
b[k++] = a[i++];
while (j<=high)
b[k++] = a[j++];
mid = (low+high)/2;
void main()
{
int n, a[20],i;
clrscr();
MergeSort(a, 0, n-1);
getch();
}
PART B
3. Write a C program that accepts the vertices and edges for a graph and stores it as an adjacency
matrix
#include<stdio.h>
#include<conio.h>
#define MAX 100
int adj[MAX][MAX];
int n;
void main()
{
int max_edges,i,j,origin,destin;
int graph_type;
clrscr();
getch();
}
4. Write a program to implement a function print In-Degree, Out-Degree and display that
adjacency matrix
#include<stdio.h>
#include<conio.h>
#define MAX 100
void main()
{
int max_edges,i,j,origin,destin;
int graph_type;
int in_deg,out_deg,deg;
clrscr();
printf("1. Undirected graph\n 2. Directed graph");
printf("\nEnter your choice : ");
scanf("%d",&graph_type);
printf("\n");
}
if(graph_type==1)
{
printf("Vertex \t Degree ");
for ( i = 1 ; i <= n ; i++ )
{
deg = 0;
int board[20],count;
void main()
{
int n,i,j;
void queen(int row,int n);
clrscr();
queen(1,n);
printf("\n\nSolution %d:\n\n",++count);
for(i=1;i<=n;++i)
printf("\t%d",i);
for(i=1;i<=n;++i)
{
printf("\n%d",i);
for(j=1;j<=n;++j) //for nxn board
{
if(board[i]==j)
printf("\tQ"); //queen at i,j position
else
printf("\t-"); //empty slot
}
}
}
/*funtion to check conflicts
If no conflict for desired postion returns 1 otherwise returns 0*/
int place(int row,int column)
{
int i;
for(i=1;i<=row-1;++i)
{
//checking column and digonal conflicts
if(board[i]==column)
return 0;
else
if(abs(board[i]-column)==abs(i-row))
return 0;
}
for(column=1;column<=n;++column)
{
if(place(row,column))
{
board[row]=column; //no conflicts so place queen
if(row==n) //dead end
print(n); //printing the board configuration
else //try queen with next position
queen(row+1,n);
}
}
}
6. Write a program to implement backtracking algorithm for the sum of subsets problem.
#include<stdio.h>
#include<conio.h>
int s[10],x[10],d;
void sumofsub(int,int,int);
void main()
{
int n,sum=0;
int i;
clrscr ();
if(sum<d || s[1]>d)
printf("\n No subset possible : ");
else
sumofsub(0,1,sum);
getch();
}
x[k]=1;
if((m + s[k]) == d)
{
printf("Subset:");
for(i=1;i<=k;i++)
if(x[i] == 1)
printf("\t%d",s[i]);
printf("\n");
}
else
if(m+s[k]+s[k+1]<=d)
sumofsub(m+s[k],k+1,r-s[k]);
if ((m+r-s[k]>=d) && (m+s[k+1]<=d))
{
x[k] = 0;
sumofsub (m,k+1,r-s[k]);
}
}
7. Write a program to implement greedy algorithm for job sequencing with deadlines
#include<stdio.h>
#include<conio.h>
#define MAX 100
return y;
}
void main()
{
int i, j;
clrscr();
//temp
Job temp;
//number of jobs
int n = 5;
jobSequencingWithDeadline(jobs, n);
getch();
}
//required jobs
printf("\nRequired Jobs: ");
for(i = 1; i <= dmax; i++) {
printf("%s", jobs[timeslot[i]].id);
//required profit
maxprofit = 0;
for(i = 1; i <= dmax; i++) {
maxprofit += jobs[timeslot[i]].profit;
}
printf("\nMax Profit: %d\n", maxprofit);
}
8. Write a program to implement dynamic programming algorithm for the optimal Binary
search tree problem
#include <stdio.h>
#include<conio.h>
#define INT_MAX 999
void main() {
clrscr();
int keys[20];
int freq[20];
int n,i;
getch();
}
9. Write a program that implements Prim’s algorithm to generate minimum cost spanning
tree.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
clrscr();
if(cost[i][j]==0)
cost[i][j]=999;
}
}
visited[1]=1;
printf("\n");
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)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
}
if(visited[u]==0 || visited[v]==0)
{
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}
10. Write a program that implements Kruskal’s algorithm to generate minimum cost
spanning tree.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
{
clrscr();
printf("\n\tImplementation of Kruskal's algorithm\n");
if(uni(u,v))
{
printf("%d edge (%d,%d) =%d\n",ne++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum cost = %d\n",mincost);
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;
}