Skip to main content
2 of 3
made briefer, clearer; formatted; linked; latexified;

Bellman ford algorithm, does it matter if I take different series of edges when updating the cost of the vertex

The Bellman-Ford algorithm determines the shortest path from a source $s$ to all other vertices. Initially the distance between $s$ and all other vertices is set to $\infty$. Then the shortest path from $s$ to each vertex is computed; this goes on for $|V|-1$ iterations. My questions are:

  • Why does there need to be $|V|-1$ iterations?
  • Would it matter if I checked the edges in a different order?
    Say, if I first check edges 1,2,3, but then on the second iteration I check 2,3,1.

MIT Prof. Eric said the order didn't matter, but this confuses me: wouldn't the algorithm incorrectly update a node based on edge $x_2$ if its value was dependent on the edge $x_1$ but $x_1$ is updated after $x_2$?

user1675999
  • 1k
  • 7
  • 16
  • 23