📚 DAA Tutorial

Design and Analysis of Algorithms

Learn Design and Analysis of Algorithms concepts with simple explanations, algorithms, examples and practical notes.

← Back to DAA

Traveling Salesperson Problem

This is a permutation problem. Let G = (V, E) be a directed graph with edge costs cij, where

cij>0for all i and jcij=∞if ⟨i, j⟩ ∉ E

Let |V|=n and assume n > 1.

A tour of G is a directed simple cycle that includes every vertex in V. The cost of a tour is the sum of the costs of the edges on the tour. The TSP finds a tour of minimum cost.

Applications

  1. Routing a postal van to pick up mail from mail boxes located at n different sites. An (n + 1)-vertex graph represents this situation. One vertex represents the post office from which the postal van starts and to which it must return. Edge ⟨i, j⟩ is assigned a cost equal to the distance from site i to site j. The aim is to minimize the tour length.
  2. Robot arm to tighten the nuts on some piece of machinery on an assembly line. The arm should start from the initial position, successively move to each of the remaining nuts, and return to the initial position. The path of the arm is the tour, and the vertices represent the nuts. The aim is to minimize the time needed for the arm to complete the task.
  3. Production environment in which several commodities are manufactured on the same set of machines. Manufacture proceeds in cycles. In each production cycle, n different commodities are produced. When the machines are changed from production of commodity i to j, a changeover cost cij is incurred. Find a sequence that minimizes the sum of changeover costs. It is necessary to include the cost of starting the next cycle, which is the changeover cost from the last to the first commodity. This is a TSP on an n-vertex graph with edge costs cij being the changeover cost from commodity i to j.

Dynamic programming formulation

Let a tour be a simple path that starts and ends at vertex 1. Every tour consists of an edge ⟨1, k⟩ for some k∈V−{1} and a path from vertex k to vertex 1. The path from vertex k to 1 goes through each vertex in V−{1,k} exactly once.

If the tour is optimal, then the path from k to 1 must be a shortest k to 1 path going through all vertices in V−{1,k}.

Let g(i,S) be the length of the shortest path starting at vertex i, going through all vertices in S, and terminating at vertex 1. Then g(1,V−{1}) is the length of an optimal salesperson tour.

From the principle of optimality:

g(1,V−{1})=min2≤k≤n{c1k+g(k,V−{1,k})}(1)

Generalizing the above equation (for i∉S):

g(i,S)=minj∈S{cij+g(j,S−{j})}(2)

Equation (1) can be solved for g(1,V−{1}) if we know g(k,V−{1,k}) for all k.

The g values can be obtained by (2). To obtain g(i,S) for all |S| = 1, start with

g(i,∅)=ci1,1≤i≤n

then obtain g(i,S) for all |S| = 2, and so on.

When |S| < n − 1, the values of i and S for which g(i, S) is needed are such that i ≠ 1, 1 ∉ S, and i ∉ S.

Example

Consider the directed graph with the following edge lengths.

1234
Every ordered pair of vertices is joined by an edge
cij=[0101520509106130128890]

Let the tour start at vertex 1. The given adjacency matrix is cij, with rows and columns numbered 1 to 4.

Step 1: |S| = 0

g(i,∅)=ci1

g(2,∅)=c21=5g(3,∅)=c31=6g(4,∅)=c41=8

Step 2: |S| = 1, i ∉ S (S can be {2}, {3} or {4})

g(i,S)=minj∈S{cij+g(j,S−{j})}

g(2,{3})=c23+g(3,∅)=9+6=15g(2,{4})=c24+g(4,∅)=10+8=18g(3,{2})=c32+g(2,∅)=13+5=18g(3,{4})=c34+g(4,∅)=12+8=20g(4,{2})=c42+g(2,∅)=8+5=13g(4,{3})=c43+g(3,∅)=9+6=15

Step 3: compute g(i, S) with |S| = 2, i ≠ 1, 1 ∉ S and i ∉ S

The value of j that gives the minimum is recorded as well.

g(2,{3,4})=min{c23+g(3,{4}),c24+g(4,{3})}=min{9+20,10+15}=min{29,25}=25and j = 4g(3,{2,4})=min{c32+g(2,{4}),c34+g(4,{2})}=min{13+18,12+13}=min{31,25}=25and j = 4g(4,{2,3})=min{c42+g(2,{3}),c43+g(3,{2})}=min{8+15,9+18}=min{23,27}=23and j = 2

Step 4: compute g(1, S) with |S| = 3

g (1, {2,3,4} ) = min { c12 + g (2, {3,4} ) , c13 + g (3, {2,4} ) , c14 + g (4, {2,3} ) } = min { 10+25 , 15+25 , 20+23 } = min { 35,40,43 } = 35 and j = 2

⇒ The optimal tour has length 35.

Constructing the tour

To construct a tour of this length, consider the value of j that minimizes the right-hand side.

  • J(i,S)=J(1,{2,3,4})=2: the tour starts at 1 and goes to 2, i.e. 1 → 2.
  • Consider g(2,{3,4}), i.e. J(2,{3,4})=4, so the next edge is ⟨2, 4⟩: 1 → 2 → 4.
  • Now g(4,{3}), i.e. J(4,{3})=3, so the tour continues 4 → 3 and then returns to 1.

⇒ 1 → 2 → 4 → 3 → 1 is the optimal tour, with length 10 + 10 + 9 + 6 = 35.

1234
The optimal tour 1 → 2 → 4 → 3 → 1

Complexity

  • Time complexity = Θ(n22n). This is better than enumerating all n! different tours and finding the best one.
  • Space complexity = O(n2n). This is too large even for modest values of n.