Skip to content

Commit 4822dd9

Browse files
committed
fix (PMD): rename short variables in the code
1 parent 6298350 commit 4822dd9

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

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

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,36 @@ public static List<List<Integer>> kShortestPaths(int[][] weights, int src, int d
3737
validate(weights, src, dst, k);
3838
final int n = weights.length;
3939
// Make a defensive copy to avoid mutating caller's matrix
40-
int[][] w = new int[n][n];
40+
int[][] weightsCopy = new int[n][n];
4141
for (int i = 0; i < n; i++) {
42-
w[i] = Arrays.copyOf(weights[i], n);
42+
weightsCopy[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> shortestPaths = new ArrayList<>();
46+
PriorityQueue<Path> candidates = new PriorityQueue<>(); // min-heap by cost then lexicographic nodes
4747
Set<String> seen = new HashSet<>(); // deduplicate candidate paths by node sequence key
4848

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

5555
for (int kIdx = 1; kIdx < k; kIdx++) {
56-
Path lastPath = a.get(kIdx - 1);
56+
Path lastPath = shortestPaths.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);
6060
List<Integer> rootPath = lastNodes.subList(0, i + 1);
6161

6262
// Build modified graph: remove edges that would recreate same root + next edge as any A path
63-
int[][] wMod = cloneMatrix(w);
63+
int[][] modifiedWeights = cloneMatrix(weightsCopy);
6464

65-
for (Path p : a) {
65+
for (Path p : shortestPaths) {
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);
69-
wMod[u][v] = -1; // remove edge
69+
modifiedWeights[u][v] = -1; // remove edge
7070
}
7171
}
7272
// Prevent revisiting nodes in rootPath (loopless constraint), except spurNode itself
@@ -75,32 +75,32 @@ public static List<List<Integer>> kShortestPaths(int[][] weights, int src, int d
7575
blocked[rootPath.get(j)] = true;
7676
}
7777

78-
Path spurPath = dijkstra(wMod, spurNode, dst, blocked);
78+
Path spurPath = dijkstra(modifiedWeights, spurNode, dst, blocked);
7979
if (spurPath != null) {
8080
// concatenate rootPath (excluding spurNode at end) + spurPath
8181
List<Integer> totalNodes = new ArrayList<>(rootPath);
8282
// spurPath.nodes starts with spurNode; avoid duplication
8383
for (int idx = 1; idx < spurPath.nodes.size(); idx++) {
8484
totalNodes.add(spurPath.nodes.get(idx));
8585
}
86-
long rootCost = pathCost(w, rootPath);
86+
long rootCost = pathCost(weightsCopy, rootPath);
8787
long totalCost = rootCost + spurPath.cost; // spurPath.cost covers from spurNode to dst
8888
Path candidate = new Path(totalNodes, totalCost);
8989
String key = candidate.key();
9090
if (seen.add(key)) {
91-
b.add(candidate);
91+
candidates.add(candidate);
9292
}
9393
}
9494
}
95-
if (b.isEmpty()) {
95+
if (candidates.isEmpty()) {
9696
break;
9797
}
98-
a.add(b.poll());
98+
shortestPaths.add(candidates.poll());
9999
}
100100

101101
// Map to list of node indices for output
102-
List<List<Integer>> result = new ArrayList<>(a.size());
103-
for (Path p : a) {
102+
List<List<Integer>> result = new ArrayList<>(shortestPaths.size());
103+
for (Path p : shortestPaths) {
104104
result.add(new ArrayList<>(p.nodes));
105105
}
106106
return result;
@@ -154,12 +154,12 @@ private static int[][] cloneMatrix(int[][] a) {
154154
return b;
155155
}
156156

157-
private static long pathCost(int[][] w, List<Integer> nodes) {
157+
private static long pathCost(int[][] weights, List<Integer> nodes) {
158158
long cost = 0;
159159
for (int i = 0; i + 1 < nodes.size(); i++) {
160160
int u = nodes.get(i);
161161
int v = nodes.get(i + 1);
162-
int c = w[u][v];
162+
int c = weights[u][v];
163163
if (c < 0) {
164164
return Long.MAX_VALUE / 4; // invalid
165165
}
@@ -168,35 +168,35 @@ private static long pathCost(int[][] w, List<Integer> nodes) {
168168
return cost;
169169
}
170170

171-
private static Path dijkstra(int[][] w, int src, int dst, boolean[] blocked) {
172-
int n = w.length;
171+
private static Path dijkstra(int[][] weights, int src, int dst, boolean[] blocked) {
172+
int n = weights.length;
173173
final long inf = Long.MAX_VALUE / 4;
174174
long[] dist = new long[n];
175175
int[] parent = new int[n];
176176
Arrays.fill(dist, inf);
177177
Arrays.fill(parent, -1);
178-
PriorityQueue<Node> pq = new PriorityQueue<>();
178+
PriorityQueue<Node> queue = new PriorityQueue<>();
179179
if (blocked[src]) {
180180
return null;
181181
}
182182
dist[src] = 0;
183-
pq.add(new Node(src, 0));
184-
while (!pq.isEmpty()) {
185-
Node cur = pq.poll();
186-
if (cur.dist != dist[cur.u]) {
183+
queue.add(new Node(src, 0));
184+
while (!queue.isEmpty()) {
185+
Node current = queue.poll();
186+
if (current.dist != dist[current.u]) {
187187
continue;
188188
}
189-
if (cur.u == dst) {
189+
if (current.u == dst) {
190190
break;
191191
}
192192
for (int v = 0; v < n; v++) {
193-
int wuv = w[cur.u][v];
194-
if (wuv >= 0 && !blocked[v]) {
195-
long nd = cur.dist + wuv;
196-
if (nd < dist[v]) {
197-
dist[v] = nd;
198-
parent[v] = cur.u;
199-
pq.add(new Node(v, nd));
193+
int edgeWeight = weights[current.u][v];
194+
if (edgeWeight >= 0 && !blocked[v]) {
195+
long newDist = current.dist + edgeWeight;
196+
if (newDist < dist[v]) {
197+
dist[v] = newDist;
198+
parent[v] = current.u;
199+
queue.add(new Node(v, newDist));
200200
}
201201
}
202202
}

0 commit comments

Comments
 (0)