Bellman-ford Shortest Path algorithm

Source: Internet
Author: User

Algorithm proves: http://courses.csail.mit.edu/6.006/spring11/lectures/lec15.pdf

Let's look at a diagram like this:

This is a negative edge, if the use of Djistra will be an infinite number of relaxation operations. It can be seen from here that the relaxation operation is a bit problematic, if there is a negative ring, will be endless relaxation, the shortest path will not exist. There is also the choice of different traversal order for the relaxation operation is very important. Get to know the smart Bellman-ford algorithm today!

Bellman-ford algorithm each round of the side in accordance with a certain order, each edge of relaxation. Pass | After the v|-1 wheel, the shortest path must be obtained.

Present the pseudo-code for the relaxation operation and Bellman-ford algorithm:

/*Slack Operation*/ forVinchV:dist[v]=∞dist[s]=0 whileSome edge (u,v) has dist[v]>dist[u]+W (u,v): Pick such an edge (u,v) Relax (u,v):ifdist[v]>dist[u]+W (u,v): Dist[v]=dist[u]+W (u,v)/*Bellman-ford*/ forVinchV:dist[v]=∞dist[s]=0 forI from 1to | v|-1:     for(U,V)inchE:relax (u,v):ifdist[v]>dist[u]+W (u,v): Dist[v]=dist[u]+w (U,V)

Next, for example, all edges are processed in the following order: (A, B), (A,c), (B,c), (B,d), (B,e), (E,d), (D,b), (d,c).

Then make the first iteration:

At the beginning, the contents of the dist array are [0,∞,∞,∞,∞], now processing (A, b), due to Dist[b]=∞,dist[a]+w (A, B) =-1, should be updated at this time dist[b], that is dist [0,-1,∞,∞,∞].

Similarly, by processing the edges sequentially, you will get the following dist change process:

[0,-1,4,∞,∞] (A,C)

[0,-1,2,∞,∞] (B,C)

[0,-1,2,1,∞] (b,d)

[0,-1,2,1,1] (b,e)

[0,-1,2,-2,1] (e,d)

[0,-1,2,-2,1] (D,B)

[0,-1,2,-2,1] (D,C)

The final result of the first round is the last line above, of course, this is not necessarily optimal, so we need to proceed to the next round, through | V-1| will be able to find out the shortest possible after a short time.

Of course, there is a negative ring is unable to get the shortest, at this time only need to judge at the end, on each side to determine whether the following can be relaxed operation, if there is a possible edge, that is the negative ring appears.

Bellman-ford Shortest Path algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.