Travelling Salesman Problem
Travelling Salesman Problem
Problem Statement
A traveler needs to visit all the cities from a list, where distances between all the cities are known and each city should be visited just
once. What is the shortest possible route that he visits each city exactly once and returns to the origin city?
Solution
Travelling salesman problem is the most notorious computational problem. We can use brute-force approach to evaluate every
possible tour and select the best one. For n number of vertices in a graph, there are (n - 1)! number of possibilities.
Instead of brute-force using dynamic programming approach, the solution can be obtained in lesser time, though there is no
polynomial time algorithm.
Let us consider a graph G = (V, E), where V is a set of cities and E is a set of weighted edges. An edge e(u, v) represents that
vertices u and v are connected. Distance between vertex u and v is d(u, v), which should be non-negative.
Suppose we have started at city 1 and after visiting some cities now we are in city j. Hence, this is a partial tour. We certainly need to
know j, since this will determine which cities are most convenient to visit next. We also need to know all the cities visited so far, so
that we don't repeat any of them. Hence, this is an appropriate sub-problem.
For a subset of cities S Є {1, 2, 3, ... , n} that includes 1, and j Є S, let C(S, j) be the length of the shortest path visiting each node in
S exactly once, starting at 1 and ending at j.
When |S| > 1, we define C(S, 1) = ∝ since the path cannot start and end at 1.
Now, let express C(S, j) in terms of smaller sub-problems. We need to start at 1 and end at j. We should select the next city in such a
way that
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_travelling_salesman_problem.htm 1/8
2/9/22, 10:21 AM Travelling Salesman Problem
C
C((S
S,, j
j)) =
= m
miin
nCC(
(SS −
−{{j
j}},, i
i))+
+dd(
(ii,, j
j))w
whhe
erre
eii ∈
∈ S
S a
annd
dii ≠
≠ j
jcc(
(SS,, j
j))
=
= m
miin
nCC(
(ss−
−{{j
j}},, i
i))+
+dd(
(ii,, j
j))w
whhe
erre
eii ∈
∈ S
S a
annd
dii ≠
≠ j
j
Algorithm: Traveling-Salesman-Problem
C ({1}, 1) = 0
for s = 2 to n do
for all subsets S Є {1, 2, 3, … , n} of size s and containing 1
C (S, 1) = ∞
for all j Є S and j ≠ 1
C (S, j) = min {C (S – {j}, i) + d(i, j) for i Є S and i ≠ j}
Return minj C ({1, 2, 3, …, n}, j) + d(j, i)
Analysis
There are at the most 2
n
n
2 .. n
n sub-problems and each one takes linear time to solve. Therefore, the total running time is
O
O((2
n
n
2 .. n
n )
)
2
2
.
Example
In the following example, we will illustrate the steps to solve the travelling salesman problem.
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_travelling_salesman_problem.htm 2/8
2/9/22, 10:21 AM Travelling Salesman Problem
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
4 8 8 9 0
S=Φ
C
Coos
stt(
(22,, Φ
Φ,, 1
1))=
=dd(
(22,, 1
1))=
=55C
Coos
stt(
(22,, Φ
Φ,, 1
1))=
=dd(
(22,, 1
1))=
=55
C
Coos
stt(
(33,, Φ
Φ,, 1
1))=
=dd(
(33,, 1
1))=
=66C
Coos
stt(
(33,, Φ
Φ,, 1
1))=
=dd(
(33,, 1
1))=
=66
C
Coos
stt(
(44,, Φ
Φ,, 1
1))=
=dd(
(44,, 1
1))=
=88C
Coos
stt(
(44,, Φ
Φ,, 1
1))=
=dd(
(44,, 1
1))=
=88
S=1
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_travelling_salesman_problem.htm 3/8
2/9/22, 10:21 AM Travelling Salesman Problem
C
Coos
stt(
(ii,, s
s))=
=mmi
inn{
{CCo
osst
t((j
j,, s
s––(
(jj)
)))+
+dd[[i
i,, j
j]]}
}CCo
osst
t((i
i,, s
s))=
=mmi
inn{
{CCo
osst
t((j
j,, s
s))−
−((j
j)))
)++d
d[[i
i,, j
j]]}
}
C
Coos
stt(
(22,, {
{33}
},, 1
1))=
=dd[[2
2,, 3
3]] +
+CCo
osst
t((3
3,, Φ
Φ,, 1
1))=
=99+
+66=
= 15
15cco
osst
t((2
2,, {
{33}
},, 1
1))=
=dd[[2
2,, 3
3]]
+
+cco
osst
t((3
3,, Φ
Φ,, 1
1))=
=99+
+66=
= 15
15
C
Coos
stt(
(22,, {
{44}
},, 1
1))=
=dd[[2
2,, 4
4]] +
+CCo
osst
t((4
4,, Φ
Φ,, 1
1))=
= 10
10 +
+88=
= 18
18cco
osst
t((2
2,, {
{44}
},, 1
1))=
=dd[[2
2,, 4
4]]
+
+cco
osst
t((4
4,, Φ
Φ,, 1
1))=
= 10
10 +
+88=
= 18
18
C
Coos
stt(
(33,, {
{22}
},, 1
1))=
=dd[[3
3,, 2
2]] +
+CCo
osst
t((2
2,, Φ
Φ,, 1
1))=
= 13
13 +
+55=
= 18
18cco
osst
t((3
3,, {
{22}
},, 1
1))=
=dd[[3
3,, 2
2]]
+
+cco
osst
t((2
2,, Φ
Φ,, 1
1))=
= 13
13 +
+55=
= 18
18
C
Coos
stt(
(33,, {
{44}
},, 1
1))=
=dd[[3
3,, 4
4]] +
+CCo
osst
t((4
4,, Φ
Φ,, 1
1))=
= 12
12 +
+88=
= 20
20cco
osst
t((3
3,, {
{44}
},, 1
1))=
=dd[[3
3,, 4
4]]
+
+cco
osst
t((4
4,, Φ
Φ,, 1
1))=
= 12
12 +
+88=
= 20
20
C
Coos
stt(
(44,, {
{33}
},, 1
1))=
=dd[[4
4,, 3
3]] +
+CCo
osst
t((3
3,, Φ
Φ,, 1
1))=
=99+
+66=
= 15
15cco
osst
t((4
4,, {
{33}
},, 1
1))=
=dd[[4
4,, 3
3]]
+
+cco
osst
t((3
3,, Φ
Φ,, 1
1))=
=99+
+66=
= 15
15
C
Coos
stt(
(44,, {
{22}
},, 1
1))=
=dd[[4
4,, 2
2]] +
+CCo
osst
t((2
2,, Φ
Φ,, 1
1))=
=88+
+55=
= 13
13cco
osst
t((4
4,, {
{22}
},, 1
1))=
=dd[[4
4,, 2
2]]
+
+cco
osst
t((2
2,, Φ
Φ,, 1
1))=
=88+
+55=
= 13
13
S=2
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_travelling_salesman_problem.htm 4/8
2/9/22, 10:21 AM Travelling Salesman Problem
C
Coos
stt(
(22,, {
{33,, 4
4}},, 1
1))
⎧ d
⎧ d[[2
2,, 3
3]] +
+CCo
osst
t((3
3,, {
{44}
},, 1
1))=
=99+
+ 20
20 =
= 29
29
=
= ⎨
⎨ d
d[[2
2,, 4
4]] +
+C Co
osst
t((4
4,, {
{33}},, 1
1))== 10
10 +
+ 15
15 =
= 25
25 == 2525C Coos
stt(
(22,, {
{33,, 4
4}},, 1
1)) =
= 25
25
⎩
⎩
{
{dd[[2
2,, 3
3]] +
+cco
osst
t((3
3,, {
{44}},, 1
1))=
=9 9++ 20
20 =
= 29
29d
d[[2
2,, 4
4]] +
+CCo osst
t((44,, {
{33}},, 1
1))=
= 10
10 +
+ 15
15 =
= 25
25
C
Coos
stt(
(33,, {
{22,, 4
4}},, 1
1))
⎧ d
⎧ d[[3
3,, 2
2]] +
+CCo
osst
t((2
2,, {
{44}
},, 1
1))=
= 13
13 +
+ 18
18 =
= 31
31
=
= ⎨
⎨ d
d[[3
3,, 4
4]] +
+C Co
osst
t((4
4,, {
{22}},, 1
1))== 12
12 +
+ 13
13 == 25
25 =
= 2525C Co os
stt(
(33,, {
{22,, 4
4}},, 1
1))
⎩
⎩
{
{dd[[3
3,, 2
2]] +
+cco
osst
t((2
2,, {
{44}},, 1
1))=
= 13
13 +
+ 18
18 =
= 31
31d
d[[3
3,, 4
4]] +
+C Co osstt(
(44,, {
{22}},, 1
1))=
= 12
12 +
+ 13
13 =
= 25
25
=
= 25
25
C
Coos
stt(
(44,, {
{22,, 3
3}},, 1
1))
⎧ d
⎧ d[[4
4,, 2
2]] +
+CCo
osst
t((2
2,, {
{33}
},, 1
1))=
=88+
+ 15
15 =
= 23
23
=
= ⎨
⎨ d
d[[4
4,, 3
3]] +
+C Co
osst
t((3
3,, {
{22}},, 1
1))==99+
+ 18
18 =
= 27
27 =
= 2323C Co os
stt(
(44,, {
{22,, 3
3}},, 1
1)) =
= 23
23
⎩
⎩
{
{dd[[4
4,, 2
2]] +
+cco
osst
t((2
2,, {
{33}},, 1
1))=
=88+
+ 15
15 =
= 23
23d
d[[4
4,, 3
3]] +
+C Co osstt(
(33,, {
{22}},, 1
1))=
=99+
+ 18
18 =
= 27
27
S=3
C
Coos
stt(
(11,, {
{22,, 3
3,, 4
4}},, 1
1))
⎧
⎧ d
d[[1
1,, 2
2]] +
+CCoosstt(
(22,, {
{33,, 4
4}},, 1
1))=
= 10
10 +
+ 25
25 =
= 35
35
⎪
⎪
⎪
⎪
⎪
⎪
⎪
⎪
⎪ d
d[[1
1,, 3
3]] +
+CCoosstt(
(33,, {
{22,, 4
4}},, 1
1))=
= 15
15 +
+ 25
25 =
= 40
40
⎪
⎪
⎪
⎪
⎪
⎪
⎪
d
d[[1
1,, 4
4]] +
+CCo os
stt(
(44,, {
{22,, 3
3}},, 1
1))== 20
20 +
+ 23
23 =
= 43
43 =
= 35
35cco
osst
t((1
1,, {
{22,, 3
3,, 4
4}})
),, 1
1))
=
= ⎨
⎨
d[1, 2] + cost(2, {3, 4}, 1) = 10 + 25 = 35
⎪ d[1, 2] + cost(2, {3, 4}, 1) = 10 + 25 = 35
⎪
⎪
⎪
⎪
⎪
⎪
⎪
⎪
⎪ d
⎪
⎪ d[[1
1,, 3
3]] +
+cco
osst
t((3
3,, {
{22,, 4
4}},, 1
1))= = 15
15 +
+ 25
25 =
= 40
40
⎪
⎪
⎩
⎪
⎩
⎪
d
d[[1
1,, 4
4]] +
+cco
osst
t((4
4,, {
{22,, 3
3}},, 1
1))= = 20
20 +
+ 23
23 =
= 43
43 =
= 35
35
Start from cost {1, {2, 3, 4}, 1}, we get the minimum value for d [1, 2]. When s = 3, select the path from 1 to 2 (cost is 10) then go
backwards. When s = 2, we get the minimum value for d [4, 2]. Select the path from 2 to 4 (cost is 10) then go backwards.
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_travelling_salesman_problem.htm 5/8
2/9/22, 10:21 AM Travelling Salesman Problem
When s = 1, we get the minimum value for d [4, 3]. Selecting path 4 to 3 (cost is 9), then we shall go to then go to s = Φ step. We get
the minimum value for d [3, 1] (cost is 6).
Arnab Chakraborty
More Detail
Video
30 Lectures 3 hours
Arnab Chakraborty
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_travelling_salesman_problem.htm 6/8
2/9/22, 10:21 AM Travelling Salesman Problem
More Detail
Video
31 Lectures 4 hours
Arnab Chakraborty
More Detail
Video
More Detail
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_travelling_salesman_problem.htm 7/8
2/9/22, 10:21 AM Travelling Salesman Problem
Video
7 Lectures 1 hours
Zach Miller
More Detail
Video
54 Lectures 4 hours
Sasha Miller
More Detail
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_travelling_salesman_problem.htm 8/8