Dijkstra Ready
vertices 0
edges 0
final 0
  • Current
  • Looking at
  • Done
  • On the shortest path

Ready

Comparisons 0 Shifts 0
Ready

Edges are written A-B:4 (letters A–H, weights 1–99, up to 14 edges) · Keyboard: ← → step · Space play / pause · R reset

How it works

Every operation, in plain steps

These are exactly the steps the visualizer animates.

Closest first, then relax

Dijkstra finds the shortest distance from ONE source vertex to every other vertex in a graph with positive weights. dist[v] is the best distance found so far (∞ at the start, 0 for the source). The algorithm repeatedly FINALISES the unfinished vertex with the smallest distance — nothing can reach it more cheaply any more — and then RELAXES its neighbours: is going through this vertex shorter than what they have? parent[v] remembers the way, so the path can be read backwards.

int dist[V];       // best distance found so far (INF at the start)
int parent[V];     // the vertex before v on that route
int done[V];       // 1 once dist[v] is final

dist[src] = 0;
for (count = 0; count < V; count++) {
    int u = vertex with the smallest dist that is not done yet;
    done[u] = 1;                                   // final!
    for (each edge u -> v with weight w)
        if (!done[v] && dist[u] + w < dist[v]) {   // relax
            dist[v] = dist[u] + w;
            parent[v] = u;
        }
}

The algorithm

  1. dist[source] = 0, every other vertex ∞.
  2. Pick the unfinished vertex u with the smallest dist: it is FINAL.
  3. Relax every edge u → v: if dist[u] + w < dist[v], update dist[v] and parent[v].
  4. Repeat until every reachable vertex is final.

Why is "closest first" safe?

  1. Any other route to u would first pass through an unfinished vertex.
  2. That vertex is at least as far as u (we picked the smallest).
  3. Weights are positive, so going further only adds distance.
  4. So no route can beat dist[u]: it is final.

Relaxing an edge

  1. Edge u → v with weight w, u final.
  2. Candidate = dist[u] + w.
  3. Candidate < dist[v]: better route found, update dist and parent.
  4. Otherwise keep the old route.

Reading the path

  1. Start at the target t.
  2. Follow parent[t], parent[parent[t]], … back to the source.
  3. Reverse the list: that is the route.
  4. dist[t] = ∞ means t cannot be reached.

Not for negative weights

  1. A negative edge could make a final vertex cheaper later.
  2. That breaks the "closest first is final" argument.
  3. For negative weights use Bellman–Ford instead.
  4. The weights in this lab are 1 to 99.

Dijkstra vs BFS

  1. BFS: every edge counts as 1, a queue gives the shortest path.
  2. Dijkstra: edges have weights, a PRIORITY queue gives the shortest path.
  3. With all weights equal, Dijkstra behaves like BFS.
  4. Both reach each vertex through the best route known so far.
Algorithm

Pseudocode

Language-independent steps. The visualizer shows the same logic in C, Python and Java.

1 DIJKSTRA(G, source)
2 for every vertex v: dist[v] ← ∞; parent[v] ← NULL; done[v] ← false
3 dist[source] ← 0
4 repeat V times
5 u ← the vertex with done[u] = false and the smallest dist[u]
6 if dist[u] = ∞ then stop (the rest is unreachable)
7 done[u] ← true
8 for each edge u → v with weight w
9 if not done[v] and dist[u] + w < dist[v] then
10 dist[v] ← dist[u] + w; parent[v] ← u
Complexity

How fast is each operation?

V = vertices, E = edges. With a simple array the "smallest dist" search costs O(V) each time, so O(V²) overall. With a binary heap every update costs O(log V): O((V + E) log V).

Operation Best Average Worst Speed
Dijkstra (array) O(V²) O(V²) O(V²) Grows with n
Dijkstra (binary heap) O((V + E) log V) O((V + E) log V) O((V + E) log V) Grows with n
Read a path (parent[ ]) O(1) O(V) O(V) Grows with n

Shortest-path algorithms

BFS Dijkstra Bellman–Ford Floyd–Warshall
Answers One source (unweighted) One source (weighted) One source (weighted) All pairs
Weights None (every edge = 1) Positive only Negative allowed Negative allowed (no negative cycle)
Helper Queue Priority queue None (V − 1 rounds) Distance table
Time O(V + E) O(V²) / O((V + E) log V) O(V × E) O(V³)

Dijkstra: what the table holds

Column Meaning
dist The best distance found so far from the source (∞ = not reached yet)
parent The vertex just before it on that route
done The distance is final — it never changes again
Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 8 correct
Question 1

At each step, which vertex does Dijkstra finalise?

Question 2

What does "relaxing" an edge u → v mean?

Question 3

Why does Dijkstra's algorithm not work with negative edge weights?

Question 4

Dijkstra ends with dist[F] = ∞. What does that tell you?

Question 5

How do you get the shortest PATH (not just its length) from Dijkstra's result?

Question 6

What is the time complexity of Dijkstra with a simple array (no heap)?

Question 7

If all edge weights are 1, Dijkstra behaves like…

Question 8

The shortest route from A to D goes A → C → B → D. What is parent[D] when Dijkstra has finished?

Assessment

Graphs quiz

Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.