Bellman-ford algorithm--Solving the shortest path problem of single source point

Source: Internet
Author: User
Tags ord

Bellman-ford algorithm with another very well-knownThe Dijkstra algorithm is used to solve the shortest path problem of single source point.In addition to solving the problem of Benquan, the Bellman-ford algorithm can solve the problem of existence of negative right side (meaning, thinking), andThe Dijkstra algorithm only handles the problem of non-negative edge, soThe Bellman-ford algorithm should be widely used. However, the originalThe time complexity of the bellman-ford algorithm isO (VE)Thandijkstra algorithm has a high time complexity, so it is often neglected by many university algorithm textbooks, even the classic "Introduction to Algorithms" only introduces the basic bellman-ford algorithm, in the common basic information science Orsay textbooks are also not mentioned, so the algorithm's visibility and mastery of the dijkstra algorithm. In fact, there are many forms of bellman-ford algorithm optimization implementation. These optimizations have been considerably improved in time efficiency, such as the SPFA (shortest-path Faster algoithm faster Shortest path algorithm) algorithm time efficiency even due to dijkstra algorithm, Therefore become the topic that the information Science Orsay contestant often discuss. However, limited to lack of information, the bellman-ford algorithm has many problems that often plague the Olympians. Such as: Is this algorithm worth mastering? How to use programming language to implement specifically? What are the optimizations? Is it related to SPFA algorithm? This paper attempts to bellman-ford algorithm to do a more comprehensive introduction. Several realization programs are given, and their time complexity is analyzed from both theoretical and practical aspects, for the noi in preparation for the provincial election.

Dijkstra algorithm cannot handle negative weight edge

Dijkstra because it is greedy, each time find a point nearest to the source point (DMin), and then set the distance as the point to the source point of the shortest path (d[i]<--dmin), but if there is a negative edge, it is possible to first pass not from the source point of the recent advantage (DMin '), Then through this negative right side L (l<0), make the path of the sum smaller (dmin ' +l<dmin), then DMin ' +l to become the shortest path, not dmin, so Dijkstra was embarrassed dropped.
For example, n=3, adjacency matrix:
0,3,4
3,0,-2
4,-2,0
With Dijkstra to find d[1,2]=3, in fact d[1,2]=2, is through 1-3-2 so that the path is reduced.

Bellman-ford Algorithm Idea

The

bellman-ford algorithm can solve the single source point shortest path problem in a more general case (there is a negative weight edge). For a given weighted (with or without direction) Figure g= (v,e) , its source point is W is Edge set E mapping. To figure g run If no such loops exist, the algorithm will give s to figure g any vertex v Shortest Path D[V].

The Bellman-ford algorithm flow is divided into three stages:

(1) Initialization: The shortest distance estimate of all vertices except the source point d[v]←+∞, d[s]←0;

(2) Iterative solution: The relaxation operation of each edge in the edge set E is repeated, so that the shortest distance estimate of each vertex v in the vertex set V is gradually approximated to its shortest distance; (run |v|-1 times)

(3) Test negative power loop: Determine whether the two endpoints of each edge in the edge set E converge. If there are non-convergent vertices, the algorithm returns false, indicating that the problem is not solved, otherwise the algorithm returns true, and the shortest distance from Vertex v that the source point can reach is saved in D[v].

The algorithm is described as follows:

Bellman-ford (G,w,s): Boolean//Figure g, Edge-Set function W, s as Source point         forEach vertex v∈v (G) Do        //Initialize 1 stagesd[v]←+∞d[s]←0;//1 stage End         forI=1To |v|-1  Do               //2 stage start, double cycle.             forEach edge (u,v) ∈e (G) Do //an array of edge sets is used, and each side is exhaustive. If D[v]> d[u]+ w (u,v) Then//Relaxation JudgmentD[v]=d[u]+w (U,V)//Relaxation Operation 2 stage end         forEach edge (u,v) ∈e (G) DoIf D[v]> d[u]+W (u,v) then ExitfalseExittrue

A descriptive proof is given below:

First, it is pointed out that any shortest path of the graph can contain neither negative weight loop nor positive weight loop, so it contains |v|-1 edge.

Second, if the shortest path exists for all vertices from the source point s, these shortest paths form a shortest path tree rooted in S. The iterative relaxation operation of the Bellman-ford algorithm is actually the process of generating the shortest path tree by layer by the level of the vertex distance s.

When 1 times slack is made on each side, the branches that start from S and have a maximum level of 1 are generated. That is, the shortest path to the vertices with a maximum of 1 edges associated with S is found, and the 2nd-pass relaxation of each edge creates a 2nd-level branch, which means that the shortest path of those vertices connected by 2 edges is found .... Because the shortest path contains up to |v|-1 edges only, you only need to loop |v|-1 times.

Each relaxation operation, the shortest path tree will have a layer of vertices to reach its shortest distance, after which the minimum distance of the vertex will remain unchanged, no longer affected by subsequent relaxation operations. (However, every time you have to judge the slack, there is a lot of wasted time, how to optimize?) is the simple optimization feasible? )

If there is no negative power loop, because the shortest path tree height can only be |v|-1, so up to the |v|-1 through the relaxation operation, all the vertices from S can reach the shortest distance. If D[V] remains +∞, it indicates that it is unreachable from S to v.

If there is a negative power loop, then the |v|-1 relaxation operation will still succeed, and the vertices on the negative weight loop will not converge.

Three, the optimization of the basic algorithm

Analyzing the Bellman-ford algorithm, it is not difficult to see that the outer loop (iteration count)|v|-1 actually achieved is the upper limit. The proof of the correctness of the algorithm on the upper face shows that the number of iterations required equals the height of the shortest path tree. If there is no negative right loop, the average height of the shortest path tree should be far less than |v|-1, in which case, the redundant shortest path tree high iteration number is a waste of time, thus, can be implemented in turn to optimize.

In detail, if, in a iteration, the relaxation of line 7 in the algorithm description is not performed, it means that all the edges of the iteration have not been loosened. can prove it (how to prove it?) ): At this point, all edges in the edge set do not need to be loosened again, so that the iterative process can be ended prematurely. In this way, the optimization measures are very simple.

Sets a Boolean flag variable relaxed, the initial value is false. In the inner loop, relaxed is set to true only if there are edges that have been successfully loosened . If no edge is slack, the outer loop is ended prematurely. This improvement can greatly reduce the number of iterations of the outer loop. The optimized Bellman-ford function is as follows.

function Bellmanford (s:longint): boolean; Begin fori:=1To NV DoD[i]:=Max; D[s]:=0;  fori:=1To nv-1  DoBegin relaxed:=false;  forj:=1To NE Do                if(D[edges[j].s]<>max) and (d[edges[j].e]>d[edges[j].s]+EDGES[J].W) THEN begin D[EDGES[J].E]:=d[edges[j].s]+EDGES[J].W; Relaxed:=true;           End ifNot relaxed Then Break;        End  fori:=1To NE Do          ifD[EDGES[J].E]&GT;D[EDGES[J].S]+EDGES[J].W Then Exit (false); Exit (true); End;

What kind of effect will this seemingly mundane optimization have? Studies have shown that for the average of randomly generated data, the estimation formula for time complexity is

1.13|                    e| If | e|<| v|

0.95*| E|*lg|              v| If | e|>| v|

When the optimized algorithm deals with the test data of the negative-weight loop, the relaxed is set to true every time, so it is impossible to terminate the outer loop prematurely. This corresponds to the worst case, and its time complexity is still O (VE).

The time complexity of the optimized algorithm is similar to the Dijkstra algorithm with binary heap optimization , and the coding complexity is much lower than the latter. In addition , the Bellman-ford algorithm can deal with the shortest path problem in the case of various boundary value weights, so it is still very good. Usaco3.2.6 's procedure, see Bellmanford_1.pas .

Four,SPFA algorithm

SPFA is currently a fairly excellent algorithm for finding the shortest path, which deserves our mastery.

SPFA the key to the optimization of the Bellman-ford algorithm is to realize that only those points that have changed the distance estimates in the previous relaxation can cause changes in the distance estimates of their adjacency points. Therefore, a FIFO queue is used to store the vertices that have been successfully loosened. Initially, the source point S is enqueued. When the queue is not empty, the first vertex is removed, and its adjacency points are relaxed. If an adjacency point is slack and the adjacency point is not in the queue, it is enqueued. After a limited number of slack operations, the queue is empty and the algorithm ends. the implementation of the SPFA algorithm requires the use of a FIFO queue and a tag array that indicates whether the vertex is in the queue mark. In order to find the adjacency point of a vertex conveniently, the graph uses the critical table storage.

The program is stored in the Spfa.pas. Take Usaco 3.2.6 question 2 as an example. The program written with the adjacency table.

It is important to note that SPFA works correctly only when the graph does not have a negative weight loop. If the graph has a negative weight loop, because the vertices on the negative weight loop can not converge, there are always vertices in the queue and out of the team round trip, the queues can not be empty, in which case the SPFA can not end normally.

There are many schemes to judge the negative right loop, the most widespread one is to record the number of each node into the team, over | v| time to show negative rights
There is also a way to record the location of this node in the path, Ord[i], each time the update ord[i]=ord[x]+1, if more than | v| indicates a negative circle .....
There are many other ways, but I think the most widely circulated method is the slowest ....

About the time complexity of SPFA, not accurate estimation, is generally considered O (KE),K is constant

Five, time efficiency measurement

The above introduction of the Bellman-ford algorithm and two optimization, only in the theoretical analysis of the time complexity, with the actual data testing, what will be the result? To this end, we choose usaco 3.2.6.

The time efficiency of SPFA is still very high. and the programming complexity of SPFA is much better than dijksta+heap optimization.

Bellman-ford algorithm--Solving the shortest path problem of single source point

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.