Traveling Salesman Problem: Introduction to Dynamic Programming

Kaan Kaçar
Coinmonks
3 min readSep 8, 2023

--

The Traveling Salesman Problem(TSP) is a classic optimization problem in which a salesman is given a list of cities, and their task is to find the shortest possible route that visits each city exactly once and returns to the starting city. This problem might sound simple with just a few cities, but as the number of cities increases, the number of possible routes grows exponentially, making it an NP-hard problem.

The TSP can be formally defined as follows:

Given:

  1. A set of cities {A, B, C, … N}.
  2. The distances between each pair of cities (e.g., the distance from A to B, B to C, etc.).

The goal is to find a permutation of the cities that minimizes the total distance traveled.

Dynamic Programming Approach to Solve TSP

Dynamic programming is a technique for solving optimization problems like the TSP. The basic idea is to break down the problem into smaller subproblems and use the solutions to those subproblems to construct the solution to the original problem.

Here’s a step-by-step explanation of how we can use dynamic programming to solve the TSP:

  1. Create a 2D array “dp,” where dp[i][j] represents the minimum distance to reach city “i” while visiting a subset of cities represented by the bitmask “j.”
  2. Initialize dp[i][0] to be the distance from the starting city to city “i.”
  3. Iterate through all subsets “j” of cities (excluding the starting city) and all cities “i” in the subset “j.”
  4. For each “i” and “j,” calculate dp[i][j] as follows: dp[i][j] = min(dp[i][j], dp[k][j — {i}] + distance[k][i]) for all k in subset “j” and k ≠ i.
  5. Finally, find the minimum distance among all dp[i][(1 << n) — 1] values, where “n” is the number of cities.

Here’s a Java implementation of the dynamic programming approach to solve the TSP:

public class TSP {

public static int tsp(int[][] distance) {
int n = distance.length;
int[][] dp = new int[n][1 << n];

// Initialize dp[i][0] to be the distance from the starting city to city i.
for (int i = 0; i < n; i++) {
dp[i][0] = distance[i][0];
}

// Fill in the dp table using dynamic programming.
for (int j = 1; j < (1 << n); j++) {
for (int i = 0; i < n; i++) {
if ((j & (1 << i)) != 0) {
dp[i][j] = Integer.MAX_VALUE;
for (int k = 0; k < n; k++) {
if (k != i && (j & (1 << k)) != 0) {
dp[i][j] = Math.min(dp[i][j], dp[k][j ^ (1 << i)] + distance[k][i]);
}
}
}
}
}

// Find the minimum distance from any city to the starting city.
int minDistance = Integer.MAX_VALUE;
for (int i = 1; i < n; i++) {
minDistance = Math.min(minDistance, dp[i][(1 << n) - 1] + distance[i][0]);
}

return minDistance;
}

public static void main(String[] args) {
int[][] distance = {
{0, 29, 20, 21},
{29, 0, 15, 17},
{20, 15, 0, 28},
{21, 17, 28, 0}
};

int minDistance = tsp(distance);
System.out.println("Minimum distance for TSP: " + minDistance);
}
}

If you found this article informative and helpful, please consider following me for more blockchain and cryptocurrency-related content. You can also subscribe to my email list to receive updates on my latest articles and projects.

--

--