1. Open set
open contains discovered nodes still waiting to be explored.
const open = new Set([start])Typorithm · hard
Step 1 of 8 · intro card
Search Algorithms
A* Search explores weighted graphs by combining known path cost with a heuristic estimate toward the goal.
A* keeps an open set of discovered nodes.
gScore tracks the cost already paid from start to each node.
fScore adds heuristic(node, goal), an estimate of remaining cost.
The algorithm expands the open node with the smallest fScore.
This lesson returns reachability; full path reconstruction can use cameFrom.
open stores candidates.
gScore is known cost so far.
fScore is gScore plus heuristic.
Update neighbors only on better paths.
Heuristic quality controls efficiency.
function aStarSearch(graph, start, goal, heuristic) {
const open = new Set([start]);
const cameFrom = new Map();
const gScore = new Map([[start, 0]]);
const fScore = new Map([[start, heuristic(start, goal)]]);
while (open.size > 0) {
const current = [...open].reduce((best, node) =>
(fScore.get(node) ?? Infinity) < (fScore.get(best) ?? Infinity) ? node : best
);
if (current === goal) return true;
open.delete(current);
for (const edge of graph[current] ?? []) {
const tentative = (gScore.get(current) ?? Infinity) + edge.cost;
if (tentative < (gScore.get(edge.to) ?? Infinity)) {
cameFrom.set(edge.to, current);
gScore.set(edge.to, tentative);
fScore.set(edge.to, tentative + heuristic(edge.to, goal));
open.add(edge.to);
}
}
}
return false;
}Connect each part of the code to the concept before you type.
open contains discovered nodes still waiting to be explored.
const open = new Set([start])gScore is the known cost from start to a node.
const gScore = new Map([[start, 0]])fScore combines known cost and estimated remaining cost.
const fScore = new Map([[start, heuristic(start, goal)]])Choose the open node with the smallest fScore.
const current = [...open].reduce((best, node) =>Stop when the best candidate is the goal.
if (current === goal) return trueCompute the cost of reaching a neighbor through current.
const tentative = (gScore.get(current) ?? Infinity) + edge.costOnly update a neighbor when this path improves its known cost.
if (tentative < (gScore.get(edge.to) ?? Infinity))Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
Input5 × 4 grid · S at 0 · goal at 19 · Manhattan heuristic
Follow temporary values, local state and supporting structures at this step.
Open set1 entries
Closed set0 entries
Saved g distances · cells 0–1920 entries
Parent map · child ← parent0 entries
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state