Data Structures & AlgorithmsModerateGATE CS · Codeforces Div 2 C

Directed graph on vertices 0 to 7 with weighted edges (u→v (w)): 3→6 (2), 4→1 (10), 6→2 (5), 7→6 (11), 5→7 (10), 7→5 (7), 0→5 (11), 1→6 (2), 2→0 (10), 1→3 (2), 2→7 (11), 1→0 (5), 7→2 (8), 2→3 (3), 7→3 (8), 5→6 (2), 3→7 (7), 6→7 (2). What is the length of a shortest path from 0 to 7, where the length is the sum of edge weights?

  1. A. 21
  2. B. 9
  3. C. 15
  4. D. 18

Correct answer: C. 15

Dijkstra finalises vertices in increasing distance and gives 15 via 0 → 5 → 6 → 7. Counting edges instead of weights picks a route with 2 edge(s) and weight 21, which is the common slip.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic