\begin{algorithm}
\begin{algorithmic}
\FUNCTION{A*}{\UPPERCASE{$G$}, $s, t$}
\STATE $closed\_list \gets $ List()
\STATE $open\_list \gets $ List($s$)
\FORALL { $n \in $ \call{V}{\UPPERCASE{$G$}} }
\STATE $n.dist \gets \infty$
\STATE $n.estimated\_dist \gets \infty$
\STATE $n.parent \gets null$
\ENDFOR
\STATE
\STATE $s.dist = 0$
\STATE $s.estimated\_dist = s.heuristic$
\STATE
\WHILE{\CALL{Length}{$open\_list$} $\neq$ 0}
\STATE $current \gets $ \Call{MinDist}{$open\_list$}\COMMENT{Minimal estimated distance.}
\STATE \CALL{Remove}{$open\_list, current$}
\STATE \CALL{Add}{$closed\_list, current$}
\STATE
\IF {$current = t$}
\RETURN{$t$}
\ENDIF
\STATE
\FORALL { $n \in $ \call{Adjacent}{\UPPERCASE{$G$}, $current$} }
\IF { $n \in closed\_set$ }
\STATE \textbf{continue}
\ENDIF
\IF { $n \notin open\_set$ }
\STATE \CALL{Add}{open\_list, n}
\ENDIF
\STATE
\STATE $new\_dist = current.dist $ + \CALL{W}{$current, n$}
\IF { $new\_dist < n.dist$ }
\STATE $n.dist = new\_dist$
\STATE $n.estimated\_dist = new\_dist + n.heuristic$
\STATE $n.parent = current$
\ENDIF
\ENDFOR
\ENDWHILE
\ENDFUNCTION
\end{algorithmic}
\end{algorithm}
function AStar(G, s, t) {
let closedList = [];
let openList = [s];
G.nodes.forEach(function(node) {
node.dist = Infinity;
node.estimatedDist = Infinity;
node.parent = null;
});
s.dist = 0;
s.estimatedDist = s.heuristic;
while(openList.length !== 0) {
openList.sort((a, b) => (a.estimatedDist - b.estimatedDist));
let current = openList.shift();
closedList.push(current);
if (current.equals(t)) {
return t;
}
let adjacent = G.getAdjacentNodes(current);
adjacent.forEach(function(node) {
if (closedList.find(node)) {
return;
}
if (!openList.find(node)) {
openList.push(node);
}
let newDist = current.dist + W(current, node);
if (newDist < node.dist) {
node.dist = newDist;
node.estimatedDist = newDist + node.heuristic;
node.parent = current;
}
});
}
}