Shortest augmented Path Algorithm for Finding the maximum stream using distance labels

Source: Internet
Author: User

A classic algorithm is used to find the largest stream, that is, BFs is used to find the augmented path every time, to ensure that the augmented path is found to have the least number of arcs, that is, the so-called Edmonds-Karp Algorithm. It can be proved that the maximum short-circuit augmented time is not more than V * E, and the BFS time is O (e) each time ), therefore, the time complexity of Edmonds-Karp is O (V * E ^ 2 ).

If we can reduce the time complexity when looking for an augmented path, we can improve the efficiency of the algorithm. The shortest augmented Path Algorithm Using distance labels is like this. The so-called distance Label refers to the minimum number of arcs from a point to a sink point (another distance Label refers to the minimum number of arcs from the source point to the point, essentially no difference ). If the number of vertex I is d [I], the arc (I, j) that satisfies d [I] = d [J] + 1 is called the allowable arc, in addition, if only the arc is allowed during the augmented mode, the effect of "how to do this is the most short circuit" can be achieved. The initial label of each vertex can be obtained from the BFS of all the reverse edges of the vertex at the beginning. The problem is how to maintain the distance label during the augmented process.

The method for maintaining the distance label is as follows: when finding the augmented path and finding that a point does not allow the arc, set the distance label of the vertex to the minimum value of the distance label of the end point of all the arcs starting from it plus one. I will not prove the correctness of this method for maintaining distance labels.

Because the distance label exists, because "How to take is the shortest path", you can use DFS to find the augmented path and use a stack to save the arc of the current path. When the distance label of a point is changed, the arc pointing to it in the stack is definitely not allowed, so let it go out of the stack, and continue to use the arc endpoints at the top of the stack to expand. To change the time for finding the augmented path to O (V), another important optimization is to save the "current arc" for each vertex ": at the beginning, the current arc is the first arc in the adjacent table. When you search in the adjacent table, you can start from the current arc and find a allowed arc. Then, you can set this arc to the current arc; when changing the distance label, reset the current arc to the first arc of the adjacent table, another way to optimize the constant is to set the current arc to the arc that provides the minimum label when changing the distance label. The reason why the current arc is correctly written is that at any time we can ensure that there is no allowed Arc before the current arc in the adjacent table.

Another constant optimization is not to stack all vertices in the path after finding the path and adding the path, but to stack the bottleneck side and the subsequent side, this is based on the idea of the dinic algorithm. Note that the "current point" to be extended at any time should be the end point of the top point of the stack. This is indeed a constant optimization. Due to the existence of the current edge structure, we can certainly restore all the sides before the bottleneck edge in the path in O (n) time.

My program has made a lot of optimizations (even using placement New for optimization when dynamic memory allocation is involved), and eventually it can be about twice faster than the dinic written by WXS, however, the program only has more than one hundred and twenty lines. I am very satisfied with this result. The augmented distance label is indeed a very good and practical algorithm.

Sample program (usaco training ditch ):
Ditch. cpp

June 4, 2007 · filed under garden

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.