Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • main
1 result

Target

Select target project
  • sfarahmand2/graph-search-lab
  • jguzman12/graph-search-lab
  • oaddison2/graph-search-lab
  • soft-core/soft-260/graph-search-lab
4 results
Select Git revision
  • main
1 result
Show changes
Commits on Source (2)
...@@ -11,17 +11,97 @@ class Edge { ...@@ -11,17 +11,97 @@ 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) {
return 0; // TODO: stub return Math.abs(maze.exit.x - cell.x) + Math.abs(maze.exit.y - cell.y);
} }
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, maze.exit),
);
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) {
for (let current = maze.exit;
current !== undefined;
current = backpointers.get(current).from) {
current.type = CellType.SOLUTION;
}
return;
}
for (const neighbor of workitem.to.neighbors) {
worklist.insert(
new Edge(workitem.to, neighbor, workitem.distance + 1),
workitem.distance + 1 + heuristic(maze, neighbor),
);
}
}
} }