[Introduction to algorithms] Bellman-Ford algorithm (single-source shortest path)

Source: Internet
Author: User

Like other well-known Dijkstra algorithms, the bellman-Ford algorithm is used to solve the single-Source Vertex shortest path problem. In addition to solving the non-negative edge weight, the bellman-Ford algorithm can also solve the negative edge weight problem (what is the meaning? Think carefully ), the Dijkstra algorithm can only deal with Edge Weight non-negative issues, so the Bellman-Ford algorithm is more widely used. However, the time complexity of the original Bellman-Ford algorithm is O (VE), which is higher than that of Dijkstra algorithm. Therefore, it is often ignored by many university algorithm textbooks, even the classical introduction to algorithms only introduces the basic Bellman-Ford algorithm, which is not mentioned in the Chinese Textbooks of the basic informatics Orsay, therefore, the algorithm is not as well known as the Dijkstra algorithm. In fact, there are multiple forms of optimization implementation of the Bellman-Ford algorithm. These optimizations have greatly improved the time efficiency, for example, the popular spfa (shortest-path
Faster algoithm's faster shortest path algorithm) the time efficiency of the algorithm has become a topic frequently discussed by contestants of the Informatics Orsay because of Dijkstra algorithm. However, due to lack of information, many problems related to the Bellman-Ford algorithm often plague contestants. For example, is this algorithm worth understanding? How to Use programming languages? What are the optimizations? Is it related to the spfa algorithm? This article attempts to give a comprehensive introduction to the Bellman-Ford algorithm. Several implementation procedures are provided to analyze their time complexity from both theoretical and practical aspects, for your reference in preparing for the provincial election and subsequent Noi.

Bellman-Ford algorithm ideas

The bellman-Ford algorithm can solve the single-source shortest path problem in a more common situation (with a negative weight edge. For a given weighted (undirected or undirected) graph G = (V, E), its source vertex is S, and the weighted function W is the ing of edge set E. The result of running the Bellman-Ford Algorithm on graph G is a Boolean value, indicating whether there is a negative weight loop reachable from the source point S. If such a loop does not exist, the algorithm will give the Shortest Path d [v] from the source point S to any vertex v of the graph G.

The bellman-Ford algorithm flow is divided into three phases:

(1) initialization: estimate the shortest distance of all vertices except the Source Vertex d [v] between + ∞,
D [s] limit 0;

(2) iterative solution: Repeated relaxation operations are performed on each edge in edge set E, so that the minimum distance estimation value of each vertex v in the vertex set is gradually approaching its shortest distance; (run | v |-1 time)

(3) Test the negative weight loop: judge whether the two endpoints of each edge in edge set E converge. If unconverged vertices exist, the algorithm returns false, indicating no solution to the problem. Otherwise, the algorithm returns true, and the shortest distance of vertex v reachable from the source is saved in D [v.

The algorithm is described as follows:

Bellman-Ford (G, W, S): Boolean // graph G. The edge set function W and s are the source points.

1 for each vertex v ε V (G) Do // initialize phase 1

2 D [v] Limit + ∞

3 D [s] running 0; // the end of phase 1

4 for I = 1 to | v |-1 do // The Dual Loop starts at stage 2.

5 for each edge (u, v) ε E (G) Do // Number of edge sets to be used. Every edge is lifted.

6 if d [v]> d [u] + W (u, v) Then // relaxation judgment

7 d [v] = d [u] + W (u, v) // the end of phase 2 of the relaxation operation

8 For each edge (u, v) ε E (G) Do

9 if d [v]> d [u] + W (u, v) then

10 Exit false

11 exit true


Sample Code:

#include <iostream>#include <stdio.h>using namespace std;#define MAX 100000#define N 1010int nNode, nEdge, original;typedef struct Edge{int u, v;int cost;}Edge;Edge edge[N];int d[N], pre[N];void Relax(int u, int v, int w){if(d[v] > d[u] + w ){d[v] = d[u] + w;pre[v] = u;}}void Initialize_Single_source(){for( int i = 1; i <= nNode; i++){d[i] = MAX;pre[i] = 0;}d[original] = 0;}bool Bellman_Ford(){Initialize_Single_source();for( int i = 1; i < nNode; i++ ){for( int j = 1; j <= nEdge; j++){Relax(edge[j].u, edge[j].v, edge[j].cost);}}for( int j = 1; j <= nEdge; j++){if( d[edge[j].u] > d[edge[j].v] + edge[j].cost){return false;}return true;}}void Print_Path(int root){  if(0 != root){Print_Path(pre[root]);printf("-->%d", root);}}int main(int argc, char* argv[]){scanf("%d%d%d", &nNode, &nEdge, &original);for( int i = 1; i <= nEdge; i++ ){scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].cost);}if (Bellman_Ford()){for( int i = 1; i <= nNode; i++){printf("\nnode%d: %d(km)\tPath:",i, d[i]);Print_Path(i);}printf("\n");}else{printf("Have negative circle\n");}return 0;}

Test results:


References:

Http://www.cppblog.com/infinity/archive/2011/10/20/66621.html

Http://blog.csdn.net/niushuai666/article/details/6791765


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.