Travelling Salesman Problem Program

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

TRAVELLING SALESMAN PROBLEM

PROGRAM

#include<iostream.h>

#include<conio.h>

mincost(int city);

int a[10][10],visited[10],n,cost=0;

void main()

int i,j;

clrscr();

cout<<"\n\t TRAVELING SALESMAN PROBLEM";

cout<<"\n\t****************************:";

cout<<"\n enter the number of cities:";

cin>>n;

cout<<"\n enter the cost of matrix:\n\n";

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

a[i][j]=a[j][i];

cin>>a[i][j];

for(i=1;i<=n;i++)

visited[i]=0;
cout<<"\n The city:";

mincost(1);

cout<<"\n\n Minimum cost is "<<cost;

getch();

int least(int c)

int i,nc=999;

int kmin,min=999;

for(i=1;i<=n;i++)

if((a[c][i]!=0)&&(visited[i]==0))

if(a[c][i]<=min)

min=a[i][1]+a[c][i];

kmin=a[c][i];

nc=i;

if(min!=999)

cost+=kmin;

return(nc);
}

mincost(int city)

int i,ncity;

visited[city]=1;

cout<<city<<"->";

ncity=least(city);

if(ncity==999)

ncity=1;

cout<<ncity;

cost+=a[city][ncity];

return 0;

mincost(ncity);

return 0;

}
OUTPUT:

TRAVELING SALESMAN PROBLEM

****************************:

Enter the number of cities: 3

Enter the cost of matrix:

0 5 8

5 0 3

8 3 0

The city: 1->2->3->1

Minimum cost is 16

You might also like