Dijkstra’s single source shortest path algorithm when run from vertex a in the…
2008
Dijkstra’s single source shortest path algorithm when run from vertex a in the below graph, computes the correct shortest path distance to

- A.
only vertex a
- B.
only vertices a, e, f, g, h
- C.
only vertices a, b, c, d
- D.
all the vertices
Attempted by 163 students.
Show answer & explanation
Correct answer: D
Correct answer: all the vertices.
Explanation: Although there is a negative-weight edge from b to e (weight −3), Dijkstra still computes the correct shortest-path distances for every vertex in this specific graph because the negative edge is relaxed before any affected vertex is permanently settled.
Initialization: dist(a)=0, all others = ∞. After relaxing edges from a: dist(b)=1, dist(d)=2.
Extract b next (smallest tentative distance = 1) and relax its edges: dist(c) becomes 3 (via b→c), and dist(e) becomes −2 (via b→e with weight −3).
Now e has the smallest tentative distance (−2) and is extracted; relaxing from e sets dist(h)=−1, dist(g)=−1, and dist(f)=0 (using the given right-side edge weights).
The remaining vertices (h, g, f, d, c) are then processed in nondecreasing order of their tentative distances and no further decreases to already settled vertices occur.
Therefore the final shortest-path distances found by Dijkstra are:
a: 0
b: 1
c: 3
d: 2
e: −2
h: −1
g: −1
f: 0
Key takeaway: Dijkstra's algorithm requires nonnegative edge weights to guarantee correctness in all cases, but in some particular graphs with negative edges the algorithm may still produce correct results. In this graph the negative edge is relaxed early and does not cause any previously settled vertex to require a decrease, so the algorithm yields correct shortest-path distances for every vertex.
A video solution is available for this question — log in and enroll to watch it.