Professional Documents
Culture Documents
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) { 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 Dept of MCA 2009 1
Algorithms Lab Manual 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(); 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); } 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]) Dept of MCA 2009 2
Algorithms Lab Manual 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) 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 choice 1 Enter the number of elements 3 Enter the elements of an array 4 8 12 Enter the elements to be searched 12
Dept of MCA
2009
Algorithms Lab Manual 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 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 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.
Dept of MCA
2009
#include<stdio.h> #include<conio.h> #include<time.h> 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]) { a[j]=a[i]; j=i; i=2*j; } else break; } a[j]=item; } Dept of MCA 2009 5
Algorithms Lab Manual 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"); 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",(endstart)/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 Dept of MCA 2009 6
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.
Dept of MCA
2009
#include<stdio.h> #include<conio.h> #include<time.h> #define max 20 void 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) { 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",(endstart)/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 8
} }
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++]; else t[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 enter the elements 6 3 4 1 9 the sorted array is 1 3 4 6 9 Dept of MCA 2009 9
time taken=0.824176
Dept of MCA
2009
10
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]) 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(); }
Dept of MCA
2009
11
OUTPUT enter the number of elements 5 enter the elements to be sorted 8 3 5 1 9 the sorted list of elements are: 1 3 5 8 9 time taken:0.824176
Dept of MCA
2009
12
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) { 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; Dept of MCA 2009 13
Algorithms Lab Manual break; } } j++; if(j>n) { printf("\n topological order is not possible\n"); getch(); exit(0); } } } printf("\n topological order obtained is...\n"); for(i=1;i<p;i++) printf("\t%d",ver[i]); getch(); }
OUTPUT enter the number of vertices 4 enter the adjacency matrix 0111 0001 0000 0010 topological order obtained is... 1 2 4 3
Dept of MCA
2009
14
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"); } 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
15
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
16
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(); 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]; else v[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"); } printf("\nThe maximum profit is %d",v[n][max]); printf("\nThe most valuable subset is:{"); j=max;
Dept of MCA
2009
17
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 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 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
Dept of MCA
2009
18
7. From a given vertex in a weighted connected graph, find shortest paths 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 edge is 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]; } 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])) Dept of MCA 2009 19
Algorithms Lab Manual 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]); 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
Dept of MCA
2009
20
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> #include<time.h> void quicksort(int[],int,int); int partition(int[],int,int); void main() { int i,n,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 array elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); begin=clock(); quicksort(a,0,n-1); end=clock(); printf("\n\nthe sorted array elements are\n\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(); } void quicksort(int a[],int low,int high) { int mid; delay(200); 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) { Dept of MCA 2009 21
Algorithms Lab Manual 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; } else { k=a[j]; a[j]=a[low]; a[low]=k; } } return j; }
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.824176
Dept of MCA
2009
22
9. Find Minimum Cost Spanning Tree of a given undirected graph using 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, 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 Dept of MCA 2009 23
Algorithms Lab Manual flag = 1; } void find_min () { min = 999; for (i = 1; i <= n; i++) 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
24
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
25
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> #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; 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); } Dept of MCA 2009 26
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 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
27
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++) { 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) Dept of MCA 2009 28
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 1111 graph is connected
Dept of MCA
2009
29
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"); 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--; Dept of MCA 2009 30
void display(int count) { int i; printf("{ "); for(i=0;i<count;i++) printf("%d ",set[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 The Program Output is: {126} {18}
Dept of MCA
2009
31
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) { 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(); }
Dept of MCA
2009
32
OUTPUT Case 1: Enter the Text acharya_computer_science_engineering Enter the Pattern science The position of the pattern is 18 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
33
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"); } printf("\n\t the binomial coefficient of C(%d,%d) is %d\n",n,k,c[n][k]); getch(); }
Dept of MCA
2009
34
OUTPUT enter the value of n & k 63 the table for valuation is 1 1 1 1 2 1 1 3 3 1 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
35
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; } } mincost=minimum; for(i=1;i<=n;i++) { if(cost[i][l]<cost[i][k]) near1[i]=l; else near1[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) {
Dept of MCA
2009
36
a[j]=cost[j][near1[j]]; if(min>a[j]) { min=a[j]; c=near1[j]; b=j; printf("\n"); } } } 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 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
37
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++) 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(); }
Dept of MCA
2009
38
OUTPUT 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
39
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++) printf("\n \t %d row <---> %d column",i,x[i]); getch(); } else queens(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; }
Dept of MCA
2009
40
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 1 row <---> 3 column 2 row <---> 1 column 3 row <---> 4 column 4 row <---> 2 column
Dept of MCA
2009
41