Data Structures & AlgorithmsVery hardCodeforces Div 2 C · GATE CS

Bellman–Ford is run from source S on the directed graph whose arcs (weights in brackets) are listed in the order they are relaxed: S→E (7), M→S (5), O→M (7), M→E (6), E→O (4), O→S (3). One pass relaxes every arc once, in the listed order. Passes are repeated until a complete pass changes no distance. Counting the final pass that changes nothing, how many passes run in total?

  1. A. 2
  2. B. 6
  3. C. 4
  4. D. 3

Correct answer: D. 3

The last pass changes no distance, so it is counted; the passes before it changed something, 2 of them. The bound n − 1 = 3 counts only the passes needed in the worst case.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic