Вы находитесь на странице: 1из 51

Design and Analysis of Algorithms

Index
S. No. Contents Page No. 2 3 5 6 8 10 12 14 16 18 20 21 23 25 26 28 30 31 35 37 39 41 44 45 46 47 48

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27.

Bubble Sort Implementation Selection Sort Implementation Insertion Sort Implementation Quick Sort Implementation Merge Sort Implementation Heap Sort Implementation Comparison Counting Sort Implementation Sequential Search Implementation Quick Sequential Search Implementation Binary Search Implementation Brute Force Pattern Matching Technique Brute Force Technique for finding closest points Matrix Multiplication Matrix Chain Multiplication Coin Problem using Greedy Strategy Coin Problem using Dynamic Programming Knap Sack Problem Prim Algorithm Implementation Breadth First Search Implementation N-Queen problem using Backtracking Subset Problem Using Backtracking N-Queen Problem using FIFO Branch & Bound Calculating Factorial Exponential xn Euclid Algorithm Implementation Fibonacci numbers using Dynamic Programming Sub Set Problem using FIFO Branch and Bound Technique

] ****************************

SCS DAVV, INDORE

Design and Analysis of Algorithms 1. Program for Bubble Sort Implementation #include<iostream.h> #include<conio.h> #include<alloc.h> void BubbleSort(int *a,int n); int n; int *a; void readData(); void display(); int main() { clrscr(); readData(); BubbleSort(a,n); display(); free(a); getch(); return 0; } void readData() { cout<<"Enter the number of elements in the array "; cin>>n; a=(int*)malloc(sizeof(int)*(n)); cout<<"\nEnter elements : \n"; for(int i=0;i<n;i++) cin>>a[i]; } void display() { cout<<"\n\nThe elements are : \n\n"; for(int i=0;i<n;i++) cout<<"\t"<<a[i]; } void BubbleSort(int *a,int n) { if(n>1) { int i=0; while(i<n-1) { if(a[i]>a[i+1]) a[i]^=a[i+1]^=a[i]^=a[i+1]; i++; } BubbleSort(a,n-1); }}

SCS DAVV, INDORE

Design and Analysis of Algorithms 2. Program for Selection Sort Implementation #include<iostream.h> #include<conio.h> #include<alloc.h> void selectionSort(int *a); int n; int *a; void readData(); void display(); int main() { clrscr(); readData(); selectionSort(a); display(); free(a); getch(); return 0; } void readData() { cout<<"Enter the number of elements in the array "; cin>>n; a=(int*)malloc(sizeof(int)*(n+1)); cout<<"\nEnter elements : \n"; for(int i=1;i<=n;i++) cin>>a[i]; } void display() { cout<<"\n\nThe elements are : \n\n"; for(int i=1;i<=n;i++) cout<<"\t"<<a[i]; } void selectionSort(int *a) { int minj,minx; for(int i=1;i<=n-1;i++) { minj=i; minx=a[i];

SCS DAVV, INDORE

Design and Analysis of Algorithms for(int j=i+1;j<=n;j++) { if(a[j]<minx) { minx=a[j]; minj=j; } } a[minj]=a[i]; a[i]=minx; } }

SCS DAVV, INDORE

Design and Analysis of Algorithms 3. Program for Insertion Sort Implementation #include<iostream.h> #include<conio.h> #include<alloc.h> void insertionSort(int *a); int n, *a; void readData(); void display(); int main() { clrscr(); readData(); insertionSort(a); display(); free(a); return 0; } void readData() { cout<<"Enter the number of elements in the array "; cin>>n; a=(int*)malloc(sizeof(int)*(n+1)); cout<<"\nEnter elements : \n"; for(int i=1;i<=n;i++) cin>>a[i]; } void display() { cout<<"\n\nThe elements are : \n\n"; for(int i=1;i<=n;i++) cout<<"\t"<<a[i]; } void insertionSort(int *a) { for(int j=2;j<=n;j++) { int key=a[j]; int i=j-1; while(i>0&& a[i]>key) { a[i+1]=a[i]; i--; } a[i+1]=key; } }

SCS DAVV, INDORE

