Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2 C

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

  1. A. 12
  2. B. 15
  3. C. 6
  4. D. 11

Correct answer: A. 12

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic