Documentos de Académico
Documentos de Profesional
Documentos de Cultura
1.Implement recursive binary search and linear search and determine the time required 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. 2.Sort a given set of elements using heap sort 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. 3.Sort a given set of elements using merge sort 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. 4.Sort a given set of elements using selection sort 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. 5a.Obtain the topological ordering of vertices in a digraph. 5b.Implement all pairs shortest paths problem using Floyd's algorithm. 6.Implement 0/1 knapsack using dynamic programming. 7.From a given vertex in a weighted connected graph, find shortest paths to other vertices using Dijkstra's algorithm. 8.Sort a given set of elements using quick sort 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. 9.Find the minimum cost spanning tree of a given undirected graph using Kruskal's algorithm. 10a.Print all the nodes reachable from a given starting node in a digraph using BFS method. 10b.Check whether a given graph is connected or not using DFS method. 11.Find a subset of a given set S={s1,s2,s3,.....,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 2 solutions {1,2,6} and {1,8}. A suitable message is to be displayed if the given problem instance does not have a solution. 12a.Implement Horspool algorithm for string matching. 12b.Find the binomial co-efficient using dynamic programming. 13.Find the minimum cost spanning tree of a given undirected graph using Prim's algorithm. 14a.Implement Floyd's algorithm for the all pairs shortest path problem.
14b.Compute the transitive closure of a given digraph using Warshall's algorithm. 15.Implement N Queens problem using back tracking.
} void main() { int i,n,temp; Timer t; clrscr(); printf("\nEnter the number of elements "); scanf("%d",&n); randomize(); printf("\nEnter the search element "); scanf("%d",&ele); printf("\nEnter 1 for linear search and 2 for binary search "); scanf("%d",&i); if(i==1) { for(i=0;i<n;i++) { a[i]=rand(); } pos=n-1; t.start(); temp=lin(); t.stop(); } else { for(i=0;i<n;i++) { a[i]=i; } low=0; high=n-1; t.start();; temp=bin(); t.stop(); } printf("\nElement found at position = %d and time taken = %lf",temp,(double)t.time()); getch(); }
2.Heap sort.
#include<stdio.h> #include<timer.h> #include<stdlib.h> #include<conio.h> void heap(int a[],int n)
{ int i,j,k,temp; for(i=2;i<=n;i++) { j=i; k=j/2; temp=a[j]; while(k>0&&a[k]<temp) { a[j]=a[k]; j=k; k=k/2; } a[j]=temp; } } void heap1(int a[],int n) { int i,j,k,temp; for(i=n/2;i>0;i--) { k=i; temp=a[k]; j=2*k; while(j<=n) { if(j<n&&a[j]<a[j+1]) { j=j+1; } if(temp<a[j]) { a[k]=a[j]; k=j; j=2*k; } else { break; } } a[k]=temp; } } void adjust(int a[],int n) { int i=2,temp=a[1]; while(i<=n) { if(i<n&&a[i]<a[i+1])
{ i=i+1; } if(a[i]>temp) { a[i/2]=a[i]; i=i*2; } else { break; } } a[i/2]=temp; } void main() { int a[10000],n,i,temp; Timer t; clrscr(); printf("\nEnter the value of n "); scanf("%d",&n); for(i=1;i<=n;i++) { a[i]=n-i; } t.start(); heap1(a,n); for(i=n;i>=2;i--) { temp=a[1]; a[1]=a[i]; a[i]=temp; adjust(a,i-1); } t.stop(); printf("\nTimetaken is %lf ",t.time()); getch(); }
3.Merge sort.(method 1)
#include<stdio.h> #include<conio.h> #include<timer.h> #include<stdlib.h> void copy(int a[],int b[],int st,int end) {
int i; for(i=st;i<end;i++) { b[i-st]=a[i]; } } void merge(int a[],int n) { int *b,*c,i,j; if(n==1) { return; } b=(int*)malloc(n/2); copy(a,b,0,n/2); merge(b,n/2); c=(int*)malloc(n-(n/2)); copy(a,c,n/2,n); merge(c,n-(n/2)); j=0; for(i=0;(i+j)<n;) { if(j==n-(n/2)) { a[i+j]=b[i]; i=i+1; } else if(i==(n/2)) { a[i+j]=c[j]; j=j+1; } else if(b[i]>c[j]) { a[i+j]=c[j]; j=j+1; } else { a[i+j]=b[i]; i=i+1; } } } void main() { int *a,n,i; Timer t; clrscr(); printf("\nEnter the value of n ");
scanf("%d",&n); a=(int*)malloc(n); randomize(); for(i=0;i<n;i++) { a[i]=rand(); } t.start(); merge(a,n); t.stop(); printf("\nTime taken = %lf",t.time()); getch(); }
3.Merge sort.(method2)
#include<stdio.h> #include<conio.h> #include<timer.h> #include<stdlib.h> #include<dos.h> void copy(int a[],int *b,int st,int end) { int i; for(i=st;i<=end;i++) { *(b+i-st)=a[i]; } } void merge(int a[],int *b,int *c,int u) { int i,j=0,k=0; for(i=0;i<=u;i++) { if(j<=(u/2)&&(k<=(u-(u/2)-1))) { if(*(b+j)>*(c+k)) { a[i]=*(c+k); k++; } else { a[i]=*(b+j); j++; } }
else if(j<=(u/2)) { a[i]=*(b+j); j++; } else { a[i]=*(c+k); k++; } } } void mergesort(int a[],int u) { int *b,*c,i; if(u==0) { return; } b=(int *)malloc(((u/2)+1)*sizeof(int)); copy(a,b,0,u/2); mergesort(b,u/2); c=(int *)malloc(((u-(u/2)))*sizeof(int)); copy(a,c,(u/2)+1,u); mergesort(c,u-(u/2)-1); merge(a,b,c,u); free(b); free(c); } void main() { int *a,i,n; Timer t; clrscr(); printf("\nEnter the value of n "); scanf("%d",&n); a=(int *)malloc(n); randomize(); for(i=0;i<n/2;i++) { a[i]=2*i; a[i+n/2]=2*i+1; } t.start(); mergesort(a,n); t.stop(); printf("\nThe value is %f ",t.time()); getch(); }
4.Selection sort.
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<malloc.h> #include<timer.h> void sel(int a[],int n) { int i,j,temp,pos; for(i=0;i<n-1;i++) { pos=i; for(j=i+1;j<n;j++) { if(a[j]<a[pos]) { pos=j; } } temp=a[i]; a[i]=a[pos]; a[pos]=temp; } } void main() { int *a,n,i; Timer t; clrscr(); printf("\nEnter the value of n "); scanf("%d",&n); a=(int *)malloc(n); randomize(); for(i=0;i<n;i++) { a[i]=rand(); } t.start(); sel(a,n); t.stop(); printf("\nTime taken = %lf",t.time()); getch(); }
5a.Topological ordering.
#include<stdio.h> #include<conio.h> void main() { int a[20][20],rem[20],ind,n,i,j,flag=0,t=0; clrscr(); printf("\nEnter the value of n "); scanf("%d",&n); printf("\nEnter the adjacency matrix "); for(i=0;i<n;i++) { rem[i]=0; for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } while(flag==0) { flag=1; for(i=0;i<n;i++) { if(rem[i]==0) { ind=0; for(j=0;j<n;j++) { if(!(rem[j]==1||a[j][i]==0)) { ind=1; break; } } if(ind==0) { printf("%s",t==0?"\nTopological ordering is ":""); rem[i]=1; printf("%d ",i+1); flag=0; t++; break; } } } } if(t!=n) { printf("\nTopological ordering is not possible(it can only be partially
ordered)!!"); } getch(); }
5b,14a.Floyd's algorithm.
#include<stdio.h> #include<conio.h> void main() { int i,j,k,a[20][20],n; clrscr(); printf("\nEnter the value of n "); scanf("%d",&n); printf("\nEnter the adjacency matrix "); for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } for(k=0;k<n;k++) { for(j=0;j<n;j++) { for(i=0;i<n;i++) { a[i][j]=a[i][j]<(a[i][k]+a[k][j])?a[i][j]:(a[i][k]+a[k][j]); } } } printf("\nFloyd's shortest path is "); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("%c%d",j==0?'\n':' ',a[i][j]); } } getch(); }
6.0/1 knapsack.
#include<stdio.h> #include<conio.h> void main() { int p[20],w[20],kn[20][20],x[20],i,j,n,weight; printf("\nEnter the value of n "); scanf("%d",&n); printf("\nEnter the price of the items "); for(i=0;i<n;i++) { scanf("%d",&p[i]); } printf("\nEnter the weight of the items "); for(i=0;i<n;i++) { scanf("%d",&w[i]); } printf("\nEnter the weight of the knapsack "); scanf("%d",&weight); printf("\nThe knapsack is "); for(i=0;i<=n;i++) { printf("\n"); for(j=0;j<=weight;j++) { if(i==0||j==0) { kn[i][j]=0; } else if(w[i-1]>j) { kn[i][j]=kn[i-1][j]; } else { kn[i][j]=(kn[i-1][j]>(kn[i-1][j-w[i-1]]+p[i-1]))?kn[i-1][j]:(kn[i-1][j-w[i1]]+p[i-1]); } printf("%d ",kn[i][j]); } } printf("\n\nThe optimal solution is %d",kn[n][weight]); i=n; j=weight; while(i!=0) { if(kn[i][j]==kn[i-1][j]) { x[i-1]=0; i=i-1;
} else { x[i-1]=1; j=j-w[i-1]; i=i-1; } } printf("\n\nThe 0/1 knapsack is "); for(i=0;i<n;i++) { printf("\nX[%d]=%d",i+1,x[i]); } getch(); }
7.Dijkstra's algorithm.(method1)
#include<stdio.h> #include<conio.h> struct prioq { int dist,pr,s; }; void display(struct prioq p[20],int source,int dest,int d) { if(dest==source) { printf("%d",source); return; } display(p,source,p[dest].s,d); printf("->%d",dest); } void main() { struct prioq p[20]; int i,j,n,k,min,pos,source,a[20][20]; clrscr(); printf("\nEnter the value of n "); scanf("%d",&n); printf("\nEnter the adjacency matrix "); for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); }
p[i].pr=0; } printf("\nEnter the source node "); scanf("%d",&source); for(i=0;i<n;i++) { p[i].s=source; p[i].dist=a[source][i]; } pos=source; for(j=0;j<n;j++) { i=pos; p[i].pr=-1; min=9999; for(k=0;k<n;k++) { if(p[i].dist+a[i][k]<p[k].dist) { p[k].dist=p[i].dist+a[i][k]; p[k].s=i; } if(p[k].pr!=-1&&p[k].dist<min) { pos=k; min=p[k].dist; } } } for(i=0;i<n;i++) { printf("\n"); if(i==source) { printf("%d->%d",source,source); } else { display(p,source,i,i); } printf(" %d",p[i].dist); } getch(); }
7.Dijkstra's algorithm.(method2)
#include<stdio.h> #include<conio.h> void display(int par[],int source,int dest1,int dest) { if(source==dest1&&dest1==dest) { printf("%d->%d",source,source); return; } if(source==dest1) { printf("%d",source); return; } display(par,source,par[dest1],dest); printf("->%d",dest1); } void main() { int par[20],dist[20],vist[20],a[20][20],n,i,j,k,min,pos,source; clrscr(); printf("\nEnter the value of n "); scanf("%d",&n); printf("\nEnter the adjacency matrix "); for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } printf("\nEnert the source node "); scanf("%d",&source); for(i=0;i<n;i++) { vist[i]=0; dist[i]=a[source][i]; par[i]=source; } i=source; for(j=0;j<n;j++) { vist[i]=1; min=9999; for(k=0;k<n;k++) { if(dist[i]+a[i][k]<dist[k]) { dist[k]=dist[i]+a[i][k]; par[k]=i;
} if(vist[k]==0&&dist[k]<min) { min=dist[k]; pos=k; } } i=pos; } for(i=0;i<n;i++) { printf("\n"); display(par,source,i,i); printf(" %d",dist[i]); } getch(); }
8.Quick sort.
#include<stdio.h> #include<conio.h> #include<TIMER.H> #include<alloc.h> #include<stdlib.h> int n; int quick(int a[],int l,int h) { int piv,temp,i,j; if(l==h) { return l; } piv=a[l]; i=l+1; j=h; while(i<=j) { for(;i<=h&&a[i]<=piv;i++); for(;j>=(l+1)&&a[j]>=piv;j--); if(j==l) { return l; } else if(i==h+1) { a[l]=a[h]; a[h]=piv;
return h; } else if(i>j) { a[l]=a[j]; a[j]=piv; return(j); } else { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } void quicksort(int a[],int st,int end) { int pos; if(st<end) { pos=quick(a,st,end); quicksort(a,st,pos-1); quicksort(a,pos+1,end); } } void main() { int *a,i; Timer t; clrscr(); printf("\nEnter the value of n "); scanf("%d",&n); a=(int *)malloc(n); randomize(); for(i=0;i<n;i++) { a[i]=rand(); } t.start(); quicksort(a,0,n-1); t.stop(); for(i=0;i<n-1;i++) { if(a[i]>a[i+1]) { printf("%d %d",a[i],i); } } printf("\nTime taken = %lf",t.time());
getch(); }
9.Kruskal's algorithm.
#include<stdio.h> #include<conio.h> void main() { int a[20][20],b[20][20],c[20][20],d[20][20],nod=0,n,val1=0,i,j,k,t,m=0,posx,posy,val; clrscr(); printf("\nEnter the value of n "); scanf("%d",&n); printf("\nEnter the adjacency matrix "); for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); b[i][j]=(i==j?0:a[i][j]); m=m+(b[i][j]?1:0); c[i][j]=0; d[i][j]=0; } } for(m=m/2;m!=0&&(nod!=(n-1));m--) { val=32767; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(b[i][j]!=0&&b[i][j]<val) { posx=i; posy=j; val=b[i][j]; } } } b[posx][posy]=0; b[posy][posx]=0; if(c[posx][posy]==0) { c[posx][posy]=1; c[posy][posx]=1; for(k=0;k<n;k++)
{ for(i=0;i<n;i++) { for(j=0;j<n;j++) { c[i][j]=c[i][j]|(c[i][k]&c[k][j]); } } } val1=val1+a[posx][posy]; nod=nod+1; d[posx][posy]=a[posx][posy]; d[posy][posx]=a[posy][posx]; } } if(nod==n-1) { for(i=0;i<n;i++) { printf("\n"); for(j=0;j<n;j++) { printf("%d ",d[i][j]); } } printf("\nSpanning tree has a cost of %d",val1); } else { printf("\nSpanning tree does not exist!!"); } getch(); }
10a.BFS.
#include<stdio.h> #include<conio.h> void main() { int q[20],a[20][20],r[20],st=0,ed=0,start,n,i,j; clrscr(); printf("\nEnter the value of n "); scanf("%d",&n); printf("\nEnter the adjacency matrix "); for(i=0;i<n;i++) { for(j=0;j<n;j++)
{ scanf("%d",&a[i][j]); r[i]=0; } } printf("\nEnter the start node "); scanf("%d",&start); q[ed++]=start-1; r[start-1]=1; printf("\nNodes reachable from the origin(%d) are %d ",start,start); while(st!=ed) { for(i=0;i<n;i++) { if((r[i]==0)&&a[q[st]][i]==1) { q[ed++]=i; r[i]=1; printf("%d ",i+1); } } st++; } if(ed!=n) { printf("\nAll nodes are not reachable from origin!!"); } getch(); }
10b.DFS.
#include<stdio.h> #include<conio.h> void main() { int a[20][20],n,i,j,st[20],tot=1,top=-1,r[20],flag; clrscr(); printf("\nEnter the value of n "); scanf("%d",&n); printf("\nEnter the adjacency matrix "); for(i=0;i<n;i++) { r[i]=0; for(j=0;j<n;j++) { scanf("%d",&a[i][j]); }
} st[++top]=0; r[0]=1; while(top!=-1) { flag=0; for(j=0;j<n;j++) { if(r[j]==0&&a[st[top]][j]==1) { st[++top]=j; tot++; r[j]=1; flag=1; break; } } if(flag==0) { top=top-1; } } if(tot==n) { printf("\nAll nodes are reachable from the origin!!"); } else { printf("\nAll nodes are not reachable from the origin!!"); } getch(); }
11.Subset.(back tracking-method1)
#include<stdio.h> #include<conio.h> void main() { int s[20],d,sum=0,n,x[20],top=0,i,tot=0; clrscr(); printf("\nEnter the number of values "); scanf("%d",&n); printf("\nEnter the values in ascending order "); for(i=0;i<n;i++) { scanf("%d",&s[i]); }
printf("\nEnter the sum "); scanf("%d",&d); x[top]=-1; printf("\nThe solution to the subset problem is "); while(top>=0) { x[top]=x[top]+1; sum=sum+s[x[top]]; if(sum==d) { printf("\n"); tot=tot+1; for(i=0;i<=top;i++) { printf("%d ",s[x[i]]); } sum=sum-s[x[top]]; } else if(sum>d||top>=n) { sum=sum-s[x[top]]; if(top>=1) { sum=sum-s[x[top-1]]; } top=top-1; } else { top=top+1; x[top]=x[top-1]; } } if(tot==0) { printf("not possible "); } getch(); }
{ int s[20],x[20],n,sum,i,tot,soln=0; long int a; clrscr(); printf("\nEnter the value of n "); scanf("%d",&n); printf("\nEnter the values "); for(i=0;i<n;i++) { scanf("%d",&s[i]); } printf("\nEnter the total sum "); scanf("%d",&tot); printf("\nSolution to the problem is "); for(a=0;a<pow(2,n);a=a+1) { sum=0; for(i=0;i<n;i++) { if((a&(int)pow(2,i))!=0) { x[i]=1; sum=sum+s[i]; } else { x[i]=0; } } if(sum==tot) { soln=soln+1; printf("\n"); for(i=0;i<n;i++) { if(x[i]==1) { printf("%d ",s[i]); } } } } if(soln==0) { printf("is not possible!!"); } getch(); }
getch(); }
13.Prim's algorithm.
#include<stdio.h> #include<conio.h> void main() { int a[20][20],add[20],f[20][20],min,posx,posy,i,j,k,n,tot,flag=0,weight=0; clrscr(); tot=0; printf("\nEnter the value of n "); scanf("%d",&n); printf("\nEnter the adjacency matrix "); for(i=0;i<n;i++) { add[i]=0; for(j=0;j<n;j++) { scanf("%d",&a[i][j]); f[i][j]=0; } } add[0]=1; while(flag==0) { flag=1; min=9999; for(i=0;i<n;i++) { if(add[i]==1) { for(j=0;j<n;j++) { if(add[j]==0&&a[i][j]!=0&&a[i][j]<min) { min=a[i][j]; posx=i; posy=j; } } } } if(min!=9999) { f[posx][posy]=min; f[posy][posx]=min; weight=weight+f[posx][posy]; tot++; flag=0;
add[posx]=1; add[posy]=1; } } if(tot!=n-1) { printf("\nMinimum spannin does not exist!!"); } else { printf("\nMinimum spanning tree with weight=%d is ",weight); for(i=0;i<n;i++) { printf("\n"); for(j=0;j<n;j++) { printf("%d ",f[i][j]); } } } getch(); }
14a.Warshall's algorithm.
#include<stdio.h> #include<conio.h> void main() { int arr[20][20],i,j,k,n; clrscr(); printf("\nEnter the number of nodes "); scanf("%d",&n); printf("\nEnter the adjacency matrix "); for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&arr[i][j]); } } for(k=0;k<n;k++) { for(j=0;j<n;j++) { for(i=0;i<n;i++) { arr[i][j]=arr[i][j]||(arr[i][k]&&arr[k][j]);
} } } printf("\nThe transitive closure formed by Warshalls algorithm is "); for(i=0;i<n;i++) { printf("\n"); for(j=0;j<n;j++) { printf("%d ",arr[i][j]); } } getch(); }
15.n-Queens problem.
#include<stdio.h> #include<conio.h> #include<math.h> void main() { int n,arr[20][20],i,j,k,tot=0,x[20],top; char ch; clrscr(); printf("\nEnter the total number of queens "); scanf("%d",&n); top=0; x[top]=-1; printf("\nThe solution to the %d queens problem is ",n); while(top>=0) { x[top]=x[top]+1; while(x[top]<n) { for(i=0;i<top;i++) { if(x[i]==x[top]||(abs(x[top]-x[i])==abs(top-i))) { break; } } if(i==top) { break; } x[top]=x[top]+1; }
if(x[top]==n) { top=top-1; } else if(top==n-1) { tot=tot+1; ch=getch(); if(ch==0) { ch=getch(); } printf("\n"); for(i=0;i<n;i++) { printf("\n"); for(j=0;j<n;j++) { if(x[i]==j) { printf("Q "); } else { printf("X "); } } } top=top-1; } else { top=top+1; x[top]=-1; } } if(tot==0) { printf("not possible!!"); } else { printf("\n\nThe total number of solutions is %d.",tot); } getch(); }
Note:For time complexity programs the following changes in the Turbo C++ compiler must be made:1.Copy the file "timer.h" from the folder './tc/classlib/include' to the folder './tc/include' 2.Copy all the files(2) from the folder '/.tc/classlib/lib' to the folder './tc/lib' 3.Start the Turbo C++ compiler and click on options. Under options goto linkers and click on libraries. Select the container class option and click on ok. It is priscriber that these files be saved as *.cpp files.