Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Bellman Ford Algorithm have any arbitary order of edges?

I have just started learning new algorithms but I got stuck when I read bellman ford algorithms on geeks for geeks :- http://www.geeksforgeeks.org/dynamic-programming-set-23-bellman-ford-algorithm/

There It is written that-

the algorithm calculate shortest paths in bottom-up manner. It first calculates the shortest distances for the shortest paths which have at-most one edge in the path. Then, it calculates shortest paths with at-most 2 edges, and so on.

After the ith iteration of outer loop, the shortest paths with at most i edges are calculated. There can be maximum |V| – 1 edges in any simple path, that is why the outer loop runs |v| – 1 times. The idea is, assuming that there is no negative weight cycle, if we have calculated shortest paths with at most i edges, then an iteration over all edges guarantees to give shortest path with at-most (i+1) edges.

Let us understand the algorithm with following example graph. The images are taken from this source.

Let the given source vertex be 0. Initialize all distances as infinite, except the distance to source itself. Total number of vertices in the graph is 5, so all edges must be processed 4 times.

In the below example if order of edges be- (AB),(BE),(ED),(DC),(AC),(BC),(DB),(BD) then in only one iteration it will calculate shortest paths with even 2-3 edges which contradict the claim that "It first calculates the shortest distances for the shortest paths which have at-most one edge in the path. Then, it calculates shortest paths with at-most 2 edges, and so on. After the ith iteration of outer loop, the shortest paths with at most i edges are calculated " So on changing the order of edges this statement will prove to be false.

Let us understand the algorithm with following example graph. The images are taken from this source.

Let the given source vertex be 0. Initialize all distances as infinite, except the distance to source itself. Total number of vertices in the graph is 5, so all edges must be processed 4 times.

enter image description here

Let all edges are processed in following order: (B,E), (D,B), (B,D), (A,B), (A,C), (D,C), (B,C), (E,D). We get following distances when all edges are processed first time. The first row in shows initial distances. The second row shows distances when edges (B,E), (D,B), (B,D) and (A,B) are processed. The third row shows distances when (A,C) is processed. The fourth row shows when (D,C), (B,C) and (E,D) are processed. enter image description here

The first iteration guarantees to give all shortest paths which are at most 1 edge long. We get following distances when all edges are processed second time (The last row shows final values).

enter image description here

The second iteration guarantees to give all shortest paths which are at most 2 edges long. The algorithm processes all edges 2 more times. The distances are minimized after the second iteration, so third and fourth iterations don’t update the distances.

like image 312
coderAJ Avatar asked Feb 06 '23 07:02

coderAJ


1 Answers

Yes, bellman ford works no matter in which order the edges are processed. In fact this is the reason why you have to do n-1 iterations. If you would know, what is the best order of edges - only one iteration would be enough.

Consider the following graph (all edges have weight 1):

 (1) --> (2) --> (3) --> (4) 

If you process the edges in the order 1->2, 2->3, 3->4. You will find the shortest way from 1 to 4 in only one iteration. For edges sorted in order 3->4, 2->3, 1->2 you will have to do all 3 iterations.

However, n-1 iterations is the worst case, no matter in which order the edges are processed (if there are no negative cycles).

like image 68
ead Avatar answered Feb 23 '23 00:02

ead