Design and Analysis of Algorithms 4. Program for Quick Sort Implementation #include<iostream.h> #include<conio.h> #include<alloc.h> void quickSort(int *a, int p , int r); int partition(int *a,int p, int r); int n; int *a; void readData(); void display(); int main() { clrscr(); readData(); quickSort(a,1,n); display(); free(a); getch(); return 0; } void readData() { cout<<"Enter the number of elements in the array "; cin>>n; a=(int*)malloc(sizeof(int)*(n+1)); cout<<"\nEnter elements : \n"; for(int i=1;i<=n;i++) cin>>a[i]; } void display() { cout<<"\n\nThe elements are : \n\n"; for(int i=1;i<=n;i++) cout<<"\t"<<a[i]; } void quickSort(int *a,int p , int r) { if(p<r) { int q=partition(a,p,r); quickSort(a,p,q-1); quickSort(a,q+1,r); } }

SCS DAVV, INDORE

Design and Analysis of Algorithms

int partition(int *a,int p, int r) { int x=a[r]; int i=p-1; for(int j=p;j<=r-1;j++) { if(a[j]<=x) { i++; int temp=a[i]; a[i]=a[j]; a[j]=temp; // a[i]^=a[j]^=a[i]^=a[j]; //a[i]^=a[i+1]^=a[i]^=a[i+1]; } } // a[i+1]^=a[r]^=a[i+1]^=a[r]; int temp=a[i+1]; a[i+1]=a[r]; a[r]=temp; return i+1; }

SCS DAVV, INDORE

Design and Analysis of Algorithms 5. Program for Merge Sort Implementation #include<iostream.h> #include<conio.h> #include<alloc.h> void mergeSort(int *a, int p , int r); void merge(int *a,int p,int q, int r); int n; int *a; void readData(); void display(); int main() { clrscr(); readData(); mergeSort(a,1,n); display(); free(a); getch(); return 0; } void readData() { cout<<"Enter the number of elements in the array "; cin>>n; a=(int*)malloc(sizeof(int)*(n+1)); cout<<"\nEnter elements : \n"; for(int i=1;i<=n;i++) cin>>a[i]; } void display() { cout<<"\n\nThe elements are : \n\n"; for(int i=1;i<=n;i++) cout<<"\t"<<a[i]; } void mergeSort(int *a,int p , int r) { if(p<r) { int q=(p+r)/2;

SCS DAVV, INDORE

Design and Analysis of Algorithms mergeSort(a,p,q); mergeSort(a,q+1,r); merge(a,p,q,r); } } void merge(int *a,int p,int q, int r) { int n1=q-p+1; int n2=r-q; int *l=(int*)malloc(sizeof(int)*(n1+1)); int *R=(int*)malloc(sizeof(int)*(n2+1)); for(int i=1;i<=n1;i++) l[i]=a[p+i+-1]; for(int j=1;j<=n2;j++) R[j]=a[q+j]; l[n1+1]=32767; R[n2+1]=32767; i=j=1; for(int k=p;k<=r;k++) { if(l[i]<=R[j]) { a[k]=l[i]; i++; } else { a[k]=R[j]; j++; } } }

SCS DAVV, INDORE

Design and Analysis of Algorithms 6. Program for Heap Sort Implementation #include<iostream.h> #include<conio.h> #include<alloc.h> void heapSort(int *a); void buildMaxHeap(int *a); void maxHeapify(int *a, int i); int n; int *a; int heapSize; void readData(); void display(); int main() { clrscr(); readData(); heapSort(a); display(); free(a); getch(); return 0; } void readData() { cout<<"Enter the number of elements in the array "; cin>>n; a=(int*)malloc(sizeof(int)*(n+1)); cout<<"\nEnter elements : \n"; for(int i=1;i<=n;i++) cin>>a[i]; } void display() { cout<<"\n\nThe elements are : \n\n"; for(int i=1;i<=n;i++) cout<<"\t"<<a[i]; } void heapSort(int *a) { buildMaxHeap(a);

SCS DAVV, INDORE

10

Design and Analysis of Algorithms for(int i=n;i>=2;i--) { a[1]^=a[i]^=a[1]^=a[i]; heapSize--; maxHeapify(a,1); } } void buildMaxHeap(int *a) { heapSize=n; for(int i=n/2;i>=1;i--) maxHeapify(a,i); } void maxHeapify(int *a, int i) { int l=2*i; int r=2*i+1; int largest; if(l<=heapSize&&a[l]>a[i]) largest=l; else largest=i; if(r<=heapSize&&a[r]>a[largest]) largest=r; if(largest!=i) { a[i]^=a[largest]^=a[i]^=a[largest]; maxHeapify(a,largest); } }

SCS DAVV, INDORE

11

Design and Analysis of Algorithms 7. Program for Comparison Counting Sort Implementation #include<iostream.h> #include<conio.h> #include<alloc.h> int *comparisionCountingSort(int *a,int n); int n; int *a; void readData(); void display(int *a); int main() { clrscr(); readData(); int *s=comparisionCountingSort(a,n); display(s); free(a); getch(); return 0; } void readData() { cout<<"Enter the number of elements in the array "; cin>>n; a=(int*)malloc(sizeof(int)*(n)); cout<<"\nEnter elements : \n"; for(int i=0;i<n;i++) cin>>a[i]; } void display(int *a) { cout<<"\n\nThe elements are : \n\n"; for(int i=0;i<n;i++) cout<<"\t"<<a[i]; } int* comparisionCountingSort(int *a,int n) { int *s=(int*)malloc(sizeof(int)*(n)); int *count=(int*)malloc(sizeof(int)*(n)); for(int i=0;i<n;i++) count[i]=0;

SCS DAVV, INDORE

12

Design and Analysis of Algorithms for( i=0;i<n-1;i++) for(int j=i+1;j<n;j++) if(a[i]<a[j]) count[j]++; else count[i]++; for( i=0;i<n;i++) s[count[i]]=a[i]; return s; }

SCS DAVV, INDORE

13

Design and Analysis of Algorithms 8. Program for Sequential Search Implementation #include<iostream.h> #include<conio.h> #include<alloc.h> int search(int); int *a,n; void readData(); void display(); int main() { clrscr(); int key; readData(); cout<<"\nEnter the element to be searched "; cin>>key; cout<<"\nThe position of element " << key<<" is "<<search(key); display(); getch(); return 0; } void readData() { cout<<"Enter the number of elements in the array "; cin>>n; a=(int*)malloc(sizeof(int)*n); cout<<"\nEnter elements : \n"; for(int i=0;i<n;i++) cin>>a[i]; } void display() { cout<<"\n\nThe elements are : \n\n"; for(int i=0;i<n;i++) cout<<"\t"<<a[i]; } int search(int key) {

SCS DAVV, INDORE

14

Design and Analysis of Algorithms

int i=0; while(i<n) { if(a[i]==key) return i+1; i++; } return -1; }

SCS DAVV, INDORE

15

Design and Analysis of Algorithms 9. Program for Quick Sequential Search Implementation #include<iostream.h> #include<conio.h> #include<alloc.h> int search(int); int *a,n; void readData(); void display(); int main() { clrscr(); int key; readData(); cout<<"\nEnter the element to be searched "; cin>>key; cout<<"\nThe position of element " << key<<" is "<<search(key); display(); free(a); getch(); return 0; } void readData() { cout<<"Enter the number of elements in the array "; cin>>n; a=(int*)malloc(sizeof(int)*(n+1)); cout<<"\nEnter elements : \n"; for(int i=0;i<n;i++) cin>>a[i]; } void display() { cout<<"\n\nThe elements are : \n\n"; for(int i=0;i<n;i++) cout<<"\t"<<a[i]; } int search(int key)

SCS DAVV, INDORE

16

Design and Analysis of Algorithms { int i=0; a[n]=key; while(a[i]!=key) i++; return i==n?-1:i+1; }

SCS DAVV, INDORE

17

Design and Analysis of Algorithms 10. Program for Binary Search Implementation #include<iostream.h> #include<conio.h> #include<alloc.h> void binarySearch(int low,int high,int key); int *a,n,found; int mid; void readData(); void display(); int main() { clrscr(); int key; readData(); cout<<"\nEnter the element to be searched "; cin>>key; binarySearch(0,n,key); if(found) cout<<"\nThe position of element " << key<<" is "<<mid+1; else cout<<"\nnot found"; display(); free(a); getch(); return 0; } void readData() { cout<<"Enter the number of elements in the array "; cin>>n; a=(int*)malloc(sizeof(int)*n); cout<<"\nEnter elements : \n"; for(int i=0;i<n;i++) cin>>a[i]; } void display() { cout<<"\n\nThe elements are : \n\n"; for(int i=0;i<n;i++)

SCS DAVV, INDORE

18

Design and Analysis of Algorithms cout<<"\t"<<a[i]; } void binarySearch(int low,int high,int key) { if(low<=high) { found=0; mid=(low+high)/2; if(a[mid]==key) { found=1; } else { if(a[mid]>key) binarySearch(low,mid-1,key); else binarySearch(mid+1,high,key); } } }

SCS DAVV, INDORE

19

Design and Analysis of Algorithms 11. Program for pattern matching in the text using Brute Force Technique #include<iostream.h> #include<conio.h> #include<string.h> int bruteforceMatch(char *t,char *p); void main() { char *t,*p; cout<<"enter text \n"; cin>>t; cout<<"enter pattern \n"; cin>>p; int pos=bruteforceMatch(t,p); if(pos!=-1) { cout<<"\n the position of pattern p in text t is "<<pos; } else cout<<"pattern is not matching"; getch(); } int bruteforceMatch(char *t,char *p) { int n=strlen(t); int m=strlen(p); for(int i=0;i<=n-m;i++) { int j=0; while (j<m && p[j]==t[i+j]) j++; if(j==m) return i; } return -1; }

SCS DAVV, INDORE

20

Design and Analysis of Algorithms 12. Program for finding closest points using Brute Force Technique #include<iostream.h> #include<conio.h> #include<math.h> #include<stdio.h> #define MAX 10 struct point { int x; int y; }; void bruteForceClosestPoint(point *p,float *minDis,int *index1,int *index2); int n; void main() { point p[MAX]; cout<<"enter how many points are there :"; cin>>n; cout<<"enter the point(x,y) :"; for(int i=0;i<n;i++) { cout<<"\nEnter x :"; cin>>p[i].x; cout<<"\nEnter y :"; cin>>p[i].y; } int index1,index2; float minDis; bruteForceClosestPoint(p,&minDis,&index1,&index2); cout<<"\nMinimum distance = "<<minDis; printf("\n\npoints are p1(%d,%d) and p2(%d, %d)",p[index1].x,p[index1].y,p[index2].x,p[index2].y); getch(); } void bruteForceClosestPoint(point *p,float *minDis,int *index1,int *index2) { *minDis=100000.00; for(int i=0;i<n-1;i++) for(int j=i+1;j<n;j++) { float d=sqrt(((p[i].x-p[j].x)*(p[i].x-p[j].x))+((p[i].y-p[j].y)*(p[i].y-p[j].y)));

SCS DAVV, INDORE

21

Design and Analysis of Algorithms if(d<(*minDis)) { *minDis=d; *index1=i; *index2=j; } } }

SCS DAVV, INDORE

22

Design and Analysis of Algorithms 13. Program for Matrix Multiplication #include<iostream.h> #include<conio.h> #include<alloc.h> #define MAX 5 void matrixMultiply(int *a[MAX],int rowa, int cola,int *b[MAX], int rowb, int colb); void readData(int *a[MAX],int row, int col); void display(int *b[MAX],int row,int col); int main() { clrscr(); int *a[MAX],*b[MAX]; int row1,row2,col1,col2; cout<<"enter the order of first matrix row & column "; cin>>row1>>col1; cout<<"enter the order of second matrix row & column "; cin>>row2>>col2; if(col1==row2) { cout<<"enter data for first matrix "; readData(a,row1,col1); cout<<"enter data for second matrix "; readData(b,row2,col2); matrixMultiply(a,row1,col1, b, row2, col2); } else { cout<<"matrix multiplication is not possible"; } getch(); return 0; } void readData(int *a[MAX], int row, int col) { for(int i=0;i<row;i++) for(int j=0;j<col;j++) cin>>a[i][j]; } void display(int *a[MAX], int row, int col) { for(int i=0;i<row;i++)

SCS DAVV, INDORE

23

Design and Analysis of Algorithms for(int j=0;j<col;j++) cout<<"\t"<<a[i][j]; cout<<"\n"; } void matrixMultiply(int *a[MAX],int rowa, int cola,int *b[MAX], int rowb, int colb) { int *c[MAX]; for(int i=0;i<rowa;i++) for(int j=0;j<colb;j++) { c[i][j]=0; for(int k=0;k<cola;k++) c[i][j]+=a[i][k]*b[k][j]; } cout<<"result matrix is "; display(c,rowa,colb); }

SCS DAVV, INDORE

24

