Bellman-Ford Shortest Path of the solving Unit

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 time efficiency of the popular spfa (shortest-path faster algoithm faster shortest-path algorithm) algorithm in the last year or two, even due to the Dijkstra algorithm, therefore, it has become a topic that contestants often discuss. 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. The bellman-Ford algorithm IDEA The Bellman-Ford algorithm can solve the single-Source Vertex 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] cosine + ∞, d [s] limit 0; (2) iterative solution: repeats the relaxation operation on each edge in edge set E, make the shortest distance estimation value of each vertex v in vertex set V gradually approach its shortest distance. (run | v |-1 times) (3) Test the negative weight loop: determine 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. Algorithm Description: Bellman-Ford (G, W, S): Boolean // graph G, edge set function W, S is the source point 1 for each vertex v ε V (G) do // initialize phase 1 2 D [v] primary + ∞ 3 D [s] Primary 0; // Phase 1 ends 4 for I = 1 to | v |-1 do // Phase 2 begins with a dual loop. 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 // returns 7 d [v] = d [u] + W (u, v) // The relaxation operation ends at stage 2. 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 give descriptive proof: first, it is pointed out that any shortest path in the figure can neither contain a negative weight loop nor a positive weight loop, therefore, it can contain up to | v |-1 edge. Second, if there is a shortest path for all vertices that can be reached from the Source Vertex s, these Shortest Paths constitute a shortest path tree with s as the root. The iterative relaxation operation of the Bellman-Ford algorithm is actually the process of generating the shortest path tree layer by layer based on the vertex distance S. When each side is relaxed once, the branches starting from S and whose layers are mostly 1 are generated. That is to say, the shortest path of those vertices associated with at most one edge of S is found. When every side is relaxed for 2nd times, 2nd layers of branches are generated, that is to say, we have found the shortest path of those vertices connected by two edges ....... Because the shortest path can only contain | v |-1 edge, you only need to loop | v |-1 times. Each time a relaxation operation is performed, a vertex on the shortest path tree reaches its shortest distance, and the shortest distance value of the vertex on the shortest path will remain unchanged and will not be affected by subsequent relaxation operations. (However, we need to judge relaxation every time. This wastes a lot of time. How can we optimize it? Is pure optimization feasible ?) If no negative weight loop exists, the shortest path tree height can only be | v |-1, so after a maximum of | v |-1 relaxation operations, all vertices reachable from S will surely find the shortest distance. If d [v] is still + ∞, it indicates that it is not reachable from S to v. If there is a negative weight loop, the nth | v |-1 time relaxation operation will still succeed. In this case, the vertices on the negative weight loop will not converge. For example, a negative weight loop exists between vertices A, B, and C. S is the source point, and the number in the vertex indicates the shortest distance estimation value of each point after the Bellman-Ford algorithm is run. In this case, the value of d [a] is 1, greater than the value of-2 of d [c] + W (C, A). Therefore, d [a] can be relaxed to-2, then d [B] can be relaxed to-5, d [c] can be relaxed to-7. in the next cycle, d [a] can be updated to a smaller value. This process will never end. Therefore, after the iteration of the shortest path stage, we can test each edge (u, v) of edge set E) whether the relationship d [v]> d [u] + W (u, v) is satisfied to determine whether a negative weight loop exists. Ii. Basic Pascal implementation of the Bellman-Ford algorithm. See the bellmanford. Pas file. 3. Optimization Based on basic algorithms. From the analysis of the Bellman-Ford algorithm, it is not difficult to see that the outer loop (number of iterations) | v |-1 is actually the upper limit. We can see from the above proof that the number of iterations required is equal to the height of the shortest path tree. If there is no negative weight loop, the height of the shortest path tree on average should be much smaller than | v |-1. In this case, iterations of the excess Shortest Path Tree height are a waste of time. Therefore, optimization can be performed in sequence. From the details, if the relaxation operation of the first row in the algorithm description is not executed in a certain iteration, all the edges of the iteration are not relaxed. I can prove it (How can I prove it ?): After that, all the edges in the edge set do not need to be relaxed, so that the iteration process can be completed in advance. In this way, the optimization measures are very simple. Set a Boolean flag variable relaxed. The initial value is false. In an in-layer loop, set relaxed to true only when the side is successfully relaxed. If no side is relaxed, the outer loop is terminated early. 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 for I: = 1 to NV Do D [I]: = max; d [s]: = 0; for I: = 1 to nv-1 do begin relaxed: = false; for J: = 1 to 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; if not relaxed then break; end; for I: = 1 to ne do if d [edges [J]. e]> d [edges [J]. s] + edges [J]. W then exit (false); exit (True); end; what is the effect of seemingly ordinary optimization? Studies have shown that for the average condition of randomly generated data, the formula for estimating time complexity is 1.13. | E | if | E | <| v | 0.95 * | E | * LG | v | if | E |> | v | optimized algorithm. when processing test data with a negative weight loop, because each time an edge is relaxed, the relaxed is set to true, so it is impossible to terminate the outer loop in advance. This corresponds to the worst case, and the time complexity is still O (VE ). The time complexity of the optimized algorithm is similar to that of the Dijkstra Algorithm Optimized using the binary heap, while the encoding complexity is far lower than that of the latter. In addition, the bellman-Ford algorithm can solve the shortest path problem in various edge value weights, so it is still very good. For details about the usaco 3.2.6 program, see bellmanford_1.pas 4. spfa is an excellent algorithm for finding the shortest path. Spfa's key to optimizing the Bellman-Ford algorithm is to realize that only those points that change the Distance Estimation value in the previous relaxation, this may cause a change in the distance estimation value of their adjacent contacts. Therefore, a first-in-first-out queue is used to store vertices that are successfully relaxed. Initially, the source node s enters the queue. When the queue is not empty, take out the first vertex and relax its adjacent contacts. If a neighboring contact is successfully relaxed and is not in the queue, the contact is queued. After a limited number of relaxation operations, the queue is empty and the algorithm ends. To implement the spfa algorithm, a first-in-first-out queue and an array mark indicating whether the vertex is in the queue. A critical table is used to store the adjacent points of a vertex. Programs are stored in spfa. Pas. Take question 2 of usaco 3.2.6 as an example. Programs written in an adjacent table. Note that spfa works normally only when the image does not have a negative weight loop. If the graph has a negative weight loop, the vertices on the negative weight loop cannot converge, and there are always vertices in the queue and out of the queue. In this case, the spfa cannot end normally. There are many solutions to determine the negative weight loop. The most widely spread in the world is to record the number of entries in each node, exceeds | v | times indicates that there is a negative weight. Another way is to record the location of the node in the path, ord [I], in each update, ord [I] = ord [x] + 1. If it exceeds | v |, it indicates a negative circle ..... there are still many other methods, but I think the most widely spread method is the slowest ....... it is difficult to accurately estimate the time complexity of spfa. Generally, it is considered that it is O (KE), K is a constant. 5. time efficiency measurement the Bellman-Ford algorithm described above and two optimizations, what is the result of analyzing the time complexity in theory and testing with actual data? To this end, we chose usaco 3.2.6. The time efficiency of spfa is very high. In addition, spfa programming is much more complex than dijksta + heap optimization. Vi. Conclusion The optimized Bellman-Ford algorithm is a very Optimized Algorithm for Finding the single-source shortest path. The spfa time efficiency is better than the first optimization method, however, the encoding complexity of the first optimization form is lower than that of spfa. The programming complexity of the Two optimization forms is lower than that of the Dijkstra algorithm. If a negative weight loop exists, the first optimization mode is recommended. Otherwise, spfa is recommended.

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.