From 81fa6cd9d43461eb6fe214d562342cf57370f59e Mon Sep 17 00:00:00 2001 From: sfarahmand2 <sfarahmand2@huskers.unl.edu> Date: Fri, 27 Sep 2024 21:25:36 -0500 Subject: [PATCH] Update solvers.js --- graph-search/src/features/maze/solvers.js | 91 ++++++++++++++++++++++- 1 file changed, 88 insertions(+), 3 deletions(-) diff --git a/graph-search/src/features/maze/solvers.js b/graph-search/src/features/maze/solvers.js index f46634f..c6f67cf 100644 --- a/graph-search/src/features/maze/solvers.js +++ b/graph-search/src/features/maze/solvers.js @@ -11,11 +11,64 @@ class Edge { } 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) { - // 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) { @@ -23,5 +76,37 @@ function heuristic(maze, cell) { } 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; } -- GitLab