Design and Analysis of Algorithms 14. Program for Matrix Chain Multiplication #include<iostream.h> #include<conio.h> int p[100],q; long f(int i,int j) /*i stands for position of first matrix,j stands of last matrix*/ { long m; int k,q; if(i==j) return 0; m=32099007; for(k=i;k<=j-1;k++) {q=f(i,k)+f(k+1,j)+p[i-1]*p[k]*p[j]; if(q<m) m=q; } return m; } void main() { int r,c,n,flag=1; clrscr(); cout<<"\t\t\t\tEnter the no of matrix:->"; cin>>n; cout<<"\n\n"; cout<<" ********Enter the dimension of matrix**************"; for(int i=1;i<=n;i++) { if(i==1) { cout<<"\n Enter row of "<<i<<" matrix:->"; cin>>r; p[i-1]=r; } cout<<"\n Enter col of cin>>c; p[i]=c; } cout<<f(1,n)<<"\n"; getch(); }

"<<i<<"

matrix:->";

SCS DAVV, INDORE

25

Design and Analysis of Algorithms 15. Program for returning change for given amt using Greedy Strategy #include<iostream.h> #include<conio.h> int coins[]={100,25,10,5,2,1}; int s[25]; int* make_change(int amt); int i=0; void main() { clrscr(); int amt; cout<<" enter amt to be return : "; cin>>amt; int *sol=make_change(amt); cout<<"\ncoins to be returned is : "<< i<< " these are :\n"; for(int j=0;j<i;j++) cout<<"\t"<<sol[j]; getch(); } int* make_change(int amt) { int n=amt; while(n>0) { if(n>=100) { s[i++]=100; n=n-100; } else{ if(n>=25) { s[i++]=25; n=n-25; } else{ if(n>=10) { s[i++]=10; n=n-10; } else{ if(n>=5) { s[i++]=5; n=n-5; }

SCS DAVV, INDORE

26

Design and Analysis of Algorithms else{ if(n>=1) { s[i++]=1; n=n-1; } } } } } } return s; }

SCS DAVV, INDORE

27

Design and Analysis of Algorithms 16. Program for finding minimum number of coins required for returning given amount using Dynamic Programming. #include<iostream.h> #include<conio.h> #define MAX 3 int C[4][100]; int d[4]={0,1,4,6}; int f(int,int); void main() { clrscr(); for(int i=0;i<4;i++) for(int j=0;j<100;j++) C[i][j]=-1; cout<<"\nenter the amount to be return : " ; int amt; cin>>amt; int minCoins=f(MAX,amt); cout<<"\n Minimum coins required : "<<minCoins; getch(); } int f(int i, int j) { int a, b; if(C[i][j]>-1) return C[i][j]; if(j==0) { C[i][j]=0; return(C[i][j]); } if((i==1)&&(j<d[i])) { C[i][j]=-2; return(C[i][j]); } if(i==1)

SCS DAVV, INDORE

28

Design and Analysis of Algorithms { C[i][j]=1+f(i,j-d[i]); return(C[i][j]); } if(j<d[i]) { C[i][j]=f(i-1,j); return(C[i][j]); } a=f((i-1),j); b=1+f(i,(j-d[i])); if(a<b) C[i][j]=a; else C[i][j]=b; return C[i][j]; }

SCS DAVV, INDORE

29

Design and Analysis of Algorithms 17. Program for Knap Sack Problem #include<stdio.h> #include<conio.h> int n=3; float M=20; float P[4]; float W[4]; float X[4]; void knapsack(); void result(); void main() { clrscr(); P[1]=24; P[2]=15; P[3]=25; W[1]=15; W[2]=10; W[3]=18; knapsack(); result(); getch(); } void knapsack() { float cu = M; int i=1; while(i<=n) { if(W[i]>cu) break; X[i]=1; cu=cu-W[i]; i++; } if(i<=n) X[i]=cu/W[i]; } void result() { int i; for(i=1;i<=n;i++) printf("\nitem no.=%d\tquantity= %f",i,X[i]); }

SCS DAVV, INDORE

30

Design and Analysis of Algorithms 18. Program for Prim Algorithm Implementation #include<stdio.h> #include<conio.h> int n[8],b[7],t[5][5],r[7],iiii=1,y=1,p,op=1,jj=0,min,rr,lo=1; void main() { clrscr(); for(int o=1;o<=7;o++) { r[o]=o; } //for int k,c[8][8]={{0,0,0,0,0,0,0}, {0,0,1,0,4,0,0}, {0,1,0,2,6,4,0}, {0,0,2,0,0,5,6}, {0,4,6,0,0,3,0}, {0,0,4,5,3,0,8}, {0,0,0,6,0,8,0} }; printf("enter random element"); scanf("%d",&p); rr=p; while(op<=6) { // printf("\nrr=%d i=%d op=%d",rr,i,op); b[op]=rr; printf("\nb[op]=%d op=%d",b[op],op); jj=0; for(int j=1;j<=7-op;j++) { // printf("\nb[op]=%d r[j]=%d ",b[op],r[j]); if(b[op]!=r[j]) { jj++; r[jj]=r[j]; } //if } //for // i=op; printf("\nr"); for(int pp=1;pp<7-op;pp++) printf("%d",r[pp]); printf("\n");

SCS DAVV, INDORE

31

Design and Analysis of Algorithms for( pp=1;pp<=7;pp++) printf("%d",b[pp]); int bb=b[lo],ll=1; rr=r[1]; min=c[bb][rr]; y=rr; if(min==0) { while((ll<6-op && min==0) || (min==0 && lo<=op)) { if(ll<6-op) { ll++; rr=r[ll]; min=c[bb][rr]; y=rr; } if(ll==6-op && min==0) { lo++; bb=b[lo]; ll=0; } } } //**************** for finiding min. *********** for( int kk=1;kk<op+1;kk++) zz: { bb=b[kk]; // printf("\nbb=%d",bb); for(int j=ll+1;j<7-op;j++) cc:{ rr=r[j]; // printf("\nrr=%d j=%d",rr,j); if(c[bb][rr]<min && c[bb][rr]!=0) { if(t[bb][rr]!=1) { min=c[bb][rr]; y=rr; } //t==1 else { if(j<=7-op) {

SCS DAVV, INDORE

32

Design and Analysis of Algorithms j++; goto cc; }//ifj<=7 else { if(kk<op+1) { kk++; goto zz; } //kk }//else }//else } //if }//for 2 // ll=0; }//for 1 printf("\n min=%d y=%d",min,y); t[p][y]=1; printf("\n op=%d",op); t[y][p]=1; printf("i=%d",iiii); if(iiii==12) op=5; printf("\n op=%d",op); p=y; rr=y; if(op==6) { goto kl; } iiii++; op++; } kl: printf("hello");/*b[++op]=r[1]; printf("nodes of minimum spanning tree in order they are added are"); for(i=1;i<=6;i++) printf("%d,",b[i]); printf("edges of minimum spanning tree in order they are added are"); for(i=1;i<=6;i++) { for(int j=1;j<=6;j++) { if(t[i][j]==1)

SCS DAVV, INDORE

33

Design and Analysis of Algorithms { printf("(%d,%d)",i,j); printf("\n"); } } } */ }

SCS DAVV, INDORE

34

Design and Analysis of Algorithms 19. Program for Breadth First Search Implementation #include<stdio.h> #include<conio.h> char c[6]; int d[6],p[6],n[6],k=0,l=1,s,q[20],pos; int z=1; void que(int); int t[6][6]={{0,0,0,0,0,0}, {0,0,1,1,0,0}, {0,1,0,0,1,0}, {0,1,0,0,1,1}, {0,0,1,1,0,1}, {0,0,0,1,1,0}}; void main() { for(int i=1;i<=5;i++) n[i]=i; clrscr(); printf("enter the given node"); scanf("%d",&pos); s=n[pos]; for(i=1;i<=5;i++) { if(i!=pos) { c[i]='w'; d[i]=0; p[i]=0; }//if }//for c[pos]='g'; d[pos]=0; p[pos]=0; /* for(int ii=1;ii<=5;ii++) { printf("c[%d]=%c , d[%d]=%d , p[%d]=%d\n",ii,c[ii],ii,d[ii],ii,p[ii]); } */ q[l]=s; que(q[l]); for(int ii=1;ii<=5;ii++) {

SCS DAVV, INDORE

35

Design and Analysis of Algorithms printf("c[%d]=%c , d[%d]=%d , p[%d]=%d\n",ii,c[ii],ii,d[ii],ii,p[ii]); } for( ii=1;ii<=5;ii++) { printf("\nq[%d]=%d",ii,q[ii]); } getch(); }//main() void que(int s) { while(k<=l) { z=1; if(k==0) k=1; while(z<=5) { if(t[s][z]==1 ) { if(c[z]=='w') { c[z]='g'; d[z]=d[s]+1; p[z]=s; q[++l]=z; } }//if z++; }//for //while c[q[k]]='b'; /* for(int ii=1;ii<=5;ii++) { printf("\nq[%d]=%d",ii,q[ii]); } for(ii=1;ii<=5;ii++) { printf("\nc[%d]=%c , d[%d]=%d , p[%d]=%d\n",ii,c[ii],ii,d[ii],ii,p[ii]); } */ k++; que(q[k]); } }

