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",(endbegin)/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",(endbegin)/CLK_TCK); getch(); break; default:printf("\nInvalid choice entered\n"); exit(0); }

Dept of MCA

2009

3

mid1).low. if(n<0) Dept of MCA 2009 4 .high). mid=(low+high)/2.a.&ch).mid+1.int a[].int high) { int mid. if(low>high) return -1.k. scanf("%d". if(k==a[mid]) return(mid). else if(k<a[mid]) return binsearch(n.Algorithms Lab Manual printf("\n Do you wish to run again (1/0) \n").int low. } int linsearch(int n. } int binsearch(int n.int a[]. else return binsearch(n.int k) { delay(1000).int k.a. } getch().k. delay(1000).

Linear Search 3.k). 1.. } OUTPUT Case 1 . if(k==a[n-1]) return(n-1).MENU.Algorithms Lab Manual return -1...a.Binary Search 2... else return linsearch(n-1.Exit Enter your choice 1 Enter the number of elements 3 Enter the elements of an array Dept of MCA 2009 5 ....

Exit Enter your choice 2 Enter the number of elements 4 Enter the elements of an array 3 6 9 12 Enter the elements to be searched 9 Dept of MCA 2009 6 ..Algorithms Lab Manual 4 8 12 Enter the elements to be searched 12 Element 12 is found at position 2 Time taken is 1... 1..978022 CPU1 cycles Case 2 .MENU.Binary Search 2.Linear Search 3.....

the number of elements in the list to be sorted and plot a graph of the time taken versus n.h> Dept of MCA 2009 7 . #include<stdio.021978 CPU cycles 2.h> #include<time.Algorithms Lab Manual Element 9 is found at position 3 Time taken is 3.h> #include<conio. Repeat the experiment for different values of n. Sort a given set of elements using the Heap sort method and determine the time taken to sort the elements.

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; } else break; } 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(); }

OUTPUT enter the number of elements to sort 5 enter the elements to sort 8 5
Dept of MCA 2009 10

Algorithms Lab Manual 6 3 1 the sorted list of elemnts is 1 3 5 6 8 Dept of MCA 2009 11 .

the number of elements in the list to be sorted and plot a graph of the time taken versus n.i. clrscr().int mid.int high). Sort a given set of elements using Merge sort method and determine the time taken to sort the elements.h> #define max 20 void mergesort(int a[].h> #include<conio. Repeat the experiment for different values of n. clock_t start.ch=1.int low.int low. #include<stdio.end. void merge(int a[].int high).Algorithms Lab Manual 3. while(ch) { Dept of MCA 2009 12 .a[max]. void main() { int n.h> #include<time.

i<n. for(i=0. mergesort(a.i<n.i++) scanf("%d". delay(100). scanf("%d".int high) { int mid.&ch).Algorithms Lab Manual printf("\n\t enter the number of elements\n"). } void mergesort(int a[]. mergesort(a. if(low<high) { mid=(low+high)/2.&a[i]). printf("\n\ndo u wish to continue(0/1) \n").int low.n-1).(end-start)/CLK_TCK). printf("\n\t enter the elements\n").0. for(i=0. start= clock(). Dept of MCA 2009 13 .i++) printf("%d\n".mid). end=clock(). scanf("%d".low. printf("\nthe sorted array is\n"). } getch().&n). printf("\n\ntime taken=%lf".a[i]).

