|
| 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