TRAVELING SALESMAN USING BRANCH AND BOUND TECHNIQUE
TRAVELING SALESMAN USING BRANCH AND
BOUND TECHNIQUE
#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
RESULT:
Thus, the above program has been compiled, executed and verified successfully.
TRAVELING
SALESMAN USING BRANCH AND
BOUND TECHNIQUE
Hello Vive..i did try your program but what appear in the MS Prompt in the "min21" part would be
ReplyDeletemin-858993440
-858993437
1-->2-->858993460-->3-->1
I am using Microsoft Visual C++ 2010 Express and the only part that was omitted was "clscr();"
Best regard.
minimum cost is getting wrong in this code
ReplyDeleteMinimum cost for the cited example should be 79. Please check your code, and rectify the errors.
ReplyDeletewrong answer
ReplyDeleteCode works.Keep sharing Artificial Intelligence Online Training
ReplyDeleteWhat is mean by 3915 in output
ReplyDelete