Professional Documents
Culture Documents
Algorithms
Algorithms
ALGORITH MS
Name: Roll No: V. Sangeetha 25369 B. Sc. (H) Computer Science III Semester, 2nd Year ARSD College
INDEX
S.No.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.
Program
WAP to implement Assembly Line Scheduling WAP to find the Longest Common Subsequence WAP to implement Matrix Chain Multiplication WAP to implement the Recursive Activity Selector WAP to implement the Iterative Activity Selector WAP to implement Insertion Sort algorithm WAP to implement Heap Sort algorithm WAP to implement Quick Sort algorithm WAP to implement Randomized Quick Sort algorithm WAP to implement Radix sort algorithm WAP to implement Bucket sort algorithm WAP to implement Counting Sort algorithm WAP to implement Priority Queue (using max_heap)
Remarks
//Program to implement Assembly Line Scheduling algorithm #include<iostream.h> #include<conio.h> #define numstn 6 #define numlines 2 void cost(int a[][numstn]); void trftime(int t[][numstn-1]); void entry(int e[]); void exit(int x[]); void fastestway(int a[][numstn],int t[][numstn-1],int e[],int x[],int n,int f[][numstn],int l[][numstn],int *f2,int *l2); void printstn(int l[][numstn],int n,int l1); void main() { clrscr(); int f[numlines][numstn]; int l[numlines][numstn]; int a[numlines][numstn]; int t[numlines][numstn-1]; int e[numlines]; int x[numlines]; int n=numstn; int f1,l1;
cost(a); trftime(t); entry(e); exit(x); fastestway(a,t,e,x,n,f,l,&f1,&l1); printstn(l,n,l1); for(int i=0;i<numlines;i++) { cout<<"\n"; for(int j=0;j<numstn;j++) { cout<<f[i][j]<<"\t"; } } cout<<"\n"; for(int p=0;p<numlines;p++) { cout<<"\n"; for(int q=0;q<numstn-1;q++) { cout<<l[p][q]<<"\t"; } } getch(); } void cost(int a[][numstn]) { for(int i=0;i<numlines;i++)
{ for(int j=0;j<numstn;j++) { cout<<"Enter the cost of ("<<i+1<<","<<j+1<<") : "; cin>>a[i][j]; } } } void trftime(int t[][numstn-1]) { for(int i=0;i<numlines;i++) { for(int j=0;j<numstn-1;j++) { if(i==0) { cout<<"Enter the transfer time between ("<<i+1<<","<<j+1<<") and ("<<i+2<<","<<j+2<<") : "; cin>>t[i][j]; } else { cout<<"Enter the transfer time between ("<<i+1<<","<<j+1<<") and ("<<i<<","<<j+2<<") : "; cin>>t[i][j]; } } }
} void entry(int e[]) { for(int i=0;i<numlines;i++) { cout<<"Enter entry time of assembly line "<<i+1<<" : "; cin>>e[i]; } } void exit(int x[]) { for(int i=0;i<numlines;i++) { cout<<"Enter exit time of assembly line "<<i+1<<" : "; cin>>x[i]; } } void fastestway(int a[][numstn],int t[][numstn-1],int e[],int x[],int n,int f[][numstn],int l[][numstn],int *f2,int *l2) { f[0][0]=e[0]+a[0][0]; f[1][0]=e[1]+a[1][0]; for(int z=1;z<n;z++) { if(f[0][z-1]+a[0][z]<=f[1][z-1]+t[1][z-1]+a[0][z]) { f[0][z]=f[0][z-1]+a[0][z]; l[0][z-1]=1;
} else { f[0][z]=f[1][z-1]+t[1][z-1]+a[0][z]; l[0][z-1]=2; } if(f[1][z-1]+a[1][z]<=f[0][z-1]+t[0][z-1]+a[1][z]) { f[1][z]=f[1][z-1]+a[1][z]; l[1][z-1]=2; } else { f[1][z]=f[0][z-1]+t[0][z-1]+a[1][z]; l[1][z-1]=1; } } if(f[0][n-1]+x[0]<=f[1][n-1]+x[1]) { *f2=f[0][n-1]+x[0]; *l2=0; } else { *f2=f[1][n-1]+x[1]; *l2=1; } }
void printstn(int l[][numstn],int n,int l1) { int i=l1; cout<<"\nline "<<i+1<<" station "<<n<<endl; for(int b=n-2;b>=0;b--) { i=l[i][b]; cout<<"line "<<i<<" station "<<b+1<<endl; i=i-1; } }
OUTPUT
Enter the cost of (1,1) : 7 Enter the cost of (1,2) : 9 Enter the cost of (1,3) : 3 Enter the cost of (1,4) : 4 Enter the cost of (1,5) : 8 Enter the cost of (1,6) : 4 Enter the cost of (2,1) : 8 Enter the cost of (2,2) : 5 Enter the cost of (2,3) : 6 Enter the cost of (2,4) : 4 Enter the cost of (2,5) : 5 Enter the cost of (2,6) : 7
Enter the transfer time between (1,1) and (2,2) : 2 Enter the transfer time between (1,2) and (2,3) : 3 Enter the transfer time between (1,3) and (2,4) : 1 Enter the transfer time between (1,4) and (2,5) : 3 Enter the transfer time between (1,5) and (2,6) : 4 Enter the transfer time between (2,1) and (1,2) : 2 Enter the transfer time between (2,2) and (1,3) : 1 Enter the transfer time between (2,3) and (1,4) : 2 Enter the transfer time between (2,4) and (1,5) : 2 Enter the transfer time between (2,5) and (1,6) : 1 Enter entry time of assembly line 1 : 2 Enter entry time of assembly line 2 : 4 Enter exit time of assembly line 1 : 3 Enter exit time of assembly line 2 : 2
line 1 station 6 line 2 station 5 line 2 station 4 line 1 station 3 line 2 station 2 line 1 station 1
9 12
18 16
20 22
24 25
32 30
35 37
1 1
2 2
1 1
1 2
2 2
void print_lcs(char b[20][20],char x[],int i,int j); void lcs(char x[],char y[]) { int m=strlen(x); int n=strlen(y); char c[20][20]; char b[20][20]; for(int i=1;i<=m;i++) c[i][0]=0; for(int j=0;j<=m;j++) c[0][j]=0; for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { if(x[i]==y[i]) { c[i][j]=c[i-1][j-1]+1;
b[i][j]='/'; } else if(c[i-1][j]>=c[i][j-1]) { c[i][j]=c[i-1][j]; b[i][j]='|'; } else { c[i][j]=c[i][j-1]; b[i][j]='-'; } } } cout<<"\n The longest common sequence found is: "<<endl; print_lcs(b,x,m,n); }
void print_lcs(char b[20][20], char x[], int i, int j) { if((i==0)||(j==0)) return; if(b[i][j]=='/') { print_lcs(b,x,i-1,j-1); cout<<x[i]<<" "; } else if(b[i][j]=='|')
void main() { clrscr(); char x[20],X[20],y[20],Y[20]; cout<<"\n Enter first sequence\t"<<endl; cin>>x; cout<<"\n Enter second sequence: "<<endl; cin>>y; for(int i=0;i<strlen(x);i++) X[i+1]=x[i]; for(i=0;i<strlen(y);i++) Y[i+1]=y[i]; lcs(X,Y); getch(); }
OUTPUT
Enter first sequence abscf Enter second sequence: abodrsc The longest common sequence found is: absc
void main() { clrscr(); int a[20][20],p[10],n,i,j,m[20][20],q; cout<<"Enter the no. of matrices: "<<endl; cin>>n; for(i=1;i<=n;i++) { cout<<"Enter the dimension of "<<i<<"matrix"<<endl; for(j=1;j<=2;j++) { cin>>a[i][j]; } } for(i=1;i<n+1;i++) { p[0]=a[1][1]; p[i]=a[i][2]; }
for(i=1;i<=n;i++) { m[i][i]=0; } for(int l=2;l<=n;l++) { for(i=1;i<=n-l+1;i++) { j=i+ l-1; m[i][j]=175751; for(int k=i;k<=j-1;k++) { q=m[i][k]+m[k+1][j]+(p[i-1]*p[k]*p[j]); if(q<m[i][j] && q>0) { m[i][j]=q; } } } }
OUTPUT
Enter the no. of matrices: 5 Enter the dimension of 1matrix 2 3 Enter the dimension of 2matrix 3 4 Enter the dimension of 3matrix 4 5 Enter the dimension of 4matrix 5 6 Enter the dimension of 5matrix
//Program to implement Recursive activity selector algorithm #include<iostream.h> #include<conio.h> #define MAX 10000
void starting_time(int n, int s[100]); void finishing_time(int n, int f[100]); void recursive_activity_selector(int s[100], int f[100], int i, int j);
void main() { clrscr(); int n, s[100], f[100]; cout<<"\n\n Enter number of activities : "; cin>>n; starting_time(n, s); finishing_time(n, f); recursive_activity_selector(s, f, 0, n+1); getch(); } void starting_time(int n, int s[100]) {
cout<<"\n\n Enter starting times of activities : "; s[0]=0; for(int i=1; i<=n; i++) cin>>s[i]; s[n+1]=MAX; }
void finishing_time(int n, int f[100]) { cout<<"\n\n Enter finishing times of activities : "; f[0]=0; for(int i=1; i<=n; i++) cin>>f[i]; f[n+1]=MAX; cout<<"\n\n Compatible activities are : \n"; } void recursive_activity_selector(int s[100], int f[100], int i, int j) { int m=i+1; while((m<j)&&(s[m]<f[i])) m=m+1; if(m<j) { cout<<"A["<<m<<"]\t"; recursive_activity_selector(s, f, m, j); } }
OUTPUT
Enter number of activities : 11 Enter starting times of activities : 1 3 0 5 3 5 6 8 8 2 12 Enter finishing times of activities : 4 5 6 7 8 9 10 11 12 13 14 Compatible activities are : A[1] A[4] A[8] A[11]
void starting_time(int n, int s[100]); void finishing_time(int n, int f[100]); void iterative_activity_selector(int s[100], int f[100], int n);
void main() { clrscr(); int n, s[100], f[100]; cout<<"\n\n Enter number of activities : "; cin>>n; starting_time(n, s); finishing_time(n, f); iterative_activity_selector(s, f, n); getch(); }
cout<<"\n\n Enter starting times of activities : "; s[0]=0; for(int i=1; i<=n; i++) cin>>s[i]; s[n+1]=MAX; }
void finishing_time(int n, int f[100]) { cout<<"\n\n Enter finishing times of activities : "; f[0]=0; for(int i=1; i<=n; i++) cin>>f[i]; f[n+1]=MAX; }
void iterative_activity_selector(int s[100], int f[100], int n) { cout<<"\n\n Compatible activities are : \n"; int i=1; cout<<"\n A["<<i<<"]\t"; for(int m=2; m<=n; m++) { if(s[m]>=f[i]) { cout<<"A["<<m<<"]\t"; i=m; }
} }
OUTPUT
Enter number of activities : 11 Enter starting times of activities : 1 3 0 5 3 5 6 8 8 2 12 Enter finishing times of activities : 4 5 6 7 8 9 10 11 12 13 14 Compatible activities are : A[1] A[4] A[8] A[11]
arr[k]=arr[k-1]; arr[k+1]=temp; } } } } void display(int arr[50],int sz) { cout<<"Sorted Array :\n"; for(int i=0;i<sz;i++) cout<<arr[i]<<" "; } void main() { clrscr(); int arr[50],size; cout<<"Enter the size of the array : "; cin>>size; input(arr,size); insertion(arr,size); display(arr,size); getch(); }
OUTPUT
Enter the size of the array : 7 Enter element 1 : 0 Enter element 2 : 4 Enter element 3 : 2 Enter element 4 : 7 Enter element 5 : 8 Enter element 6 : 3 Enter element 7 : 4 Sorted Array : 0234478
void maxheapify(int *a,int i); void buildmaxheap(int *a); void heapsort(int *a);
int n,size;
void main()
clrscr();
int a[100]; cout<<"Enter the size of the heap : "<<endl; cin>>n; size=n; cout<<"Enter the values of the heap : "<<endl;
getch();
{ int l=2*i; int r=2*i+1; int largest; if(l<=n && a[l]>a[i]) largest=l;
{ buildmaxheap(a); for(int i=n;i>=2;i--) { int temp=a[1]; a[1]=a[i]; a[i]=temp; n=n-1; maxheapify(a,1); } cout<<"The sorted array is :"<<endl;
OUTPUT
Enter the size of the heap : 6 Enter the values of the heap : 31 72 50 19 12 3 the sorted array is : 3 12 19 31 50 72
void quick(int *,int p,int r); int partition(int *,int p,int r);
void main() { clrscr(); int a[100],s; cout<<"Enter the no of elements of the array : "<<endl; cin>>s; cout<<"Enter the values"<<endl; for(int i=0;i<s;i++) cin>>a[i]; quick(a,0,s-1); cout<<"The sorted array is"<<endl; for(i=0;i<s;i++) cout<<a[i]<<" "; getch();
OUTPUT
Enter the no of elements of the array : 6 Enter the values 23 12 10 15 56 36 The sorted array is 10 12 15 23 36 56
a[i]=a[j]; a[j]=temp; } } temp=a[i+1]; a[i+1]=a[r]; a[r]=temp; return i+1; } void quicksort(int a[],int l,int r) { if(l<r) { int i=partition(a,l,r); quicksort(a,l,i-1); quicksort(a,i+1,r); } } void main() { clrscr(); int a[20],n,i; for(i=1;i<=20;++i) a[i]=0; cout<<"\nEnter the no.of elements"; cin>>n; cout<<"\nEnter the elements"; for(i=1;i<=n;++i)
OUTPUT
Enter the no of elements5 Enter the elements6 4 2 7 9 Sorted array is: 24679
void radixsort(int x[],int n,int m) { int i,j,k,c,d,flag,exp; for(i=1;i<=m;i++) { exp=pow(10,i-1); for(j=0;j<n-1;j++) { for(k=0;k<n-j-1;k++) { c=(x[k]/exp)%10; d=(x[k+1]/exp)%10; if(c>d) { flag=x[k]; x[k]=x[k+1]; x[k+1]=flag;
} } } } }
void main() { clrscr(); int a[20],n,s,i; cout<<"Enter the no. of digits: "<<endl; cin>>s; cout<<"Enter the no. of elements in the array: "<<endl; cin>>n; cout<<"Enter the elements: "<<endl; for(i=0;i<n;i++) cin>>a[i]; radixsort(a,n,s); cout<<"The sorted order: "<<endl; for(i=0;i<n;i++) cout<<a[i]<<"\n"; getch(); }
OUTPUT
Enter the no of elements of the array : 5 Enter the no of digits of the nos : 3 Enter the elements of the array. 450 230 540 125 100 The sorted array is 100 125 230 450 540
} } count++; }
} void main() { clrscr(); float a[100]; int n; cout<<" Enter the no of elements : "; cin>>n; node z[10]; cout<<"enter the elements........"<<endl; for(int i=0;i<n;i++) cin>>a[i]; for(int g=0;g<10;g++)
{ float d=(float)g/10; for(int j=0;j<n;j++) { if(a[j]>=d && a[j]<(float)d+0.1) { z[g].create(a[j]); } } } cout<<"the sorted array is......"<<endl; for(i=0;i<10;i++) z[i].display(); getch(); }
OUTPUT
Enter the no of elements : 5 Enter the elements 0.54 0.23 0.54 0.12 0.10 The sorted array is 0.1 0.12 0.23 0.54 0.54
} b[i]=count; }
OUTPUT
Enter the no of elements of the array : 5 Enter the nos 2 6 4 7 9 Enter the max no: 9 The sorted array is 2 4 6 7 9
class priority { int heapsize, largest, a[50], count; public: priority() { count=0; heapsize=0; } void swap(int &a, int &b); int heap_extract_max(); int parent(int); int left(int); int right(int); int heap_max(); void heapify(int); void heap_increase_key(int, int); void max_heap_insert(int); void getarray();
void getcount(); };
int priority::heap_extract_max() { if(heapsize<1) { cout<<"\n\n Heap Underflow!!!"; return 0; } else { int max=a[1]; a[1]=a[heapsize]; heapsize-=1; heapify(1); return max; } }
void priority::heapify(int i) { int l=left(i); int r=right(i); int largest=i; if(l<=heapsize) { count++;
if(a[l]>a[i]) largest=l; else largest=i; } if(r<=heapsize) { count++; if(a[r]>a[largest]) largest=r; } if(largest!=i) { swap(a[largest], a[i]); heapify(largest); } }
void priority::swap(int &a, int &b) { int temp=0; temp=a; a=b; b=temp; }
if(key<a[i]) { cout<<"\n\n New key smaller than current key!!!"; count++; } else { a[i]=key; while((i>1)&&(a[parent(i)]<a[i])) { count++; int temp=a[i]; a[i]=a[parent(i)]; a[parent(i)]=temp; i=parent(i); } } }
void priority::getarray() {
cout<<"\n\n The resultant array is : "; for(int i=1; i<=heapsize; i++) cout<<" "<<a[i]; }
void priority::getcount() { cout<<"\n\n The number of comparisons are : "<<count; } int priority::parent(int i) { return (i/2); } int priority::left(int i) { return (2*i); } int priority::right(int i) { return (2*i+1); }
priority ob; do { cout<<"\n\n 1. Insert an element" <<"\n 2. Display array" <<"\n 3. Extract maximum element" <<"\n\n Enter your choice : "; cin>>ch; switch(ch) { case 1 : cout<<"\n\n Enter the value : "; cin>>num; ob.max_heap_insert(num); break; case 2 : ob.getarray(); ob.getcount(); break; case 3 : cout<<"Maximum element in array is : "<<ob.heap_extract_max(); break; default : cout<<"\n\n Wrong choice!! Try again...!"; } cout<<"\n\n Do you want to continue?"; cin>>ch1; }while((ch1=='y')||(ch1=='Y')); getch(); }
OUTPUT