Post on 07-Mar-2015
Algorithms Lab Manual
1. Implement Recursive Binary search and Linear search and determine the time taken to search an element. Repeat the experiment for different values of n, the number of elements in the list to be searched and plot a graph of the time taken versus n.
/* Implementation of recursive binary search and sequential search */
#include<stdio.h>#include<conio.h>#include<time.h>#include<stdlib.h>#define max 20
int pos;int binsearch(int,int[],int,int,int);int linsearch(int,int[],int);
void main(){ int ch=1; double t; int n,i,a[max],k,op,low,high,pos; clock_t begin,end; clrscr(); while(ch) {
Dept of MCA 2009 1
Algorithms Lab Manual
printf("\n.....MENU.....\n 1.Binary Search\n 2.Linear Search\n 3.Exit\n");
printf("\nEnter your choice\n"); scanf("%d",&op);
switch(op) { case 1:printf("\nEnter the number of elements \n");
scanf("%d",&n); printf("\nEnter the elements of an array in order\n"); for(i=0;i<n;i++)
scanf("%d",&a[i]); printf("\nEnter the elements to be searched\n"); scanf("%d",&k); low=0;high=n-1; begin=clock(); pos=binsearch(n,a,k,low,high); end=clock(); if(pos==-1) printf("\n\n Unsuccessful
search"); else
printf("\n Element %d is found at position %d",k,pos+1);
printf("\n Time taken is %lf CPU1 cycles\n",(end-begin)/CLK_TCK);
getch();
Dept of MCA 2009 2
Algorithms Lab Manual
break; case 2:printf("\nEnter the number of elements\n");
scanf("%d",&n); printf("\nEnter the elements of
an array\n"); for(i=0;i<n;i++)
scanf("%d",&a[i]); printf("\nEnter the elements to be searched\n");
scanf("%d",&k); begin=clock(); pos=linsearch(n,a,k); end=clock(); if(pos==-1)
printf("\n\n Unsuccessful search");
else printf("\n Element %d is
found at position %d",k,pos+1); printf("\n Time taken is %lf CPU cycles\n",(end-begin)/CLK_TCK);
getch(); break;
default:printf("\nInvalid choice entered\n");
exit(0);
}
Dept of MCA 2009 3
Algorithms Lab Manual
printf("\n Do you wish to run again (1/0) \n"); scanf("%d",&ch); } getch(); }
int binsearch(int n,int a[],int k,int low,int high) { int mid; delay(1000); mid=(low+high)/2; if(low>high) return -1; if(k==a[mid]) return(mid); else if(k<a[mid]) return binsearch(n,a,k,low,mid-1); else return binsearch(n,a,k,mid+1,high);
}
int linsearch(int n,int a[],int k) { delay(1000); if(n<0)
Dept of MCA 2009 4
Algorithms Lab Manual
return -1; if(k==a[n-1]) return(n-1); else return linsearch(n-1,a,k); }
OUTPUT
Case 1
.....MENU..... 1.Binary Search 2.Linear Search 3.Exit
Enter your choice1
Enter the number of elements3
Enter the elements of an array
Dept of MCA 2009 5
Algorithms Lab Manual
4812
Enter the elements to be searched12
Element 12 is found at position 2 Time taken is 1.978022 CPU1 cycles
Case 2
.....MENU..... 1.Binary Search 2.Linear Search 3.Exit
Enter your choice2
Enter the number of elements4
Enter the elements of an array36912
Enter the elements to be searched9
Dept of MCA 2009 6
Algorithms Lab Manual
Element 9 is found at position 3 Time taken is 3.021978 CPU cycles
2. Sort a given set of elements using the Heap sort method and determine the time taken 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.
#include<stdio.h>#include<conio.h>#include<time.h>
Dept of MCA 2009 7
Algorithms Lab Manual
void heapcom(int a[],int n){
int i,j,k,item;for(i=1;i<=n;i++){
item=a[i];j=i;k=j/2;while(k!=0 && item>a[k]){
a[j]=a[k];j=k;k=j/2;
}a[j]=item;
}}void adjust(int a[],int n){
int item,i,j;j=1;item=a[j];i=2*j;while(i<n){
if((i+1)<n){
if(a[i]<a[i+1])i++;
}if(item<a[i])
Dept of MCA 2009 8
Algorithms Lab Manual
{a[j]=a[i];j=i;i=2*j;
}elsebreak;
}a[j]=item;
}void heapsort(int a[],int n){
int i,temp;delay(1000);heapcom(a,n);for(i=n;i>=1;i--){
temp=a[1];a[1]=a[i];a[i]=temp;adjust(a,i);
}}void main()
{ int i,n,a[20],ch=1; clock_t start,end; clrscr(); while(ch) {
printf("\n enter the number of elements to sort\n");
Dept of MCA 2009 9
Algorithms Lab Manual
scanf("%d",&n);printf("\n enter the elements to
sort\n");for(i=1;i<=n;i++) scanf("%d",&a[i]);start=clock();heapsort(a,n);end=clock();printf("\n the sorted list of
elemnts is\n");for(i=1;i<=n;i++) printf("%d\n",a[i]);printf("\n Time taken is %lf CPU
cycles\n",(end-start)/CLK_TCK);printf("do u wish to run again
(0/1)\n");scanf("%d",&ch);
}getch();
}
OUTPUTenter the number of elements to sort5
enter the elements to sort85
Dept of MCA 2009 10
Algorithms Lab Manual
631
the sorted list of elemnts is13568
Dept of MCA 2009 11
Algorithms Lab Manual
3. Sort a given set of elements using Merge sort method and determine the time taken 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.
#include<stdio.h>#include<conio.h>#include<time.h>#define max 20void mergesort(int a[],int low,int high);void merge(int a[],int low,int mid,int high);void main(){
int n,i,a[max],ch=1;clock_t start,end;clrscr();while(ch) {
Dept of MCA 2009 12
Algorithms Lab Manual
printf("\n\t enter the number of elements\n");
scanf("%d",&n); printf("\n\t enter the elements\
n"); for(i=0;i<n;i++)
scanf("%d",&a[i]); start= clock(); mergesort(a,0,n-1); end=clock(); printf("\nthe sorted array is\
n"); for(i=0;i<n;i++)
printf("%d\n",a[i]); printf("\n\ntime taken=%lf",
(end-start)/CLK_TCK); printf("\n\ndo u wish to
continue(0/1) \n"); scanf("%d",&ch);}getch();
}
void mergesort(int a[],int low,int high){
int mid;delay(100);if(low<high){
mid=(low+high)/2;mergesort(a,low,mid);mergesort(a,mid+1,high);
Dept of MCA 2009 13
Algorithms Lab Manual
merge(a,low,mid,high);}
}
void merge(int a[],int low,int mid,int high){
int i,j,k,t[max];i=low;j=mid+1;k=low; while((i<=mid) && (j<=high))if(a[i]<=a[j])t[k++]=a[i++];elset[k++]=a[j++];while(i<=mid)t[k++]=a[i++];while(j<=high)t[k++]=a[j++];for(i=low;i<=high;i++)a[i]=t[i];
}
OUTPUT
Enter the number of elements 5
Dept of MCA 2009 14
Algorithms Lab Manual
Enter the elements 63419
The sorted array is 13469
time taken=0.824176
Dept of MCA 2009 15
Algorithms Lab Manual
4. Sort a given set of elements using Selection sort and hence find the time required to sort 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.
#include<stdio.h>#include<conio.h>#include<time.h> void main() {
int i,n,j,min,k,a[20],ch=1;clock_t begin,end;clrscr();while(ch){ printf("\n enter the number of
elements\n"); scanf("%d",&n); printf("\n enter the elements to
be sorted\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); begin=clock(); for(i=0;i<=n-2;i++) {
min=i;delay(200);for(j=i+1;j<=n-1;j++){
if(a[j]<a[min])
Dept of MCA 2009 16
Algorithms Lab Manual
min=j;}k=a[i];a[i]=a[min];a[min]=k;
} end=clock(); printf("\n\t the sorted list of
elements are:\n"); for(i=0;i<n;i++) printf("\n%d",a[i]); printf("\n\n\t time taken:%lf",
(end-begin)/CLK_TCK); printf("\n\n do u wish to
continue (0/1)\n"); scanf("%d",&ch);}getch();
}
OUTPUTenter the number of elements5
enter the elements to be sorted8351
Dept of MCA 2009 17
Algorithms Lab Manual
9
the sorted list of elements are: 1 3 5 8 9 time taken:0.824176
Dept of MCA 2009 18
Algorithms Lab Manual
5. a. Obtain the Topological ordering of vertices in a given digraph.
#include<stdio.h>#include<conio.h>#define max 20
int a[max][max],n;void topological_sort();void main(){
int i,j;clrscr();printf("\n enter the number of
vertices\n");scanf("%d",&n);printf("\n enter the adjacency
matrix\n");for(i=1;i<=n;i++)for(j=1;j<=n;j++)scanf("%d",&a[i][j]);topological_sort();getch();
}
void topological_sort(){
int v[max],ver[max],i,j,p=1,flag=0;for(i=1;i<=n;i++)v[i]=0;while(p<=n)
Dept of MCA 2009 19
Algorithms Lab Manual
{j=1;while(j<=n){
flag=0;if(v[j]==0){
for(i=1;i<=n;i++)
if((a[i][j]!=0) && (v[i]==0))
{flag=1;break;
}if(flag==0){
v[j]=1;ver[p++]=j;break;
}}j++;if(j>n){
printf("\n topological order is not
possible\n");getch();exit(0);
}}
Dept of MCA 2009 20
Algorithms Lab Manual
}printf("\n topological order
obtained is...\n");for(i=1;i<p;i++)printf("\t%d",ver[i]);getch();
}
OUTPUTenter the number of vertices4
enter the adjacency matrix0 1 1 10 0 0 10 0 0 00 0 1 0
topological order obtained is... 1 2 4 3
Dept of MCA 2009 21
Algorithms Lab Manual
5 b. Implement All Pair Shortest paths problem using Floyd's algorithm.
#include<stdio.h>#include<conio.h>#include<stdlib.h>int cost[10][10],a[10][10];void all_paths(int [10][10],int [10][10],int);int min1(int,int);
void main(){
int i,j,n;clrscr();printf("\n enter the number of
vertices\n");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]);all_paths(cost,a,n);printf("\n\t the shortest path
obtained is\n");for(i=1;i<=n;i++){
for(j=1;j<=n;j++)printf("\t %d",a[i][j]);printf("\n");
Dept of MCA 2009 22
Algorithms Lab Manual
}getch();
}void all_paths(int cost[10][10],int a[10][10],int n){
int i,j,k;for(i=1;i<=n;i++)for(j=1;j<=n;j++)a[i][j]=cost[i][j];for(k=1;k<=n;k++)for(i=1;i<=n;i++)for(j=1;j<=n;j++)a[i][j]=min1(a[i][j],a[i][k]+a[k]
[j]);}int min1(int a,int b){
return(a<b)?a:b;}
Dept of MCA 2009 23
Algorithms Lab Manual
OUTPUT
enter the number of vertices4
enter the adjacency matrix999 999 3 9992 999 999 999999 7 999 1 6 999 999 999
the shortest path obtained is 10 10 3 4 2 12 5 6 7 7 10 1 6 16 9 10
Dept of MCA 2009 24
Algorithms Lab Manual
6. Implement 0/1 Knapsack problem using dynamic programming.
#include<stdio.h>#include<conio.h>int v[20][20];int max1(int a,int b){
return(a>b)?a:b;}void main(){
int i,j,p[20],w[20],n,max;clrscr();
Dept of MCA 2009 25
Algorithms Lab Manual
printf("\n enter the number of items\n");
scanf("%d",&n);for(i=1;i<=n;i++){
printf("\n enter the weight and profit of the
item %d:",i);scanf("%d %d",&w[i],&p[i]);
}printf("\n enter the capacity of the
knapsack");scanf("%d",&max);for(i=0;i<=n;i++)v[i][0]=0;for(j=0;j<=max;j++)v[0][j]=0;for(i=1;i<=n;i++)for(j=1;j<=max;j++){
if(w[i]>j)v[i][j]=v[i-1][j];elsev[i][j]=max1(v[i-1][j],v[i-1][j-
w[i]]+p[i]);}printf("\n\nThe table is\n");for(i=0;i<=n;i++)
{for(j=0;j<=max;j++)printf("%d\t",v[i][j]);printf("\n");
Dept of MCA 2009 26
Algorithms Lab Manual
}printf("\nThe maximum profit is
%d",v[n][max]);printf("\nThe most valuable subset
is:{");j=max;
for(i=n;i>=1;i--)if(v[i][j]!=v[i-1][j]){
printf("\t item %d:",i);j=j-w[i];
}printf("}");getch();
}
OUTPUT
enter the number of items4
enter the weight and profit of the item 1:2 12
enter the weight and profit of the item 2:1 10
enter the weight and profit of the item 3:3 20
Dept of MCA 2009 27
Algorithms Lab Manual
enter the weight and profit of the item 4:2 15
enter the capacity of the knapsack5
The table is0 0 0 0 0 00 0 12 12 12 120 10 12 22 22 220 10 12 22 30 320 10 15 25 30 37
The maximum profit is 37The most valuable subset is:{ item 4: item 2: item 1:}
Dept of MCA 2009 28
Algorithms Lab Manual
7. From a given vertex in a weighted connected graph, find shortestpaths to other vertices using Dijkstra's algorithm.
#include<stdio.h>main (){ int n, cost[15][15], i, j, s[15], v, u, w, dist[15],
num, min; clrscr(); printf ("Enter the vertices please\n"); scanf ("%d", &n); printf ("Enter the cost of the edges please\n"); printf ("Enter 999 if the edgeis not present or for the
self loop\n"); for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) scanf ("%d", &cost[i][j]); printf ("Enter the Source vertex please\n"); scanf ("%d", &v);
for (i = 1; i <= n; i++) { s[i] = 0; dist[i] = cost[v][i]; }
Dept of MCA 2009 29
Algorithms Lab Manual
s[v] = 1; dist[v] = 0;
for (num = 2; num <= n - 1; num++) { min = 999; for (w = 1; w <= n; w++)
if (s[w] == 0 && dist[w] < min) { min = dist[w]; u = w; }
s[u] = 1;
for (w = 1; w <= n; w++){ if (s[w] == 0) { if (dist[w] > (dist[u] +
cost[u][w]))dist[w] = (dist[u] + cost[u][w]);
}}
}
printf ("VERTEX\tDESTINATION\tCOST\n"); for (i = 1; i <= n; i++) printf (" %d\t %d\t\t %d\n", v, i, dist[i]);
Dept of MCA 2009 30
Algorithms Lab Manual
getch();}
OUTPUT
Enter the vertices pleasen = 5
Enter the cost of the edges pleaseEnter 999 if the edge is not present or for the self loopThe cost of the edges are :
999 1 2 999 9991 999 3 4 9992 3 999 5 6999 4 5 999 6999 999 6 6 999
Enter the Source vertex please : 1
VERTEX DESTINATION COST 1 1 0 1 2 1 1 3 2 1 4 5 1 5 8
Dept of MCA 2009 31
Algorithms Lab Manual
8. Sort a given set of elements using Quick sort method and determine the time taken 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.
#include<stdio.h>#include<conio.h>void quicksort(int[],int,int);int partition(int[],int,int);void main(){
int i,n,a[20],ch=1;clrscr();while(ch){ printf("\n enter the number of
elements\n"); scanf("%d",&n); printf("\n enter the array
elements\n"); for(i=0;i<n;i++)
scanf("%d",&a[i]); quicksort(a,0,n-1); printf("\n\nthe sorted array
elements are\n\n"); for(i=0;i<n;i++)
printf("\n%d",a[i]); printf("\n\n do u wish to
continue (0/1)\n"); scanf("%d",&ch);
Dept of MCA 2009 32
Algorithms Lab Manual
}getch();
}
void quicksort(int a[],int low,int high){
int mid;if(low<high){
mid=partition(a,low,high);quicksort(a,low,mid-1);quicksort(a,mid+1,high);
}}int partition(int a[],int low,int high){
int key,i,j,temp,k;key=a[low];i=low+1;j=high;while(i<=j){
while(i<=high && key>=a[i])i=i+1;while(key<a[j])j=j-1;if(i<j) {
temp=a[i];a[i]=a[j];a[j]=temp;
}
Dept of MCA 2009 33
Algorithms Lab Manual
else{
k=a[j];a[j]=a[low];a[low]=k;
}}return j;
}
OUTPUT
enter the number of elements5
enter the elements to be sorted85241
the sorted list of elements are: 1 2 4 5 8 time taken:0.824176
Dept of MCA 2009 34
Algorithms Lab Manual
9. Find Minimum Cost Spanning Tree of a given undirected graphusing Kruskal's algorithm-
#include<stdio.h>#include<conio.h>int root[10], flag = 0, count=0, temp, min;int a[20], cost[20][20], n, i, j, k, totalcost = 0, x, y;void find_min (), check_cycle (), update ();main (){ clrscr(); printf ("Enter the number of vertices please\n"); scanf ("%d", &n); printf ("Enter the cost of the matrix please\n"); for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) scanf ("%d", &cost[i][j]); find_min (); while (min != 999 && count != n - 1) { check_cycle (); if (flag)
{ printf ("%d ---> %d = %d\n",
x, y,
Dept of MCA 2009 35
Algorithms Lab Manual
cost[x][y]); totalcost += cost[x][y]; update (); count++;}
cost[x][y] = cost[y][x] = 999; find_min (); }
if (count < n - 2) printf ("The graph is not connected\n"); else printf ("The graph is connected & the min cost is
%d\n", totalcost); getch();}
void check_cycle (){ if ((root[x] == root[y]) && (root[x] != 0)) flag = 0; else flag = 1;}
void find_min (){ min = 999; for (i = 1; i <= n; i++)
Dept of MCA 2009 36
Algorithms Lab Manual
for (j = 1; j <= n; j++) if (min > cost[i][j])
{ min = cost[i][j]; x = i; y = j;}
}
void update (){
if (root[x] == 0 && root[y] == 0) root[x] = root[y] = x;
else if (root[x] == 0) root[x] = root[y];
else if (root[y] == 0) root[y] = root[x];
else { temp = root[y]; for (i = 1; i <= n; i++)
if (root[i] == temp) root[i] = root[x];
}}
Dept of MCA 2009 37
Algorithms Lab Manual
OUTPUT
Enter the number of vertices please4Enter the cost of the matrix please999 1 5 2 1 999 999 999 5 999 999 3 2 999 3 9991 ---> 2 = 11 ---> 4 = 23 ---> 4 = 3The graph is connected & the min cost is 6
Dept of MCA 2009 38
Algorithms Lab Manual
10. a. Print all the nodes reachable from a given starting node in a digraph using Breadth First Search method.
#include<stdio.h>
Dept of MCA 2009 39
Algorithms Lab Manual
#include<conio.h>void distance(int,int);int a[10][10];void main(){
int i,j,n;clrscr();printf("\n Enter the number of vertices in the
diagraph:");scanf("%d",&n);printf("\n Enter the adjacency matrix\n");for(i=1;i<=n;i++)for(j=1;j<=n;j++)scanf("%d",&a[i][j]);for(i=1;i<=n;i++){
printf("\n\t the starting vertex is %d\n",i);distance(i,n);
printf("\n \t press enter for other source vertex\n");
getch();}
}void distance(int v,int n){
int queue[40],visited[20],dis[20],front,rear,i,j;for(i=1;i<=n;i++)visited[i]=dis[i]=0;front=rear=0;queue[rear++]=v;visited[v]=1;
Dept of MCA 2009 40
Algorithms Lab Manual
do{
i=queue[front++];for(j=1;j<=n;j++)if(a[i][j] && !visited[j]){
dis[j]=dis[i]+1;queue[rear++]=j;visited[j]=1;printf("\n\t the vertex %d to %d is of
distance=%d\n",v,j,dis[j]);}
}while(front<rear);
}
OUTPUTEnter the number of vertices in the diagraph:4
Enter the adjacency matrix0 1 1 10 0 0 10 0 0 00 0 1 0
the starting vertex is 1
the vertex 1 to 2 is of distance=1
Dept of MCA 2009 41
Algorithms Lab Manual
the vertex 1 to 3 is of distance=1
the vertex 1 to 4 is of distance=1
press enter for other source vertex
the starting vertex is 2
the vertex 2 to 4 is of distance=1
the vertex 2 to 3 is of distance=2
press enter for other source vertex the starting vertex is 3 press enter for other source vertex the starting vertex is 4 the vertex 4 to 3 is of distance=1 press enter for other source vertex
Dept of MCA 2009 42
Algorithms Lab Manual
10 b. Check whether a given graph is connected or not using DFS method.
#include<stdio.h>#include<conio.h>void dfs(int n,int cost[10][10],int u,int s[]){
int v;s[u]=1;for(v=0;v<n;v++){
if(cost[u][v]==1 && s[v]==0){
dfs(n,cost,v,s);}
}}
void main(){
int n,i,j,cost[10][10],s[10],connected,flag;
clrscr();printf("\n enter the number of
nodes\n");scanf("%d",&n);printf("\n enter the adjacency
matrix\n");for(i=0;i<n;i++)
Dept of MCA 2009 43
Algorithms Lab Manual
{for(j=0;j<n;j++){
scanf("%d",&cost[i][j]);}
}connected=0;for(j=0;j<n;j++){
for(i=0;i<n;i++)s[i]=0;dfs(n,cost,j,s);flag=0;for(i=0;i<n;i++){
if(s[i]==0)flag=1;
}if(flag==0)connected=1;
}
if(connected==1)printf("graph is connected\n");elseprintf("graph is not connected\n");getch();
}
Dept of MCA 2009 44
Algorithms Lab Manual
OUTPUT
Case 1:
enter the number of nodes4
enter the adjacency matrix0 0 0 10 0 0 00 0 1 00 0 0 1graph is not connected
Case 2:
enter the number of nodes4
enter the adjacency matrix1 1 1 11 1 1 11 1 1 11 1 1 1graph is connected
Dept of MCA 2009 45
Algorithms Lab Manual
11. 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.
#include<stdio.h>int s[10],d,n,set[10],count=0;void display(int);int flag = 0;
void main(){
int subset(int,int);int i;clrscr();printf("Enter the Number of elements
in the set\n");scanf("%d",&n);printf("enter the set values\n");
Dept of MCA 2009 46
Algorithms Lab Manual
for(i=0;i<n;++i)scanf("%d",&s[i]);printf("\nEnter the sum\n");scanf("%d",&d);printf(" The Program Output is:\n");subset(0,0);if(flag == 0)printf(" There is no solution \n");getch();
}int subset(int sum,int i){
if(sum == d){
flag = 1;display(count);return;
}if(sum>d || i>=n) return;else{
set[count]=s[i];count++;subset(sum+s[i],i+1);count--;subset(sum,i+1);
}}
void display(int count)
Dept of MCA 2009 47
Algorithms Lab Manual
{int i;printf("{ ");for(i=0;i<count;i++)printf("%d ",set[i]);printf("}\n");
}
OUTPUT
Enter the Number of elements in the set5enter the set values12568
Enter the sum9 The Program Output is:{ 1 2 6 }{ 1 8 }
Dept of MCA 2009 48
Algorithms Lab Manual
12. a. Implement Horspool algorithm for String Matching,
#include<stdio.h>#include<stdlib.h>void main(){
int table[126];char t[100],p[25];int n,i,k,j,m,flag=0;clrscr();printf("Enter the Text\n");gets(t);n=strlen(t);printf("Enter the Pattern\n");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){
Dept of MCA 2009 49
Algorithms Lab Manual
printf("The position of the pattern is
%d\n",i-m+2);flag=1;break;
}else
i=i+table[t[i]];
}
if(!flag)printf("Pattern is not found in
the given text\n");
getch();
}
OUTPUT
Case 1:
Enter the Textacharya_computer_science_engineeringEnter the PatternscienceThe position of the pattern is 18
Dept of MCA 2009 50
Algorithms Lab Manual
Case 2:
Enter the Textacharya_computer_sceince_engineeringEnter the PatterncjPattern is not found in the given text
Dept of MCA 2009 51
Algorithms Lab Manual
12. b. Find the Binomial Co-efficient using Dynamic Programming.
#include<stdio.h>#include<conio.h>void main(){
int i,j,k,n,c[50][50];clrscr();printf("\n enter the value of n & k\
n");scanf("%d%d",&n,&k);for(i=0;i<=n;i++)for(j=0;j<=k;j++)c[i][j]=0;for(i=0;i<=n;i++){
c[i][0]=1;c[i][i]=1;
}for(i=2;i<=n;i++)for(j=1;j<=i-1;j++)c[i][j]=c[i-1][j-1]+c[i-1][j];printf("\n the table for valuation
is\n");for(i=0;i<=n;i++){
for(j=0;j<=k;j++)if(c[i][j]!=0)printf("\t%d",c[i][j]);printf("\n");
}
Dept of MCA 2009 52
Algorithms Lab Manual
printf("\n\t the binomial coefficient of C(%d,%d) is %d\n",n,k,c[n][k]);
getch();}
OUTPUT
enter the value of n & k6 3
the table for valuation is 1 1 1 1 2 1 1 3 3 1
Dept of MCA 2009 53
Algorithms Lab Manual
1 4 6 4 1 5 10 10 1 6 15 20
the binomial coefficient of C(6,3) is 20
Dept of MCA 2009 54
Algorithms Lab Manual
13. Find Minimum Cost Spanning Tree of a given undirected graph using Prims algorithm.
#include<stdio.h>#include<conio.h>void main(){
int cost[20][20],t[20][20],near1[20],a[20];
int i,j,n,min,minimum,k,l,mincost,c,b;
clrscr();printf("\n enter the number of
nodes\n");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]);minimum=cost[1][1];for(i=1;i<=n;i++)for(j=1;j<=n;j++){
if(minimum>=cost[i][j]){
minimum=cost[i][j];k=i;l=j;
}
Dept of MCA 2009 55
Algorithms Lab Manual
}mincost=minimum;t[1][1]=k;t[1][2]=l;for(i=1;i<=n;i++){
if(cost[i][l]<cost[i][k])near1[i]=l;elsenear1[i]=k;
}near1[k]=near1[l]=0;for(i=2;i<=n-1;i++){
min=999;for(j=1;j<=n;j++){
if(near1[j]!=0){
a[j]=cost[j][near1[j]];{
min=a[j];c=near1[j];b=j;printf("\n");
}}
Dept of MCA 2009 56
Algorithms Lab Manual
}mincost=mincost+cost[b][c];near1[b]=0;for(k=1;k<=n;k++)if((near1[k]!=0) &&
(cost[k][near1[k]]>cost[k][b]))
near1[k]=b;}printf("\n\ the cost of minimum
spanning tree is=%d",mincost);getch();
}
OUTPUT
enter the number of nodes4
enter the adjacency matrix999 1 5 2 1 999 999 999 5 999 999 3 2 999 3 999
the cost of minimum spanning tree is=6
Dept of MCA 2009 57
Algorithms Lab Manual
14. Compute the transitive closure of a given directed graph using Warshall's algorithm.
#include<stdio.h>#include<conio.h>
int a[10][10];void main(){
int i,j,k,n;clrscr();printf("\n enter the number of
vertices\n");scanf("%d",&n);printf("\n enter the adjacency
matrix\n");for(i=1;i<=n;i++)for(j=1;j<=n;j++)scanf("%d",&a[i][j]);
for(k=1;k<=n;k++)for(i=1;i<=n;i++)for(j=1;j<=n;j++)
Dept of MCA 2009 58
Algorithms Lab Manual
a[i][j]=a[i][j] || a[i][k] && a[k][j];
printf("\n\t the tranitive closure is\n");
for(i=1;i<=n;i++){
for(j=1;j<=n;j++)printf("\t %d",a[i][j]);printf("\n");
}getch();
}
OUTPUTenter the number of vertices
Dept of MCA 2009 59
Algorithms Lab Manual
4
enter the adjacency matrix0 1 0 00 0 0 11 0 1 00 0 0 0
the tranitive closure is 0 1 0 1 0 0 0 1 1 1 1 1 0 0 0 0
Dept of MCA 2009 60
Algorithms Lab Manual
15. Implement N Queen's problem using Back Tracking
#include<stdio.h>#include<conio.h>#include<math.h>int x[20],count=1;void queens(int,int);int place(int,int);
void main(){
int n,k=1;clrscr();printf("\n enter the number of
queens to be placed\n");scanf("%d",&n);queens(k,n);
}void queens(int k,int n){
int i,j;for(j=1;j<=n;j++){
if(place(k,j)){
x[k]=j;if(k==n){
printf("\n %d solution",count);
count++;for(i=1;i<=n;i++)
Dept of MCA 2009 61
Algorithms Lab Manual
printf("\n \t %d row <---> %d
column",i,x[i]);getch();
}elsequeens(k+1,n);
}}
}int place(int k,int j){
int i;for(i=1;i<k;i++)if((x[i]==j) || (abs(x[i]-
j))==abs(i-k))return 0;return 1;
}
OUTPUTenter the number of queens to be placed4
1 solution 1 row <---> 2 column 2 row <---> 4 column 3 row <---> 1 column 4 row <---> 3 column 2 solution
Dept of MCA 2009 62
Algorithms Lab Manual
1 row <---> 3 column 2 row <---> 1 column 3 row <---> 4 column 4 row <---> 2 column
Dept of MCA 2009 63