low.high).int low. while(j<=high) t[k++]=a[j++]. } } void merge(int a[]. } OUTPUT Enter the number of elements Dept of MCA 2009 14 .Algorithms Lab Manual mergesort(a.k.int mid. i=low. j=mid+1.high).i++) a[i]=t[i]. k=low. while((i<=mid) && (j<=high)) if(a[i]<=a[j]) t[k++]=a[i++]. while(i<=mid) t[k++]=a[i++]. merge(a.j.mid+1. for(i=low.mid.int high) { int i.t[max]. else t[k++]=a[j++].i<=high.

Algorithms Lab Manual 5 Enter the elements 6 3 4 1 9 The sorted array is 1 3 4 6 9 time taken=0.824176 Dept of MCA 2009 15 .

delay(200).a[20].j<=n-1.h> #include<time. while(ch) { printf("\n enter the number of elements\n").end. clrscr().i++) { min=i. #include<stdio.j++) { if(a[j]<a[min]) Dept of MCA 2009 16 .min.i++) scanf("%d".k.h> void main() { int i. scanf("%d". begin=clock(). printf("\n enter the elements to be sorted\n").&a[i]).&n).i<n.j.i<=n-2. the number of elements in the list to be sorted and plot a graph of the time taken versus n. Sort a given set of elements using Selection sort and hence find the time required to sort elements. for(i=0.n.Algorithms Lab Manual 4. for(j=i+1. for(i=0.h> #include<conio. clock_t begin.ch=1. Repeat the experiment for different values of n.

printf("\n\n\t time taken:%lf". } end=clock().i++) printf("\n%d".&ch).(end-begin)/CLK_TCK). a[i]=a[min]. for(i=0. } getch().a[i]). printf("\n\t the sorted list of elements are:\n"). printf("\n\n do u wish to continue (0/1)\n").i<n. scanf("%d".Algorithms Lab Manual min=j. } OUTPUT enter the number of elements 5 enter the elements to be sorted 8 3 5 1 Dept of MCA 2009 17 . a[min]=k. } k=a[i].

824176 Dept of MCA 2009 18 .Algorithms Lab Manual 9 the sorted list of elements are: 1 3 5 8 9 time taken:0.

