You are on page 1of 6

Ex.

No:5 TRAVELLING SALESMAN PROBLEM


PROBLEM STATEMENT
Write a program to solve traveling salesman problem using
dynamic programming technique.
AIM
To solve traveling salesman problem using dynamic
programming.
ALGORITHM
1. Declare a class dynamic with data members cost matrix,
number of cities, distance array.
2. Declare a private member function to find out minimum of an
array and public member functions to getdata!, display!,
swap!, fact!, permutations and solutions.
". getdata!
o #ead member of cities as n.
o $or value of i from 1 to n by 1 repeat.
$or values of % from 1 to n by 1 repeat.
#ead c&i'&%'
o $or values of ( varying from ) to factn*1! by 1.
+et d&i',)
-. swapa,b!
o +et t,a.
o a,b
o b,t
.. min!
o +et m,d&)'
o $or values of i to factn*1! by 1.
/hec0 if m1d&i'! if so
o #eturn m
2. sol!
o $or values of ( from ) to n*2 by 1 repeat.
+et b&i',i32
o /all permutation function with b,o,n*2 as
parameters.
4. fact!
o +et f,1.
o $or values of ( from 1 to m by 1 repeat.
f,f5i
o #eturn f
1
6. display!
o $or values of ( from ) to factn*1! by 1 repeat
7rint d&i'
o 7rint mind!
o 8sing a while loop %9factn*1! print the path also.
:. permlist&', 0, m!
o /hec0 if 0,m if so
;1,1
$or values of ( from ) to m by 1 repeat.
7ath&i'&i',list&i'
d&i', c&01'&list&i''
01,list&i'
d&i',d&i'3c&01'&1'
let i,i31
o else
for values of i from 0 to m by 1 repeat.
/all swaplist&0', list&i'!
/all permlist, 031, m!
/all swaplist&0', list&i'!
PROGRAM
<<Travelling =alesman problem
>include 9iostream.h1
>include 9conio.h1
class dynamic
?
private@
int c&.'&.',n,d&2-',p&2-'&2',list&.',rA
public@
dynamic!A
void getdata!A
void display!A
int factint num!A
int minint list&'!A
void permint list&', int 0, int m!A
void sol!A
BA
2
dynamic@@dynamic!
?
r,)A
B
void dynamic@@getdata!
?
cout99CDnter no. of cities@CA
cin11nA
cout99endlA
for int i,)Ai9nAi33!
for int %,)A%9nA%33!
c&i'&%',)A
for i,)Ai9nAi33!
?
for %,)A%9nA%33!
?
if iE,%!
?
if c&i'&%',,)!
?
cout99CDnter cost from C99i99C to C99%99C @CA
cin11c&i'&%'A
c&%'&i',c&i'&%'A
B
B
B
B
for i,)Ai9n*1Ai33!
list&i',i31A
B
int dynamic@@factint num!
?
int f,1A
if numE,)!
for int i,1Ai9,numAi33!
f,f5iA
return fA
B
void dynamic@@permint list&', int 0, int m!
?
int i,tempA
if 0,,m!
3
?
for i,)Ai9,mAi33!
?
p&r'&i31',list&i'A
B
r33A
B
else
for i,0Ai9,mAi33!
?
temp,list&0'A list&0',list&i'A list&i',tempA
permlist,031,m!A
temp,list&0'A list&0',list&i'A list&i',tempA
B
B
void dynamic@@sol!
?
permlist,),n*2!A
for int i,)Ai9factn*1!Ai33!
?
p&i'&)',)A
p&i'&n',)A
B
for i,)Ai9factn*1!Ai33!
?
d&i',)A
for int %,)A%9nA%33!
?
d&i',d&i'3c&p&i'&%''&p&i'&%31''A
B
B
B
int dynamic@@minint list&'!
?
int minimum,list&)'A
for int i,)Ai9factn*1!Ai33!
?
if list&i'9minimum!
minimum,list&i'A
B
return minimumA
B
4
void dynamic@@display!
?
int i,%A
cout99endl99CThe cost Fatrix@C99endlA
for i,)Ai9nAi33!
?
for %,)A%9nA%33!
cout99c&i'&%'99CGtCA
cout99endlA
B
cout99endl99CThe 7ossible paths and their corresponding
cost@C99endlA
for i,)Ai9factn*1!Ai33!
?
for %,)A%9n31A%33!
cout99p&i'&%'99CGtCA
cout99C**1 C99d&i'99endlA
B
cout99endl99CThe shortest path @C99endlA
for i,)Ai9factn*1!Ai33!
?
if d&i',,mind!!
brea0A
B
for %,)A%9,nA%33!
?
cout99p&i'&%'99C CA
B
cout99endl99CGnThe cost of this path is C99d&i'99endlA
B
void main!
?
clrscr!A
dynamic tsA
ts.getdata!A
ts.sol!A
ts.display!A
getch!A
B
5
OUTPUT
Dnter no. of cities@-
Dnter cost from ) to 1 @4)
Dnter cost from ) to 2 @")
Dnter cost from ) to " @4.
Dnter cost from 1 to 2 @6)
Dnter cost from 1 to " @2)
Dnter cost from 2 to " @:)
The cost Fatrix@
) 4) ") 4.
4) ) 6) 2)
") 6) ) :)
4. 2) :) )
The 7ossible paths and their corresponding cost@
) 1 2 " ) **1 "1.
) 1 " 2 ) **1 21)
) 2 1 " ) **1 2).
) 2 " 1 ) **1 21)
) " 2 1 ) **1 "1.
) " 1 2 ) **1 2).
The shortest path @
) 2 1 " )
The cost of this path is 2).
6

You might also like