DAA Lab Manual B20CI0404
DAA Lab Manual B20CI0404
DAA Lab Manual B20CI0404
TECHNOLOGY
Fourth S em est e r
B. Tech in CSIT, AIML, ISE, CSSE
Design and Analysis of Algorithms Lab REVA University
INDEX
SL. No Contents Page. no
1 Lab Objectives 3
2 Lab Outcomes 3
3 Lab Requirements 4
4 Guidelines to Students 5
1. Lab Objectives:
2. Lab Outcomes:
3.Lab Requirements:
Following are the required hardware and software for this lab, which is available in the laboratory.
• Hardware: Desktop system or Virtual machine in a cloud with OS installed. Presently in the Lab,
Pentium IV Processor having 1 GB RAM and 250 GB Hard Disk is available. Desktop systems
are dual boot having Windows as well as Linux OS installed on them.
• Software: C-compiler. Many C-compilers are available. As we have dual boot, we have gcc
compiler in Linux environment (fedora 8 to fedora 20), and Turbo C-IDE on Windows
Environment.
This Design and Analysis of Algorithm lab manual is designed to increase the practical knowledge,
thinkingability and creativity of students. Here, students are expected to read, think, design and implement
the programs for given problems in ”C”. The Linux and windows environments are used as explained
below.
Linux environment: All the programs have been written using “C” and tested using the cc compiler in
Linux environment.
Steps for the successful program completion are :
Windows environment: Programs can be edited, compiled and executed using Turbo C/C++ IDE. All
operations like opening a file, saving a file, compiling the program and executing the program are listed
under different menus inside the IDE. Keyboard shortcuts are also available for the above mentioned
operations.
Note: Every problem in the Lab manual includes problem statement, learning outcomes, theoretical
description, algorithm, program, program description, expected results, implementation phase,simulation
of syntax and logical errors, final program with results.
Recommendation: It is advised to use Linux environment for executing the programs given as part of
this manual.
4. Guidelines to Students
➢ Equipment in the lab for the use of student community. Students need to maintain a
proper decorum in the computer lab. Students must use the equipment with care.
Any damage is caused is punishable.
➢ Students are required to carry their observation / programs book with completed
exercises while entering the lab.
➢ Students are supposed to occupy the machines allotted to them and are not supposed
to talk or make noise in the lab. The allocation is put up on the lab notice board.
➢ Lab can be used in free time / lunch hours by the students who need to use the systems
should take prior permission from the lab in-charge.
1 Search for a given pattern in a text string using Brute Force String Matching.
3 Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s algorithms.
4 From a given vertex in a weighted connected graph, find shortest paths to other
vertices using Dijikstra’s algorithm
5 Design and Implement 0/1 Knapsack problem using Dynamic Programming.
8 Implement Horspool’s algorithm for String Matching and find the number of key
comparisons in successful search and unsuccessful search
9 Sort a given set of elements in ascending order which has duplicate entries. Use the sorting
by counting algorithm
10 Implement N Queen's problem using Back Tracking.
#include <stdio.h>
#include <string.h>
int main()
{
}
if(j==m)
{
printf("SUBSTRING FOUND AT LOCATION %d\n",i+1);
return 0;
}
Program 2
Sort a set of elements in ascending order using Quick Sort algorithm.
#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
void main()
{
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
}
OUTPUT:
#include<stdio.h>
#define INFINITY 999
int prim(int cost[10][10],int source,int n)
{
int i,j,sum=0,visited[10];
int distance[10],vertex[10];
int min,u,v;
for(i=1;i<=n;i++)
{
vertex[i]=source;
visited[i]=0;
distance[i]=cost[source][i];
}
visited[source]=1;
for(i=1;i<n;i++)
{
min=INFINITY;
for(j=1;j<=n;j++)
{
if(!visited[j]&&distance[j]<min)
{
min=distance[j];
u=j;
}
}
visited[u]=1;
sum=sum+distance[u];
printf("\n%d->%d",vertex[u],u);
for(v=1;v<=n;v++)
{
if(!visited[v]&&cost[u][v]<distance[v])
{
distance[v]=cost[u][v];
vertex[v]=u;
School of Computing and Information Technology Page 11
Design and Analysis of Algorithms Lab REVA University
}
}
}
return sum;
}
void main()
{
int a[10][10],n,i,j,m,source;
printf("\n enter the number of vertices:\n");
scanf("%d",&n);
printf("\nenter the cost matrix:\n 0-self loop and 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:\n");
scanf("%d",&source);
m=prim(a,source,n);
printf("\n the cost of spanning tree=%d",m);
}
OUTPUT:
Enter the cost matrix 0-for self edge and 999-if no edge
0 3 4 999 5
3 0 999 6 1
4 999 0 9 7
999 6 9 0 2
5 1 7 2 0
2->5
5->4
2->1
1->3
Cost = 10
#include<stdio.h>
#define INFINITY 999
void dijkstra(int cost[10][10],int n,int source,int distance[10])
{
int visited[10],min,u;
int i,j;
for(i=1;i<=n;i++)
{
distance[i]=cost[source][i];
visited[i]=0;
}
visited[source]=1;
for(i=1;i<=n;i++)
{
min=INFINITY;
for(j=1;j<=n;j++)
if(visited[j]==0 && distance[j]<min)
{
min=distance[j];
u=j;
}
visited[u]=1;
for(j=1;j<=n;j++)
if(visited[j]==0 && (distance[u]+cost[u][j])<distance[j])
{
distance[j]=distance[u]+cost[u][j];
}
}
}
void main()
{
int n,cost[10][10],distance[10];
OUTPUT:
Cost Matrix
Enter 999 for no edge
#include<stdio.h>
int w[10],p[10],n;
int max(int a,int b)
{
return a>b?a:b;
}
int knap(int i,int m)
{
if(i==n) return w[i]>m?0:p[i];
if (w[i]>m) return knap(i+1,m);
return max(knap(i+1,m),knap(i+1,m-w[i])+p[i]);
}
void main()
{
int m,i,max_profit;
printf("\nEnter the number of objects: ");
scanf("%d",&n);
printf("\nEnter the knapsack capacity: ");
scanf("%d",&m);
printf("\nEnter profit followed by weight: ");
for(i=1;i<=n;i++)
scanf("%d%d",&p[i],&w[i]);
max_profit=knap(1,m);
printf("\nMax profit = %d",max_profit);
}
OUTPUT:
Implement All-Pairs Shortest Paths Problem using Floyd's algorithm. Parallelize this algorithm,
implement it using OpenMP and determine the speed-up achieved.
#include<stdio.h>
#include<omp.h>
#define INFINITY 999
int min(int i,int j)
{
if(i<j)
return i;
else
return j;
}
void floyd(int n,int p[10][10])
{
int i,j,k;
#pragma omp parallel for private(i,j,k) shared(p)
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
int main()
{
int i,j,n,a[10][10],d[10][10],source;
double starttime,end time;
printf("Enter the no.of nodes: ");
scanf("%d",&n);
printf("\nEnter the adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
starttime=omp_get_wtime();
floyd(n,a);
endtime=omp_get_wtime();
School of Computing and Information Technology Page 16
Design and Analysis of Algorithms Lab REVA University
printf("\n\nThe distance matrix is \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("\n\nThe time taken is %l0.9f\n",(double)(starttime-endtime));
return 0;
}
OUTPUT:
#include<stdio.h>
int i,visit[20],n,adj[20][20],s,topo_order[10];
void dfs(int v)
{
int w;
visit[v]=1;
for(w=1;w<=n;w++)
if((adj[v][w]==1) && (visit[w]==0))
dfs(w);
topo_order[i--]=v;
}
void main()
{
int v,w;
printf("Enter the number of vertices:\n");
scanf("%d",&n);
printf("Enter the adjacency matrix:\n");
for(v=1;v<=n;v++)
for(w=1;w<=n;w++)
scanf("%d",&adj[v][w]);
for(v=1;v<=n;v++)
visit[v]=0;
i=n;
for(v=1;v<=n;v++)
{
if(visit[v]==0)
dfs(v);
}
printf("\nTopological sorting is:");
for(v=1;v<=n;v++)
printf("v%d ",topo_order[v]);
OUTPUT 1 :
Topological ordering is v1 v2 v3 v4 v5
OUTPUT 2 :
enter the number of vertices:
3
Enter the adjacency matrix
010
001
100
Topological ordering is v1 v2 v3
#include<stdio.h>
void main()
{
int table[126];
char t[100],p[25];
int n,i,k,j,m,flag=0;
printf(“Enter the text : “);
gets(t);
n=strlen(t);
printf(“Enter the pattern : “);
gets(p);
m=strlen(p);
for(i=0;i<126;i++)
table[i]=m;
for(j=0;j<m-2;j++)
table[p[j]]=m-1-j;
i=m-1;
while(i<=n-1)
{
k=0;
while(k<=m-1 && p[m-1-k]==t[i-k])
k++;
if(k==m)
{
printf(“The position of the pattern is %dn”,i-m+2);
flag=1;
break;
}
else
i=i+table[t[i]];
}
if(!flag)
OUTPUT:
pattern=5
pattern=4
#include <stdio.h>
void main()
{
int n, k = 0, a[15], i;
printf("Input number of elements: ");
scanf("%d", &n);
printf("Input the array elements one by one: \n");
for (i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
OUTPUT:
Output:
15,
12
01
13
11
#include<stdio.h>
#include<stdlib.h>
#define MAX 50
int can_place(int c[],int r)
{
int i;
for(i=0;i<r;i++)
if(c[i]==c[r] || (abs(c[i]-c[r])==abs(i-r)))
return 0;
return 1;
}
void display(int c[],int n)
{
int i,j;
char cb[10][10];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cb[i][j]='-';
for(i=0;i<n;i++)
cb[i][c[i]]='Q';
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%c",cb[i][j]);
printf("\n");
}
}
void n_queens(int n)
{
int r;
int c[MAX];
c[0]= -1;
r=0;
OUTPUT:
- Q - -
- - - Q
Q - - -
School of Computing and Information Technology Page 25
Design and Analysis of Algorithms Lab REVA University
- - Q -
- - Q -
Q - - -
- - - Q
- Q - -