j.i. for(i=1.i<=n.Algorithms Lab Manual 5.i<=n. a.n.h> #include<conio.j++) scanf("%d".flag=0. void topological_sort().j. while(p<=n) Dept of MCA 2009 19 .ver[max]. getch().i++) for(j=1. clrscr().&a[i][j]). void main() { int i. #include<stdio. topological_sort(). printf("\n enter the number of vertices\n"). scanf("%d".j<=n. printf("\n enter the adjacency matrix\n").&n). } void topological_sort() { int v[max].h> #define max 20 int a[max][max]. for(i=1.p=1.i++) v[i]=0. Obtain the Topological ordering of vertices in a given digraph.

if(v[j]==0) { for(i=1. } } Dept of MCA 2009 20 . break. exit(0).i++) if((a[i][j]!=0) && (v[i]==0)) { flag=1. } } j++.i<=n. while(j<=n) { flag=0. } if(flag==0) { v[j]=1. getch(). if(j>n) { printf("\n topological order is not possible\n"). ver[p++]=j.Algorithms Lab Manual { j=1. break.

\n")..i++) printf("\t%d". getch()..i<p.Algorithms Lab Manual } printf("\n topological order obtained is.. 1 2 4 3 Dept of MCA 2009 21 . for(i=1. } OUTPUT enter the number of vertices 4 enter the adjacency matrix 0111 0001 0000 0010 topological order obtained is..ver[i]).

int [10][10].h> #include<conio. void main() { int i.j<=n. printf("\n\t the shortest path obtained is\n"). printf("\n enter the adjacency matrix\n").n). scanf("%d".a[i][j]).j++) printf("\t %d".&n). int min1(int.&cost[i][j]). #include<stdio. for(i=1. printf("\n").h> #include<stdlib.j.a.h> int cost[10][10].j++) scanf("%d". clrscr(). all_paths(cost. for(i=1.int). void all_paths(int [10][10].n. Dept of MCA 2009 22 .i++) for(j=1.i<=n. Implement All Pair Shortest paths problem using Floyd's algorithm.a[10][10].int).Algorithms Lab Manual 5 b. printf("\n enter the number of vertices\n").i<=n.j<=n.i++) { for(j=1.

j++) a[i][j]=cost[i][j].Algorithms Lab Manual } getch().j<=n.int n) { int i. for(i=1.i++) for(j=1.k++) for(i=1.j. } void all_paths(int cost[10][10].k<=n.j++) a[i][j]=min1(a[i][j].int b) { return(a<b)?a:b. } Dept of MCA 2009 23 . } int min1(int a.i<=n.i<=n. for(k=1.j<=n.i++) for(j=1.a[i][k]+a[k][j] ).k.int a[10][10].

Algorithms Lab Manual OUTPUT enter the number of vertices 4 enter the adjacency matrix 999 999 3 999 2 999 999 999 999 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 .

Implement 0/1 Knapsack problem using dynamic programming. int max1(int a. #include<stdio. Dept of MCA 2009 25 .p[20].h> int v[20][20].h> #include<conio.int b) { return(a>b)?a:b.max.Algorithms Lab Manual 6.w[20]. } void main() { int i. clrscr().j.n.

Dept of MCA 2009 26 . for(i=0.i<=n.&max).v[i][j]). else v[i][j]=max1(v[i-1][j].&w[i].i<=n. for(i=0. scanf("%d %d". for(i=1.v[i-1][jw[i]]+p[i]).Algorithms Lab Manual printf("\n enter the number of items\n").j<=max.i<=n.&p[i]). for(i=1.&n). } printf("\n enter the capacity of the knapsack").j++) { if(w[i]>j) v[i][j]=v[i-1][j].j++) v[0][j]=0.j++) printf("%d\t". for(j=0.i).i++) { for(j=0. scanf("%d". printf("\n").i++) v[i][0]=0.i++) { printf("\n enter the weight and profit of the item %d:".i++) for(j=1.j<=max. } printf("\n\nThe table is\n"). scanf("%d".j<=max.i<=n.

} OUTPUT enter the number of items 4 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 . j=j-w[i].i>=1. printf("\nThe most valuable subset is:{"). getch().i--) if(v[i][j]!=v[i-1][j]) { printf("\t item %d:". } printf("}").i).Algorithms Lab Manual } printf("\nThe maximum profit is %d". for(i=n.v[n][max]). j=max.

Algorithms Lab Manual enter the weight and profit of the item 4:2 15 enter the capacity of the knapsack5 The table is 0 0 0 0 0 12 0 10 12 0 10 12 0 10 15 0 0 0 12 12 12 22 22 22 22 30 32 25 30 37 The maximum profit is 37 The most valuable subset is:{ item 1:} item 4: item 2: Dept of MCA 2009 28 .

i++) { s[i] = 0. i++) for (j = 1. dist[i] = cost[v][i]. } Dept of MCA 2009 29 . &cost[i][j]). scanf ("%d". &v). min.Algorithms Lab Manual 7. printf ("Enter the vertices please\n"). &n). dist[15]. cost[15][15]. u. i <= n. find shortest paths to other vertices using Dijkstra's algorithm. printf ("Enter the cost of the edges please\n"). i. w.h> main () { int n. printf ("Enter the Source vertex please\n"). s[15]. for (i = 1. scanf ("%d". num. #include<stdio. j. From a given vertex in a weighted connected graph. for (i = 1. printf ("Enter 999 if the edgeis not present or for the self loop\n"). i <= n. clrscr(). v. j <= n. j++) scanf ("%d".

