Data Structures & AlgorithmsVery hardGATE CS · Product-company placement

Bellman–Ford runs from A on the directed graph whose edges are relaxed in EVERY pass in this fixed order: F→A (9), D→A (4), E→D (8), C→B (5), D→C (0), E→F (-2), B→F (-1), B→E (5), A→E (0). It stops early after the first pass that changes nothing. How many passes change at least one distance, and what is the final distance to F?

  1. A. 5 passes, dist -2
  2. B. 4 passes, dist -2
  3. C. 2 passes, dist -2
  4. D. 3 passes, dist -2

Correct answer: D. 3 passes, dist -2

Simulate pass by pass in the given edge order: a change can travel along several edges in one pass when they appear in path order, so only 3 passes change something (then one pass confirms nothing changes). Final distances: A=0, B=13, C=8, D=8, E=0, F=-2.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic