Skip to content

Commit 434848f

Browse files
committed
style: fix Checkstyle in Yens algorithm and tests
1 parent 6ada459 commit 434848f

File tree

2 files changed

+48
-28
lines changed

2 files changed

+48
-28
lines changed

src/main/java/com/thealgorithms/graph/YensKShortestPaths.java

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ public static List<List<Integer>> kShortestPaths(int[][] weights, int src, int d
4242
w[i] = Arrays.copyOf(weights[i], n);
4343
}
4444

45-
List<Path> A = new ArrayList<>();
46-
PriorityQueue<Path> B = new PriorityQueue<>(); // min-heap by cost then lexicographic nodes
45+
List<Path> a = new ArrayList<>();
46+
PriorityQueue<Path> b = new PriorityQueue<>(); // min-heap by cost then lexicographic nodes
4747
Set<String> seen = new HashSet<>(); // deduplicate candidate paths by node sequence key
4848

4949
Path first = dijkstra(w, src, dst, new boolean[n]);
5050
if (first == null) {
5151
return List.of();
5252
}
53-
A.add(first);
53+
a.add(first);
5454

5555
for (int kIdx = 1; kIdx < k; kIdx++) {
56-
Path lastPath = A.get(kIdx - 1);
56+
Path lastPath = a.get(kIdx - 1);
5757
List<Integer> lastNodes = lastPath.nodes;
5858
for (int i = 0; i < lastNodes.size() - 1; i++) {
5959
int spurNode = lastNodes.get(i);
@@ -62,7 +62,7 @@ public static List<List<Integer>> kShortestPaths(int[][] weights, int src, int d
6262
// Build modified graph: remove edges that would recreate same root + next edge as any A path
6363
int[][] wMod = cloneMatrix(w);
6464

65-
for (Path p : A) {
65+
for (Path p : a) {
6666
if (startsWith(p.nodes, rootPath) && p.nodes.size() > i + 1) {
6767
int u = p.nodes.get(i);
6868
int v = p.nodes.get(i + 1);
@@ -88,20 +88,20 @@ public static List<List<Integer>> kShortestPaths(int[][] weights, int src, int d
8888
Path candidate = new Path(totalNodes, totalCost);
8989
String key = candidate.key();
9090
if (!seen.contains(key)) {
91-
B.add(candidate);
91+
b.add(candidate);
9292
seen.add(key);
9393
}
9494
}
9595
}
96-
if (B.isEmpty()) {
96+
if (b.isEmpty()) {
9797
break;
9898
}
99-
A.add(B.poll());
99+
a.add(b.poll());
100100
}
101101

102102
// Map to list of node indices for output
103-
List<List<Integer>> result = new ArrayList<>(A.size());
104-
for (Path p : A) {
103+
List<List<Integer>> result = new ArrayList<>(a.size());
104+
for (Path p : a) {
105105
result.add(new ArrayList<>(p.nodes));
106106
}
107107
return result;
@@ -135,46 +135,61 @@ private static void validate(int[][] weights, int src, int dst, int k) {
135135
}
136136

137137
private static boolean startsWith(List<Integer> list, List<Integer> prefix) {
138-
if (prefix.size() > list.size()) return false;
138+
if (prefix.size() > list.size()) {
139+
return false;
140+
}
139141
for (int i = 0; i < prefix.size(); i++) {
140-
if (!Objects.equals(list.get(i), prefix.get(i))) return false;
142+
if (!Objects.equals(list.get(i), prefix.get(i))) {
143+
return false;
144+
}
141145
}
142146
return true;
143147
}
144148

145149
private static int[][] cloneMatrix(int[][] a) {
146150
int n = a.length;
147151
int[][] b = new int[n][n];
148-
for (int i = 0; i < n; i++) b[i] = Arrays.copyOf(a[i], n);
152+
for (int i = 0; i < n; i++) {
153+
b[i] = Arrays.copyOf(a[i], n);
154+
}
149155
return b;
150156
}
151157

152158
private static long pathCost(int[][] w, List<Integer> nodes) {
153159
long cost = 0;
154160
for (int i = 0; i + 1 < nodes.size(); i++) {
155-
int u = nodes.get(i), v = nodes.get(i + 1);
161+
int u = nodes.get(i);
162+
int v = nodes.get(i + 1);
156163
int c = w[u][v];
157-
if (c < 0) return Long.MAX_VALUE / 4; // invalid
164+
if (c < 0) {
165+
return Long.MAX_VALUE / 4; // invalid
166+
}
158167
cost += c;
159168
}
160169
return cost;
161170
}
162171

163172
private static Path dijkstra(int[][] w, int src, int dst, boolean[] blocked) {
164173
int n = w.length;
165-
final long INF = Long.MAX_VALUE / 4;
174+
final long inf = Long.MAX_VALUE / 4;
166175
long[] dist = new long[n];
167176
int[] parent = new int[n];
168-
Arrays.fill(dist, INF);
177+
Arrays.fill(dist, inf);
169178
Arrays.fill(parent, -1);
170179
PriorityQueue<Node> pq = new PriorityQueue<>();
171-
if (blocked[src]) return null;
180+
if (blocked[src]) {
181+
return null;
182+
}
172183
dist[src] = 0;
173184
pq.add(new Node(src, 0));
174185
while (!pq.isEmpty()) {
175186
Node cur = pq.poll();
176-
if (cur.dist != dist[cur.u]) continue;
177-
if (cur.u == dst) break;
187+
if (cur.dist != dist[cur.u]) {
188+
continue;
189+
}
190+
if (cur.u == dst) {
191+
break;
192+
}
178193
for (int v = 0; v < n; v++) {
179194
int wuv = w[cur.u][v];
180195
if (wuv >= 0 && !blocked[v]) {
@@ -187,7 +202,7 @@ private static Path dijkstra(int[][] w, int src, int dst, boolean[] blocked) {
187202
}
188203
}
189204
}
190-
if (dist[dst] >= INF) {
205+
if (dist[dst] >= inf) {
191206
// If src==dst and not blocked, the path is trivial with cost 0
192207
if (src == dst) {
193208
List<Integer> nodes = new ArrayList<>();
@@ -231,12 +246,17 @@ String key() {
231246
@Override
232247
public int compareTo(Path o) {
233248
int c = Long.compare(this.cost, o.cost);
234-
if (c != 0) return c;
249+
if (c != 0) {
250+
return c;
251+
}
235252
// tie-break lexicographically on nodes
236253
int m = Math.min(this.nodes.size(), o.nodes.size());
237254
for (int i = 0; i < m; i++) {
238-
int a = this.nodes.get(i), b = o.nodes.get(i);
239-
if (a != b) return Integer.compare(a, b);
255+
int aNode = this.nodes.get(i);
256+
int bNode = o.nodes.get(i);
257+
if (aNode != bNode) {
258+
return Integer.compare(aNode, bNode);
259+
}
240260
}
241261
return Integer.compare(this.nodes.size(), o.nodes.size());
242262
}

src/test/java/com/thealgorithms/graph/YensKShortestPathsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ class YensKShortestPathsTest {
1414
void basicKPaths() {
1515
// Graph (directed) with non-negative weights, -1 = no edge
1616
// 0 -> 1 (1), 0 -> 2 (2), 1 -> 3 (1), 2 -> 3 (1), 0 -> 3 (5), 1 -> 2 (1)
17-
int N = 4;
18-
int[][] w = new int[N][N];
19-
for (int i = 0; i < N; i++) {
20-
for (int j = 0; j < N; j++) {
17+
int n = 4;
18+
int[][] w = new int[n][n];
19+
for (int i = 0; i < n; i++) {
20+
for (int j = 0; j < n; j++) {
2121
w[i][j] = -1;
2222
}
2323
}

0 commit comments

Comments
 (0)