for (w = 1. for (w = 1. w++) if (s[w] == 0 && dist[w] < min) { min = dist[w]. u = w. Dept of MCA 2009 30 . w <= n. dist[i]). i <= n. w <= n. dist[v] = 0.1. } s[u] = 1. for (i = 1. } } } printf ("VERTEX\tDESTINATION\tCOST\n"). v. for (num = 2. i. num <= n .Algorithms Lab Manual s[v] = 1. i++) printf (" %d\t %d\t\t %d\n". num++) { min = 999. w++) { if (s[w] == 0) { if (dist[w] > (dist[u] + cost[u][w])) dist[w] = (dist[u] + cost[u][w]).

Algorithms Lab Manual getch(). } OUTPUT Enter the vertices please n = 5 Enter the cost of the edges please Enter 999 if the edge is not present or for the self loop The cost of the edges are : 999 1 2 999 999 1 999 3 4 999 2 3 999 5 6 999 4 5 999 6 999 999 6 6 999 Enter the Source vertex please : 1 VERTEX DESTINATION 1 1 1 2 1 3 1 4 1 5 COST 0 1 2 5 8 Dept of MCA 2009 31 .

printf("\n\nthe sorted array elements are\n\n").h> void quicksort(int[].i++) printf("\n%d".&ch).n-1).i<n. for(i=0.int. void main() { int i.Algorithms Lab Manual 8.int.ch=1. Sort a given set of elements using Quick sort method and determine the time taken to sort the elements. printf("\n\n do u wish to continue (0/1)\n").&n). #include<stdio. clrscr(). for(i=0. while(ch) { printf("\n enter the number of elements\n"). printf("\n enter the array elements\n").int).h> #include<conio. quicksort(a.i++) scanf("%d".a[20]. the number of elements in the list to be sorted and plot a graph of the time taken versus n. Repeat the experiment for different values of n. int partition(int[]. Dept of MCA 2009 32 .a[i]).0. scanf("%d".&a[i]).int). scanf("%d".n.i<n.

k.j.i. } } int partition(int a[]. j=high.low.int low. a[j]=temp. quicksort(a.low. a[i]=a[j].mid+1.int low. if(i<j) { temp=a[i].int high) { int mid.high).temp.high). key=a[low]. while(i<=j) { while(i<=high && key>=a[i]) i=i+1.mid-1). quicksort(a. while(key<a[j]) j=j-1. Dept of MCA 2009 33 . i=low+1. if(low<high) { mid=partition(a.Algorithms Lab Manual } getch().int high) { int key. } void quicksort(int a[].

a[j]=a[low]. } } return j. a[low]=k.824176 Dept of MCA 2009 34 . } OUTPUT enter the number of elements 5 enter the elements to be sorted 8 5 2 4 1 the sorted list of elements are: 1 2 4 5 8 time taken:0.Algorithms Lab Manual } else { k=a[j].

main () { clrscr(). y. for (i = 1. count=0. check_cycle (). flag = 0. cost[20][20]. i++) for (j = 1. k. totalcost = 0. int a[20]. scanf ("%d". min. i <= n.1) { check_cycle (). printf ("Enter the cost of the matrix please\n"). update (). printf ("Enter the number of vertices please\n"). if (flag) { printf ("%d ---> %d = %d\n". Dept of MCA 2009 35 . n. i. temp. find_min ().h> #include<conio. while (min != 999 && count != n . &cost[i][j]). j++) scanf ("%d". x. j. Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal's algorithm#include<stdio.h> int root[10]. x. &n). j <= n. void find_min (). y.Algorithms Lab Manual 9.

update (). count++. } if (count < n . i <= n. } cost[x][y] = cost[y][x] = 999. i++) Dept of MCA 2009 36 . } void find_min () { min = 999. find_min (). } void check_cycle () { if ((root[x] == root[y]) && (root[x] != 0)) flag = 0. for (i = 1. getch(). totalcost += cost[x][y].Algorithms Lab Manual cost[x][y]). else flag = 1. else printf ("The graph is connected & the min cost is %d\n". totalcost).2) printf ("The graph is not connected\n").

} } void update () { if (root[x] == 0 && root[y] == 0) root[x] = root[y] = x. j++) if (min > cost[i][j]) { min = cost[i][j]. y = j. j <= n. } } Dept of MCA 2009 37 . else if (root[x] == 0) root[x] = root[y].Algorithms Lab Manual for (j = 1. for (i = 1. x = i. else if (root[y] == 0) root[y] = root[x]. i++) if (root[i] == temp) root[i] = root[x]. else { temp = root[y]. i <= n.

Algorithms Lab Manual OUTPUT Enter the number of vertices please 4 Enter the cost of the matrix please 999 1 5 2 1 999 999 999 5 999 999 3 2 999 3 999 1 ---> 2 = 1 1 ---> 4 = 2 3 ---> 4 = 3 The graph is connected & the min cost is 6 Dept of MCA 2009 38 .

h> Dept of MCA 2009 39 .Algorithms Lab Manual 10. a. #include<stdio. Print all the nodes reachable from a given starting node in a digraph using Breadth First Search method.

j.i++) visited[i]=dis[i]=0.j++) scanf("%d".i).i<=n.n.&a[i][j]). visited[v]=1.i<=n.visited[20].i.Algorithms Lab Manual #include<conio. front=rear=0.int n) { int queue[40]. printf("\n \t press enter for other source vertex\n"). printf("\n Enter the adjacency matrix\n").front.h> void distance(int.n).int). int a[10][10]. for(i=1. for(i=1. } } void distance(int v.rear. clrscr().&n). printf("\n Enter the number of vertices in the diagraph:"). void main() { int i. getch(). Dept of MCA 2009 40 .j<=n.j.dis[20].i++) for(j=1. scanf("%d". for(i=1. distance(i. queue[rear++]=v.i++) { printf("\n\t the starting vertex is %d\n".i<=n.