SCS DAVV, INDORE

36

Design and Analysis of Algorithms 20. Program for solving N-Queen problem using Backtracking #include<iostream.h> #include<conio.h> #define MAX 4 int queen(int); int conflict(int); int result[9]; void main(){ clrscr(); if(queen(1)) { for(int i=1;i<=MAX;i++) cout<<"\t"<<result[i]; } else cout<<"result not possible"; getch(); } int queen(int level) { if(level==MAX+1) return 1; int i=1; while(i<=MAX) { result[level]=i; if(conflict(level)) { result[level]=0; i++; } else { if(queen(level+1)) return 1; else { result[level]=0; i++; } } } return 0; }

SCS DAVV, INDORE

37

Design and Analysis of Algorithms

int conflict(int level) { int m,n,p; m=n=p=result[level]; for(int i=level-1;i>=1;i--) { if((result[i]==++m)||(result[i]==--n)||(result[i]==p)) return 1; } return 0; }

SCS DAVV, INDORE

38

Design and Analysis of Algorithms 21. Program for Subset Problem Using Backtracking #include<iostream.h> #include<conio.h> #include<process.h> #define MAX 5 int setResult(int); int check(int); int result[9]; int result1[9]; int sum,sum1=0; int set[9]; void main(){ clrscr(); cout<<"Enter Elements \n"; for(int i=1;i<=MAX;i++) { cin>>set[i]; result[i]=result[1]=0; } cout<<"\n enter sum "; cin>>sum; setResult(1); getch(); } int setResult(int col) { if(col==MAX+1) { cout<<"result not possible"; return 1; } int i=1; while(i<=MAX) { result[col]=i; if(check(col)) { result[col]=0; i++; } else { if(sum==sum1)

SCS DAVV, INDORE

39

Design and Analysis of Algorithms { for(int j=1;j<=MAX;j++) { if(result[j]!=0) result1[result[j]]=1; //cout<<set[result[j]]<<" "; cout<<"\n\n\t"<<set[j]<<"\t"<<result1[j]; } getch(); exit(0); } if(setResult(col+1)) return 1; else { result[col]=0; i++; } } } return 0; } int check(int col) { for(int i=1;i<=MAX;i++) { if((result[col]==result[i]) &&(col!= i)) return 1; } sum1=0; for(i=1;i<=MAX;i++) { if(result[i]!=0) sum1+=set[result[i]]; } if(sum1> sum) return 1; return 0; }

SCS DAVV, INDORE

40

Design and Analysis of Algorithms 22. Program for Solving N-Queen Problem using FIFO Branch and Bound Technique #include<iostream.h> #include<conio.h> #define MAX 50 #define M 5 class state { public: int result[M]; int level; }; class queue{ int front,rear; state a[MAX]; public: queue() { front=-1; rear=0; } void add(state x) { if(front==-1) front=0; if(rear==MAX-1) cout<<"queue is full"; else a[rear++]=x; } int empty() { int i=0; if(front ==-1|| front==rear) i=1; return i; } state remove() { state x; x=a[front++]; if(rear==front) {

SCS DAVV, INDORE

41

Design and Analysis of Algorithms front=-1; rear=0; } return x; } }; state queen(); int conflict(state x); void main() { clrscr(); state x=queen(); if(x.level==-1) cout<<"solution not found"; else { for(int i=1;i<M;i++) cout<<x.result[i]<<"\t"; } getch(); } state queen() { int i; state a,x; a.level=0; queue b; b.add(a); while(!(b.empty())) { x=b.remove(); x.level++; i=1; while(i<M) { x.result[x.level]=i; if(!conflict(x)) { if(x.level==M-1) return x; b.add(x); } i++; }

SCS DAVV, INDORE

42

Design and Analysis of Algorithms } a.level=-1; return a; } int conflict(state x) { int m,n,p; m=n=p=x.result[x.level]; for(int i=x.level-1;i>=1;i--) { if((x.result[i]==++m)||(x.result[i]==--n)||(x.result[i]==p)) return 1; } return 0; }

SCS DAVV, INDORE

43

Design and Analysis of Algorithms 23. Program for calculating Factorial #include<iostream.h> #include<conio.h> int fact(int); int main(){ clrscr(); cout<<"Enter the number "; int n; cin>>n; cout<<"\nThe factorial is "<<fact(n); getch(); return 0; } int fact(int n) { return (n==2?2:(n==0||n==1?1:n*fact(n-1))); }

