Skip to content

Commit d4b28b3

Browse files
Add Constrained Shortest Path Problem (CSPP) / Shortest Path Problem with Resource Constraints (SPPRC) (#6155)
1 parent 4d667e1 commit d4b28b3

File tree

2 files changed

+341
-0
lines changed

2 files changed

+341
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.thealgorithms.graph;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* This class implements a solution for the Constrained Shortest Path Problem (CSPP).
9+
* also known as Shortest Path Problem with Resource Constraints (SPPRC).
10+
* The goal is to find the shortest path between two nodes while ensuring that
11+
* the resource constraint is not exceeded.
12+
*
13+
* @author <a href="https://github.com/DenizAltunkapan">Deniz Altunkapan</a>
14+
*/
15+
public class ConstrainedShortestPath {
16+
17+
/**
18+
* Represents a graph using an adjacency list.
19+
* This graph is designed for the Constrained Shortest Path Problem (CSPP).
20+
*/
21+
public static class Graph {
22+
23+
private List<List<Edge>> adjacencyList;
24+
25+
public Graph(int numNodes) {
26+
adjacencyList = new ArrayList<>();
27+
for (int i = 0; i < numNodes; i++) {
28+
adjacencyList.add(new ArrayList<>());
29+
}
30+
}
31+
32+
/**
33+
* Adds an edge to the graph.
34+
* @param from the starting node
35+
* @param to the ending node
36+
* @param cost the cost of the edge
37+
* @param resource the resource required to traverse the edge
38+
*/
39+
public void addEdge(int from, int to, int cost, int resource) {
40+
adjacencyList.get(from).add(new Edge(from, to, cost, resource));
41+
}
42+
43+
/**
44+
* Gets the edges that are adjacent to a given node.
45+
* @param node the node to get the edges for
46+
* @return the list of edges adjacent to the node
47+
*/
48+
public List<Edge> getEdges(int node) {
49+
return adjacencyList.get(node);
50+
}
51+
52+
/**
53+
* Gets the number of nodes in the graph.
54+
* @return the number of nodes
55+
*/
56+
public int getNumNodes() {
57+
return adjacencyList.size();
58+
}
59+
60+
public record Edge(int from, int to, int cost, int resource) {
61+
}
62+
}
63+
64+
private Graph graph;
65+
private int maxResource;
66+
67+
/**
68+
* Constructs a CSPSolver with the given graph and maximum resource constraint.
69+
*
70+
* @param graph the graph representing the problem
71+
* @param maxResource the maximum allowable resource
72+
*/
73+
public ConstrainedShortestPath(Graph graph, int maxResource) {
74+
this.graph = graph;
75+
this.maxResource = maxResource;
76+
}
77+
78+
/**
79+
* Solves the CSP to find the shortest path from the start node to the target node
80+
* without exceeding the resource constraint.
81+
*
82+
* @param start the starting node
83+
* @param target the target node
84+
* @return the minimum cost to reach the target node within the resource constraint,
85+
* or -1 if no valid path exists
86+
*/
87+
public int solve(int start, int target) {
88+
int numNodes = graph.getNumNodes();
89+
int[][] dp = new int[maxResource + 1][numNodes];
90+
91+
// Initialize dp table with maximum values
92+
for (int i = 0; i <= maxResource; i++) {
93+
Arrays.fill(dp[i], Integer.MAX_VALUE);
94+
}
95+
dp[0][start] = 0;
96+
97+
// Dynamic Programming: Iterate over resources and nodes
98+
for (int r = 0; r <= maxResource; r++) {
99+
for (int u = 0; u < numNodes; u++) {
100+
if (dp[r][u] == Integer.MAX_VALUE) {
101+
continue;
102+
}
103+
for (Graph.Edge edge : graph.getEdges(u)) {
104+
int v = edge.to();
105+
int cost = edge.cost();
106+
int resource = edge.resource();
107+
108+
if (r + resource <= maxResource) {
109+
dp[r + resource][v] = Math.min(dp[r + resource][v], dp[r][u] + cost);
110+
}
111+
}
112+
}
113+
}
114+
115+
// Find the minimum cost to reach the target node
116+
int minCost = Integer.MAX_VALUE;
117+
for (int r = 0; r <= maxResource; r++) {
118+
minCost = Math.min(minCost, dp[r][target]);
119+
}
120+
121+
return minCost == Integer.MAX_VALUE ? -1 : minCost;
122+
}
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
package com.thealgorithms.graph;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import com.thealgorithms.graph.ConstrainedShortestPath.Graph;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class ConstrainedShortestPathTest {
9+
10+
/**
11+
* Tests a simple linear graph to verify if the solver calculates the shortest path correctly.
12+
* Expected: The minimal path cost from node 0 to node 2 should be 5 while not exceeding the resource limit.
13+
*/
14+
@Test
15+
public void testSimpleGraph() {
16+
Graph graph = new Graph(3);
17+
graph.addEdge(0, 1, 2, 3);
18+
graph.addEdge(1, 2, 3, 2);
19+
20+
int maxResource = 5;
21+
ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource);
22+
23+
assertEquals(5, solver.solve(0, 2));
24+
}
25+
26+
/**
27+
* Tests a graph where no valid path exists due to resource constraints.
28+
* Expected: The solver should return -1, indicating no path is feasible.
29+
*/
30+
@Test
31+
public void testNoPath() {
32+
Graph graph = new Graph(3);
33+
graph.addEdge(0, 1, 2, 6);
34+
graph.addEdge(1, 2, 3, 6);
35+
36+
int maxResource = 5;
37+
ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource);
38+
39+
assertEquals(-1, solver.solve(0, 2));
40+
}
41+
42+
/**
43+
* Tests a graph with multiple paths between source and destination.
44+
* Expected: The solver should choose the path with the minimal cost of 5, considering the resource limit.
45+
*/
46+
@Test
47+
public void testMultiplePaths() {
48+
Graph graph = new Graph(4);
49+
graph.addEdge(0, 1, 1, 1);
50+
graph.addEdge(1, 3, 5, 2);
51+
graph.addEdge(0, 2, 2, 1);
52+
graph.addEdge(2, 3, 3, 2);
53+
54+
int maxResource = 3;
55+
ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource);
56+
57+
assertEquals(5, solver.solve(0, 3));
58+
}
59+
60+
/**
61+
* Verifies that the solver allows a path exactly matching the resource limit.
62+
* Expected: The path is valid with a total cost of 5.
63+
*/
64+
@Test
65+
public void testExactResourceLimit() {
66+
Graph graph = new Graph(3);
67+
graph.addEdge(0, 1, 2, 3);
68+
graph.addEdge(1, 2, 3, 2);
69+
70+
int maxResource = 5;
71+
ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource);
72+
73+
assertEquals(5, solver.solve(0, 2));
74+
}
75+
76+
/**
77+
* Tests a disconnected graph where the destination node cannot be reached.
78+
* Expected: The solver should return -1, as the destination is unreachable.
79+
*/
80+
@Test
81+
public void testDisconnectedGraph() {
82+
Graph graph = new Graph(4);
83+
graph.addEdge(0, 1, 2, 2);
84+
graph.addEdge(2, 3, 3, 2);
85+
86+
int maxResource = 5;
87+
ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource);
88+
89+
assertEquals(-1, solver.solve(0, 3));
90+
}
91+
92+
/**
93+
* Tests a graph with cycles to ensure the solver does not fall into infinite loops and correctly calculates costs.
94+
* Expected: The solver should compute the minimal path cost of 6.
95+
*/
96+
@Test
97+
public void testGraphWithCycles() {
98+
Graph graph = new Graph(4);
99+
graph.addEdge(0, 1, 2, 1);
100+
graph.addEdge(1, 2, 3, 1);
101+
graph.addEdge(2, 0, 1, 1);
102+
graph.addEdge(1, 3, 4, 2);
103+
104+
int maxResource = 3;
105+
ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource);
106+
107+
assertEquals(6, solver.solve(0, 3));
108+
}
109+
110+
/**
111+
* Tests the solver's performance and correctness on a large linear graph with 1000 nodes.
112+
* Expected: The solver should efficiently calculate the shortest path with a cost of 999.
113+
*/
114+
@Test
115+
public void testLargeGraphPerformance() {
116+
int nodeCount = 1000;
117+
Graph graph = new Graph(nodeCount);
118+
for (int i = 0; i < nodeCount - 1; i++) {
119+
graph.addEdge(i, i + 1, 1, 1);
120+
}
121+
122+
int maxResource = 1000;
123+
ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource);
124+
125+
assertEquals(999, solver.solve(0, nodeCount - 1));
126+
}
127+
128+
/**
129+
* Tests a graph with isolated nodes to ensure the solver recognizes unreachable destinations.
130+
* Expected: The solver should return -1 for unreachable nodes.
131+
*/
132+
@Test
133+
public void testIsolatedNodes() {
134+
Graph graph = new Graph(5);
135+
graph.addEdge(0, 1, 2, 1);
136+
graph.addEdge(1, 2, 3, 1);
137+
138+
int maxResource = 5;
139+
ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource);
140+
141+
assertEquals(-1, solver.solve(0, 3));
142+
}
143+
144+
/**
145+
* Tests a cyclic large graph with multiple overlapping paths.
146+
* Expected: The solver should calculate the shortest path cost of 5.
147+
*/
148+
@Test
149+
public void testCyclicLargeGraph() {
150+
Graph graph = new Graph(10);
151+
for (int i = 0; i < 9; i++) {
152+
graph.addEdge(i, (i + 1) % 10, 1, 1);
153+
}
154+
graph.addEdge(0, 5, 5, 3);
155+
156+
int maxResource = 10;
157+
ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource);
158+
159+
assertEquals(5, solver.solve(0, 5));
160+
}
161+
162+
/**
163+
* Tests a large complex graph with multiple paths and varying resource constraints.
164+
* Expected: The solver should identify the optimal path with a cost of 19 within the resource limit.
165+
*/
166+
@Test
167+
public void testLargeComplexGraph() {
168+
Graph graph = new Graph(10);
169+
graph.addEdge(0, 1, 4, 2);
170+
graph.addEdge(0, 2, 3, 3);
171+
graph.addEdge(1, 3, 2, 1);
172+
graph.addEdge(2, 3, 5, 2);
173+
graph.addEdge(2, 4, 8, 4);
174+
graph.addEdge(3, 5, 7, 3);
175+
graph.addEdge(3, 6, 6, 2);
176+
graph.addEdge(4, 6, 3, 2);
177+
graph.addEdge(5, 7, 1, 1);
178+
graph.addEdge(6, 7, 2, 2);
179+
graph.addEdge(7, 8, 3, 1);
180+
graph.addEdge(8, 9, 2, 1);
181+
182+
int maxResource = 10;
183+
ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource);
184+
185+
assertEquals(19, solver.solve(0, 9));
186+
}
187+
188+
/**
189+
* Edge case test where the graph has only one node and no edges.
190+
* Expected: The minimal path cost is 0, as the start and destination are the same.
191+
*/
192+
@Test
193+
public void testSingleNodeGraph() {
194+
Graph graph = new Graph(1);
195+
196+
int maxResource = 0;
197+
ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource);
198+
199+
assertEquals(0, solver.solve(0, 0));
200+
}
201+
202+
/**
203+
* Tests a graph with multiple paths but a tight resource constraint.
204+
* Expected: The solver should return -1 if no path can be found within the resource limit.
205+
*/
206+
@Test
207+
public void testTightResourceConstraint() {
208+
Graph graph = new Graph(4);
209+
graph.addEdge(0, 1, 3, 4);
210+
graph.addEdge(1, 2, 1, 2);
211+
graph.addEdge(0, 2, 2, 2);
212+
213+
int maxResource = 3;
214+
ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource);
215+
216+
assertEquals(2, solver.solve(0, 2));
217+
}
218+
}

0 commit comments

Comments
 (0)