Faculty of Degree Engineering – 083
Department of Information Technology - 016
SEMESTER: 5
LAB MANUAL
Analysis And Design of Algorithms
3150703
Name:
Enrollment No:
Batch:
DEPARTMENT OF INFORMATION TECHNOLOGY
[Link] TECHNICAL CAMPUS
Faculty of Degree Engineering – 083
Department of Information Technology - 016
CERTIFICATE
Roll No.:- Enrollment No.:-
This is to certify that the practical work satisfactorily carried out
and hence recorded in this journal is the confide work of Mr.
/Miss_________________________________ student of
Information Technology Semester 5 th in the Analysis and design of
algorithm (3150703) Laboratory of Dr. Subhash Technical Campus
during the academic year 2023 -24.
Submission Date: ……………
Subject in Charge HOD
Examiner
Faculty of Degree Engineering – 083
Department of Information Technology - 016
INDEX
SR. NO. AIM PAGE NO DATE SIGN
Write a program to implementation of
1-2
1 Bubble Sort.
Write a program to implementation of
3-4
2 Selection Sort.
Write a program to implementation of
5-6
3 insertion Sort.
Write a program to implementation of
7-9
4 merge Sort.
Write a program to implementation of
10-13
5 Quick Sort.
Write a program to insert values of
14-16
6 Binary Search Tree.
Write a program to implementation of
17-19
7 max-Heap Sort
Write a program for factorial using
20-21
8 Recursive Method.
Implementation of a knapsack problem
22-24
9 using dynamic programming
Implementation of chain matrix
multiplication using dynamic 25-27
10
programming
Implementation of making change
28-29
11 problem using dynamic programming.
Faculty of Degree Engineering – 083
Department of Information Technology - 016
Implementation of a knapsack problem
30-32
12 using greedy algorithm.
Write a program for Depth first search. 33-36
13
Write a program for Breath first
37-40
14 search.
Write a program to implementation of
41-43
15 Prim’s Algorithm.
Write a program to implementation of
44-46
16 Kruskal’s Algorithm.
Implementation LCS problem. 47-50
17
ADA 3150703
1. Write a program to implementation of Bubble Sort.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[5],t;
clrscr();
for(i=1;i<=5;i++)
{
printf("Enter no of bubble sort");
scanf("%d",&a[i]);
}
for(i=1;i<=5;i++)
{
for(j=i+1;j<=5;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=1;i<=5;i++)
{
printf("\n%d",a[i]);
}
getch();
}
[Link] TECHNICAL CAMPUS Page 1
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 2
ADA 3150703
2. Write a program to implementation of Selection Sort.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[5],t,s;
clrscr();
for(i=1;i<=5;i++)
{
printf("Enter no for selection sort ");
scanf("%d",&a[i]);
}
for(i=1;i<5;i++)
{
for(j=i+1;j<=5;j++)
{
s=i;
if(a[j]<a[s])
{
s=j;
}
t=a[i];
a[i]=a[s];
a[s]=t;
}
}
for(i=1;i<=5;i++)
{
printf("\n%d",a[i]);
}
getch();
}
[Link] TECHNICAL CAMPUS Page 3
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 4
ADA 3150703
3. Write a program to implementation of Insertion Sort.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[5],x;
clrscr();
for(i=0;i<5;i++)
{
printf("Enter no for insertion sort ");
scanf("%d",&a[i]);
}
for(i=1;i<=5;i++)
{
x=a[i];
j=i-1;
while(j>=0 && a[j]>x)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=x;
}
for(i=1;i<=5;i++)
{
printf("\n%d",a[i]);
}
getch();
}
[Link] TECHNICAL CAMPUS Page 5
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 6
ADA 3150703
4. Write a program to implementation of Merge Sort.
#include<stdio.h>
void mergeSort(int[],int,int);
void merge(int[],int,int,int);
void main ()
{
int a[10]= {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
int i;
mergeSort(a,0,9);
printf("printing the sorted elements");
for(i=0;i<10;i++)
{
printf("\n%d\n",a[i]);
}
}
void mergeSort(int a[], int beg, int end)
{
int mid;
if(beg<end)
{
mid = (beg+end)/2;
mergeSort(a,beg,mid);
mergeSort(a,mid+1,end);
merge(a,beg,mid,end);
}
}
void merge(int a[], int beg, int mid, int end)
{
int i=beg,j=mid+1,k,index = beg;
int temp[10];
while(i<=mid && j<=end)
{
if(a[i]<a[j])
{
temp[index] = a[i];
i = i+1;
}
else
{
temp[index] = a[j];
j = j+1;
[Link] TECHNICAL CAMPUS Page 7
ADA 3150703
}
index++;
}
if(i>mid)
{
while(j<=end)
{
temp[index] = a[j];
index++;
j++;
}
}
else
{
while(i<=mid)
{
temp[index] = a[i];
index++;
i++;
}
}
k = beg;
while(k<index)
{
a[k]=temp[k];
k++;
}
}
[Link] TECHNICAL CAMPUS Page 8
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 9
ADA 3150703
5. Write a program to implementation of Quick Sort.
#include<stdio.h>
#include<conio.h>
void main()
int arr[20], n, i;
clrscr();
printf("Enter the size of the array for Quick Sort\n");
scanf("%d", &n);
printf("Enter the elements to be sorted\n");
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
quicksort(arr, 0, n-1);
printf("Sorted array\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
getch();
int quicksort(int *arr, int low, int high)
int pivot, i, j, temp;
if(low < high)
[Link] TECHNICAL CAMPUS Page 10
ADA 3150703
pivot = low;
i = low;
j = high;
while(i < j){
while(arr[i] <= arr[pivot] && i <= high)
i++;
while(arr[j] > arr[pivot] && j >= low)
j--;
if(i < j)
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
temp = arr[j];
arr[j] = arr[pivot];
arr[pivot] = temp;
quicksort(arr, low, j-1);
[Link] TECHNICAL CAMPUS Page 11
ADA 3150703
quicksort(arr, j+1, high);
[Link] TECHNICAL CAMPUS Page 12
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 13
ADA 3150703
6. Write a program to insert values of Binary Search Tree.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* createNode(value){
struct node* newNode = malloc(sizeof(struct node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* insert(struct node* root, int data)
{
if (root == NULL) return createNode(data);
if (data < root->data)
root->left = insert(root->left, data);
else if (data > root->data)
root->right = insert(root->right, data);
return root;
}
void inorder(struct node* root){
if(root == NULL) return;
inorder(root->left);
printf("%d ->", root->data);
inorder(root->right);
}
int main(){
[Link] TECHNICAL CAMPUS Page 14
ADA 3150703
struct node *root = NULL;
root = insert(root, 8);
insert(root, 3);
insert(root, 1);
insert(root, 6);
insert(root, 7);
insert(root, 10);
insert(root, 14);
insert(root, 4);
inorder(root);
}
[Link] TECHNICAL CAMPUS Page 15
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 16
ADA 3150703
7. Write a program to implementation of max-Heap Sort
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
void maxheapify(int *, int, int);
int* buildmaxheap(int *, int);
void main()
{
int i, t, n;
int *a = calloc(MAX, sizeof(int));
int *m = calloc(MAX, sizeof(int));
printf("Enter no of elements in the array\n");
scanf("%d", &n);
printf("Enter the array\n");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
m = buildmaxheap(a, n);
printf("The heap is\n");
for (t = 0; t < n; t++) {
printf("%d\n", m[t]);
}
}
int* buildmaxheap(int a[], int n)
{
int heapsize = n;
int j;
for (j = n/2; j >= 0; j--) {
maxheapify(a, j, heapsize);
}
return a;
}
void maxheapify(int a[], int i, int heapsize)
{
int temp, largest, left, right, k;
left = (2*i+1);
right = ((2*i)+2);
if (left >= heapsize)
return;
else {
if (left < (heapsize) && a[left] > a[i])
[Link] TECHNICAL CAMPUS Page 17
ADA 3150703
largest = left;
else
largest = i;
if (right < (heapsize) && a[right] > a[largest])
largest = right;
if (largest != i) {
temp = a[i];
a[i] = a[largest];
a[largest] = temp;
maxheapify(a, largest, heapsize);
}
}
}
[Link] TECHNICAL CAMPUS Page 18
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 19
ADA 3150703
8. Write a program for factorial using Recursive Method.
#include<stdio.h>
#include<conio.h>
#include<string.h>
long long int fact[10000];
//char f[10000];
int i,n;
int factorial(int k);
void main()
{
clrscr();
fact[0]=1;
printf("Enter Any Number Between 1 To 10 for factorial recursiove method:-
");
scanf("%d",&n);
factorial(n);
printf("fact=%lli",fact[n]);
getch();
}
int factorial(int k)
{
n = k;
for(i=1;i<=n;i++)
if(fact[i]==1)
{
return 0;
}
else
{
fact[i] = fact[i-1]*i;
}
return fact[i];
}
[Link] TECHNICAL CAMPUS Page 20
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 21
ADA 3150703
9. Implementation of a knapsack problem using dynamic programming.
#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;
printf("\nEnter the number of elements\n"); scanf("%d",&n);
printf("Enter the profit and weights of the elements\n");
for(i=1;i<=n;i++)
{
printf("For item no %d\n",i);
scanf("%d%d",&p[i],&w[i]);
}
printf("\nEnter the capacity \n");
scanf("%d",&cap);
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;
[Link] TECHNICAL CAMPUS Page 22
ADA 3150703
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--;
}
printf("Items included are\n");
printf("[Link]\tweight\tprofit\n");
for(i=1;i<=n;i++)
if(x[i])
printf("%d\t%d\t%d\n",++count,w[i],p[i]); printf("Total profit = %d\n",profit);
getch();
[Link] TECHNICAL CAMPUS Page 23
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 24
ADA 3150703
10. Implementation of chain matrix multiplication using dynamic programming
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
int matrixChainOrder(int p[], int, int);
void main()
{
int n, i;
printf("enter the no of matrices\n");
scanf("%d",&n);
int arr[n++];
printf("enter the dimensions\n");
for(i=0;i<n;i++)
{
printf("enter the d %d : ",i);
scanf("%d",&arr[i]);
}
printf("total no. of multilication are %d",matrixChainOrder(arr, 1, n-1));
}
int matrixChainOrder(int p[], int i, int j)
{
if(i == j)
{
return 0;
}
int k, minimum, count;
minimum = INT_MAX;
[Link] TECHNICAL CAMPUS Page 25
ADA 3150703
for(k=i;k<j;k++)
{
count = matrixChainOrder(p, i, k) + matrixChainOrder(p, k+1, j) + p[i-
1]*p[k]*p[j];
if(count < minimum)
{
minimum = count;
}
}
return minimum;
}
[Link] TECHNICAL CAMPUS Page 26
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 27
ADA 3150703
11. Implementation of making a change problem using dynamic programming
#include <stdio.h>
int coins( int S[], int m, int n ) {
int i, j, x, y;
int table[n+1][m];
for (i=0; i<m; i++)
table[0][i] = 1;
for (i = 1; i < n+1; i++) {
for (j = 0; j < m; j++) {
x = (i-S[j] >= 0)? table[i - S[j]][j]: 0;
y = (j >= 1)? table[i][j-1]: 0;
table[i][j] = x + y;
}
}
return table[n][m-1];
}
int main() {
int arr[] = {1, 4, 6};
int m = sizeof(arr)/sizeof(arr[0]);
int n = 8;
printf("The total number of combinations of coins that sum up to %d",n);
printf(" is %d ", coins(arr, m, n));
return 0;
}
[Link] TECHNICAL CAMPUS Page 28
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 29
ADA 3150703
12. Implementation of a knapsack problem using greedy algorithm
#include<stdio.h>
#include<stdlib.h>
int knapsack(int, int[], int[], int);
int max(int, int);
void main()
{
int n, W, i;
int weight[10], value[10];
printf("enter the no. of items\n");
scanf("%d",&n);
printf("enter the value and weight\n");
for(i=0;i<n;i++)
{
scanf("%d%d",&value[i],&weight[i]);
}
printf("enter the capacity of knapsack\n");
scanf("%d",&W);
printf("%d",knapsack(W, weight, value, n));
int knapsack(int W, int weight[], int value[], int n)
{
if(n==0 || W==0)
{
return 0;
}
else if(weight[n-1] > W)
[Link] TECHNICAL CAMPUS Page 30
ADA 3150703
{
return knapsack(W, weight, value, n-1);
}
else
{
return max(value[n-1] + knapsack(W-weight[n-1], weight, value, n-1),
knapsack(W, weight, value, n-1));
}
}
int max(int a, int b)
{
if(a > b)
{
return a;
}
else
{
return b;
}
}
[Link] TECHNICAL CAMPUS Page 31
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 32
ADA 3150703
13. Write a program for Depth first search
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
struct node *next;
int vertex;
}node;
node *G[20];
//heads of linked list
int visited[20];
int n;
void read_graph();
//create adjacency list
void insert(int,int);
//insert an edge (vi,vj) in te adjacency list
void DFS(int);
void main()
{
int i;
read_graph();
//initialised visited to 0
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
node *p;
printf("\n%d",i);
p=G[i];
visited[i]=1;
while(p!=NULL)
{
i=p->vertex;
[Link] TECHNICAL CAMPUS Page 33
ADA 3150703
if(!visited[i])
DFS(i);
p=p->next;
}
}
void read_graph()
{
int i,vi,vj,no_of_edges;
printf("Enter number of vertices:");
scanf("%d",&n);
//initialise G[] with a null
for(i=0;i<n;i++)
{
G[i]=NULL;
//read edges and insert them in G[]
printf("Enter number of edges:");
scanf("%d",&no_of_edges);
for(i=0;i<no_of_edges;i++)
{
printf("Enter an edge(u,v):");
scanf("%d%d",&vi,&vj);
insert(vi,vj);
}
}
}
void insert(int vi,int vj)
{
node *p,*q;
//acquire memory for the new node
q=(node*)malloc(sizeof(node));
q->vertex=vj;
q->next=NULL;
//insert the node in the linked list number vi
[Link] TECHNICAL CAMPUS Page 34
ADA 3150703
if(G[vi]==NULL)
G[vi]=q;
else
{
//go to end of the linked list
p=G[vi];
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}
[Link] TECHNICAL CAMPUS Page 35
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 36
ADA 3150703
14. Write a program for Breath first search
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
#define initial 1
#define waiting 2
#define visited 3
int n;
int adj[MAX][MAX];
int state[MAX];
void create_graph();
void BF_Traversal();
void BFS(int v);
int queue[MAX], front = -1,rear = -1;
void insert_queue(int vertex);
int delete_queue();
int isEmpty_queue();
int main()
{
create_graph();
BF_Traversal();
return 0;
}
void BF_Traversal()
{
int v;
for(v=0; v<n; v++)
state[v] = initial;
printf("Enter Start Vertex for BFS: \n");
scanf("%d", &v);
BFS(v);
}
[Link] TECHNICAL CAMPUS Page 37
ADA 3150703
void BFS(int v)
{
int i;
insert_queue(v);
state[v] = waiting;
while(!isEmpty_queue())
{
v = delete_queue( );
printf("%d ",v);
state[v] = visited;
for(i=0; i<n; i++)
{
if(adj[v][i] == 1 && state[i] == initial)
{
insert_queue(i);
state[i] = waiting;
}
}
}
printf("\n");
}
void insert_queue(int vertex)
{
if(rear == MAX-1)
printf("Queue Overflow\n");
else
{
if(front == -1)
front = 0;
rear = rear+1;
queue[rear] = vertex ;
}
}
int isEmpty_queue()
{
if(front == -1 || front > rear)
return 1;
else
[Link] TECHNICAL CAMPUS Page 38
ADA 3150703
return 0;
}
int delete_queue()
{
int delete_item;
if(front == -1 || front > rear)
{
printf("Queue Underflow\n");
exit(1);
}
delete_item = queue[front];
front = front+1;
return delete_item;
}
void create_graph()
{
int count,max_edge,origin,destin;
printf("Enter number of vertices : ");
scanf("%d",&n);
max_edge = n*(n-1);
for(count=1; count<=max_edge; count++)
{
printf("Enter edge %d( -1 -1 to quit ) : ",count);
scanf("%d %d",&origin,&destin);
if((origin == -1) && (destin == -1))
break;
if(origin>=n || destin>=n || origin<0 || destin<0)
{
printf("Invalid edge!\n");
count--;
}
else
{
adj[origin][destin] = 1;
}
}
}
[Link] TECHNICAL CAMPUS Page 39
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 40
ADA 3150703
15. Write a program to implementation of Prim’s Algorithm.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n, ne = 1, i, j;
int c[10][10], traversed[10] = {0};
int minimum, minc = 0, a, b, x, y;
printf("enter no. of vertices\n");
scanf("%d",&n);
printf("enter matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&c[i][j]);
if(c[i][j] == 0)
{
c[i][j] = 999;
}
}
}
traversed[1] = 1;
while(ne < n)
{
minimum = 999;
for(i=1;i<=n;i++)
{
[Link] TECHNICAL CAMPUS Page 41
ADA 3150703
for(j=1;j<=n;j++)
{
if(c[i][j] < minimum)
{
if(traversed[i] != 0)
{
minimum = c[i][j];
a = x = i;
b = y = j;
}
}
}
}
if(traversed[x] == 0 || traversed[y] == 0)
{
printf("%d edge(%d, %d) = %d\n",ne++, a, b, minimum);
minc = minc + minimum;
traversed[b] = 1;
}
c[a][b] = c[b][a] = 999;
printf("\n total cost = %d",minc);
return 0;
}
[Link] TECHNICAL CAMPUS Page 42
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 43
ADA 3150703
16. Write a program to implementation of kruskal’s Algorithm.
#include<stdio.h>
#include<conio.h>
int i,j,k,x,y,u,v,n,adj=1,min,minweight=0;
int weight[10][10],parent[10];
int find(int);
int uni(int,int);
int main()
{
printf("\nEnter the no. of vertices in graph:");
scanf("%d",&n);
printf("\nEnter the weight adjacency matrix:\n ");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&weight[i][j]);
if(weight[i][j]==0)
{
weight[i][j]=999;
}
}
}
printf("The edges of minimum weight spanning tree are\n");
while(adj<n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(weight[i][j]<min)
{
min=weight[i][j];
x=u=i;
y=v=j;
}
}
}
u=find(u);
[Link] TECHNICAL CAMPUS Page 44
ADA 3150703
v=find(v);
if(uni(u,v))
{
printf("%d Edge(%d-%d)=%d\n",adj++,x,y,min);
minweight=minweight+min;
}
weight[x][y]=weight[x][y]=999;
}
printf("\n\tminimum weight=%d",minweight);
return 0;
}
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;
}
[Link] TECHNICAL CAMPUS Page 45
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 46
ADA 3150703
17. Implement LCS problem.
#include<stdlib.h>
#include<string.h>
int LCS();
int display(int, int);
int i, j, p, q;
char G[20], H[20], b[20][20], c[20][20];
void main()
{
printf("enter the first subsequence\n");
gets(G);
printf("enter the second subsequence\n");
gets(H);
printf("LCS is : ");
LCS();
display(p, q);
}
int LCS()
{
p = strlen(G);
q = strlen(H);
for(i=0;i<=p;i++)
{
c[i][0] = 0;
[Link] TECHNICAL CAMPUS Page 47
ADA 3150703
}
for(i=0;i<=q;i++)
{
c[0][i] = 0;
}
for(i=1;i<=p;i++)
{
for(j=1;j<=q;j++)
{
if(G[i-1] == H[j-1])
{
c[i][j] = c[i-1][j-1] + 1;
b[i][j] = 'c';
}
else if(c[i-1][j] >= c[i][j-1])
{
c[i][j] = c[i-1][j];
b[i][j] = 'u';
else
{
c[i][j] = c[i][j-1];
b[i][j] = 'l';
}
}
}
return 0;
}
int display(int i, int j)
{
if(i==0 || j==0)
[Link] TECHNICAL CAMPUS Page 48
ADA 3150703
{
return;
}
if(b[i][j] == 'c')
{
display(i-1, j-1);
printf("%c",G[i-1]);
else if(b[i][j] == 'u')
{
display(i-1, j);
}
else
{
display(i, j-1);
}
return 0;
}
[Link] TECHNICAL CAMPUS Page 49
ADA 3150703
Signature:
Date: _________________________
[Link] TECHNICAL CAMPUS Page 50