Bellman Ford Algorithm

Source: Internet
Author: User

Dijkstra algorithm is effective for processing single-source shortest pathsAlgorithmBut it is limited to the case where the edge weight is not negative. If an edge with the negative weight is displayed in the figure, the Dijkstra algorithm will fail and the obtained shortest path may be wrong. At this time, we need to use other algorithms to solve the shortest path. The Bellman-Ford algorithm is one of the most commonly used algorithms. The algorithm was invented by American mathematician Richard Bellman and Lester Ford. The flow of the Bellman-Ford algorithm is as follows:
Given graph G (V, E) (where V and E are vertex sets and edge sets of graph G respectively), Source Vertex s,

Array distant [I] records the path length from the source point S to the vertex I. The initialized array distant [N] is, and distant [s] is 0;
The following operations can be performed cyclically for up to n-1 times, and N is the number of vertices:
For each edge e (u, v), if distant [u] + W (u, v) <distant [v], then distant [v] = distant [u] + W (u, v ). W (u, v) is the weight of edge e (u, v;
If the preceding operation does not update distant, it indicates that the shortest path has been searched, or some vertices are not reachable. Otherwise, execute the next loop. to check whether a negative loop exists in the graph, that is, the loop with the sum of weights less than 0. For each edge e (u, v), if there is an edge of distant [u] + W (u, v) <distant [v], there is a negative loop in the figure, that is, the shortest path of a single source cannot be obtained by modifying the image. Otherwise, the shortest path length from the Source Vertex s to each vertex is recorded in the array distant [N.

We can see that the time complexity of the Bellman-Ford Algorithm for Finding the single-source shortest path is O (V * E ).

First, we will introduce the relaxation calculation. For example:


 

Before the relaxation calculation, the value of point B is 8, but the value of Point A is smaller than the value of point B (8) by adding the weight of 2 on the edge, the value of point B is reduced to 5. The significance of this process is to find a shorter route to point B, which first passes through point A and then reaches Point B through the edge with a weight of 2.
Of course


 

The value of point B is not modified, because 3 + 4> 6.
 
The bellman-Ford algorithm can be roughly divided into three parts:
First, Initialize all vertices. Save a value for each vertex, indicating the distance from the origin to this vertex. Set the value of the origin to 0, and set the value of other vertices to infinity (indicating that the origin cannot be reached ).
Second, perform a loop. The subscripts of the loop are from 1 to n-1 (N is equal to the number of vertices in the graph ). In the loop, traverse all edges for relaxation calculation.
Third, traverse all the edges (u, v) on the way to determine whether such a situation exists:
D (v)> d (u) + W (u, v)
The return value is false, indicating that there is a loop with a negative permission from the source point.
 
The reason for the third part is that if there is a loop with the right to reach from the source point as negative. Therefore, the shortest path cannot be obtained because it cannot be converged.
Consider the following figure:
 

After the first traversal, the value of point B is changed to 5, and the value of point C is changed to 8. At this time, pay attention to the edge with a weight of-10 and the existence of this edge, the value of vertex A changes to-2. (8 +-10 =-2)
 
 

After the second traversal, the value of point B changes to 3, point C changes to 6, and point a changes to-4. It is precisely because there is a negative edge in the loop that the values of each vertex keep decreasing after each traversal.
 
Let's look back at the third part of the Bellman-Ford algorithm. traverse all edges and check whether d (v)> d (u) + W (u, v) exists ). Because the number of cycles in the second part is fixed, if convergence is not possible, it can be checked out in the third part. For example
 

In this case, the value of vertex A is-2, the value of vertex B is 5, and the weight of edge AB is 5, 5,>-2 + 5. Check that this edge does not converge.
 
Therefore, the bellman-Ford algorithm can solve the single-source shortest path problem of an edge with a negative number in the graph.

Reprinted from http://hi.baidu.com/qinning199/blog/item/2140c496946e0151d1135eda.html

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.