Algorithms · Visualized

A* Search

Graph Algorithms

Traverse, order, and find shortest paths and spanning trees on graphs — the machinery behind maps, networks, schedulers and dependency resolvers.

A* is best-first search with a cost estimate. Time: O(E log V) with a binary heap average, O(b^d) — the heuristic degenerates to brute force worst; space: O(V) for the open list, closed set, g-values and parent pointers.

Big-O: Traversals O(V + E); Dijkstra O(E log V) with a heap; Bellman–Ford O(V·E); Floyd–Warshall O(V³); MST O(E log E).

A* Search

Graph Algorithms

A* is best-first search with a cost estimate. Every open node is ranked by f = g + h, where g is the real cost already paid to reach it and h is a heuristic guess of the remaining cost to the goal. The visualizer uses the fixed straight-line guesses h = [7, 6, 5, 4, 3, 0] for nodes A to F, where F is the goal and therefore 0. Because those guesses never exceed the true remaining distance they are admissible, which is what guarantees the path A* returns is optimal rather than merely cheap-looking. A heuristic that overestimates would let the search settle for a worse route faster.

How A* Search Works

  1. Set g[start] = 0 and open = [start]; every other node is unvisited and infinitely far.
  2. Pick the open node with the smallest f = g + h — first A with f = 0 + 7.
  3. Close it, then relax each neighbour: if g[u] + weight beats the neighbour’s current g, record the improvement and its parent.
  4. Re-insert improved neighbours into the open list and repeat until the goal is the node popped.
  5. Reconstruct the path by following parent pointers from the goal back to the start.

A* Search Worked Example

Input: {start: 0, goal: 5}

On the default weighted graph (A–B 4, A–C 2, B–C 1, B–D 5, C–D 8, C–E 10, D–E 2, D–F 6, E–F 3) the search starts at A with f = 7 and expands C next (g = 2, h = 5, f = 7). C improves B to g = 3 (f = 9), so B is expanded before D and E; B improves D to g = 8, and D improves E to g = 10 and gives F a first candidate of 14. E then improves F to g = 13. The goal is popped with cost 13 along A→C→B→D→E→F, and the direct D→F edge (cost 8 from D, so 14 total) is never used.

A* SearchO(E) guided, exponential worst
Best-first search guided by g + h: cheapest-known cost plus an admissible guess.
4215810263ABCDEF
openA
Step 1 / 17
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
A*: expand the node with smallest f = g (cost so far) + h (guess to F).
Python · Graph Algorithms in Code
import heapq

def astar(adj, w, h, start, goal):
    g = {start: 0}
    open = [(h[start], start)]   # f = g + h
    while open:
        _, u = heapq.heappop(open)
        if u == goal:
            return g[u]
        for v in adj[u]:
            ng = g[u] + w[(u, v)]
            if ng < g.get(v, float('inf')):
                g[v] = ng
                heapq.heappush(open, (ng + h[v], v))

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

A* Search Time & Space Complexity

BestO(E) with an exact heuristic
AverageO(E log V) with a binary heap
WorstO(b^d) — the heuristic degenerates to brute force
SpaceO(V) for the open list, closed set, g-values and parent pointers

When to Use A* Search

  • Route planning on road networks, where straight-line distance is a natural admissible heuristic.
  • Puzzle and game solvers, where a relaxed version of the puzzle supplies the estimate.
  • Any Dijkstra problem where a good guess can cut most of the frontier away.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.