dis[j]).j. queue[rear++]=j.Algorithms Lab Manual do { i=queue[front++]. } OUTPUT Enter the number of vertices in the diagraph:4 Enter the adjacency matrix 0111 0001 0000 0010 the starting vertex is 1 the vertex 1 to 2 is of distance=1 Dept of MCA 2009 41 . for(j=1. printf("\n\t the vertex %d to %d is of distance=%d\n". } } while(front<rear).j++) if(a[i][j] && !visited[j]) { dis[j]=dis[i]+1. visited[j]=1.j<=n.v.

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 .

} } } void main() { int n.Algorithms Lab Manual 10 b. clrscr().&n).v. #include<stdio. scanf("%d".cost[10][10]. Dept of MCA 2009 43 .int u.v<n.int s[]) { int v. for(v=0.cost. printf("\n enter the number of nodes\n").int cost[10][10].s).h> void dfs(int n.h> #include<conio.v++) { if(cost[u][v]==1 && s[v]==0) { dfs(n.s[10]. Check whether a given graph is connected or not using DFS method.connected.i. s[u]=1.flag .j.

i<n.j<n. } } connected=0. for(j=0. } if(flag==0) connected=1.i<n.cost. for(i=0. } if(connected==1) printf("graph is connected\n").j. getch().&cost[i][j]). else printf("graph is not connected\n"). dfs(n.i++) s[i]=0.Algorithms Lab Manual printf("\n enter the adjacency matrix\n"). flag=0. Dept of MCA 2009 44 .j++) { scanf("%d".s).i++) { if(s[i]==0) flag=1. for(i=0.j++) { for(i=0.i<n.j<n.i++) { for(j=0.

Algorithms Lab Manual } OUTPUT Case 1: enter the number of nodes 4 enter the adjacency matrix 0001 0000 0010 0001 graph is not connected Case 2: enter the number of nodes 4 enter the adjacency matrix 1111 1111 1111 Dept of MCA 2009 45 .

. void display(int).sn} of n positive integers whose sum is equal to a given positive integer d. clrscr(). #include<stdio.d. void main() { int subset(int. A suitable message is to be displayed if the given problem instance doesn't have a solution. For example..8}.n. int i..count=0. 2.int). 5. int flag = 0. 8} and d = 9 there are two solutions {1.. Dept of MCA 2009 46 ..2. 6.s2..Algorithms Lab Manual 1111 graph is connected 11.6} and {1. Find a subset of a given set S == {sl. if S== (1.h> int s[10].set[10].

scanf("%d". } } Dept of MCA 2009 47 . printf("\nEnter the sum\n"). } int subset(int sum.&d).&s[i]). subset(sum.Algorithms Lab Manual printf("Enter the Number of elements in the set\n"). display(count). else { set[count]=s[i].&n). count--. return. count++. printf("enter the set values\n").i+1). subset(sum+s[i].++i) scanf("%d".i<n.0). scanf("%d".int i) { if(sum == d) { flag = 1. subset(0. for(i=0. if(flag == 0) printf(" There is no solution \n").i+1). printf(" The Program Output is:\n"). getch(). } if(sum>d || i>=n) return.

i++) printf("%d ".set[i]). for(i=0. printf("{ ").i<count.Algorithms Lab Manual void display(int count) { int i. printf("}\n"). } OUTPUT Enter the Number of elements in the set 5 enter the set values 1 2 5 6 8 Enter the sum 9 Dept of MCA 2009 48 .

Algorithms Lab Manual The Program Output is: {126} {18} Dept of MCA 2009 49 .

k.p[25].i<126. gets(t). m=strlen(p). while(k<=m-1 && p[m-1-k] == t[ik]) k++.h> #include<stdlib. while(i<=n-1) { k=0. for(i=0. clrscr().flag=0.m. printf("Enter the Text\n").j<=m-2. if(k == m) { Dept of MCA 2009 50 .h> void main() { int table[126].j. printf("Enter the Pattern\n"). i=m-1.j++) table[p[j]]=m-1-j.Algorithms Lab Manual 12. a. for(j=0. int n.i. char t[100]. #include<stdio. Implement Horspool algorithm for String Matching. n=strlen(t). gets(p).i++) table[i]=m.

break.Algorithms Lab Manual printf("The position of the pattern is %d\n". getch(). flag=1.i-m+2). } else i=i+table[t[i]]. } if(!flag) printf("Pattern is not found in the given text\n"). } OUTPUT Case 1: Enter the Text acharya_computer_science_engineering Enter the Pattern science The position of the pattern is 18 Dept of MCA 2009 51 .

