AA Lab Manual Session 2022-23
AA Lab Manual Session 2022-23
1 VISION/MISSION 4
2. PEOs 4
3. POs 5
4. COs 6
6. SYLLABUS 7
7. BOOKS 8
8. INSTRUCTIONAL METHODS 8
9. LEARNING MATERIALS 9
LIST OF EXPERIMENTS
Exp:1 Sort a given set of elements using the Quicksort method and determine 10
the time required to sort the elements. The elements can be read from a
file or can be generated using the random number generator.
2
Exp:2 Implement Merge Sort algorithm to sort a given set of elements and 12
determine the time required to sort the elements. The elements can be
read from a file or can be generated using the random number generator.
Exp:3 Implement Bubble Sort algorithm to sort a given set of elements and 14
determine the time required to sort the elements. The elements can be
read from a file or can be generated using the random number generator.
Exp:4 Implement Linear Search algorithm to search a value from a given set of 15
elements. The elements can be read from a file or can be generated using
the random number generator.
Exp:5 Implement Binary Search algorithm to search a value from a given set of 17
elements. The elements can be read from a file or can be generated using
the random number generator.
Exp:6 Compute two 2 by 2 matrix multiplication to find multiplication matrix 19
using Strassen’s Matrix Multiplication.
Exp:7 Implement Knapsack problem using Greedy Method. 21
Exp:8 Find Minimum Cost Spanning Tree of a given undirected graph using 25
Prim’s algorithm.
Exp:9 Find Minimum Cost Spanning Tree of a given undirected graph using 30
Kruskal's algorithm.
Exp:14 Implement Naive String Matching Algorithm to match a pattern with the 41
string.
3
1. VISION & MISSION
VISION:
To become renowned Centre of excellence in computer science and engineering and make
competent engineers & professionals with high ethical values prepared for lifelong learning.
MISSION:
M1: To impart outcome based education for emerging technologies in the field of computer
science and engineering.
M2: To provide opportunities for interaction between academia and industry.
M3: To provide platform for lifelong learning by accepting the change in technologies
M4: To develop aptitude of fulfilling social responsibilities
PEO5: To prepare students to excel in Industry and Higher education by Educating Students
along with High moral values and Knowledge in Computer Science & Engineering.
4
3. PROGRAM OUTCOMES (POs)
2. Problem analysis: Identify, formulate, research literature, and analyse complex Computer
Science and Engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modelling to complex Computer
Science Engineering activities with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant
to the professional Computer Science and Engineering practice.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the Computer Science and Engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader
in diverse teams, and in multidisciplinary settings in Computer Science and Engineering.
5
11. Project management and finance: Demonstrate knowledge and understanding of the
Computer Science and Engineering and management principles and apply these to one’s own
work, as a member and leader in a team, to manage projects and in multidisciplinary
environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage
in independent and life-long learning in the broadest context of technological change in
Computer Science and Engineering.
6
6. SYLLABUS
Branch: Computer Science & Engineering Examination Time = Two (2) Hours
Objectives:
At the end of the semester, the students should have clearly understood and implemented the
following:
List of exercises:
1. Sort a given set of elements using the Quick Sort method and determine the time
required to sort the elements. The elements can be read from a file or can be generated
using the random number generator.
2. Implement Merge Sort algorithm to sort a given set of elements and determine the
time required to sort the elements. The elements can be read from a file or can be
generated using the random number generator.
3. Implement Bubble Sort algorithm to sort a given set of elements and determine the
time required to sort the elements. The elements can be read from a file or can be
generated using the random number generator.
4. Implement Linear Search algorithm to search a value from a given set of elements.
The elements can be read from a file or can be generated using the random number
generator.
5. Implement Binary Search algorithm to search a value from a given set of elements.
The elements can be read from a file or can be generated using the random number
generator.
6. Compute two 2 by 2 matrix multiplication to find multiplication matrix using
Strassen’s Matrix Multiplication.
7. Implement Knapsack problem using Greedy Method.
7
8. Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s
algorithm.
9. Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal's
algorithm.
10. Implement 0/1 Knapsack problem using Dynamic Programming.
11. Implement Longest Common Subsequence problem using Dynamic Programming.
12. Implement Matrix Chain Multiplication problem using Dynamic Programming.
13. Implement N Queen's problem using Back Tracking.
14. Implement Naive String Matching Algorithm to match a match a pattern with the
string.
Outcomes:
At the end of the semester, the students should have clearly understood and implemented the
following:
• Performed analysis on various algorithms.
• Performed & implemented programs on dynamic and greedy approaches.
7. BOOKS
3. Aho A.V , J.D Ullman: Design and analysis of Algorithms, Addison Wesley
8
8. INSTRUCTIONAL METHODS
• Coding
• Problem solving
9. LEARNING MATERIALS
2. Discussion on website
9
11. INSTRUCTIONS OF LAB
DO’s
DON’Ts
10
12. INSTRUCTIONS FOR STUDENTS
● All the students are supposed to prepare the theory regarding the next program.
● Students are supposed to bring the practical file and the lab copy.
● Previous programs should be written in the practical file.
● Any student not following these instructions will be denied entry in the lab.
11
LIST OF EXPERIMENTS
Experiment-1
Aim - Sort a given set of elements using the Quick Sort method and determine the time
required to sort the elements. The elements can be read from a file or can be generated
using the random number generator.
while(pivot>a[i]&&i<=high)
i++;
while(pivot<a[j]&&j>=low)
12
j--;
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
else
break;
}
a[low]=a[j];
a[j]=pivot;
quicksort(a,low,j-1);
quicksort(a,j+1,high);
}
}
Experiment – 2
Aim - Implement Merge Sort algorithm to sort a given set of elements and determine
the time required to sort the elements. The elements can be read from a file or can be
generated using the random number generator.
#include<stdio.h>
#include<conio.h>
void disp();
void mergesort(int,int,int);
void msortdiv(int,int);
int a[50],n;
void main()
{
int i;
clrscr();
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter elements for an array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nBefore Sorting the elements are:");
disp();
msortdiv(0,n-1);
printf("\nAfter Sorting the elements are:");
disp();
13
getch( );
}
void disp( )
{
int i;
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
Experiment – 3
14
Aim - Implement Bubble Sort algorithm to sort a given set of elements and determine
the time required to sort the elements. The elements can be read from a file or can be
generated using the random number generator.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,temp,j,arr[25];
clrscr();
printf("Enter the number of elements in the Array: ");
scanf("%d",&n);
printf("\nEnter the elements:\n\n");
for(i=0 ; i<n ; i++)
{
printf(" Array[%d] = ",i);
scanf("%d",&arr[i]);
}
for(i=0 ; i<n ; i++)
{
for(j=0 ; j<n-i-1 ; j++)
{
if(arr[j]>arr[j+1]) //Swapping Condition is Checked
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\nThe Sorted Array is:\n\n");
for(i=0 ; i<n ; i++)
{
printf(" %d",arr[i]);
}
getch();
}
Experiment – 4
Aim - Implement Linear Search algorithm to search a value from a given set of
elements. The elements can be read from a file or can be generated using the random
number generator.
15
//Write a C program that searches a value in a stored array using linear search.
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[20], pos = -1, n, k, i;
clrscr( );
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter elements for an array:");
for(i=0; i<n ;i++)
scanf("%d",&a[i]);
printf("\nEnter the element to be searched:");
scanf("%d",&k);
pos=linear(a,n,k);
if(pos != -1)
printf("\n Search successful element found at position %d",pos);
else
printf("\n Search unsuccessful, element not found");
getch( );
}
int linear(int a[ ],int n,int k)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==k)
return(i);
}
return -1;
}
////////////////////////////////////////////////////////////////////
//Write a C program that searches a value in a stored array using recursive linear search.
/* recursive program for Linear Search*/
#include<stdio.h>
#include<conio.h>
void main( )
{
16
int a[20],pos=-1,n,k,i;
clrscr();
printf("\nEnter n value:");
scanf("%d",&n);
printf("\nEnter elements for an array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter the element to be searched:");
scanf("%d",&k);
pos=linear(a,n,k);
if(pos!=-1)
printf("\n Search successful, Element found at Position %d",pos);
else
printf("Search unsuccessful, element not found ");
getch( );
}
Experiment – 5
Aim - Implement Binary Search algorithm to search a value from a given set of
elements. The elements can be read from a file or can be generated using the random
number generator.
//Write a C program that searches a value in a stored array using non recursive binary search.
#include<stdio.h>
#include<conio.h>
17
void main( )
{
int a[20],pos,n,k,i;
clrscr();
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter elements for an array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the key value:");
scanf("%d",&k);
pos=bsearch(a,n,k);
if(pos!= -1)
printf("Search successful, element found at position %d",pos);
else
printf("Search unsuccessful, element not found");
getch( );
}
///////////////////////////////////////////////////////////////////////////////////////
// Write a program for recursive binary search to find the given element within array
#include<stdio.h>
#include<conio.h>
18
void main( )
{
int a[20],pos,n,k,i,lb,ub;
clrscr( );
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter elements for an array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the key value:");
scanf("%d",&k);
lb=0;
ub=n-1;
pos=bsearch(a,k,lb,ub);
if(pos!=-1)
printf("Search successful, element found at position %d",pos);
else
printf("Search unsuccessful, element not found");
getch( );
}
19
Experiment – 6
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2],i,j;
int m1,m2,m3,m4,m5,m6,m7;
clrscr();
printf("Enter the 4 elements of first matrix: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
20
m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
c[0][0]=m1+m4-m5+m7;
c[0][1]=m3+m5;
c[1][0]=m2+m4;
c[1][1]=m1-m2+m3+m6;
getch();
}
Experiment – 7
# include<stdio.h>
#include<conio.h>
21
tp = tp + (x[i] * profit[i]);
printf("\nThe result vector is:- ");
for (i = 0; i < n; i++)
printf("%.2f\t", x[i]);
printf("\nMaximum profit is:- %.2f", tp);
}
void main()
{
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
clrscr();
printf("\nEnter the no. of objects:- ");
scanf("%d", &num);
printf("\nEnter the profits and wts of %d object ",num);
for (i = 0; i < num; i++)
{
printf("\nEnter the profits and wts of %d object",i+1);
scanf("%f %f", &profit[i], &weight[i]);
}
printf("\nEnter the capacityacity of knapsack:- ");
scanf("%f", &capacity);
for (i = 0; i < num; i++)
{
ratio[i] = profit[i] / weight[i];
}
for (i = 0; i < num; i++)
{
for (j = i + 1; j < num; j++)
{
if (ratio[i] < ratio[j])
{
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
22
knapsack(num, weight, profit, capacity);
getch();
}
Experiment – 8
Aim - 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,n,i,j,ne=1;
int visited[10]={0},min,idx,mincost=0,cost[10][10];
void main()
{
clrscr();
printf("\n 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",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
{
idx=i;
printf("the value of i is %d\t",i);
}
visited[idx]=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;
23
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();
}
Experiment – 9
Aim - Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal's
algorithm.
#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\n\tImplementation of Kruskal's algorithm\n\n");
printf("\nEnter the no. of vertices\n");
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]);
if(cost[i][j]==0)
cost[i][j]=999;
24
}
}
printf("\nThe edges of Minimum Cost Spanning Tree are\n\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;
}
}
}
printf("The min value is %d",min);
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("\n%d edge (%d,%d) =%d\n",ne++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999; //remove the minimum edge
}
printf("\n\tMinimum cost = %d\n",mincost);
getch();
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
25
Experiment – 10
#include <conio.h>
#include <stdio.h>
void knapsack01(int v[],int w[],int n,int capacity)
{
int x=0,b[100][100],i=0;
for(x=0;x<=capacity;x++)
b[0][x]=0;
for(i=1;i<=n;i++)
b[i][0]=0;
for(i=1;i<=n;i++)
{
for(x=1;x<=capacity;x++)
{
if(w[i]<=x)
{
if( (v[i]+b[i-1][x-w[i]] ) > b[i-1][x] )
b[i][x]= (v[i]+b[i-1][x-w[i]] ) ;
else
b[i][x]=b[i-1][x];
}
else
b[i][x]=b[i-1][x];
}
}
printf("the matix is\n");
for(i=0;i<=n;i++)
{
for(x=0;x<=capacity;x++)
printf("\t%d",b[i][x]);
printf("\n");
}
printf("the benifits is \n%d",b[n][capacity]);
printf("\nthe selected items is\n");
i=n,x=capacity;
while(i>0 && x>0)
{
if(b[i][x]!=b[i-1][x])
{
printf("\t%d",i);
b[i][x]=b[i-1][x-w[i]];
26
x=x-w[i];
i=i-1;
}
else
{
b[i][x]=b[i-1][x];
i=i-1;
}
}
}
void main()
{
int i,n,capacity,v[100],w[100];
clrscr();
printf("enter the total capacity of knapsack \n");
scanf("%d",&capacity);
printf("enter the no of items\n");
scanf("%d",&n);
//value of each item's
printf("enter the weight and value of items\n");
for(i=1;i<=n;i++)
scanf("%d %d",&w[i],&v[i]);
knapsack01(v,w,n,capacity);
getch();
}
Experiment – 11
#include<stdio.h>
#include<conio.h>
#include<string.h>
int i,j,m,n,a,c[20][20];
char x[15],y[15],b[20][20];
void print_lcs(int i,int j)
{
if(i==0 || j==0)
return;
if(b[i][j]=='c')
{
print_lcs(i-1,j-1);
printf(" %c",x[i-1]);
27
}
else if(b[i][j]=='u')
print_lcs(i-1,j);
else
print_lcs(i,j-1);
}
void lcs_length(void)
{
m=strlen(x);
n=strlen(y);
for(i=0;i<=m;i++)
c[i][0]=0;
for(i=0;i<=n;i++)
c[0][i]=0;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
if(x[i-1]==y[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';
}
}
print_lcs(m,n);
}
void main()
{
printf("Enter 1st sequence : ");
gets(x);
printf("Enter 2nd sequence : ");
gets(y);
printf("\nlongest common subsequence is : ");
lcs_length();
getch();
}
28
Experiment – 12
#include<stdio.h>
#include<conio.h>
void prin(int,int);
void matrixchain(int [],int);
int m[100][100],s[100][100];
void main()
{
int i,j,p[100],n;
clrscr();
printf("enter the no. of matrix chian");
scanf("%d",&n);
printf("enter the order of chain matrix");
for(i=0;i<=n;i++)
scanf("%d",&p[i]);
matrixchain(p,n);
printf("cost of matrix \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\t%d",m[i][j]);
}
printf("\n");
}
printf("sequence matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\t%d",s[i][j]);
}
printf("\n");
}
prin(1,n);
printf("\ncost of matrix chain multiplication %d", m[1][n]);
getch();
}
29
for(i=1;i<=n;i++)
{
m[i][i]=0;
}
for(l=2;l<=n;l++)
{
for(i=1;i<=n-l+1;i++)
{
j=i+l-1;
m[i][j]=9999;
for(k=i;k<=j-1;k++)
{
q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
if(q<m[i][j])
{
m[i][j]=q;
s[i][j]=k;
}
}
}
}
}
}
}
Experiment – 13
30
Aim - Implement N Queen's problem using Back Tracking.
#include <stdio.h>
#include<conio.h>
int row[8],s=0;
int safe(int,int);
void putboard();
void queen(int);
void putboard()
{
int x,y;
printf("\nSolution # %d",++s);
printf(":\n---------------------------------\n");
for(y=0;y<8; y++)
{
for (x=0;x<8;x++)
if(x==row[y])
printf("| Q ");
else
printf("| ");
printf("|\n---------------------------------\n");
}
getch();
}
void queen(int y)
{
int x;
for(x=0;x<8;x++)
{
row[y-1]=x;
if( safe(x,y-1) )
if (y<8)
queen(y+1);
else
putboard();
31
}
}
void main()
{
clrscr();
queen(1);
}
Experiment – 14
Aim - Implement Naive String Matching Algorithm to match a match a pattern with the
string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void match(char st[100],char pat[100]);
void main()
{
char st[100],pat[100];
int status;
clrscr();
printf("*** Naive String Matching Algorithm ***\n");
printf("Enter the String.\n");
gets(st);
printf("Enter the pattern to match.\n");
gets(pat);
match(st,pat);
getch();
}
void match(char st[100],char pat[100])
{
int n,m,i,j,count=0,temp=0;
n=strlen(st);
m=strlen(pat);
for(i=0;i<=n-m;i++)
{
for(j=0;j<m;j++)
{
if(st[i+j]==pat[j])
count++;
}
if(count==m)
32
{
printf("Match has been found at %d position\n",i);
temp=temp+1;
}
count=0;
}
if(temp==0)
{
printf("No match is found");
}
else
{
printf("Pattern has been matched with string %d times",temp);
}
}
33