Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern

A directed graph has weighted edges A→B (4), A→C (1), C→B (2), B→D (1), C→D (5), D→E (3). Dijkstra's algorithm is run from A. What is the shortest distance to E?

  1. A. 8
  2. B. 7
  3. C. 4
  4. D. 16

Correct answer: B. 7

A→C = 1, then C→B makes B = 3 (better than 4), B→D makes D = 4 (better than 1 + 5 = 6), and D→E makes E = 7. Keeping the direct A→B edge gives 8, 4 is the distance to D, not E, and 16 adds every edge weight.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic