From 4e65032e655a42a62180a6c374144bc2b0282a83 Mon Sep 17 00:00:00 2001 From: DBasu2610 Date: Sun, 13 Oct 2024 17:27:07 +0530 Subject: [PATCH 1/2] Traveling Salesman Problem added --- .../graphs/DijkstraAlgorithm.java | 2 +- .../graphs/TravelingSalesmanDP.java | 64 +++++++++++++++++++ .../audiofilters/IIRFilterTest.java | 2 +- .../graphs/DijkstraAlgorithmTest.java | 2 +- .../graphs/TravelingSalesmanTest.java | 20 ++++++ 5 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanDP.java create mode 100644 src/test/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithm.java index 70699a9461f7..f4abd210132a 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithm.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithm.java @@ -7,7 +7,7 @@ */ public class DijkstraAlgorithm { - private final int vertexCount; + private final int vertexCount; /** * Constructs a Dijkstra object with the given number of vertices. diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanDP.java b/src/main/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanDP.java new file mode 100644 index 000000000000..5e28eb66dd9c --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanDP.java @@ -0,0 +1,64 @@ +package com.thealgorithms.datastructures.graphs; + +/** + * Problem Statement: + * The Traveling Salesman Problem (TSP) asks for the shortest possible route + * that visits a given set of cities and returns to the origin city. + * Each city is connected to every other city, and the goal is to find the shortest route + * that visits each city exactly once and returns to the starting point. + * + * Approach: + * This implementation uses dynamic programming (DP) with bitmasking + * to represent visited cities. The DP state is dp[mask][i], + * where 'mask' represents the set of visited cities, and 'i' is the current city. + * + * Time Complexity: O(n^2 * 2^n), where 'n' is the number of cities. + * Space Complexity: O(n * 2^n), due to the DP table and bitmask representation. + */ + + import java.util.Arrays; + + public class TravelingSalesmanDP { + private static final int INF = Integer.MAX_VALUE / 2; // Infinity value for unvisited paths + + /** + * Solves the TSP using dynamic programming. + * + * @param dist Matrix where dist[i][j] represents the distance between city i and city j. + * @return Minimum cost of traveling through all cities and returning to the start. + */ + public static int tsp(int[][] dist) { + int n = dist.length; + int[][] dp = new int[n][(1 << n)]; // DP table + + // Initialize DP table with infinity + for (int[] row : dp) { + Arrays.fill(row, INF); + } + + // Start at city 0 with only the first city visited + dp[0][1] = 0; + + // Iterate over all subsets of visited cities + for (int mask = 1; mask < (1 << n); mask++) { + for (int u = 0; u < n; u++) { + if ((mask & (1 << u)) == 0) continue; // If city u is not visited in this subset + + for (int v = 0; v < n; v++) { + if ((mask & (1 << v)) != 0 || dist[u][v] == INF) continue; // If city v is already visited + int newMask = mask | (1 << v); // Visit city v + dp[v][newMask] = Math.min(dp[v][newMask], dp[u][mask] + dist[u][v]); + } + } + } + + // Return the minimum cost of visiting all cities and returning to city 0 + int result = INF; + for (int i = 1; i < n; i++) { + result = Math.min(result, dp[i][(1 << n) - 1] + dist[i][0]); + } + + return result; + } + } + \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java b/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java index 66d7d60c501b..6d461c56cd59 100644 --- a/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java +++ b/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java @@ -80,4 +80,4 @@ void testProcessWithCoeffsSet() { // check if the method runs and returns a result within reasonable bounds assertTrue(result >= -1.0 && result <= 1.0, "Processed result should be in the range [-1, 1]"); } -} +} diff --git a/src/test/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithmTest.java b/src/test/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithmTest.java index c5df9acdf33b..1870dc9ce02c 100644 --- a/src/test/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithmTest.java +++ b/src/test/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithmTest.java @@ -5,7 +5,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - + public class DijkstraAlgorithmTest { private DijkstraAlgorithm dijkstraAlgorithm; diff --git a/src/test/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanTest.java b/src/test/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanTest.java new file mode 100644 index 000000000000..770a786d2f96 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanTest.java @@ -0,0 +1,20 @@ +package com.thealgorithms.datastructures.graphs; + +public class TravelingSalesmanTest { + public static void main(String[] args) { + // Test case: Distance matrix representing the cities + int[][] dist = { + { 0, 10, 15, 20 }, + { 10, 0, 35, 25 }, + { 15, 35, 0, 30 }, + { 20, 25, 30, 0 } + }; + + // Calling the tsp method from the TravelingSalesmanDP class + int result = TravelingSalesmanDP.tsp(dist); + + // Output the result + System.out.println("Minimum cost: " + result); + // Expected output: Minimum cost: 80 + } +} From b0ae9e6f9a793ec24985fffaf7654846213dc475 Mon Sep 17 00:00:00 2001 From: DBasu2610 Date: Sun, 13 Oct 2024 17:35:07 +0530 Subject: [PATCH 2/2] Traveling Salesman Problem added --- .../graphs/TravelingSalesmanDP.java | 124 +++++++++--------- .../graphs/TravelingSalesmanTest.java | 1 + 2 files changed, 66 insertions(+), 59 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanDP.java b/src/main/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanDP.java index 5e28eb66dd9c..3c8111828796 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanDP.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanDP.java @@ -1,64 +1,70 @@ package com.thealgorithms.datastructures.graphs; /** - * Problem Statement: - * The Traveling Salesman Problem (TSP) asks for the shortest possible route - * that visits a given set of cities and returns to the origin city. - * Each city is connected to every other city, and the goal is to find the shortest route - * that visits each city exactly once and returns to the starting point. - * - * Approach: - * This implementation uses dynamic programming (DP) with bitmasking - * to represent visited cities. The DP state is dp[mask][i], - * where 'mask' represents the set of visited cities, and 'i' is the current city. - * - * Time Complexity: O(n^2 * 2^n), where 'n' is the number of cities. - * Space Complexity: O(n * 2^n), due to the DP table and bitmask representation. - */ +* Problem Statement: +* The Traveling Salesman Problem (TSP) asks for the shortest possible route +* that visits a given set of cities and returns to the origin city. +* Each city is connected to every other city, and the goal is to find the shortest route +* that visits each city exactly once and returns to the starting point. +* +* More information on TSP can be found here: +* https://en.wikipedia.org/wiki/Travelling_salesman_problem +* +* Approach: +* This implementation uses dynamic programming (DP) with bitmasking +* to represent visited cities. The DP state is dp[mask][i], +* where 'mask' represents the set of visited cities, and 'i' is the current city. +* +* Time Complexity: O(n^2 * 2^n), where 'n' is the number of cities. +* Space Complexity: O(n * 2^n), due to the DP table and bitmask representation. +*/ - import java.util.Arrays; +import java.util.Arrays; - public class TravelingSalesmanDP { - private static final int INF = Integer.MAX_VALUE / 2; // Infinity value for unvisited paths - - /** - * Solves the TSP using dynamic programming. - * - * @param dist Matrix where dist[i][j] represents the distance between city i and city j. - * @return Minimum cost of traveling through all cities and returning to the start. - */ - public static int tsp(int[][] dist) { - int n = dist.length; - int[][] dp = new int[n][(1 << n)]; // DP table - - // Initialize DP table with infinity - for (int[] row : dp) { - Arrays.fill(row, INF); - } - - // Start at city 0 with only the first city visited - dp[0][1] = 0; - - // Iterate over all subsets of visited cities - for (int mask = 1; mask < (1 << n); mask++) { - for (int u = 0; u < n; u++) { - if ((mask & (1 << u)) == 0) continue; // If city u is not visited in this subset - - for (int v = 0; v < n; v++) { - if ((mask & (1 << v)) != 0 || dist[u][v] == INF) continue; // If city v is already visited - int newMask = mask | (1 << v); // Visit city v - dp[v][newMask] = Math.min(dp[v][newMask], dp[u][mask] + dist[u][v]); - } - } - } - - // Return the minimum cost of visiting all cities and returning to city 0 - int result = INF; - for (int i = 1; i < n; i++) { - result = Math.min(result, dp[i][(1 << n) - 1] + dist[i][0]); - } - - return result; - } - } - \ No newline at end of file +public class TravelingSalesmanDP { + private static final int INF = Integer.MAX_VALUE / 2; // Infinity value for unvisited paths + + /** + * Solves the TSP using dynamic programming. + * + * @param dist Matrix where dist[i][j] represents the distance between city i and city j. + * @return Minimum cost of traveling through all cities and returning to the start. + */ + public static int tsp(int[][] dist) { + int n = dist.length; + int[][] dp = new int[n][(1 << n)]; // DP table + + // Initialize DP table with infinity + for (int[] row : dp) { + Arrays.fill(row, INF); + } + + // Start at city 0 with only the first city visited + dp[0][1] = 0; + + // Iterate over all subsets of visited cities + for (int mask = 1; mask < (1 << n); mask++) { + for (int u = 0; u < n; u++) { + if ((mask & (1 << u)) == 0) { + continue; // If city u is not visited in this subset + } + + for (int v = 0; v < n; v++) { + if ((mask & (1 << v)) != 0 || dist[u][v] == INF) { + continue; // If city v is already visited + } + int newMask = mask | (1 << v); // Visit city v + dp[v][newMask] = Math.min(dp[v][newMask], dp[u][mask] + dist[u][v]); + } + } + } + + // Return the minimum cost of visiting all cities and returning to city 0 + int result = INF; + for (int i = 1; i < n; i++) { + result = Math.min(result, dp[i][(1 << n) - 1] + dist[i][0]); + } + + return result; + } +} diff --git a/src/test/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanTest.java b/src/test/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanTest.java index 770a786d2f96..8654f591b696 100644 --- a/src/test/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanTest.java +++ b/src/test/java/com/thealgorithms/datastructures/graphs/TravelingSalesmanTest.java @@ -18,3 +18,4 @@ public static void main(String[] args) { // Expected output: Minimum cost: 80 } } +