Skip to content

Dijkstra's Shortest Paths
Step by Step

Settle the nearest unsettled node first, and with no negative edges no route found later can beat it.

Move forward and back at your own pace, then share a link to the step where you got stuck.

What You Watch While Distances Settle

Distances change on every step, and the visualiser tracks them for you.

Settling the nearest node first works because no edge is negative, so no later route can beat it. That logic sounds clean and gets hard to follow once distances start changing on step nine.

CodeStory's visualiser shows the frontier spreading and marks which nodes are settled for good. Code lines light up beside the graph as they execute. Move forward, move back or let the algorithm play.

Try a graph with a longer cheap route, a node you cannot reach and two routes of equal cost. Edge cases like these decide interview answers.

How Dijkstra Finds the Shortest Path

Steps below match the picture, so you can follow them beside the drawing.

  1. 1Give the source distance 0 and every other node an infinite distance.
  2. 2Take the unsettled node with the smallest distance from the frontier.
  3. 3Check every neighbour and update its distance when the route through this node is shorter.
  4. 4Mark the node settled, because no later route can beat it.
  5. 5Repeat until every reachable node is settled.

Dijkstra Cost in Time and Space

Interviewers ask about time and space, so the visualiser counts both live.

Time
O((V + E) log V)measured as real operations on your graph, not copied from a table
Space
O(V + E)extra memory for distances and the frontier
Pattern
GraphsEdge weights decide the order, not the number of edges.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

Dijkstra Visualiser FAQs

Open the visualiser and watch the answers play out.

Open the visualiser
What does the Dijkstra visualiser show?

CodeStory's visualiser shows the frontier spreading and marks which nodes are settled for good, on a graph you choose. It highlights the line of code being executed, and you can step forward, step back or let it play.

What is the time complexity of Dijkstra's algorithm?

Dijkstra's algorithm takes O((V + E) log V) time and O(V + E) space. CodeStory's visualiser counts real operations as it plays, so you watch the number grow instead of trusting the formula.

Why does Dijkstra fail with negative edges?

Dijkstra settles the nearest node first, which is only safe when no edge is negative. Negative edges can make a later route cheaper than a node you already settled.

What is a settled node?

Settled nodes have their final shortest distance, so no later route can beat them.

Can I use my own input?

Yes. Type your own graph and the visualiser plays Dijkstra's algorithm on it, edge cases included.

Is the visualiser free?

Yes. You start free with 100 AI credits when you sign up and 100 every month, and watching an algorithm costs no credits.

Stuck on Graphs? Get a Free Callback

Tell CodeStory where graphs lose you, and a mentor plans your next steps.

  1. 1CodeStory finds your starting point, from your first edge to interview ready.
  2. 2CodeStory explains online and campus batches, with timings for your mornings or evenings.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.