Dijkstra's Shortest Path Visualizer — Free Interactive Algorithm Demo

Dijkstra's Shortest Path Visualizer · Visualizers · Plain HTML, CSS & JS · Live preview

CategoryVisualizers

What's included

Features

Step-by-step execution
One settled node per step with a plain-language log.
Live distance table
Distance and predecessor per node.
Relaxation highlighting
Changed distances flash; relaxed edges change colour.
Early exit at target
Stops once the target is settled.
Path reconstruction
Follows predecessors back to the start.
Choose start and target
Mouse, keyboard and long-press on touch.
SVG graph
Scales cleanly to any width.
No libraries
About 150 lines of readable JavaScript.

About this UI Snippet

Dijkstra's Algorithm, One Settled Node at a Time

Screenshot of the Dijkstra's Shortest Path Visualizer snippet rendered live

Dijkstra's algorithm finds the shortest path from one node to every other node in a graph with non-negative edge weights. It's behind route planners, network routing and game pathfinding, and it's a staple of technical interviews. It's also easy to memorise without understanding, which is what this visualizer is for: every step shows exactly what changes and why.

The two ideas

Every node carries a *tentative* distance, starting at ∞ except the start node at 0. Repeatedly, the algorithm takes the unsettled node with the smallest tentative distance and *settles* it: that distance is now final. Then it *relaxes* each edge out of that node: if going through it gives a neighbour a shorter distance than the one it has, the neighbour's distance and "via" node are updated.

Why settled means final

When a node is the closest unsettled one, any other route to it would have to pass through another unsettled node that is already at least as far away. With no negative weights, that route can't be shorter. That argument is also why Dijkstra breaks with negative edges — use Bellman–Ford there.

Reading the visual

The yellow node is being settled this step, purple nodes are settled, and orange-ringed nodes are in the queue with a tentative distance. Edges turn amber once they have been relaxed. The table shows each node's current distance and the node it was reached from, flashing when a value improves, and the log explains the step in words.

Stopping early

Because a settled distance never changes, the algorithm can stop as soon as the target is settled, then walk the "via" links backwards to recover the path, highlighted in green.

About the queue

Real implementations use a binary heap priority queue, giving O((V + E) log V). This demo sorts a small array each step to keep the code readable; see the binary heap visualizer for how the heap works.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Paste this visualizer into an AI assistant like Claude and ask it to explain why a settled node's distance can't change, using a specific step from the demo. Ask it to replace the sorted array with a binary heap, add A* with a straight-line heuristic and compare how many nodes each settles, or let users drag nodes and edit weights. It can also generate practice questions: change a weight and ask what the new path will be.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build an interactive Dijkstra's shortest path visualizer in plain HTML, CSS and JavaScript using an SVG graph.

Requirements:
- Nine nodes at fixed positions and about fifteen undirected weighted edges, with weights drawn at edge midpoints.
- Click a node to set the start and shift-click (or long-press on touch) to set the target; nodes are keyboard focusable.
- Step button: settle the unsettled queued node with the smallest tentative distance, then relax its edges to unsettled neighbours, updating distance and predecessor when shorter.
- Run/Pause button that steps automatically, and a Reset button.
- Colour nodes by state (current, settled, in queue), mark start and target, colour relaxed edges, and show each node's distance under it.
- A table of node, distance and predecessor that flashes changed distances, plus a log explaining each step in words.
- Stop when the target is settled, reconstruct the path from predecessors, and highlight it in green with its total cost.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

<div class="dj">
  <div class="dj-top">
    <div>
      <h2>Dijkstra's shortest path</h2>
      <p>Click a node to set the start, shift-click (or long-press) to set the target.</p>
    </div>
    <div class="dj-btns">
      <button type="button" id="djStep">Step</button>
      <button type="button" id="djRun">Run</button>
      <button type="button" id="djReset" class="ghost">Reset</button>
    </div>
  </div>
  <div class="dj-body">
    <svg id="djSvg" viewBox="0 0 600 360" role="img" aria-label="Weighted graph"></svg>
    <div class="dj-side">
      <table class="dj-table" aria-label="Distance table">
        <thead><tr><th>Node</th><th>Dist</th><th>Via</th></tr></thead>
        <tbody id="djRows"></tbody>
      </table>
      <div class="dj-log" id="djLog" aria-live="polite"></div>
    </div>
  </div>
  <div class="dj-legend"><span class="k cur"></span>current <span class="k done"></span>settled <span class="k front"></span>in queue <span class="k path"></span>shortest path</div>
</div>

Step by step

How to Use

  1. 1
    Choose endpointsClick a node for the start; shift-click or long-press for the target.
  2. 2
    StepEach click settles one node and relaxes its edges.
  3. 3
    Watch the tableDistances flash when an edge relaxation improves them.
  4. 4
    RunAuto-step every 0.9 s; press again to pause.
  5. 5
    Read the resultThe shortest path and its total cost are highlighted and logged.
  6. 6
    Edit the graphChange NODES positions and EDGES weights in the JS.

Real-world uses

Common Use Cases

Interview preparation
See the invariant behind the code.
Teaching graph algorithms
Project it and step through with a class.
Game development
Understand weighted pathfinding before A*.
Networking courses
Link-state routing uses the same idea.
Self-study
Change weights and predict the path before running.
Related: Binary Heap Priority Queue
The data structure that makes Dijkstra fast: Binary Heap Priority Queue Visualizer.
Related: Pathfinding Grid Visualizer
Grid-based searches: Pathfinding Grid Visualizer.

Got questions?

Frequently Asked Questions

It keeps a tentative distance for every node, starting at 0 for the source and infinity elsewhere. It repeatedly settles the unsettled node with the smallest distance and relaxes its edges, lowering neighbours' distances when a shorter route is found. A settled distance is final.

Its correctness relies on the fact that extending a path can never make it shorter. A negative edge breaks that, so a node settled early might later be reachable more cheaply. Use Bellman–Ford for graphs with negative weights.

With a binary heap priority queue it is O((V + E) log V). With a simple array scan, as in this demo, it is O(V²), which is fine for small or dense graphs.

Checking whether reaching a neighbour through the current node is shorter than the neighbour's current distance, and if so, updating the distance and remembering the current node as its predecessor.

Each improved node records the node it was reached from. After the target is settled, follow those predecessor links backwards from the target to the start and reverse the list.