SCS DAVV, INDORE

44

Design and Analysis of Algorithms 24. Program for calculating Power(X,N) #include<iostream.h> #include<conio.h> int exp(int,int); int main() { clrscr(); cout<<"Enter the number "; int n,x; cin>>x; cout<<"\nEnter the exponent "; cin>>n; cout<<"\nThe power(x,n) is "<<exp(x,n); getch(); return 0; } int exp(int x,int n) { if(n==0)return 1; if(n==1) return x; if(!(n%2)) { int temp=exp(x,n/2); return temp*temp; } else { return x*exp(x,n-1); } }

SCS DAVV, INDORE

45

Design and Analysis of Algorithms 25. Program for finding GCD of two numbers using Euclid Algorithm #include<stdio.h> #include<conio.h> void gcdByEuclid(int,int); void main() { clrscr(); int x,y; printf("\n Enter the first Number :\n"); scanf("%d",&x); printf("\n Enter the second number :\n"); scanf("%d",&y); gcdByEuclid(x,y); getch(); } void gcdByEuclid(int x, int y) { int remainder, dividend, divisor; dividend =x, divisor = y; remainder = dividend % divisor; while (remainder > 0) { dividend=divisor; divisor =remainder; remainder =dividend % divisor; } printf("\n The GCD of input is :"); printf("%d",divisor); }

SCS DAVV, INDORE

46

Design and Analysis of Algorithms 26. Program for generating Fibonacci Number using Dynamic Programming #include<iostream.h> #include<conio.h> int result[50]; int fib(int); void main() { clrscr(); int n=5; cout<<fib(n-1); getch(); } int fib(int n) { if(n==0||n==1) return n; if(n==2) return 1; int a,b,c; if(result[n]>0) return result[n]; if(n%2==0) { a=fib(n/2-1); b=fib(n/2); c=a+b; result[n]=b*(a+c); } else { a=fib((n+1)/2); b=fib((n-1)/2); c=a+b; result[n]=a*a+b*b; } return result[n]; }

SCS DAVV, INDORE

47

Design and Analysis of Algorithms 27. Program for solving Sub Set Problem using FIFO Branch and Bound Technique #include<iostream.h> #include<conio.h> #include<stdlib.h> #define MAX 100 #define M 6 class state { public: state(); int result[M]; int level; }; state::state() { for(int i=1;i<M;i++) result[i]=0; } class queue{ int front,rear; state a[MAX]; public: queue() { front=-1; rear=0; } void add(state x) { if(front==-1) front=0; if(rear==MAX-1) cout<<"queue is full"; else a[rear++]=x; } int empty() { int i=0; if(front ==-1|| front==rear) i=1; return i; }

SCS DAVV, INDORE

48

Design and Analysis of Algorithms

state remove() { state x; x=a[front++]; if(rear==front) { front=-1; rear=0; } return x; } }; state SetResult(); int conflict(state x); int result1[9]; int sum,sum1=0; int set[9]; void main(){ clrscr(); cout<<"Enter Elements \n"; for(int i=1;i<M;i++) { cin>>set[i]; result1[i]=0; } cout<<"\n enter sum "; cin>>sum; state x=SetResult(); if(x.level==-1) cout<<"solution not found"; else { for(int i=1;i<M;i++) cout<<result1[i]<<"\t"; } getch(); } state SetResult() { int i; state a,x; a.level=0;

SCS DAVV, INDORE

49

Design and Analysis of Algorithms queue b; b.add(a); while(!(b.empty())) { x=b.remove(); x.level++; i=1; while(i<M) { x.result[x.level]=i; if(!conflict(x)) { if(sum==sum1) { for(int j=1;j<M;j++) { if(x.result[j]!=0) result1[x.result[j]]=1; //cout<<set[result[j]]<<" "; cout<<"\n\n\t"<<set[j]<<"\t"<<result1[j]; } getch(); exit(0); }

if(x.level==M-1) return x; b.add(x); } i++; } } a.level=-1; return a; } int conflict(state x) { for(int i=1;i<M;i++) { if((x.result[x.level]==x.result[i]) &&(x.level!= i)) return 1; } sum1=0;

SCS DAVV, INDORE

50

Design and Analysis of Algorithms for(i=1;i<M;i++) { if(x.result[i]!=0) sum1+=set[x.result[i]]; } if(sum1> sum) return 1; return 0; }

****************************

SCS DAVV, INDORE

51

Вам также может понравиться