Skip to content

Commit b0ae9e6

Browse files
committed
Traveling Salesman Problem added
1 parent 4e65032 commit b0ae9e6

File tree

2 files changed

+66
-59
lines changed

2 files changed

+66
-59
lines changed
Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,70 @@
11
package com.thealgorithms.datastructures.graphs;
22

33
/**
4-
* Problem Statement:
5-
* The Traveling Salesman Problem (TSP) asks for the shortest possible route
6-
* that visits a given set of cities and returns to the origin city.
7-
* Each city is connected to every other city, and the goal is to find the shortest route
8-
* that visits each city exactly once and returns to the starting point.
9-
*
10-
* Approach:
11-
* This implementation uses dynamic programming (DP) with bitmasking
12-
* to represent visited cities. The DP state is dp[mask][i],
13-
* where 'mask' represents the set of visited cities, and 'i' is the current city.
14-
*
15-
* Time Complexity: O(n^2 * 2^n), where 'n' is the number of cities.
16-
* Space Complexity: O(n * 2^n), due to the DP table and bitmask representation.
17-
*/
4+
* Problem Statement:
5+
* The Traveling Salesman Problem (TSP) asks for the shortest possible route
6+
* that visits a given set of cities and returns to the origin city.
7+
* Each city is connected to every other city, and the goal is to find the shortest route
8+
* that visits each city exactly once and returns to the starting point.
9+
*
10+
* More information on TSP can be found here:
11+
* https://en.wikipedia.org/wiki/Travelling_salesman_problem
12+
*
13+
* Approach:
14+
* This implementation uses dynamic programming (DP) with bitmasking
15+
* to represent visited cities. The DP state is dp[mask][i],
16+
* where 'mask' represents the set of visited cities, and 'i' is the current city.
17+
*
18+
* Time Complexity: O(n^2 * 2^n), where 'n' is the number of cities.
19+
* Space Complexity: O(n * 2^n), due to the DP table and bitmask representation.
20+
*/
1821

19-
import java.util.Arrays;
22+
import java.util.Arrays;
2023

21-
public class TravelingSalesmanDP {
22-
private static final int INF = Integer.MAX_VALUE / 2; // Infinity value for unvisited paths
23-
24-
/**
25-
* Solves the TSP using dynamic programming.
26-
*
27-
* @param dist Matrix where dist[i][j] represents the distance between city i and city j.
28-
* @return Minimum cost of traveling through all cities and returning to the start.
29-
*/
30-
public static int tsp(int[][] dist) {
31-
int n = dist.length;
32-
int[][] dp = new int[n][(1 << n)]; // DP table
33-
34-
// Initialize DP table with infinity
35-
for (int[] row : dp) {
36-
Arrays.fill(row, INF);
37-
}
38-
39-
// Start at city 0 with only the first city visited
40-
dp[0][1] = 0;
41-
42-
// Iterate over all subsets of visited cities
43-
for (int mask = 1; mask < (1 << n); mask++) {
44-
for (int u = 0; u < n; u++) {
45-
if ((mask & (1 << u)) == 0) continue; // If city u is not visited in this subset
46-
47-
for (int v = 0; v < n; v++) {
48-
if ((mask & (1 << v)) != 0 || dist[u][v] == INF) continue; // If city v is already visited
49-
int newMask = mask | (1 << v); // Visit city v
50-
dp[v][newMask] = Math.min(dp[v][newMask], dp[u][mask] + dist[u][v]);
51-
}
52-
}
53-
}
54-
55-
// Return the minimum cost of visiting all cities and returning to city 0
56-
int result = INF;
57-
for (int i = 1; i < n; i++) {
58-
result = Math.min(result, dp[i][(1 << n) - 1] + dist[i][0]);
59-
}
60-
61-
return result;
62-
}
63-
}
64-
24+
public class TravelingSalesmanDP {
25+
private static final int INF = Integer.MAX_VALUE / 2; // Infinity value for unvisited paths
26+
27+
/**
28+
* Solves the TSP using dynamic programming.
29+
*
30+
* @param dist Matrix where dist[i][j] represents the distance between city i and city j.
31+
* @return Minimum cost of traveling through all cities and returning to the start.
32+
*/
33+
public static int tsp(int[][] dist) {
34+
int n = dist.length;
35+
int[][] dp = new int[n][(1 << n)]; // DP table
36+
37+
// Initialize DP table with infinity
38+
for (int[] row : dp) {
39+
Arrays.fill(row, INF);
40+
}
41+
42+
// Start at city 0 with only the first city visited
43+
dp[0][1] = 0;
44+
45+
// Iterate over all subsets of visited cities
46+
for (int mask = 1; mask < (1 << n); mask++) {
47+
for (int u = 0; u < n; u++) {
48+
if ((mask & (1 << u)) == 0) {
49+
continue; // If city u is not visited in this subset
50+
}
51+
52+
for (int v = 0; v < n; v++) {
53+
if ((mask & (1 << v)) != 0 || dist[u][v] == INF) {
54+
continue; // If city v is already visited
55+
}
56+
int newMask = mask | (1 << v); // Visit city v
57+
dp[v][newMask] = Math.min(dp[v][newMask], dp[u][mask] + dist[u][v]);
58+
}
59+
}
60+
}
61+
62+
// Return the minimum cost of visiting all cities and returning to city 0
63+
int result = INF;
64+
for (int i = 1; i < n; i++) {
65+
result = Math.min(result, dp[i][(1 << n) - 1] + dist[i][0]);
66+
}
67+
68+
return result;
69+
}
70+
}

src/test/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ public static void main(String[] args) {
1818
// Expected output: Minimum cost: 80
1919
}
2020
}
21+

0 commit comments

Comments
 (0)