Analysis of Algorithms Lab Manual
Analysis of Algorithms Lab Manual
1 VISION/MISSION 4
2. PEO 4
3. POS 5
4. COS 6
5. MAPPING OF CO & PO 6
6. SYLLABUS 6
7. BOOKS 7
8. INSTRUCTIONAL METHODS 7
9. LEARNING MATERIALS 7
Exp:1 Sort a given set of elements using the Quicksort method and determine the time 10
required to sort the elements. The elements can be read from a file or can be
generated using the random number generator.
Exp:2 Implement Merge Sort algorithm to sort a given set of elements and determine 12
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 determine 14
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 using 19
Strassen’s Matrix Multiplication.
Exp:8 Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s 25
algorithm.
Exp:9 Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal's 30
algorithm.
Exp:14 Implement Naive String Matching Algorithm to match a match a pattern with 41
the string.
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTER
Department of Computer Science and Engineering
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
2. PEO
PEO1: To provide students with the fundamentals of Engineering Sciences with more
emphasis in Computer Science & Engineering by way of analyzing and exploiting
engineering challenges.
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.
3. PROGRAM OUTCOMES
5. MAPPING OF CO & PO
CO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
1. To implement H H H H - - - - - - - M
various searching and
sorting techniques on
linear and non linear
data structures to
solve various
computing problems
2. Design and H H H H H L - - - - - M
implement efficient
algorithms for a
specified application.
3. Strengthen the H H H H H L - - - - - M
ability to identify and
apply the suitable
algorithms for the
given real world
problem
6. SYLLABUS
COURSE ASSESMENT METHODS: Internal Viva and end term Practical examination.
COURSE OBJECTIVE:
3. Aho A.V , J.D Ulman: Design and analysis of Algorithms, Addison Wesley
8. INSTRUCTIONAL METHODS
• coding
• Problem solving
9. LEARNING MATERIALS
2. Discussion on website
INSTRUCTIONS OF LAB
DO’s
Please switch off the Mobile/Cell phone before entering Lab.
Enter the Lab with complete source code and data.
Check whether all peripheral are available at desktop before proceeding for program.
Intimate the Lab In Charge whenever you are incompatible in using the system or in
case software get corrupted/ infected by virus.
Arrange all the peripheral and seats before leaving the lab.
Properly shutdown the system before leaving the lab.
Keep the bag outside in the racks.
Enter the lab on time and leave at proper time.
Maintain the decorum of the lab.
Utilize lab hours in the corresponding experiment.
Get your Cd / Pen drive checked by Lab In Charge before using it in the lab.
DON’Ts
Don’t mishandle the system.
Don’t leave the system on standing for long.
Don’t bring any external material in the lab.
Don’t make noise in the lab.
Don’t bring the mobile in the lab. If extremely necessary then keep ringers off.
Don’t enter in the lab without permission of Lab In Charge.
Don’t litter in the lab.
Don’t delete or make any modification in system files.
Don’t carry any lab equipments outside the lab.
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.
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)
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();
getch( );
}
void disp( )
{
int i;
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
Experiment – 3
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.
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;
}
////////////////////////////////////////////////////////////////////
void main( )
{
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.
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( );
}
///////////////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include<conio.h>
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( );
}
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]);
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
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;
}
}
}
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;
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;
}
}
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;
}
Experiment – 10
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];
if(i==0 || j==0)
return;
if(b[i][j]=='c')
print_lcs(i-1,j-1);
printf(" %c",x[i-1]);
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()
gets(x);
gets(y);
lcs_length();
getch();
Experiment – 12
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();
}
}
}
}
}
}
void prin(int i,int j)
{
if(i==j)
{
printf("a%d",i);
}
else
{
printf("(");
prin(i,s[i][j]);
prin((s[i][j]+1),j);
printf(")");
}
}
Experiment – 13
#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();
}
}
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)
{
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);
}
}