Algorithms Lab Manual Case 2: Enter the Text acharya_computer_sceince_engineering Enter the Pattern cj Pattern is not found in the given text Dept of MCA 2009 52 .

scanf("%d%d". for(i=0.j++) c[i][j]=c[i-1][j-1]+c[i-1][j].h> #include<conio.i<=n.n.j<=k. printf("\n").&n. clrscr().h> void main() { int i. b.c[i][j]).k. } Dept of MCA 2009 53 .j++) c[i][j]=0.i++) for(j=0.j++) if(c[i][j]!=0) printf("\t%d".i++) { for(j=0. c[i][i]=1.i<=n. for(i=0.&k). #include<stdio. printf("\n enter the value of n & k\n").j<=k.i++) { c[i][0]=1.i<=n. Find the Binomial Co-efficient using Dynamic Programming.j<=i-1. for(i=0.j.i++) for(j=1.Algorithms Lab Manual 12. printf("\n the table for valuation is\n").i<=n. } for(i=2.c[50][50].

%d) is %d\n".c[n][k]).k. } OUTPUT enter the value of n & k 63 the table for valuation is 1 1 1 1 2 1 1 3 3 1 Dept of MCA 2009 54 .Algorithms Lab Manual printf("\n\t the binomial coefficient of C(%d. getch().n.

3) is 20 Dept of MCA 2009 55 .Algorithms Lab Manual 1 1 1 4 5 6 6 10 15 4 10 20 the binomial coefficient of C(6.

for(i=1.mincost. printf("\n enter the number of nodes\n").t[20][20].i<=n.j<=n.i++) for(j=1.j<=n.i<=n. #include<stdio. } Dept of MCA 2009 56 . Find Minimum Cost Spanning Tree of a given undirected graph using Prims algorithm.a[20]. int i. clrscr().c.&cost[i][j]). for(i=1.l. l=j.j++) { if(minimum>=cost[i][j]) { minimum=cost[i][j].near1[20].j. printf("\n enter the adjacency matrix\n").Algorithms Lab Manual 13. k=i.k.min.h> #include<conio. minimum=cost[1][1].&n).n.minimum.j++) scanf("%d".h> void main() { int cost[20][20].i++) for(j=1.b. scanf("%d".

for(i=2. for(j=1. printf("\n"). } } Dept of MCA 2009 57 .i<=n. { min=a[j]. c=near1[j].i++) { if(cost[i][l]<cost[i][k]) near1[i]=l.i<=n-1. b=j.j<=n. t[1][2]=l. t[1][1]=k.j++) { if(near1[j]!=0) { a[j]=cost[j][near1[j]]. for(i=1.i++) { min=999. } near1[k]=near1[l]=0.Algorithms Lab Manual } mincost=minimum. else near1[i]=k.

getch(). } OUTPUT enter the number of nodes 4 enter the adjacency matrix 999 1 5 2 1 999 999 999 5 999 999 3 2 999 3 999 Dept of MCA 2009 58 . near1[b]=0. } printf("\n\ the cost of minimum spanning tree is=%d".k++) if((near1[k]!=0) && (cost[k][near1[k]]>cost[k][b])) near1[k]=b. for(k=1.k<=n.mincost).Algorithms Lab Manual } mincost=mincost+cost[b][c].

printf("\n enter the adjacency matrix\n"). clrscr().n.&a[i][j]).k.j. void main() { int i.i++) for(j=1.h> int a[10][10].j++) scanf("%d". Compute the transitive closure of a given directed graph using Warshall's algorithm. for(k=1.i++) Dept of MCA 2009 59 .k++) for(i=1. printf("\n enter the number of vertices\n").&n). scanf("%d".k<=n.h> #include<conio.Algorithms Lab Manual the cost of minimum spanning tree is=6 14.j<=n. for(i=1.i<=n.i<=n. #include<stdio.

} getch().j++) printf("\t %d". for(i=1.j++) a[i][j]=a[i][j] || a[i][k] && a[k][j]. printf("\n\t the tranitive closure is\n"). } OUTPUT Dept of MCA 2009 60 . printf("\n").j<=n.i<=n.Algorithms Lab Manual for(j=1.j<=n.i++) { for(j=1.a[i][j]).

Algorithms Lab Manual enter the number of vertices 4 enter the adjacency matrix 0100 0001 1010 0000 the tranitive closure is 0 1 0 1 0 0 0 1 1 1 1 1 0 0 0 0 Dept of MCA 2009 61 .

clrscr(). for(j=1.&n). if(k==n) { printf("\n %d solution".i<=n.i++) Dept of MCA 2009 62 .int).j)) { x[k]=j. queens(k.count).h> #include<math.Algorithms Lab Manual 15. void queens(int. void main() { int n. printf("\n enter the number of queens to be placed\n").h> int x[20]. scanf("%d".int n) { int i. int place(int.n).j. } void queens(int k. Implement N Queen's problem using Back Tracking #include<stdio.int).count=1. count++.j++) { if(place(k. for(i=1.h> #include<conio.j<=n.k=1.

i++) if((x[i]==j) || (abs(x[i]j))==abs(i-k)) return 0. } OUTPUT enter the number of queens to be placed 4 1 solution 1 row <---> 2 column 2 row <---> 4 column 3 row <---> 1 column 4 row <---> 3 column 2 solution Dept of MCA 2009 63 . for(i=1. getch().x[i]).int j) { int i.i<k. } } } int place(int k. return 1.Algorithms Lab Manual printf("\n \t %d row <---> %d column". } else queens(k+1.n).i.

Algorithms Lab Manual 1 row <---> 3 column 2 row <---> 1 column 3 row <---> 4 column 4 row <---> 2 column Dept of MCA 2009 64 .

Sign up to vote on this title
UsefulNot useful