Skip to main content

Dijkstra shortest path (2075 Bhadra)

Short answer

Using Dijkstra's algorithm, the shortest distances from A are: B=10, C=12, D=10, E=11, F=8, G=5, H=8. The paths are A–B, A–H–C, A–G–F–D, A–G–F–E or A–G–F–D–E, A–G–F, A–G, and A–H respectively.

Use Dijkstra's algorithm to find the length of a shortest path from the vertices A to other in the graph below. [Figure: weighted undirected graph on vertices A, B, C, D, E, F, G, H with edges A-B 10, A-G 5, A-H 8, B-C 3, B-H 4, B-D 6, C-H 4, C-D 3, G-H 4, G-F 3, H-F 10, H-E 4, H-D 9, F-D 2, F-E 3, E-D 1; edge readings approximate from scan]

Dijkstra's algorithm (non-negative weights) labels every vertex with the best distance known so far and fixes one vertex at a time:

  1. Give the source distance 0 and every other vertex ∞. All vertices are temporary.
  2. Make the temporary vertex with the smallest label permanent.
  3. For each temporary neighbour v of the new permanent vertex u: if d(u) + w(u,v) < d(v), set d(v) = d(u) + w(u,v) and record u as predecessor.
  4. Repeat steps 2 and 3 until the target (or all vertices) is permanent.

Graph as read

The figure is read with the edge weights listed below (the scan is approximate). Source is A; distances to all other vertices are required.

Edges: A–B 10, A–G 5, A–H 8, B–C 3, B–H 4, B–D 6, C–H 4, C–D 3, G–H 4, G–F 3, H–F 10, H–E 4, H–D 9, F–D 2, F–E 3, E–D 1.

Trace

Each cell shows label = distance(predecessor). Bold marks the vertex made permanent in that step; ✓ means already permanent.

StepFixedABCDEFGH
1A010(A)∞∞∞∞5(A)8(A)
2G✓10(A)∞∞∞8(G)5(A)8(A)
3F✓10(A)∞10(F)11(F)8(G)✓8(A)
4H✓10(A)12(H)10(F)11(F)✓✓8(A)
5B✓10(A)12(H)10(F)11(F)✓✓✓
6D✓✓12(H)10(F)11(F)✓✓✓
7E✓✓12(H)✓11(F)✓✓✓
8C✓✓12(H)✓✓✓✓✓

Result

VertexShortest distance from AShortest path
B10A–B
C12A–H–C
D10A–G–F–D
E11A–G–F–D–E or A–G–F–E
F8A–G–F
G5A–G
H8A–H

Answer: shortest distances from A: B = 10, C = 12, D = 10, E = 11, F = 8, G = 5, H = 8. (E has two shortest paths of length 11: A–G–F–E and A–G–F–D–E.)

✓ Answer checked by Claude Opus 5.5 (high effort)

All Discrete Structure chapter 4 questions →