Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2

Directed weighted graph on vertices A, B, C, D, E, F with edges listed in this exact order: E→D (1); F→D (5); F→C (1); B→E (5); F→A (4); D→B (2); E→C (-1); E→B (-1); A→D (0); C→A (-1). Bellman–Ford from A makes passes over the edge list in that order, relaxing in place (each edge is tried once per pass). Counting the passes from the first, how many passes change at least one distance?

  1. A. 4
  2. B. 3
  3. C. 5
  4. D. 10

Correct answer: B. 3

The distances settle after 3 passes that change something; one more pass is needed only to confirm nothing changes, and that final pass is not counted.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic