#define max 100 #define infinity 999 int tspdp(int c[][max],int tour[],int star,int n); int main() { int n; int i,j,c[max][max]; int tour[max],cost; cout<<"Travelling Salesman Problem Using Dynamic Programming\n"; cout<<"\nEnter number of cities to traverse"; cin>>n; cout<<"Enter cost matrix\n"<<endl; for(i=0;i<n;i++) for(j=0;j<n;j++) { cin>>c[i][j]; if(c[i][j]==0) c[i][j]=999; } for(i=0;i<n;i++) tour[i]=i; cost=tspdp(c,tour,0,n); cout<<"Minimum Cost:"<<cost<<endl; cout<<"Tour:\n"; for(i=0;i<n;i++) cout<<tour[i]+1<<"-"; cout<<"1\n"; getch(); return 0; } int tspdp(int c[][max],int tour[],int start,int n) { int i,j,temp[max],mintour[max]; int mincost,ccost; if(start==n-2) return c[tour[n-2]][tour[n-1]]+c[tour[n-1]][0]; mincost=infinity; for(i=start+1;i<n;i++) { for(j=0;j<n;j++) temp[j]=tour[j]; temp[start+1]=tour[i]; temp[i]=tour[start+1];
if(c[tour[start]][tour[i]]+(ccost=tspdp(c,temp,start+1,n))<mincost) { mincost=c[tour[start]][tour[i]]+cost; for(k=0;k<n;k++) mintour[k]=temp[k]; } } for(i=o;i<n;i++) tour[i]=mintour[i]; return mincost; } </n;i++) </n;k++) </mincost) </n;j++) </n;i++) </tour[i]+1<<"-"; </n;i++) </cost<<endl; </n;i++) </n;j++) </n;i++) </endl;
#include<iostream.h> #include<conio.h> int main() { int i,j,k,n,min,g[20][20],c[20][20],s,s1[20][1],s2,lb; clrscr(); cout<<"\n TRAVELLING SALESMAN PROBLEM"; cout<<"\n Input number of cities:"; cin>>n; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { c[i][j]=0; }} for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(i==j) continue; else{ cout<<"input"<<i<<"to"<<j<<"cost:"; cin>>c[i][j]; } } } for(i=2;i<=n;i++) { g[i][0]=c[i][1]; } for(i=2;i<=n;i++) { for(j=2;j<=n;j++) { if(i!=j) g[i][j]=c[i][j]+g[j][0]; } } for(i=2;i<=n;i++) { for(j=2;j<=n;j++) { if(i!=j) break; } } for(k=2;k<=n;k++){
if(i!=k && j!=k){ if((c[i][j]+g[i][k])<(c[i][k]+g[k][j])) { g[i][j]=c[i][j]+g[j][k]; s1[i][j]=j; } else { g[i][1]=c[i][k]+g[k][j]; s1[i][1]=k; } } } min=c[1][2]+g[2][1]; s=2; for(i=3;i<n;i++) { if((c[i][i]+g[i][i])<min) { min=c[1][i]+g[i][1]; s=i; } } int y=g[i][1]+g[i][j]+g[i][i]; lb=(y/2); cout<<"Edge Matrix"; for(i=1;i<=n;i++) { cout<<"\n"; for(j=1;j<=n;j++) { cout<<"\t"<<c[i][j]; } } cout<<"\n min"<<min; cout<<"\n\b"<<lb; for(i=2;i<=n;i++) { if(s!=i && s1[s][1]!=i) { s2=i; } } cout<<"\n"<<1<<"-->"<<s<<"-->"<<s1[s][1]<<"-->"<<s2<<"-->"<<1<<"\n"; getch(); return (0); }
OUTPUT: TRAVELING SALESMAN PROBLEM Input number of cities: 3 input1to2cost:20 input1to3cost:12 input2to1cost:33 input2to3cost:23 input3to1cost:34 input3to2cost:12 Edge Matrix 0 20 12 33 0 23 34 12 0 min21 3915 1-->2-->3-->1
if((cp>fp)&&(k==n)) { fp=cp; fw=cw; for(j=1;j<=n;j++) x[j]=y[j]; } } } int back::bound(int cp,int cw,int k) { int i,b,c; b=cp; c=cw; for(i=k+1;i<=n;i++) { c=c+w[i]; if(c<max) b=b+p[i]; else { return(b+(1-(c-max)/w[i])*p[i]); } } return b; } void main() { clrscr(); cout<<"\n\n\tKnapsack Using Backtracking\n"; back obj; obj.get(); k=1; cp=0; cw=0; obj.knapsack(k,cp,cw); cout<<"\nSelected Object are:\n"; for(i=1;i<=n;i++) { if(x[i]==1) cout<<"\nObject :"<<" "<<i; } cout<<"\nMax Profit of the knapsack is:"<<fp; cout<<"\nTotal weight of the knapsack is:"<<fw; getch(); }
OUTPUT: Knapsack Using Backtracking Enter the Capacity:20 Enter the no.of Object:3 Enter the weight of the object{w}1:20 Enter the profit of the object{p}1:34 Enter the weight of the object{w}2:25 Enter the profit of the object{p}2:39 Enter the weight of the object{w}3:13 Enter the profit of the object{p}3:44 Selected Object are: Object : 1 Object : 2 Object : 3 Max Profit of the knapsack is:117 Total weight of the knapsack is:58
KNAPSACK PROBLEM
cout<<"\n\nTotal Weight of Knapsack is:"<<max_cap; getch(); } void main() { clrscr(); cout<<"\nKnapsack Problem using Greedy Method:"; getdata(); knapsack(p,w,x,max,n); display(); getch(); } OUTPUT: Knapsack Problem using Greedy Method: Enter the capacity of knapsack bag:6 Enter the number of objects:5 Enter the weight of objects {w[i]}1:3 Enter the profit of object{p[i]}1:25 Enter the weight of objects{w[i]}2:2 Enter the profit of object{p[i]}2:20 Enter the weight of objects{w[i]}3:1 Enter the profit of object{p[i]}3:15. Enter the weight of objects {w[i]}4:4 Enter the profit of object{p[i]}4:40 Enter the weight of objects {w[i]}5:5 Enter the profit of object{p[i]}5:50 The optimal solution Becomes Object Weight Profit I w[i] p[i] 3 25 2 20 1 15 4 40 5 50 Total Profit of Knapsack is: 65 Total Weight of Knapsack is: 6 X x[i] 0 0 1 0 1
C++ programs for the implementation of Breadth First Search(BFS) for a given graph
#include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10]; main() { int m; cout <<"enterno of vertices"; cin >> n; cout <<"ente no of edges"; cin >> m; cout <<"\nEDGES \n"; for(k=1;k<=m;k++) { cin >>i>>j; cost[i][j]=1; } cout <<"enter initial vertex"; cin >>v; cout <<"Visitied vertices\n"; cout << v; visited[v]=1; k=1; while(k<n) { for(j=1;j<=n;j++) if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1) { visit[j]=1; qu[rare++]=j; } v=qu[front++]; cout<<v << " "; k++; visit[v]=0; visited[v]=1; } } OUTPUT enter no of vertices 9 ente no of edges9 EDGES 12 23 15 14 47 78 89 26 57 enter initial vertex1 Visited vertices 12 4 5 3 6 7 8 9
C++ programs for the implementation of Depth-first search(DFS) for a given graph
#include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10]; main() { int m; cout <<"enterno of vertices"; cin >> n; cout <<"ente no of edges"; cin >> m; cout <<"\nEDGES \n"; for(k=1;k<=m;k++) { cin >>i>>j; cost[i][j]=1; } cout <<"enter initial vertex"; cin >>v; cout <<"ORDER OF VISITED VERTICES"; cout << v <<" "; visited[v]=1; k=1; while(k<n) { for(j=n;j>=1;j--) if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1) { visit[j]=1; stk[top]=j; top++; } v=stk[--top]; cout<<v << " "; k++; visit[v]=0; visited[v]=1; } } OUTPUT enterno of vertices9 ente no of edges9 EDGES 12 23 26 15 14 47 57 78 89 enter initial vertex1 ORDER OF VISITED VERTICES1 2 3 6 4 7 8 9 5
} void bst::inordertrav() { inorder(root); } void bst::inorder(tree_node *ptr) { if(ptr!=NULL) { inorder(ptr->left); cout<<" "<<ptr->data<<" "; inorder(ptr->right); } } void bst::postordertrav() { postorder(root); } void bst::postorder(tree_node *ptr) { if(ptr!=NULL) { postorder(ptr->left); postorder(ptr->right); cout<<" "<<ptr->data<<" "; } } void bst::preordertrav() { preorder(root); } void bst::preorder(tree_node *ptr) { if(ptr!=NULL) { cout<<" "<<ptr->data<<" "; preorder(ptr->left); preorder(ptr->right); } } void main() { bst b; b.insert(52); b.insert(25); b.insert(50); b.insert(15); b.insert(40); b.insert(45); b.insert(20); cout<<"inorder"<<endl; b.inordertrav(); cout<<endl<<"postorder"<<endl; b.postordertrav(); cout<<endl<<"preorder"<<endl; b.preordertrav(); getch(); }
C++ programs to implement the Prims algorithm to generate a minimum cost spanning tree
#include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10],u; main() {
int m,c; cout <<"enterno of vertices"; cin >> n; cout <<"ente no of edges"; cin >> m; cout <<"\nEDGES Cost\n"; for(k=1;k<=m;k++) { cin >>i>>j>>c; cost[i][j]=c; } for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(cost[i][j]==0) cost[i][j]=31999; cout <<"ORDER OF VISITED VERTICES"; k=1; while(k<n) { m=31999; if(k==1) { for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(cost[i][j]<m) { m=cost[i][j]; u=i; } } else { for(j=n;j>=1;j--) if(cost[v][j]<m && visited[j]!=1 && visit[j]!=1) { visit[j]=1; stk[top]=j; top++; m=cost[v][j]; u=j; } } cost[v][u]=31999; v=u; cout<<v << " "; k++; visit[v]=0; visited[v]=1; }
OUTPUT enterno of vertices7 ente no of edges9 EDGES Cost 1 6 10 6 5 25 5 4 22 4 3 12 3 2 16 2 7 14 5 7 24 4 7 18 1 2 28 ORDER OF VISITED VERTICES1 6 5 4 3 2
C++ program that uses dynamic programming algorithm to solve the optimal binary search tree problem
#include<iostream> #include<conio.h> #include<stdio.h> using namespace std; #define MAX 10 int find(int i,int j); void print(int,int); int p[MAX],q[MAX],w[10][10],c[10][10],r[10][10],i,j,k,n,m; char idnt[7][10]; main() {
cout << "enter the no, of identifiers"; cin >>n; cout <<"enter identifiers"; for(i=1;i<=n;i++) gets(idnt[i]); cout <<"enter success propability for identifiers"; for(i=1;i<=n;i++) cin >>p[i]; cout << "enter failure propability for identifiers"; for(i=0;i<=n;i++) cin >> q[i]; for(i=0;i<=n;i++) { w[i][i]=q[i]; c[i][i]=r[i][i]=0; w[i][i+1]=q[i]+q[i+1]+p[i+1]; r[i][i+1]=i+1; c[i][i+1]=q[i]+q[i+1]+p[i+1]; } w[n][n]=q[n]; r[n][n]=c[n][n]=0; for(m=2;m<=n;m++) { for(i=0;i<=n-m;i++) { j=i+m; w[i][j]=w[i][j-1]+p[j]+q[j]; k=find(i,j); r[i][j]=k; c[i][j]=w[i][j]+c[i][k-1]+c[k][j]; } } cout <<"\n"; print(0,n); }
int find(int i,int j) { int min=2000,m,l; for(m=i+1;m<=j;m++) if(c[i][m-1]+c[m][j]<min) { min=c[i][m-1]+c[m][j]; l=m; } return l; } void print(int i,int j) { if(i<j) puts(idnt[r[i][j]]);
enter the no, of identifiers4 enter identifiers do if int while enter success propability for identifiers3 3 1 1 enter failure propability for identifiers2 3 1 1 1 tree in preorder form if do int while