CleverSphere

Dijkstra’s algorithm

Dijkstra finds shortest paths from a start vertex as long as no weight is negative.

Rules

  • Start with distance 0
  • Always pick the open vertex with the smallest distance
  • Relax its neighbours

Your turn

Three problems on this topic. Separate lists with commas.

  1. 1 · Dijkstra
  2. 2
  3. 3

Dijkstra from A on the undirected graph (ties alphabetically). Find the shortest distances and the order in which the vertices are finalised.

EdgeWeight
A – B2
B – C1
A – D9
D – E2
E – F1
B – F7
B – E6
A – E1

Distances A, B, C, D, E, F (comma-separated)

Order (comma-separated)

Worked example

Dijkstra from A on the undirected graph (ties alphabetically). Find the shortest distances and the order in which the vertices are finalised.

EdgeWeight
A – B3
A – C5
C – D4
B – E1
A – F7
D – E2
A – E3
B – F7

Worked solution

  1. 1A: 0
  2. 2B: 3
  3. 3E: 3
  4. 4C: 5
  5. 5D: 5
  6. 6F: 7

Typical mistake: A vertex is only final once it has the smallest tentative distance, not when first discovered.

Common questions

Why no negative edges?

A vertex already finalised could otherwise get cheaper later.

Related

More topics