Skip to content
Snippets Groups Projects
Commit 81fa6cd9 authored by sfarahmand2's avatar sfarahmand2
Browse files

Update solvers.js

parent 805b8bce
Branches
No related tags found
No related merge requests found
...@@ -11,11 +11,64 @@ class Edge { ...@@ -11,11 +11,64 @@ class Edge {
} }
export function solveByBFS(maze) { export function solveByBFS(maze) {
// TODO: stub const backpointers = new Map();
const worklist = new Queue();
worklist.insert(new Edge(undefined, maze.entrance));
while (worklist.size > 0) {
const workitem = worklist.remove();
if (backpointers.has(workitem.to)) {
continue;
}
workitem.to.type = CellType.CONSIDERED;
backpointers.set(workitem.to, workitem);
if (workitem.to === maze.exit) {
const reversedPath = [];
for (let current = workitem.to;
// cells to CellType.SOLUTION
// current.type = CellType.SOLUTION;
current !== undefined;
current = backpointers.get(current).from) {
reversedPath.push(current);
current.type = CellType.SOLUTION;
}
return reversedPath.reverse();
}
for (const incidence of workitem.to.neighbors) {
worklist.insert(new Edge(workitem.to, incidence));
}
}
return undefined;
} }
export function solveByDijkstras(maze) { export function solveByDijkstras(maze) {
// TODO: stub const backpointers = new Map();
const worklist = new PriorityQueue();
worklist.insert(new Edge(undefined, maze.entrance, 0), 0);
while (worklist.size > 0) {
const workitem = worklist.remove();
if (backpointers.has(workitem.to)) {
continue;
}
workitem.to.type = CellType.CONSIDERED;
backpointers.set(workitem.to, workitem);
if (workitem.to === maze.exit) {
const reversedPath = [];
for (let current = maze.exit;
current !== undefined;
current = backpointers.get(current).from) {
reversedPath.push(current);
current.type = CellType.SOLUTION;
}
return reversedPath.reverse();
}
for (const incidence of workitem.to.neighbors) {
worklist.insert(
new Edge(workitem.to, incidence, workitem.distance + 1),
workitem.distance,
);
}
}
return undefined;
} }
function heuristic(maze, cell) { function heuristic(maze, cell) {
...@@ -23,5 +76,37 @@ function heuristic(maze, cell) { ...@@ -23,5 +76,37 @@ function heuristic(maze, cell) {
} }
export function solveByAStar(maze) { export function solveByAStar(maze) {
// TODO: stub const backpointers = new Map();
const worklist = new PriorityQueue();
worklist.insert(
new Edge(undefined, maze.entrance, 0),
0 + heuristic(maze.entrance, maze.exit),
);
while (worklist.size > 0) {
const workitem = worklist.remove();
if (backpointers.has(workitem.to) &&
backpointers.get(workitem.to).distance <= workitem.distance) {
continue;
}
workitem.to.type = CellType.CONSIDERED;
backpointers.set(workitem.to, workitem);
if (workitem.to === maze.exit) {
const reversedPath = [];
for (let current = maze.exit;
current !== undefined;
current = backpointers.get(current).from) {
reversedPath.push(current);
current.type = CellType.SOLUTION;
}
return reversedPath.reverse();
}
for (const incidence of workitem.to.neighbors) {
worklist.insert(
new Edge(workitem.to, incidence.destination, workitem.distance),
workitem.distance +
heuristic(incidence.destination, maze.exit),
);
}
}
return undefined;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment