forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstrainedShortestPathTest.java
218 lines (179 loc) · 6.9 KB
/
ConstrainedShortestPathTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.thealgorithms.graph;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.thealgorithms.graph.ConstrainedShortestPath.Graph;
import org.junit.jupiter.api.Test;
public class ConstrainedShortestPathTest {
/**
* 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;
ConstrainedShortestPath solver = new ConstrainedShortestPath(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;
ConstrainedShortestPath solver = new ConstrainedShortestPath(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;
ConstrainedShortestPath solver = new ConstrainedShortestPath(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;
ConstrainedShortestPath solver = new ConstrainedShortestPath(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;
ConstrainedShortestPath solver = new ConstrainedShortestPath(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;
ConstrainedShortestPath solver = new ConstrainedShortestPath(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;
ConstrainedShortestPath solver = new ConstrainedShortestPath(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;
ConstrainedShortestPath solver = new ConstrainedShortestPath(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;
ConstrainedShortestPath solver = new ConstrainedShortestPath(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;
ConstrainedShortestPath solver = new ConstrainedShortestPath(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;
ConstrainedShortestPath solver = new ConstrainedShortestPath(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;
ConstrainedShortestPath solver = new ConstrainedShortestPath(graph, maxResource);
assertEquals(2, solver.solve(0, 2));
}
}