From 4c53d067cbeeebd0dcdd0e7ef6228dc1bf465583 Mon Sep 17 00:00:00 2001 From: Deniz Altunkapan Date: Mon, 27 Jan 2025 23:39:51 +0100 Subject: [PATCH 1/9] Implement solution for Constrained Shortest Path Problem (CSPP) with resource constraints. --- .../graph/ConstraintShortestPath.java | 121 ++++++++++ .../graph/ConstraintShortestPathTest.java | 218 ++++++++++++++++++ 2 files changed, 339 insertions(+) create mode 100644 src/main/java/com/thealgorithms/graph/ConstraintShortestPath.java create mode 100644 src/test/java/com/thealgorithms/graph/ConstraintShortestPathTest.java diff --git a/src/main/java/com/thealgorithms/graph/ConstraintShortestPath.java b/src/main/java/com/thealgorithms/graph/ConstraintShortestPath.java new file mode 100644 index 000000000000..d4109fee08d7 --- /dev/null +++ b/src/main/java/com/thealgorithms/graph/ConstraintShortestPath.java @@ -0,0 +1,121 @@ +package com.thealgorithms.graph; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * This class implements a solution for the Constrained Shortest Path Problem (CSPP). + * also known as Shortest Path Problem with Resource Constraints (SPPRC). + * The goal is to find the shortest path between two nodes while ensuring that + * the resource constraint is not exceeded. + * + * @author Deniz Altunkapan + */ +public class ConstraintShortestPath { + + /** + * Represents a graph using an adjacency list. + * This graph is designed for the Constrained Shortest Path Problem (CSPP). + */ + public static class Graph { + + private List> adjacencyList; + + public Graph(int numNodes) { + adjacencyList = new ArrayList<>(); + for (int i = 0; i < numNodes; i++) { + adjacencyList.add(new ArrayList<>()); + } + } + + /** + * Adds an edge to the graph. + * @param from the starting node + * @param to the ending node + * @param cost the cost of the edge + * @param resource the resource required to traverse the edge + */ + public void addEdge(int from, int to, int cost, int resource) { + adjacencyList.get(from).add(new Edge(from, to, cost, resource)); + } + + /** + * Gets the edges that are adjacent to a given node. + * @param node the node to get the edges for + * @return the list of edges adjacent to the node + */ + public List getEdges(int node) { + return adjacencyList.get(node); + } + + /** + * Gets the number of nodes in the graph. + * @return the number of nodes + */ + public int getNumNodes() { + return adjacencyList.size(); + } + + public static record Edge(int from, int to, int cost, int resource) { + } + } + + private Graph graph; + private int maxResource; + + /** + * Constructs a CSPSolver with the given graph and maximum resource constraint. + * + * @param graph the graph representing the problem + * @param maxResource the maximum allowable resource + */ + public ConstraintShortestPath(Graph graph, int maxResource) { + this.graph = graph; + this.maxResource = maxResource; + } + + /** + * Solves the CSP to find the shortest path from the start node to the target node + * without exceeding the resource constraint. + * + * @param start the starting node + * @param target the target node + * @return the minimum cost to reach the target node within the resource constraint, + * or -1 if no valid path exists + */ + public int solve(int start, int target) { + int numNodes = graph.getNumNodes(); + int[][] dp = new int[maxResource + 1][numNodes]; + + // Initialize dp table with maximum values + for (int i = 0; i <= maxResource; i++) { + Arrays.fill(dp[i], Integer.MAX_VALUE); + } + dp[0][start] = 0; + + // Dynamic Programming: Iterate over resources and nodes + for (int r = 0; r <= maxResource; r++) { + for (int u = 0; u < numNodes; u++) { + if (dp[r][u] == Integer.MAX_VALUE) continue; + for (Graph.Edge edge : graph.getEdges(u)) { + int v = edge.to(); + int cost = edge.cost(); + int resource = edge.resource(); + + if (r + resource <= maxResource) { + dp[r + resource][v] = Math.min(dp[r + resource][v], dp[r][u] + cost); + } + } + } + } + + // Find the minimum cost to reach the target node + int minCost = Integer.MAX_VALUE; + for (int r = 0; r <= maxResource; r++) { + minCost = Math.min(minCost, dp[r][target]); + } + + return minCost == Integer.MAX_VALUE ? -1 : minCost; + } +} diff --git a/src/test/java/com/thealgorithms/graph/ConstraintShortestPathTest.java b/src/test/java/com/thealgorithms/graph/ConstraintShortestPathTest.java new file mode 100644 index 000000000000..7fcb84c8d60f --- /dev/null +++ b/src/test/java/com/thealgorithms/graph/ConstraintShortestPathTest.java @@ -0,0 +1,218 @@ +package com.thealgorithms.graph; + +import static org.junit.jupiter.api.Assertions.*; + +import com.thealgorithms.graph.ConstraintShortestPath.*; +import org.junit.jupiter.api.Test; + +public class ConstraintShortestPathTest { + + /** + * Tests a simple linear graph to verify if the solver calculates the shortest path correctly. + * Expected: The minimal path cost from node 0 to node 2 should be 5 while not exceeding the resource limit. + */ + @Test + public void testSimpleGraph() { + Graph graph = new Graph(3); + graph.addEdge(0, 1, 2, 3); + graph.addEdge(1, 2, 3, 2); + + int maxResource = 5; + ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + + assertEquals(5, solver.solve(0, 2)); + } + + /** + * Tests a graph where no valid path exists due to resource constraints. + * Expected: The solver should return -1, indicating no path is feasible. + */ + @Test + public void testNoPath() { + Graph graph = new Graph(3); + graph.addEdge(0, 1, 2, 6); + graph.addEdge(1, 2, 3, 6); + + int maxResource = 5; + ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + + assertEquals(-1, solver.solve(0, 2)); + } + + /** + * Tests a graph with multiple paths between source and destination. + * Expected: The solver should choose the path with the minimal cost of 5, considering the resource limit. + */ + @Test + public void testMultiplePaths() { + Graph graph = new Graph(4); + graph.addEdge(0, 1, 1, 1); + graph.addEdge(1, 3, 5, 2); + graph.addEdge(0, 2, 2, 1); + graph.addEdge(2, 3, 3, 2); + + int maxResource = 3; + ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + + assertEquals(5, solver.solve(0, 3)); + } + + /** + * Verifies that the solver allows a path exactly matching the resource limit. + * Expected: The path is valid with a total cost of 5. + */ + @Test + public void testExactResourceLimit() { + Graph graph = new Graph(3); + graph.addEdge(0, 1, 2, 3); + graph.addEdge(1, 2, 3, 2); + + int maxResource = 5; + ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + + assertEquals(5, solver.solve(0, 2)); + } + + /** + * Tests a disconnected graph where the destination node cannot be reached. + * Expected: The solver should return -1, as the destination is unreachable. + */ + @Test + public void testDisconnectedGraph() { + Graph graph = new Graph(4); + graph.addEdge(0, 1, 2, 2); + graph.addEdge(2, 3, 3, 2); + + int maxResource = 5; + ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + + assertEquals(-1, solver.solve(0, 3)); + } + + /** + * Tests a graph with cycles to ensure the solver does not fall into infinite loops and correctly calculates costs. + * Expected: The solver should compute the minimal path cost of 6. + */ + @Test + public void testGraphWithCycles() { + Graph graph = new Graph(4); + graph.addEdge(0, 1, 2, 1); + graph.addEdge(1, 2, 3, 1); + graph.addEdge(2, 0, 1, 1); + graph.addEdge(1, 3, 4, 2); + + int maxResource = 3; + ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + + assertEquals(6, solver.solve(0, 3)); + } + + /** + * Tests the solver's performance and correctness on a large linear graph with 1000 nodes. + * Expected: The solver should efficiently calculate the shortest path with a cost of 999. + */ + @Test + public void testLargeGraphPerformance() { + int nodeCount = 1000; + Graph graph = new Graph(nodeCount); + for (int i = 0; i < nodeCount - 1; i++) { + graph.addEdge(i, i + 1, 1, 1); + } + + int maxResource = 1000; + ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + + assertEquals(999, solver.solve(0, nodeCount - 1)); + } + + /** + * Tests a graph with isolated nodes to ensure the solver recognizes unreachable destinations. + * Expected: The solver should return -1 for unreachable nodes. + */ + @Test + public void testIsolatedNodes() { + Graph graph = new Graph(5); + graph.addEdge(0, 1, 2, 1); + graph.addEdge(1, 2, 3, 1); + + int maxResource = 5; + ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + + assertEquals(-1, solver.solve(0, 3)); + } + + /** + * Tests a cyclic large graph with multiple overlapping paths. + * Expected: The solver should calculate the shortest path cost of 5. + */ + @Test + public void testCyclicLargeGraph() { + Graph graph = new Graph(10); + for (int i = 0; i < 9; i++) { + graph.addEdge(i, (i + 1) % 10, 1, 1); + } + graph.addEdge(0, 5, 5, 3); + + int maxResource = 10; + ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + + assertEquals(5, solver.solve(0, 5)); + } + + /** + * Tests a large complex graph with multiple paths and varying resource constraints. + * Expected: The solver should identify the optimal path with a cost of 19 within the resource limit. + */ + @Test + public void testLargeComplexGraph() { + Graph graph = new Graph(10); + graph.addEdge(0, 1, 4, 2); + graph.addEdge(0, 2, 3, 3); + graph.addEdge(1, 3, 2, 1); + graph.addEdge(2, 3, 5, 2); + graph.addEdge(2, 4, 8, 4); + graph.addEdge(3, 5, 7, 3); + graph.addEdge(3, 6, 6, 2); + graph.addEdge(4, 6, 3, 2); + graph.addEdge(5, 7, 1, 1); + graph.addEdge(6, 7, 2, 2); + graph.addEdge(7, 8, 3, 1); + graph.addEdge(8, 9, 2, 1); + + int maxResource = 10; + ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + + assertEquals(19, solver.solve(0, 9)); + } + + /** + * Edge case test where the graph has only one node and no edges. + * Expected: The minimal path cost is 0, as the start and destination are the same. + */ + @Test + public void testSingleNodeGraph() { + Graph graph = new Graph(1); + + int maxResource = 0; + ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + + assertEquals(0, solver.solve(0, 0)); + } + + /** + * Tests a graph with multiple paths but a tight resource constraint. + * Expected: The solver should return -1 if no path can be found within the resource limit. + */ + @Test + public void testTightResourceConstraint() { + Graph graph = new Graph(4); + graph.addEdge(0, 1, 3, 4); + graph.addEdge(1, 2, 1, 2); + graph.addEdge(0, 2, 2, 2); + + int maxResource = 3; + ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + + assertEquals(2, solver.solve(0, 2)); + } +} From 100ba517f808a8cff185f74d81aac1ec1728742d Mon Sep 17 00:00:00 2001 From: Deniz Altunkapan Date: Tue, 28 Jan 2025 00:43:43 +0100 Subject: [PATCH 2/9] Fix issues to make the project build successfully and rename files --- ...Path.java => ConstrainedShortestPath.java} | 10 ++++--- ....java => ConstrainedShortestPathTest.java} | 30 +++++++++---------- 2 files changed, 21 insertions(+), 19 deletions(-) rename src/main/java/com/thealgorithms/graph/{ConstraintShortestPath.java => ConstrainedShortestPath.java} (93%) rename src/test/java/com/thealgorithms/graph/{ConstraintShortestPathTest.java => ConstrainedShortestPathTest.java} (82%) diff --git a/src/main/java/com/thealgorithms/graph/ConstraintShortestPath.java b/src/main/java/com/thealgorithms/graph/ConstrainedShortestPath.java similarity index 93% rename from src/main/java/com/thealgorithms/graph/ConstraintShortestPath.java rename to src/main/java/com/thealgorithms/graph/ConstrainedShortestPath.java index d4109fee08d7..f397989911d9 100644 --- a/src/main/java/com/thealgorithms/graph/ConstraintShortestPath.java +++ b/src/main/java/com/thealgorithms/graph/ConstrainedShortestPath.java @@ -12,7 +12,7 @@ * * @author Deniz Altunkapan */ -public class ConstraintShortestPath { +public class ConstrainedShortestPath { /** * Represents a graph using an adjacency list. @@ -57,7 +57,7 @@ public int getNumNodes() { return adjacencyList.size(); } - public static record Edge(int from, int to, int cost, int resource) { + public record Edge(int from, int to, int cost, int resource) { } } @@ -70,7 +70,7 @@ public static record Edge(int from, int to, int cost, int resource) { * @param graph the graph representing the problem * @param maxResource the maximum allowable resource */ - public ConstraintShortestPath(Graph graph, int maxResource) { + public ConstrainedShortestPath(Graph graph, int maxResource) { this.graph = graph; this.maxResource = maxResource; } @@ -97,7 +97,9 @@ public int solve(int start, int target) { // Dynamic Programming: Iterate over resources and nodes for (int r = 0; r <= maxResource; r++) { for (int u = 0; u < numNodes; u++) { - if (dp[r][u] == Integer.MAX_VALUE) continue; + if (dp[r][u] == Integer.MAX_VALUE) { + continue; + } for (Graph.Edge edge : graph.getEdges(u)) { int v = edge.to(); int cost = edge.cost(); diff --git a/src/test/java/com/thealgorithms/graph/ConstraintShortestPathTest.java b/src/test/java/com/thealgorithms/graph/ConstrainedShortestPathTest.java similarity index 82% rename from src/test/java/com/thealgorithms/graph/ConstraintShortestPathTest.java rename to src/test/java/com/thealgorithms/graph/ConstrainedShortestPathTest.java index 7fcb84c8d60f..eccd359f2634 100644 --- a/src/test/java/com/thealgorithms/graph/ConstraintShortestPathTest.java +++ b/src/test/java/com/thealgorithms/graph/ConstrainedShortestPathTest.java @@ -1,11 +1,11 @@ package com.thealgorithms.graph; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; -import com.thealgorithms.graph.ConstraintShortestPath.*; +import com.thealgorithms.graph.ConstrainedShortestPath.Graph; import org.junit.jupiter.api.Test; -public class ConstraintShortestPathTest { +public class ConstrainedShortestPathTest { /** * Tests a simple linear graph to verify if the solver calculates the shortest path correctly. @@ -18,7 +18,7 @@ public void testSimpleGraph() { graph.addEdge(1, 2, 3, 2); int maxResource = 5; - ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource); assertEquals(5, solver.solve(0, 2)); } @@ -34,7 +34,7 @@ public void testNoPath() { graph.addEdge(1, 2, 3, 6); int maxResource = 5; - ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource); assertEquals(-1, solver.solve(0, 2)); } @@ -52,7 +52,7 @@ public void testMultiplePaths() { graph.addEdge(2, 3, 3, 2); int maxResource = 3; - ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource); assertEquals(5, solver.solve(0, 3)); } @@ -68,7 +68,7 @@ public void testExactResourceLimit() { graph.addEdge(1, 2, 3, 2); int maxResource = 5; - ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource); assertEquals(5, solver.solve(0, 2)); } @@ -84,7 +84,7 @@ public void testDisconnectedGraph() { graph.addEdge(2, 3, 3, 2); int maxResource = 5; - ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource); assertEquals(-1, solver.solve(0, 3)); } @@ -102,7 +102,7 @@ public void testGraphWithCycles() { graph.addEdge(1, 3, 4, 2); int maxResource = 3; - ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource); assertEquals(6, solver.solve(0, 3)); } @@ -120,7 +120,7 @@ public void testLargeGraphPerformance() { } int maxResource = 1000; - ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource); assertEquals(999, solver.solve(0, nodeCount - 1)); } @@ -136,7 +136,7 @@ public void testIsolatedNodes() { graph.addEdge(1, 2, 3, 1); int maxResource = 5; - ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource); assertEquals(-1, solver.solve(0, 3)); } @@ -154,7 +154,7 @@ public void testCyclicLargeGraph() { graph.addEdge(0, 5, 5, 3); int maxResource = 10; - ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource); assertEquals(5, solver.solve(0, 5)); } @@ -180,7 +180,7 @@ public void testLargeComplexGraph() { graph.addEdge(8, 9, 2, 1); int maxResource = 10; - ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource); assertEquals(19, solver.solve(0, 9)); } @@ -194,7 +194,7 @@ public void testSingleNodeGraph() { Graph graph = new Graph(1); int maxResource = 0; - ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource); assertEquals(0, solver.solve(0, 0)); } @@ -211,7 +211,7 @@ public void testTightResourceConstraint() { graph.addEdge(0, 2, 2, 2); int maxResource = 3; - ConstraintShortestPath solver = new ConstraintShortestPath(graph, maxResource); + ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource); assertEquals(2, solver.solve(0, 2)); } From fb3ba561d3f3f95e4205bce09f313ba250ee2459 Mon Sep 17 00:00:00 2001 From: Deniz Altunkapan Date: Fri, 28 Feb 2025 23:32:56 +0100 Subject: [PATCH 3/9] add dp solution for maximum weighted matching in a tree --- .../graphs/UndirectedAdjacencyListGraph.java | 69 ++++++++++ .../dynamicprogramming/TreeMatching.java | 80 ++++++++++++ .../dynamicprogramming/TreeMatchingTest.java | 120 ++++++++++++++++++ 3 files changed, 269 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/graphs/UndirectedAdjacencyListGraph.java create mode 100644 src/main/java/com/thealgorithms/dynamicprogramming/TreeMatching.java create mode 100644 src/test/java/com/thealgorithms/dynamicprogramming/TreeMatchingTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/UndirectedAdjacencyListGraph.java b/src/main/java/com/thealgorithms/datastructures/graphs/UndirectedAdjacencyListGraph.java new file mode 100644 index 000000000000..8aafc1ef3368 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/graphs/UndirectedAdjacencyListGraph.java @@ -0,0 +1,69 @@ +package com.thealgorithms.datastructures.graphs; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; + +public class UndirectedAdjacencyListGraph { + private ArrayList> adjacencyList = new ArrayList<>(); + + /** + * Adds a new node to the graph by adding an empty HashMap for its neighbors. + * @return the index of the newly added node in the adjacency list + */ + public int addNode() { + adjacencyList.add(new HashMap<>()); + return adjacencyList.size() - 1; + } + + /** + * Adds an undirected edge between the origin node (@orig) and the destination node (@dest) with the specified weight. + * If the edge already exists, no changes are made. + * @param orig the index of the origin node + * @param dest the index of the destination node + * @param weight the weight of the edge between @orig and @dest + * @return true if the edge was successfully added, false if the edge already exists or if any node index is invalid + */ + public boolean addEdge(int orig, int dest, int weight) { + int numNodes = adjacencyList.size(); + if (orig >= numNodes || dest >= numNodes || orig < 0 || dest < 0) { + return false; + } + + if (adjacencyList.get(orig).containsKey(dest)) { + return false; + } + + adjacencyList.get(orig).put(dest, weight); + adjacencyList.get(dest).put(orig, weight); + return true; + } + + /** + * Returns the set of all adjacent nodes (neighbors) for the given node. + * @param node the index of the node whose neighbors are to be retrieved + * @return a HashSet containing the indices of all neighboring nodes + */ + public HashSet getNeighbors(int node) { + return new HashSet<>(adjacencyList.get(node).keySet()); + } + + /** + * Returns the weight of the edge between the origin node (@orig) and the destination node (@dest). + * If no edge exists, returns null. + * @param orig the index of the origin node + * @param dest the index of the destination node + * @return the weight of the edge between @orig and @dest, or null if no edge exists + */ + public Integer getEdgeWeight(int orig, int dest) { + return adjacencyList.get(orig).getOrDefault(dest, null); + } + + /** + * Returns the number of nodes currently in the graph. + * @return the number of nodes in the graph + */ + public int size() { + return adjacencyList.size(); + } +} diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/TreeMatching.java b/src/main/java/com/thealgorithms/dynamicprogramming/TreeMatching.java new file mode 100644 index 000000000000..655686aecbb2 --- /dev/null +++ b/src/main/java/com/thealgorithms/dynamicprogramming/TreeMatching.java @@ -0,0 +1,80 @@ +package com.thealgorithms.dynamicprogramming; + +import com.thealgorithms.datastructures.graphs.UndirectedAdjacencyListGraph; + +/** + * This class implements the algorithm for calculating the maximum weighted matching in a tree. + * The tree is represented as an undirected graph with weighted edges. + * + * Problem Description: + * Given an undirected tree G = (V, E) with edge weights γ: E → N and a root r ∈ V, + * the goal is to find a maximum weight matching M ⊆ E such that no two edges in M + * share a common vertex. The sum of the weights of the edges in M, ∑ e∈M γ(e), should be maximized. + * For more Information: Matching (graph theory) + * + * @author Deniz Altunkapan + */ +public class TreeMatching { + + private UndirectedAdjacencyListGraph graph; + private int[][] dp; + + /** + * Constructor that initializes the graph and the DP table. + * + * @param graph The graph that represents the tree and is used for the matching algorithm. + */ + public TreeMatching(UndirectedAdjacencyListGraph graph) { + this.graph = graph; + this.dp = new int[graph.size()][2]; + } + + /** + * Calculates the maximum weighted matching for the tree, starting from the given root node. + * + * @param root The index of the root node of the tree. + * @param parent The index of the parent node (used for recursion). + * @return The maximum weighted matching for the tree, starting from the root node. + * + */ + public int getMaxMatching(int root, int parent){ + if (root < 0 || root >= graph.size()) { + throw new IllegalArgumentException("Invalid root: " + root); + } + MaxMatching(root, parent); + return Math.max(dp[root][0],dp[root][1]); + } + + /** + * Recursively computes the maximum weighted matching for a node, assuming that the node + * can either be included or excluded from the matching. + * + * @param node The index of the current node for which the matching is calculated. + * @param parent The index of the parent node (to avoid revisiting the parent node during recursion). + */ + private void MaxMatching(int node, int parent) { + dp[node][0] = 0; + dp[node][1] = 0; + + int sumWithoutEdge = 0; + for (int adjNode : graph.getNeighbors(node)) { + if (adjNode == parent){ + continue; + } + MaxMatching(adjNode, node); + sumWithoutEdge += Math.max(dp[adjNode][0], dp[adjNode][1]); + } + + dp[node][0] = sumWithoutEdge; + + for (int adjNode : graph.getNeighbors(node)) { + if (adjNode == parent){ + continue; + } + int weight = graph.getEdgeWeight(node, adjNode); + dp[node][1] = Math.max(dp[node][1], + sumWithoutEdge - Math.max(dp[adjNode][0], dp[adjNode][1]) + dp[adjNode][0] + weight); + } + } + +} diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/TreeMatchingTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/TreeMatchingTest.java new file mode 100644 index 000000000000..a833e3256f68 --- /dev/null +++ b/src/test/java/com/thealgorithms/dynamicprogramming/TreeMatchingTest.java @@ -0,0 +1,120 @@ +package com.thealgorithms.dynamicprogramming; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import com.thealgorithms.datastructures.graphs.UndirectedAdjacencyListGraph; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class TreeMatchingTest { + UndirectedAdjacencyListGraph graph; + + @BeforeEach + void setUp() { + graph = new UndirectedAdjacencyListGraph(); + for (int i = 0; i < 14; i++) { + graph.addNode(); + } + } + + @Test + void testMaxMatchingForGeneralTree() { + graph.addEdge(0, 1, 20); + graph.addEdge(0, 2, 30); + graph.addEdge(1, 3, 40); + graph.addEdge(1, 4, 10); + graph.addEdge(2, 5, 20); + graph.addEdge(3, 6, 30); + graph.addEdge(3, 7, 30); + graph.addEdge(5, 8, 40); + graph.addEdge(5, 9, 10); + + TreeMatching treeMatching = new TreeMatching(graph); + assertEquals(110,treeMatching.getMaxMatching(0,-1)); + } + + @Test + void testMaxMatchingForBalancedTree() { + graph.addEdge(0, 1, 20); + graph.addEdge(0, 2, 30); + graph.addEdge(0, 3, 40); + graph.addEdge(1, 4, 10); + graph.addEdge(1, 5, 20); + graph.addEdge(2, 6, 20); + graph.addEdge(3, 7, 30); + graph.addEdge(5, 8, 10); + graph.addEdge(5, 9, 20); + graph.addEdge(7, 10, 10); + graph.addEdge(7, 11, 10); + graph.addEdge(7, 12, 5); + TreeMatching treeMatching = new TreeMatching(graph); + assertEquals(100,treeMatching.getMaxMatching(0,-1)); + } + + @Test + void testMaxMatchingForTreeWithVariedEdgeWeights() { + graph.addEdge(0, 1, 20); + graph.addEdge(0, 2, 30); + graph.addEdge(0, 3, 40); + graph.addEdge(0, 4, 50); + graph.addEdge(1, 5, 20); + graph.addEdge(2, 6, 20); + graph.addEdge(3, 7, 30); + graph.addEdge(5, 8, 10); + graph.addEdge(5, 9, 20); + graph.addEdge(7, 10, 10); + graph.addEdge(4, 11, 50); + graph.addEdge(4, 12, 20); + TreeMatching treeMatching = new TreeMatching(graph); + assertEquals(140,treeMatching.getMaxMatching(0,-1)); + } + + @Test + void emptyTree() { + TreeMatching treeMatching = new TreeMatching(graph); + assertEquals(0,treeMatching.getMaxMatching(0,-1)); + } + + @Test + void testSingleNodeTree() { + UndirectedAdjacencyListGraph singleNodeGraph = new UndirectedAdjacencyListGraph(); + singleNodeGraph.addNode(); + + TreeMatching treeMatching = new TreeMatching(singleNodeGraph); + assertEquals(0, treeMatching.getMaxMatching(0, -1)); + } + + @Test + void testLinearTree() { + graph.addEdge(0, 1, 10); + graph.addEdge(1, 2, 20); + graph.addEdge(2, 3, 30); + graph.addEdge(3, 4, 40); + + TreeMatching treeMatching = new TreeMatching(graph); + assertEquals(60, treeMatching.getMaxMatching(0, -1)); + } + + @Test + void testStarShapedTree() { + graph.addEdge(0, 1, 15); + graph.addEdge(0, 2, 25); + graph.addEdge(0, 3, 35); + graph.addEdge(0, 4, 45); + + TreeMatching treeMatching = new TreeMatching(graph); + assertEquals(45, treeMatching.getMaxMatching(0, -1)); + } + + @Test + void testUnbalancedTree() { + graph.addEdge(0, 1, 10); + graph.addEdge(0, 2, 20); + graph.addEdge(1, 3, 30); + graph.addEdge(2, 4, 40); + graph.addEdge(4, 5, 50); + + TreeMatching treeMatching = new TreeMatching(graph); + assertEquals(100, treeMatching.getMaxMatching(0, -1)); + } + +} From 02d4cb0dc6f196ef484d9f7cd19db4304b24e3ae Mon Sep 17 00:00:00 2001 From: Deniz Altunkapan Date: Fri, 28 Feb 2025 23:47:09 +0100 Subject: [PATCH 4/9] Formatted code using clang-format to ensure consistent styling --- .../dynamicprogramming/TreeMatching.java | 12 +++++------- .../dynamicprogramming/TreeMatchingTest.java | 14 +++++++------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/TreeMatching.java b/src/main/java/com/thealgorithms/dynamicprogramming/TreeMatching.java index 655686aecbb2..9d0093a59f69 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/TreeMatching.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/TreeMatching.java @@ -37,12 +37,12 @@ public TreeMatching(UndirectedAdjacencyListGraph graph) { * @return The maximum weighted matching for the tree, starting from the root node. * */ - public int getMaxMatching(int root, int parent){ + public int getMaxMatching(int root, int parent) { if (root < 0 || root >= graph.size()) { throw new IllegalArgumentException("Invalid root: " + root); } MaxMatching(root, parent); - return Math.max(dp[root][0],dp[root][1]); + return Math.max(dp[root][0], dp[root][1]); } /** @@ -58,7 +58,7 @@ private void MaxMatching(int node, int parent) { int sumWithoutEdge = 0; for (int adjNode : graph.getNeighbors(node)) { - if (adjNode == parent){ + if (adjNode == parent) { continue; } MaxMatching(adjNode, node); @@ -68,13 +68,11 @@ private void MaxMatching(int node, int parent) { dp[node][0] = sumWithoutEdge; for (int adjNode : graph.getNeighbors(node)) { - if (adjNode == parent){ + if (adjNode == parent) { continue; } int weight = graph.getEdgeWeight(node, adjNode); - dp[node][1] = Math.max(dp[node][1], - sumWithoutEdge - Math.max(dp[adjNode][0], dp[adjNode][1]) + dp[adjNode][0] + weight); + dp[node][1] = Math.max(dp[node][1], sumWithoutEdge - Math.max(dp[adjNode][0], dp[adjNode][1]) + dp[adjNode][0] + weight); } } - } diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/TreeMatchingTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/TreeMatchingTest.java index a833e3256f68..d5418770a5d1 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/TreeMatchingTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/TreeMatchingTest.java @@ -1,9 +1,10 @@ package com.thealgorithms.dynamicprogramming; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.thealgorithms.datastructures.graphs.UndirectedAdjacencyListGraph; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import com.thealgorithms.datastructures.graphs.UndirectedAdjacencyListGraph; -import static org.junit.jupiter.api.Assertions.assertEquals; class TreeMatchingTest { UndirectedAdjacencyListGraph graph; @@ -29,7 +30,7 @@ void testMaxMatchingForGeneralTree() { graph.addEdge(5, 9, 10); TreeMatching treeMatching = new TreeMatching(graph); - assertEquals(110,treeMatching.getMaxMatching(0,-1)); + assertEquals(110, treeMatching.getMaxMatching(0, -1)); } @Test @@ -47,7 +48,7 @@ void testMaxMatchingForBalancedTree() { graph.addEdge(7, 11, 10); graph.addEdge(7, 12, 5); TreeMatching treeMatching = new TreeMatching(graph); - assertEquals(100,treeMatching.getMaxMatching(0,-1)); + assertEquals(100, treeMatching.getMaxMatching(0, -1)); } @Test @@ -65,13 +66,13 @@ void testMaxMatchingForTreeWithVariedEdgeWeights() { graph.addEdge(4, 11, 50); graph.addEdge(4, 12, 20); TreeMatching treeMatching = new TreeMatching(graph); - assertEquals(140,treeMatching.getMaxMatching(0,-1)); + assertEquals(140, treeMatching.getMaxMatching(0, -1)); } @Test void emptyTree() { TreeMatching treeMatching = new TreeMatching(graph); - assertEquals(0,treeMatching.getMaxMatching(0,-1)); + assertEquals(0, treeMatching.getMaxMatching(0, -1)); } @Test @@ -116,5 +117,4 @@ void testUnbalancedTree() { TreeMatching treeMatching = new TreeMatching(graph); assertEquals(100, treeMatching.getMaxMatching(0, -1)); } - } From 834850e64f7cc2fe9e67185a6293f58dcfa7b0a1 Mon Sep 17 00:00:00 2001 From: Deniz Altunkapan Date: Fri, 28 Feb 2025 23:56:07 +0100 Subject: [PATCH 5/9] =?UTF-8?q?Fix=20method=20name=20to=20follow=20Java=20?= =?UTF-8?q?naming=20conventions=20(MaxMatching=20=E2=86=92=20maxMatching)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/thealgorithms/dynamicprogramming/TreeMatching.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/TreeMatching.java b/src/main/java/com/thealgorithms/dynamicprogramming/TreeMatching.java index 9d0093a59f69..9fd82ccaf078 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/TreeMatching.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/TreeMatching.java @@ -41,7 +41,7 @@ public int getMaxMatching(int root, int parent) { if (root < 0 || root >= graph.size()) { throw new IllegalArgumentException("Invalid root: " + root); } - MaxMatching(root, parent); + maxMatching(root, parent); return Math.max(dp[root][0], dp[root][1]); } @@ -52,7 +52,7 @@ public int getMaxMatching(int root, int parent) { * @param node The index of the current node for which the matching is calculated. * @param parent The index of the parent node (to avoid revisiting the parent node during recursion). */ - private void MaxMatching(int node, int parent) { + private void maxMatching(int node, int parent) { dp[node][0] = 0; dp[node][1] = 0; @@ -61,7 +61,7 @@ private void MaxMatching(int node, int parent) { if (adjNode == parent) { continue; } - MaxMatching(adjNode, node); + maxMatching(adjNode, node); sumWithoutEdge += Math.max(dp[adjNode][0], dp[adjNode][1]); } From b071a9eca3f588e5c570f6b8b962db3e28ec9395 Mon Sep 17 00:00:00 2001 From: Deniz Altunkapan Date: Mon, 31 Mar 2025 16:29:42 +0200 Subject: [PATCH 6/9] add Traveling Salesman Problem implementation --- .../graph/TravelingSalesman.java | 150 ++++++++++++++++++ .../graph/TravelingSalesmanTest.java | 127 +++++++++++++++ 2 files changed, 277 insertions(+) create mode 100644 src/main/java/com/thealgorithms/graph/TravelingSalesman.java create mode 100644 src/test/java/com/thealgorithms/graph/TravelingSalesmanTest.java diff --git a/src/main/java/com/thealgorithms/graph/TravelingSalesman.java b/src/main/java/com/thealgorithms/graph/TravelingSalesman.java new file mode 100644 index 000000000000..aa3adb0b3915 --- /dev/null +++ b/src/main/java/com/thealgorithms/graph/TravelingSalesman.java @@ -0,0 +1,150 @@ +package com.thealgorithms.graph; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * This class provides solutions to the Traveling Salesman Problem (TSP) using both brute-force and dynamic programming approaches. + * For more information, see Wikipedia. + * @author Deniz Altunkapan + */ +public class TravelingSalesman { + + /** + * Solves the Traveling Salesman Problem (TSP) using brute-force approach. + * This method generates all possible permutations of cities, calculates the total distance for each route, and returns the shortest distance found. + * + * @param distanceMatrix A square matrix where element [i][j] represents the distance from city i to city j. + * @return The shortest possible route distance visiting all cities exactly once and returning to the starting city. + */ + public static int bruteForce(int[][] distanceMatrix) { + if (distanceMatrix.length <= 1) { + return 0; + } + + List cities = new ArrayList<>(); + for (int i = 1; i < distanceMatrix.length; i++) { + cities.add(i); + } + + List> permutations = generatePermutations(cities); + int minDistance = Integer.MAX_VALUE; + + for (List permutation : permutations) { + List route = new ArrayList<>(); + route.add(0); + route.addAll(permutation); + int currentDistance = calculateDistance(distanceMatrix, route); + if (currentDistance < minDistance) { + minDistance = currentDistance; + } + } + + return minDistance; + } + + /** + * Computes the total distance of a given route. + * + * @param distanceMatrix A square matrix where element [i][j] represents the + * distance from city i to city j. + * @param route A list representing the order in which the cities are visited. + * @return The total distance of the route, or Integer.MAX_VALUE if the route is invalid. + */ + public static int calculateDistance(int[][] distanceMatrix, List route) { + int distance = 0; + for (int i = 0; i < route.size() - 1; i++) { + int d = distanceMatrix[route.get(i)][route.get(i + 1)]; + if (d == Integer.MAX_VALUE) { + return Integer.MAX_VALUE; + } + distance += d; + } + int returnDist = distanceMatrix[route.get(route.size() - 1)][route.get(0)]; + return (returnDist == Integer.MAX_VALUE) ? Integer.MAX_VALUE : distance + returnDist; + } + + /** + * Generates all permutations of a given list of cities. + * + * @param cities A list of cities to permute. + * @return A list of all possible permutations. + */ + private static List> generatePermutations(List cities) { + List> permutations = new ArrayList<>(); + permute(cities, 0, permutations); + return permutations; + } + + /** + * Recursively generates permutations using backtracking. + * + * @param arr The list of cities. + * @param k The current index in the permutation process. + * @param output The list to store generated permutations. + */ + private static void permute(List arr, int k, List> output) { + if (k == arr.size()) { + output.add(new ArrayList<>(arr)); + return; + } + for (int i = k; i < arr.size(); i++) { + Collections.swap(arr, i, k); + permute(arr, k + 1, output); + Collections.swap(arr, i, k); + } + } + + /** + * Solves the Traveling Salesman Problem (TSP) using dynamic programming with the Held-Karp algorithm. + * + * @param distanceMatrix A square matrix where element [i][j] represents the distance from city i to city j. + * @return The shortest possible route distance visiting all cities exactly once and returning to the starting city. + * @throws IllegalArgumentException if the input matrix is not square. + */ + public static int dynamicProgramming(int[][] distanceMatrix) { + if (distanceMatrix.length == 0) { + return 0; + } + int n = distanceMatrix.length; + + for (int[] row : distanceMatrix) { + if (row.length != n) { + throw new IllegalArgumentException("Matrix must be square"); + } + } + + int[][] dp = new int[n][1 << n]; + for (int[] row : dp) { + Arrays.fill(row, Integer.MAX_VALUE); + } + dp[0][1] = 0; + + for (int mask = 1; mask < (1 << n); mask++) { + for (int u = 0; u < n; u++) { + if ((mask & (1 << u)) == 0 || dp[u][mask] == Integer.MAX_VALUE) { + continue; + } + for (int v = 0; v < n; v++) { + if ((mask & (1 << v)) != 0 || distanceMatrix[u][v] == Integer.MAX_VALUE) { + continue; + } + int newMask = mask | (1 << v); + dp[v][newMask] = Math.min(dp[v][newMask], dp[u][mask] + distanceMatrix[u][v]); + } + } + } + + int minDistance = Integer.MAX_VALUE; + int fullMask = (1 << n) - 1; + for (int i = 1; i < n; i++) { + if (dp[i][fullMask] != Integer.MAX_VALUE && distanceMatrix[i][0] != Integer.MAX_VALUE) { + minDistance = Math.min(minDistance, dp[i][fullMask] + distanceMatrix[i][0]); + } + } + + return minDistance == Integer.MAX_VALUE ? 0 : minDistance; + } +} diff --git a/src/test/java/com/thealgorithms/graph/TravelingSalesmanTest.java b/src/test/java/com/thealgorithms/graph/TravelingSalesmanTest.java new file mode 100644 index 000000000000..0c595625d4ac --- /dev/null +++ b/src/test/java/com/thealgorithms/graph/TravelingSalesmanTest.java @@ -0,0 +1,127 @@ +package com.thealgorithms.graph; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +public class TravelingSalesmanTest { + + // Test Case 1: A simple distance matrix with 4 cities + @Test + public void testBruteForceSimple() { + int[][] distanceMatrix = {{0, 10, 15, 20}, {10, 0, 35, 25}, {15, 35, 0, 30}, {20, 25, 30, 0}}; + int expectedMinDistance = 80; + int result = TravelingSalesman.bruteForce(distanceMatrix); + assertEquals(expectedMinDistance, result); + } + + @Test + public void testDynamicProgrammingSimple() { + int[][] distanceMatrix = {{0, 10, 15, 20}, {10, 0, 35, 25}, {15, 35, 0, 30}, {20, 25, 30, 0}}; + int expectedMinDistance = 80; + int result = TravelingSalesman.dynamicProgramming(distanceMatrix); + assertEquals(expectedMinDistance, result); + } + + // Test Case 2: A distance matrix with 3 cities + @Test + public void testBruteForceThreeCities() { + int[][] distanceMatrix = {{0, 10, 15}, {10, 0, 35}, {15, 35, 0}}; + int expectedMinDistance = 60; + int result = TravelingSalesman.bruteForce(distanceMatrix); + assertEquals(expectedMinDistance, result); + } + + @Test + public void testDynamicProgrammingThreeCities() { + int[][] distanceMatrix = {{0, 10, 15}, {10, 0, 35}, {15, 35, 0}}; + int expectedMinDistance = 60; + int result = TravelingSalesman.dynamicProgramming(distanceMatrix); + assertEquals(expectedMinDistance, result); + } + + // Test Case 3: A distance matrix with 5 cities (larger input) + @Test + public void testBruteForceFiveCities() { + int[][] distanceMatrix = {{0, 2, 9, 10, 1}, {2, 0, 6, 5, 8}, {9, 6, 0, 4, 3}, {10, 5, 4, 0, 7}, {1, 8, 3, 7, 0}}; + int expectedMinDistance = 15; + int result = TravelingSalesman.bruteForce(distanceMatrix); + assertEquals(expectedMinDistance, result); + } + + @Test + public void testDynamicProgrammingFiveCities() { + int[][] distanceMatrix = {{0, 2, 9, 10, 1}, {2, 0, 6, 5, 8}, {9, 6, 0, 4, 3}, {10, 5, 4, 0, 7}, {1, 8, 3, 7, 0}}; + int expectedMinDistance = 15; + int result = TravelingSalesman.dynamicProgramming(distanceMatrix); + assertEquals(expectedMinDistance, result); + } + + // Test Case 4: A distance matrix with 2 cities (simple case) + @Test + public void testBruteForceTwoCities() { + int[][] distanceMatrix = {{0, 1}, {1, 0}}; + int expectedMinDistance = 2; + int result = TravelingSalesman.bruteForce(distanceMatrix); + assertEquals(expectedMinDistance, result); + } + + @Test + public void testDynamicProgrammingTwoCities() { + int[][] distanceMatrix = {{0, 1}, {1, 0}}; + int expectedMinDistance = 2; + int result = TravelingSalesman.dynamicProgramming(distanceMatrix); + assertEquals(expectedMinDistance, result); + } + + // Test Case 5: A distance matrix with identical distances + @Test + public void testBruteForceEqualDistances() { + int[][] distanceMatrix = {{0, 10, 10, 10}, {10, 0, 10, 10}, {10, 10, 0, 10}, {10, 10, 10, 0}}; + int expectedMinDistance = 40; + int result = TravelingSalesman.bruteForce(distanceMatrix); + assertEquals(expectedMinDistance, result); + } + + @Test + public void testDynamicProgrammingEqualDistances() { + int[][] distanceMatrix = {{0, 10, 10, 10}, {10, 0, 10, 10}, {10, 10, 0, 10}, {10, 10, 10, 0}}; + int expectedMinDistance = 40; + int result = TravelingSalesman.dynamicProgramming(distanceMatrix); + assertEquals(expectedMinDistance, result); + } + + // Test Case 6: A distance matrix with only one city + @Test + public void testBruteForceOneCity() { + int[][] distanceMatrix = {{0}}; + int expectedMinDistance = 0; + int result = TravelingSalesman.bruteForce(distanceMatrix); + assertEquals(expectedMinDistance, result); + } + + @Test + public void testDynamicProgrammingOneCity() { + int[][] distanceMatrix = {{0}}; + int expectedMinDistance = 0; + int result = TravelingSalesman.dynamicProgramming(distanceMatrix); + assertEquals(expectedMinDistance, result); + } + + // Test Case 7: Distance matrix with large numbers + @Test + public void testBruteForceLargeNumbers() { + int[][] distanceMatrix = {{0, 1000000, 2000000}, {1000000, 0, 1500000}, {2000000, 1500000, 0}}; + int expectedMinDistance = 4500000; + int result = TravelingSalesman.bruteForce(distanceMatrix); + assertEquals(expectedMinDistance, result); + } + + @Test + public void testDynamicProgrammingLargeNumbers() { + int[][] distanceMatrix = {{0, 1000000, 2000000}, {1000000, 0, 1500000}, {2000000, 1500000, 0}}; + int expectedMinDistance = 4500000; + int result = TravelingSalesman.dynamicProgramming(distanceMatrix); + assertEquals(expectedMinDistance, result); + } +} From 7a5ce9c477e6837c787ea871235e7bf03b9e9e8a Mon Sep 17 00:00:00 2001 From: Deniz Altunkapan Date: Mon, 31 Mar 2025 16:46:52 +0200 Subject: [PATCH 7/9] resolve Checkstyle errors (constructor + imports) --- src/main/java/com/thealgorithms/graph/TravelingSalesman.java | 4 ++++ .../java/com/thealgorithms/graph/TravelingSalesmanTest.java | 3 +-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/graph/TravelingSalesman.java b/src/main/java/com/thealgorithms/graph/TravelingSalesman.java index aa3adb0b3915..be2a23e4d640 100644 --- a/src/main/java/com/thealgorithms/graph/TravelingSalesman.java +++ b/src/main/java/com/thealgorithms/graph/TravelingSalesman.java @@ -10,8 +10,12 @@ * For more information, see Wikipedia. * @author Deniz Altunkapan */ + public class TravelingSalesman { + // Private constructor to prevent instantiation + private TravelingSalesman() {} + /** * Solves the Traveling Salesman Problem (TSP) using brute-force approach. * This method generates all possible permutations of cities, calculates the total distance for each route, and returns the shortest distance found. diff --git a/src/test/java/com/thealgorithms/graph/TravelingSalesmanTest.java b/src/test/java/com/thealgorithms/graph/TravelingSalesmanTest.java index 0c595625d4ac..1f4d14c93fa0 100644 --- a/src/test/java/com/thealgorithms/graph/TravelingSalesmanTest.java +++ b/src/test/java/com/thealgorithms/graph/TravelingSalesmanTest.java @@ -1,7 +1,6 @@ package com.thealgorithms.graph; -import static org.junit.jupiter.api.Assertions.*; - +import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class TravelingSalesmanTest { From fad634f9ba2d0904e156a7075c6e98d2243898de Mon Sep 17 00:00:00 2001 From: Deniz Altunkapan Date: Mon, 31 Mar 2025 16:49:01 +0200 Subject: [PATCH 8/9] clang-Format #6205 --- src/main/java/com/thealgorithms/graph/TravelingSalesman.java | 3 ++- .../java/com/thealgorithms/graph/TravelingSalesmanTest.java | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/graph/TravelingSalesman.java b/src/main/java/com/thealgorithms/graph/TravelingSalesman.java index be2a23e4d640..9f9c4fe249a3 100644 --- a/src/main/java/com/thealgorithms/graph/TravelingSalesman.java +++ b/src/main/java/com/thealgorithms/graph/TravelingSalesman.java @@ -14,7 +14,8 @@ public class TravelingSalesman { // Private constructor to prevent instantiation - private TravelingSalesman() {} + private TravelingSalesman() { + } /** * Solves the Traveling Salesman Problem (TSP) using brute-force approach. diff --git a/src/test/java/com/thealgorithms/graph/TravelingSalesmanTest.java b/src/test/java/com/thealgorithms/graph/TravelingSalesmanTest.java index 1f4d14c93fa0..b93c9f89c944 100644 --- a/src/test/java/com/thealgorithms/graph/TravelingSalesmanTest.java +++ b/src/test/java/com/thealgorithms/graph/TravelingSalesmanTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.graph; import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; public class TravelingSalesmanTest { From 22071c6b38d0f419b29e74eeb10f3108f2154820 Mon Sep 17 00:00:00 2001 From: Deniz Altunkapan Date: Mon, 31 Mar 2025 16:59:13 +0200 Subject: [PATCH 9/9] declare TravelingSalesman as final --- src/main/java/com/thealgorithms/graph/TravelingSalesman.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/graph/TravelingSalesman.java b/src/main/java/com/thealgorithms/graph/TravelingSalesman.java index 9f9c4fe249a3..14bf89f57cf3 100644 --- a/src/main/java/com/thealgorithms/graph/TravelingSalesman.java +++ b/src/main/java/com/thealgorithms/graph/TravelingSalesman.java @@ -11,7 +11,7 @@ * @author Deniz Altunkapan */ -public class TravelingSalesman { +public final class TravelingSalesman { // Private constructor to prevent instantiation private TravelingSalesman() {