CCT: Minor simplification of 'Shortest Path' solver (#1288)

BFS shouldn't need some checks.
Also allows deletion of a helper file used by this function only.
This commit is contained in:
gmcew
2024-05-23 08:44:41 +01:00
committed by GitHub
parent 08eb60d21b
commit fe14d4fef3
2 changed files with 7 additions and 147 deletions
+7 -14
View File
@@ -1,5 +1,4 @@
import { getRandomIntInclusive } from "../utils/helpers/getRandomIntInclusive";
import { MinHeap } from "../utils/Heap";
import { comprGenChar, comprLZGenerate, comprLZEncode, comprLZDecode } from "../utils/CompressionContracts";
import { HammingEncode, HammingDecode, HammingEncodeProperly } from "../utils/HammingCodeTools";
@@ -994,7 +993,7 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
const distance: [number][] = new Array(height);
//const prev: [[number, number] | undefined][] = new Array(height);
const queue = new MinHeap<[number, number]>();
const queue: [number, number][] = [];
for (let y = 0; y < height; y++) {
distance[y] = new Array(width).fill(Infinity) as [number];
@@ -1015,21 +1014,15 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
// Prepare starting point
distance[0][0] = 0;
queue.push([0, 0], 0);
queue.push([0, 0]);
// Take next-nearest position and expand potential paths from there
while (queue.size > 0) {
const [y, x] = queue.pop() as [number, number];
while (queue.length > 0) {
const [y, x] = queue.shift() as [number, number];
for (const [yN, xN] of neighbors(y, x)) {
const d = distance[y][x] + 1;
if (d < distance[yN][xN]) {
if (distance[yN][xN] == Infinity)
// Not reached previously
queue.push([yN, xN], d);
// Found a shorter path
else queue.changeWeight(([yQ, xQ]) => yQ == yN && xQ == xN, d);
//prev[yN][xN] = [y, x];
distance[yN][xN] = d;
if (distance[yN][xN] == Infinity) {
queue.push([yN, xN]);
distance[yN][xN] = distance[y][x] + 1;
}
}
}