Skip to content

Commit 4e65032

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

File tree

5 files changed

+87
-3
lines changed

5 files changed

+87
-3
lines changed

src/main/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
public class DijkstraAlgorithm {
99

10-
private final int vertexCount;
10+
private final int vertexCount;
1111

1212
/**
1313
* Constructs a Dijkstra object with the given number of vertices.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.thealgorithms.datastructures.graphs;
2+
3+
/**
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+
*/
18+
19+
import java.util.Arrays;
20+
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+

src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,4 @@ void testProcessWithCoeffsSet() {
8080
// check if the method runs and returns a result within reasonable bounds
8181
assertTrue(result >= -1.0 && result <= 1.0, "Processed result should be in the range [-1, 1]");
8282
}
83-
}
83+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import org.junit.jupiter.api.BeforeEach;
77
import org.junit.jupiter.api.Test;
8-
8+
99
public class DijkstraAlgorithmTest {
1010

1111
private DijkstraAlgorithm dijkstraAlgorithm;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.thealgorithms.datastructures.graphs;
2+
3+
public class TravelingSalesmanTest {
4+
public static void main(String[] args) {
5+
// Test case: Distance matrix representing the cities
6+
int[][] dist = {
7+
{ 0, 10, 15, 20 },
8+
{ 10, 0, 35, 25 },
9+
{ 15, 35, 0, 30 },
10+
{ 20, 25, 30, 0 }
11+
};
12+
13+
// Calling the tsp method from the TravelingSalesmanDP class
14+
int result = TravelingSalesmanDP.tsp(dist);
15+
16+
// Output the result
17+
System.out.println("Minimum cost: " + result);
18+
// Expected output: Minimum cost: 80
19+
}
20+
}

0 commit comments

Comments
 (0)