Network stream-based Dinic Algorithm

Source: Internet
Author: User
The basic steps of the Dinic algorithm are as follows: 1) Calculate the hierarchy chart of the residual network. We define h [I] as the minimum edge number from vertex I to source S, and find the h value of all vertices. The vertex with the same h [] value belongs to the same layer, this is the hierarchy of the network. 2) perform BFS augmented on the hierarchy chart until there is no augmented path. In this case, the vertices in the obtained augmented path are layered, and two vertices in the path cannot belong to the same layer, that is, h [I] = h [j] (I! = J ). At the same time, after obtaining the hierarchy chart, we can perform multiple augmented operations on the hierarchy chart. 3) Repeat 1 and 2. Until there is no augmented path. Minimum Cut: for the two vertices in the graph (generally the Source and Sink vertices), if you remove some edges in the graph, if they cannot be connected, the set composed of these edges is called cut. If the edge has the right value, the minimum cut refers to a cut with the smallest sum of the weights. Maximum Flow minimum cut: Used in the network, the total traffic does not exceed the maximum value that can be carried by the link, and the minimum traffic is allowed on each sub-path. For any graph with only one source vertex and one sink vertex, the maximum flow from the Source Vertex to the sink vertex is equal to the minimum cut. Struct {int u, v, next; double c;} bf [2 * eMax]; // edge information int ne, head [nMax]; // head [I] records the id of the last edge starting with I, that is, the last edge starting with I is bf [head [I], bf [head [I]. next is the penultimate edge starting with I, and so on int cur [nMax], ps [nMax], dep [nMax]; // dep [I] records the level of the I point. Void addEdge (int u, int v, double c) {// Edge Addition of dinic, which is a bit different. Bf [ne]. u = u; bf [ne]. v = v; bf [ne]. c = c; bf [ne]. next = head [u]; head [u] = ne ++; bf [ne]. u = v; bf [ne]. v = u; bf [ne]. c = 0; bf [ne]. next = head [v]; head [v] = ne ++;} double dinic (int s, int t) // dinic template: the source point is s, the sink point is t {double tr, res = 0; int I, j, k, f, r, top; while (1) {memset (dep,-1, sizeof (dep); for (f = dep [ps [0] = s] = 0, r = 1; f! = R;) for (I = ps [f ++], j = head [I]; j = bf [j]. next) if (bf [j]. c & dep [k = bf [j]. v] =-1) {dep [k] = dep [I] + 1; ps [r ++] = k; if (k = t) {f = r; break ;}}if (dep [t] =-1) break; memcpy (cur, head, sizeof (cur); I = s, top = 0; while (1) {if (I = t) {for (tr = inf, k = 0; k <top; k ++) if (bf [ps [k]. c <tr) tr = bf [ps [f = k]. c; for (k = 0; k <top; k ++) {bf [ps [k]. c-= tr; bf [ps [k] ^ 1]. c + = tr;} I = Bf [ps [top = f]. u; res + = tr; // the current maximum stream, which is accumulated each time.} For (j = cur [I]; cur [I]; j = cur [I] = bf [cur [I]. next) if (bf [j]. c & dep [I] + 1 = dep [bf [j]. v]) break; if (cur [I]) {ps [top ++] = cur [I]; I = bf [cur [I]. v; // I = bf [cur [I]. v cannot be written as bf [cur [I]. v = I, the brain is exhausted once.} Else {if (top = 0) break; dep [I] =-1; I = bf [ps [-- top]. u ;}} return res ;}

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.