Adt Lab Manual 240429 095259
Adt Lab Manual 240429 095259
Adt Lab Manual 240429 095259
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
1 Sort a given set of elements using Bubble Sort/Selection Sort and determine the time
required to sort the elements. Plot a graph of number of elements versus time taken. Specify
the time efficiency class of this algorithm. 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. 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.
3 Sort a given set of elements using the Quicksort 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.
4 From a given vertex in a weighted connected graph, find shortest paths to other vertices using
Dijkstra's algorithm.
5 Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal's algorithm.
6 Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s algorithm.
11 Implement Horspool’s algorithm for String Matching using space & time tradeoff concept
13 Find a subset of a given set S = {sl,s2,.....,sn} of n positive integers whose sum is equal to a
given positive integer d. For example, if S= {1, 2, 5, 6, 8} and d = 9 there are two solutions
{1,2,6}and {1,8}. A suitable message is to be displayed if the given problem instance doesn't
have a solution.
PAGE
SL. NO PROGRAM
NO.
ALGORITHM BubbleSort(a,n)
//Purpose: To arrange the numbers in ascending order
//Input: n- The number of items in the array
a- Array of unsorted elements
//Output: a contains sorted elements
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<time.h>
void bubblesort(int a[10000],int n)
{
int i,j,temp;
for(j=1;j<n;j++)
{
for(i=0;i<n-j;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
}
OUTPUT
1)
Enter Number of elements:
10
ALGORITHM SelectionSort(a,n)
//Purpose: To arrange the numbers in ascending order
//Input: n- The number of items in the array
a- Array of unsorted elements
//Output: a contains sorted elements
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<time.h>
void selectionsort(int a[10000],int n)
{
int i,j,min,temp,pos;
for(j=0;j<n-1;j++)
{
pos=j;
void main()
{
int n, a[10000],k;
clock_t st,et;
float ts;
clrscr();
printf("\n Enter How many Numbers:");
scanf("%d", &n);
printf("\nThe Random Numbers are:\n");
for(k=1; k<=n; k++)
{
a[k]=rand();
printf("%d\t", a[k]);
}
st=clock();
selectionsort(a, n);
et=clock();
ts=et-st/CLOCKS_PER_SEC;
printf("\n Sorted Numbers are : \n ");
for(k=1; k<=n; k++)
printf("%d\t", a[k]);
printf("\nThe time taken is %f",ts);
getch();
}
OUTPUT
1>
Enter Number of elements:
10
ALGORITHM mergesort(a,low,high)
//This algorithm sorts array A [0...n-1] by recursive mergesort.
//Input: An array A [0...n-1] of orderable elements.
//Output: Array A [0...n-1] sorted in non-decreasing order
Begin
if(low>=high) return
mid<-(low+high)/2
mergesort(a,low,mid)
mergesort(a,mid+1,high)
simplemerge(a,low,mid,high)
End
ALGORITHM simplemerge(a,low,mid,high)
/*This algorithm merges the given list into 2 list till the list is totally sorted
recursively*/
//Merges two sorted arrays into one sorted array.
//Input: Arrays B [0…p-1] and C [0...q-1] both sorted.
//Output: Sorted array A [0...p+q-1] of the elements of B and C.
Begin
i<-low
j<-mid+1
k<-low
while(i<=mid and j<=high)
do
if(a[i]<a[j])
then
c[k] <-- a[i]
i<-i+1
k<-k+1
else
c[k] <-- a[j]
j<-j+1
k<-k+1
endif-else
PROGRAM
# include <stdio.h>
# include <conio.h>
#include<time.h>
void Merge(int a[], int low, int mid, int high)
{
int i, j, k, b[10000];
i=low; j=mid+1; k=low;
while ( i<=mid && j<=high )
{
if( a[i] <= a[j] )
b[k++] = a[i++] ;
else
b[k++] = a[j++] ;
}
while (i<=mid)
b[k++] = a[i++] ;
while (j<=high)
b[k++] = a[j++] ;
for(k=low; k<=high; k++)
a[k] = b[k];
}
void main()
{
int n, a[10000],k;
clock_t st,et;
float ts;
clrscr();
printf("\n Enter Numbers of elements:");
scanf("%d", &n);
printf("\nThe Random Numbers are:\n");
for(k=1; k<=n; k++)
{
a[k]=rand();
printf("%d\t", a[k]);
}
st=clock();
MergeSort(a, 1, n);
et=clock();
ts=et-st/CLOCKS_PER_SEC;
printf("\n Sorted Numbers are : \n ");
for(k=1; k<=n; k++)
printf("%d\t", a[k]);
printf("\nThe time taken is %f",ts);
getch();
}
OUTPUT
1)
Enter Number of elements:
10
2)
Enter the size of array
1000
The time taken to sort 1000 elements is 2.0329
ALGORITHM quicksort(a,low,high)
//Sorts a subarray by quicksort
//Input: A subarray A[l..r] of A[0..n-1],defined by its left and right indices l and r
//Output: Subarray A[l..r] sorted in nondecreasing order
Begin
if(low>high) return
k<-partition(a,low,high)
quicksort(a,low,k-1)
quicksort(a,k+1,high)
End
ALGORITHM partition(a,low,high)
/*This algorithm partitons the given list into 2 list till the list contains only 1 element
recursively*/
//Partition a subarray by using its first element as its pivot
//Input:A subarray A[l..r] of A[0..n-1],defined by its left and right indices l and r (l<r)
//Output:A partition of A[l..r],with the split position returned as this function’s value
Begin
key<-a[low]
i<-low
j<-high+1
while(i<=j)
do i<-i+1 while(key>=a[i])
do j<-j-1 while(key<a[j])
if (i<j)
then
exchange(a[i],a[j])
endif
endwhile
exchange(a[low],a[j])
return j
End
void main()
{
int n, a[10000],k;
OUTPUT
1)
Enter Number of elements:
10
2)
Enter the size of array
1000
The time taken to sort 1000 elements is 1.3736
ALGORITHM dijikstra(cost,n,src,dist)
/*This algorithm calculates the shortest path from the given vertex in a weighted
directed graph and gives the path of that vertex to the vertex */
//Input: cost[][] – 2D array gives the weight between each pair ofvertex
// n-The number of vertices in the given weighted directed graph
// src-The vertex from where the path to all other vertex is found
// dist[] -The single dimensional array which gives the minimumdistance from
//the src to all other vertices.
//Output: shortest distance from src to all other vertices
Begin
for i<-0 to n do
dist[i]<-cost[src][i]
visited[i]<-0
end-for
visited[src]<-1
for i<-0 to n do
min<-999
for j<-0 to n do
if(visited[j]=0 and dist[j]<min)
then
min<-dist[j]
u=j
end-if
end-for
for j<-0 to n do
if(visited[j]=0 and (dist[j]+cost[u][j]<dist[j])
then
dist[j]<-dist[u]+cost[u][j]
end-if
end-for
end-for
End
#include<stdio.h>
#include<conio.h>
void dijkstra(int cost[10][10],int n,int src,int dist[10])
{
int vis[10],u,min,i,j,p[10];
for(i=0;i<n;i++)
{
dist[i]=cost[src][i];
vis[i]=0;
p[i]=src;
}
vis[src]=1;
for(i=0;i<n;i++)
{
min=999;
for(j=0;j<n;j++)
{
if(vis[j]==0 && dist[j]<min)
{
min=dist[j];
u=j;
}
}
vis[u]=1;
for(j=0;j<n;j++)
{
if(vis[j]==0 && (dist[u]+cost[u][j]<dist[j]))
{
dist[j]=dist[u]+cost[u][j];
p[j]=u;
}
}
}
for(i=0;i<n;i++)
{
printf("\n\nShortest path from %d to %d\n",src,i);
j=i;
while(j!=src)
{
printf("%d <- ",j);
j=p[j];
}
void main()
{
int cost[10][10],n,src,dist[10],i,j;
clrscr();
printf("\n\n\n\n\n\n\n Enter the number of vertices:\n");
scanf("%d",&n);
printf("\n Enter the cost matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&cost[i][j]);
}
}
printf("\n Enter the source vertex:");
scanf("%d",&src);
dijkstra(cost,n,src,dist);
getch();
}
OUTPUT
Enter the number of vertices
6
Enter the cost adjacency matrix
999 3 999 999 6 5
3 999 1 999 999 4
999 1 999 6 999 4
999 999 6 999 8 5
6 999 999 8 999 2
5 4 4 5 2 999
ALGORITHM kruskal(n,cost)
/*This algorithm calculates the minimum cost spanning tree from the given weighted
undirected graph*/
//Input:cost[][]—cost adjacency matrix
// n ----- number of vertices
//Output: minimum spanning tree edges and minimum cost
Begin
for i<-0 to n do
parent[i]<-0
endfor
sum<-0
count=0
while count<n-1
begin
min<-999
for i<-0 to n do
for j<-0 to n
if(i=j) continue
if(cost[i][j]!=0 &&cost[i][j]<min)
then
min=cost[i][j]
u=i
v=j
endif
endfor
endfor
i=find(u,parent)
j=find(v,parent)
if(i!=j)
then
t[k][1]=u
t[k][2]=v
k++
count++
sum+=min
for i<-1 to n do
write spanning tree
endfor
End
ALGORITHM unions(i,j)
Begin
parent[j]<-i
End
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
for(i=0;i<n;i++)
{
p[i]=i;
}
while(count<n-1)
{
mincost=999;
if(mincost==999)
break;
i=find(u,p);
j=find(v,p);
if(i!=j)
{
t[k][0]=u;
t[k][1] =v;
k++;
count=count+1;
sum=sum+mincost;
if(i<j)
p[j]=i;
else
p[i]=i;
}
cost[u][v]=cost[v][u]=999;
}
if(count==n-1)
{
printf("\nThe edges of Minimum Cost Spanning Tree are\n\n");
for(i=0;i<n-1;i++)
{
printf("\n%d ->%d ",t[k][0],t[k][1]);
}
printf("\n\tMinimum cost = %d\n",sum);
}
else
printf("\nSpanning tree does not exists");
}
void main()
{
int i,j,cost[100][100],n;
clrscr();
Kruskal(cost,n);
getch();
}
OUTPUT
ALGORITHM : Prim(cost,src,n)
// Prim’s algorithm for constructing a minimum spanning tree
//Input: A weighted connected graph G=(V,E),where src is the source node,
// and cost[][] is the cost adjacency matrix,and n is the number of nodes.
//Output: set of edges composing a minimum spanning tree of G
Begin
sum<-0
min<-999
for i<-0 to n do
for j<-0 to n do
if(cost[i][j]<min)
then
min<-cost[i][j]
src<-i
endif
endfor
endfor
for i<-0 to n do
p[i]<-src
visited[i]<-0
d[i]<-cost[src][i]
endfor
visited[src]<-1
for i<-0 to n do
u=-1
for j<-0 to n do
if(visited[j]=0 and d[j]<min])
then
min<-d[j]
u<-j
endif
endfor
endfor
if u==-1
break;
t[k][0]<-p[u]
t[k][1]<-u
count++
k++
sum+=min
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,j,i,cost[100][100],visited[100],p[100],d[100];
clrscr();
printf("Enter the no. of vertices: ");
scanf("%d",&n);
printf("Enter the cost adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);
OUTPUT
ALGORITHM toposort(a,n)
/*algorithm for obtaining the topological ordering of the vertices*/
// Input: a->is the adjacency matrix which is the input to the algorithm
n->number of vertices in the input graph
//Output: Topological ordering of vertices
Begin
for i<- 0 to n-1
do
sum <- 0
for j<-0 to n-1
do
sum <- sum + a[j][i]
end-for
indegree[i] <- sum
end-for
top = -1
k=0
for i <- 0 to n-1
do
if (indegree[i]==0)
then
top <- top+1
s[top] <- i
end-if
end-for
while (top != -1)
do
u < -s[top]
top < -top+1
add u to solution vector t
for each vertex v adjacent to u
decrement indegree[v] by 1
if (indegree[v]==0)
then
top <- top+1
s[top] <- v
end-if
end-for
end-while
write t
PROGRAM
#include<stdio.h>
#include<conio.h>
#define SIZE 30
void find_indegree()
{
int i,j,sum;
void topological_sort()
{
int i,j,u,v,t[SIZE],s[SIZE],top,k;
find_indegree();
top = -1;
k = 0;
void main()
{
int i,j;
int u,v;
clrscr();
topological_sort();
getch();
}
ALGORITHM Floyd(W)
//Implements Floyd’s algorithm for the all-pairs shortest paths problem
//Input: The weight matrix W of a graph
//Output: The distance matrix of shortest paths length
Begin
for i<-0 to n-1
do
for <-0 to n-1
do
D[i,j]<- W[i,j]
end-for
end-for
for k←1 to n do
for i ← 1 to n do
for j ← 1 to n do
D[i,j] ← min (D[i, j], D[i, k]+D[k, j] )
endfor
endfor
endfor
return d
End
PROGRAM
#include<stdio.h>
#include<conio.h>
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
}
}
void main()
{
int n,a[10][10],i,j;
printf("Enter the number of nodes\n ");
scanf("%d",&n);
printf("Enter the cost adjacency matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
}
floyd(a,n);
getch();
}
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,a[20][20];
clrscr();
warshall(a,n);
getch();
}
OUTPUT
ALGORITHM kanpsack(n,w,p,m,v)
/* this algorithm uses dynamic programming technique to solve the knapsack
problem.it accepts the following input*/
//Input: N->number of the jobs.
W[]->wieght of each job.
P[]->profit of each job.
M->capacity of the knapsack.
//Output: the algorithm gives the output 2 dimensional array v[][],
v[N][M] which is the optimal solution.
Begin
for i<-0 to n
do
for j<-0 to n
do
if(i=0 or j=0)
then
v[i,j]=0
else if(w[i]>j)
then
v[i,j]=v[i-1,j]
else
v[i,j]=max(v[i-1,j],v[i-1,j-w[i]]+p[i])
endif
endfor
endfor
End
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,p[10],w[10],m,i,j;
clrscr();
knapsack(n,m,w,p);
getch();
}
OUTPUT
Enter the number of objects
4
Enter the weights of objects
2
1
3
2
Enter the profit of each object
12
10
20
15
Enter the capacity of knapsack
5
The output is
0 0 0 0 0 0
0 0 12 12 12 12
0 10 12 22 22 22
0 10 12 22 30 32
0 10 15 25 30 37
ALGORITHM TSP(S)
//to find minimum cost trip of salesman
//Input: N- number of vertices, i - starting vertex,
// k - the next vertex to visit, cost adjacency matrix C
// S - set of vertices to be visited
//Output: minimum cost trip
for each of the vertices do
if size of S is ∅
then
g ( i, S ) = Ci1
end if
if size S is greater than or equal to 1
then
g ( i, S ) = min { Cik + g( k, S-{k}) }
end if
endfor
PROGRAM
#include<stdio.h>
#include<conio.h>
int a[10][10],visited[10],n,cost=0;
int mincost(int c)
void main()
{
int i,j;
clrscr();
printf("Enter No. of Cities:\n ");
scanf("%d",&n);
printf("Enter Cost adjacency Matrix\n");
for(i=0;i < n;i++)
{
printf("Enter Row %d Elements \n",i+1);
for( j=0;j < n;j++)
scanf("%d",&a[i][j]);
visited[i]=0;
}
printf("The cost matrix:\n");
for( i=0;i < n;i++)
{
for(j=0;j < n;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
printf("The Path is:\n");
tsp(0);
printf("\nMinimum cost:: %d", cost);
OUTPUT
Enter No. of Cities: 4
Enter Cost adjacency Matrix
Enter Row 1 Elements
0 10 15 20
Enter Row 2 Elements
5 0 9 10
Enter Row 3 Elements
6 13 0 12
Enter Row 4 Elements
8890
The cost matrix:
0 10 15 20
5 0 9 10
6 13 0 12
8 8 9 0
The Path is:
1 --> 2 --> 4 --> 3 --> 1
Minimum cost:: 35
n = strlen(s);
m = strlen(p);
i = m-1;
while(i<n)
{
k = 0;
while((k<m)&&(p[m-1-k]==s[i-k]))
k++;
if (k == m)
return i-m+1;
else
i = i+t[s[i]];
}
return -1;
}
void main()
{
char text[MAX];
char pattern[MAX];
int s[MAX];
Shifttable(pattern,s);
pos = Horspool(text,pattern,s);
if(pos==-1)
printf("\nPattern not found.\n");
else
printf("\nPattern found at position: %d\n",pos+1);
return 0;
}
OUTPUT
1)
Enter the text string
This is the program of horspool string matching
Enter the pattern string
program
Pattern string found at 13 position
2)
Enter the text string
Can you find me
Enter the pattern string
found
Pattern string not found
PROGRAM
#include<stdio.h>
#include<math.h>
int c[20],count=0;
void queen(int k,int n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++) //for nxn board
{
if(c[i]==j)
printf("\tQ"); //queen at i,j position
else
printf("\t-"); //empty slot
}
printf("\n");
}
}
OUTPUT
Enter the number of queens
4
solution 1
x Q x x
x x x Q
Q x x x
x x Q x
solution 2
x x Q x
Q x x x
x x x Q
x Q x x
Begin
x[k]<-1
if( s+a[k] = m )
then
for i<-1 to k do
if( x[k] = 1)
write a[i]
endfor
endif
else if( s+a[k]+a[k+1] <= m )
then
subset( s+a[k], k+1, total-a[k] )
if ( ( s+total-a[k] >= m ) && ( s+a[k] <=m ) )
then
x[k]<-0
subset( s, k+1, total-a[k] )
endif
return
PROGRAM
#include<stdio.h>
#include<conio.h>
}
else if ( s + a[k] + a[k+1] <= d )
sumofsub ( s + a[k] , k + 1 , r - a[k] ) ;
if ( ( s + r - a[k] >= d ) && ( s + a[k+1] <=d ) )
{
x[k] = 0;
sumofsub ( s , k + 1 , r - a[k] ) ;
}
}