问题
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.
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.
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).
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.
回答1:
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).
回答2:
That's correct. The order of relaxation does not matter.
For example,
There are 3 nodes, so two iterations of relaxation can be done. If the relaxation happens backwards starting from the node b and then to node a, the first iteration would yield b: infinity, a: 2. Then the second iteration is b:5, a:2. In the first iteration b can’t be updated, since only one edge was used, only the a node, which is one edge away from it, can be updated. The second iteration is when two edges were used from the source to anywhere, so b can finally be updated. In the first iteration, b simply couldn’t be updated since a was not ready.
Then how about we take a look at the forward update? In the first iteration, it would be a: 2 and b: 5. The second iteration is a: 2 and b: 5 again. Going forward lets all the successive nodes be ready with updated distances, so more iterations do not change value. But then one might wonder what if I am doing 100 iterations for 101 nodes, and I was doing the forward update. Would it be possible that everything was updated in the first iteration and there was no change up to the 99th iteration and all of a sudden there was a node that decreased in its distance at the 100th iteration? No, it is not possible, because the forward update does not cause change after the 1st iteration unless there is a negative cycle.
You can read more about Bellman-Ford here: https://medium.com/@logicdevildotcom/dynamic-programming-applied-to-graphs-f33b6b8a8247
回答3:
The table row you are referring to with the comment
The fourth row shows when (D,C), (B,C) and (E,D) are processed.
is not correct. It would imply the existence of a path from A
to C
with length 2 which consists out of at most one edge - however such a path does not exist.
回答4:
sample graph
In this directed weighted graph, suppose we would relax the edges in the following order: (u,v), (s,u) and (s,v). Here source is “s”. So, in the initialization step of Bellman Ford algorithm, we have d(s)=0, d(u)=d(v)=∞. As here we have total 3 number of vertices, so there should be 3-1=2 number of iterations for the algorithm to produce shortest distance for the vertices from the source. In each step of iteration, we need to do the relaxation step for each edge (u,v) which checks if d(u) + w(u,v) < d(v) and if this condition holds, then d(v) would be made equal to d(u) + w(u,v)
After the first iteration: d(u)=3, d(v)=7 After the second iteration: d(u)=3, d(v)=5
So, as you can see, the first iteration is not guaranteed to give all shortest paths which are at most 1 edge long. Because in this example, we get the shortest distance for 1 edge long node “v” only after the 2nd iteration. However, if we enforce triangle inequality in our graph, then the statement will be true.
Now coming to your original question, yes Bellman Ford algorithm can relax the edges in any arbitrary order as nicely answered by @ead above. But at the end of the final iteration step, the algorithm would give you the shortest distance for each of the nodes from the source node.
来源:https://stackoverflow.com/questions/41758089/can-bellman-ford-algorithm-have-any-arbitary-order-of-edges