From 3696395521829ed3920348f5a0a246398d807aa7 Mon Sep 17 00:00:00 2001 From: Sujal1804 <148648987+Sujal1804@users.noreply.github.com> Date: Thu, 10 Oct 2024 02:01:07 +0530 Subject: [PATCH 1/3] Solved TravelingSalesManProblem Using DynamicProgramming(memoization) In the above code, we implement a solution to the Traveling Salesman Problem (TSP) using dynamic programming with memoization. The algorithm calculates the shortest possible route that visits a set of cities exactly once and returns to the starting city by recursively exploring all possible paths and storing the results of subproblems to avoid redundant calculations. The program outputs both the minimum cost of the tour and the optimal path taken. --- .../TravelingSalesManProblem | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/main/java/com/thealgorithms/dynamicprogramming/TravelingSalesManProblem diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/TravelingSalesManProblem b/src/main/java/com/thealgorithms/dynamicprogramming/TravelingSalesManProblem new file mode 100644 index 000000000000..fd90958686a0 --- /dev/null +++ b/src/main/java/com/thealgorithms/dynamicprogramming/TravelingSalesManProblem @@ -0,0 +1,67 @@ +import java.util.Arrays; + +public class TSP { + static int n; + static int MAX = 1000000; + static int[][] dist; + static int[][] memo; + static int[][] parent; + + static int tsp(int i, int mask) { + if (mask == (1 << n) - 1) { + return dist[i][0]; + } + + if (memo[i][mask] != -1) { + return memo[i][mask]; + } + + int res = MAX; + + for (int j = 0; j < n; j++) { + if ((mask & (1 << j)) == 0) { + int newRes = dist[i][j] + tsp(j, mask | (1 << j)); + if (newRes < res) { + res = newRes; + parent[i][mask] = j; + } + } + } + + return memo[i][mask] = res; + } + + static void printPath(int i, int mask) { + System.out.print(i + " → "); + if (mask == (1 << n) - 1) { + System.out.println(0); + return; + } + int nextCity = parent[i][mask]; + printPath(nextCity, mask | (1 << nextCity)); + } + + public static void main(String[] args) { + n = 4; + dist = new int[][] { + { 0, 10, 15, 20 }, + { 10, 0, 35, 25 }, + { 15, 35, 0, 30 }, + { 20, 25, 30, 0 } + }; + + memo = new int[n][1 << n]; + parent = new int[n][1 << n]; + + for (int[] row : memo) { + Arrays.fill(row, -1); + } + + int minCost = tsp(0, 1); + + System.out.println("The minimum cost of the tour is: " + minCost); + + System.out.print("The optimal path is: "); + printPath(0, 1); + } +} From 5714528a935d601b35a9d61f59c84e8003d3ed1f Mon Sep 17 00:00:00 2001 From: Sujal1804 <148648987+Sujal1804@users.noreply.github.com> Date: Sun, 13 Oct 2024 16:22:13 +0530 Subject: [PATCH 2/3] Update TravelingSalesManProblem --- .../TravelingSalesManProblem | 84 +++++++++++++++---- 1 file changed, 66 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/TravelingSalesManProblem b/src/main/java/com/thealgorithms/dynamicprogramming/TravelingSalesManProblem index fd90958686a0..b9e620586553 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/TravelingSalesManProblem +++ b/src/main/java/com/thealgorithms/dynamicprogramming/TravelingSalesManProblem @@ -7,7 +7,7 @@ public class TSP { static int[][] memo; static int[][] parent; - static int tsp(int i, int mask) { + public static int tsp(int i, int mask) { if (mask == (1 << n) - 1) { return dist[i][0]; } @@ -31,37 +31,85 @@ public class TSP { return memo[i][mask] = res; } - static void printPath(int i, int mask) { - System.out.print(i + " → "); + public static String printPath(int i, int mask) { + StringBuilder path = new StringBuilder(i + " → "); if (mask == (1 << n) - 1) { - System.out.println(0); - return; + path.append(0); + return path.toString(); } int nextCity = parent[i][mask]; - printPath(nextCity, mask | (1 << nextCity)); + path.append(printPath(nextCity, mask | (1 << nextCity))); + return path.toString(); } - public static void main(String[] args) { - n = 4; - dist = new int[][] { + public static void initialize(int[][] distanceMatrix) { + n = distanceMatrix.length; + dist = distanceMatrix; + memo = new int[n][1 << n]; + parent = new int[n][1 << n]; + for (int[] row : memo) { + Arrays.fill(row, -1); + } + } +} +import org.junit.Test; +import static org.junit.Assert.*; + +public class TSPTest { + + @Test + public void testTSPWithSmallGraph() { + int[][] distanceMatrix = { { 0, 10, 15, 20 }, { 10, 0, 35, 25 }, { 15, 35, 0, 30 }, { 20, 25, 30, 0 } }; - memo = new int[n][1 << n]; - parent = new int[n][1 << n]; + TSP.initialize(distanceMatrix); - for (int[] row : memo) { - Arrays.fill(row, -1); - } + int minCost = TSP.tsp(0, 1); + + assertEquals(80, minCost); + } + + @Test + public void testOptimalPath() { + int[][] distanceMatrix = { + { 0, 10, 15, 20 }, + { 10, 0, 35, 25 }, + { 15, 35, 0, 30 }, + { 20, 25, 30, 0 } + }; + + TSP.initialize(distanceMatrix); + + TSP.tsp(0, 1); + + String expectedPath = "0 → 1 → 3 → 2 → 0"; + String actualPath = TSP.printPath(0, 1); + + assertEquals(expectedPath, actualPath); + } + + @Test + public void testDifferentGraph() { + int[][] distanceMatrix = { + { 0, 29, 20, 21 }, + { 29, 0, 15, 17 }, + { 20, 15, 0, 28 }, + { 21, 17, 28, 0 } + }; + + TSP.initialize(distanceMatrix); - int minCost = tsp(0, 1); + int minCost = TSP.tsp(0, 1); + + assertEquals(75, minCost); - System.out.println("The minimum cost of the tour is: " + minCost); + String expectedPath = "0 → 2 → 1 → 3 → 0"; + String actualPath = TSP.printPath(0, 1); - System.out.print("The optimal path is: "); - printPath(0, 1); + assertEquals(expectedPath, actualPath); } } From 3a0caee50ec8386f720180d2dcb1f3302bbd7cac Mon Sep 17 00:00:00 2001 From: Sujal1804 <148648987+Sujal1804@users.noreply.github.com> Date: Wed, 16 Oct 2024 01:06:26 +0530 Subject: [PATCH 3/3] Update TravelingSalesManProblem removed test and put in seperate file --- .../TravelingSalesManProblem | 61 ------------------- 1 file changed, 61 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/TravelingSalesManProblem b/src/main/java/com/thealgorithms/dynamicprogramming/TravelingSalesManProblem index b9e620586553..842614d526ce 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/TravelingSalesManProblem +++ b/src/main/java/com/thealgorithms/dynamicprogramming/TravelingSalesManProblem @@ -52,64 +52,3 @@ public class TSP { } } } -import org.junit.Test; -import static org.junit.Assert.*; - -public class TSPTest { - - @Test - public void testTSPWithSmallGraph() { - int[][] distanceMatrix = { - { 0, 10, 15, 20 }, - { 10, 0, 35, 25 }, - { 15, 35, 0, 30 }, - { 20, 25, 30, 0 } - }; - - TSP.initialize(distanceMatrix); - - int minCost = TSP.tsp(0, 1); - - assertEquals(80, minCost); - } - - @Test - public void testOptimalPath() { - int[][] distanceMatrix = { - { 0, 10, 15, 20 }, - { 10, 0, 35, 25 }, - { 15, 35, 0, 30 }, - { 20, 25, 30, 0 } - }; - - TSP.initialize(distanceMatrix); - - TSP.tsp(0, 1); - - String expectedPath = "0 → 1 → 3 → 2 → 0"; - String actualPath = TSP.printPath(0, 1); - - assertEquals(expectedPath, actualPath); - } - - @Test - public void testDifferentGraph() { - int[][] distanceMatrix = { - { 0, 29, 20, 21 }, - { 29, 0, 15, 17 }, - { 20, 15, 0, 28 }, - { 21, 17, 28, 0 } - }; - - TSP.initialize(distanceMatrix); - - int minCost = TSP.tsp(0, 1); - - assertEquals(75, minCost); - - String expectedPath = "0 → 2 → 1 → 3 → 0"; - String actualPath = TSP.printPath(0, 1); - - assertEquals(expectedPath, actualPath); - } -}