Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

An undirected connected graph on vertices 0 to 5 has these weighted edges: 0-1 (w=9), 1-2 (w=2), 1-3 (w=10), 1-5 (w=6), 2-3 (w=2), 3-4 (w=9), 3-5 (w=6), 4-5 (w=7). The length of a path is the sum of its edge weights. What is the length of the shortest path from vertex 0 to vertex 4?

  1. A. 22
  2. B. 9
  3. C. 3
  4. D. 28

Correct answer: A. 22

Shortest weighted paths need Dijkstra (or a careful search), not the path with the fewest edges. The path found by breadth-first search may use heavy edges, so summing along it overstates the true distance.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic