Maximum stream (network stream) BASICS (editing)

Source: Internet
Author: User
Network stream Summary

Learn more: lrj algorithm competition entry classic

Related Concepts:

Maximum stream: (maximum-Flow Problem)

Some items are shipped from the center of the Source Vertex s to the sink vertex T.

There is a maximum number of shipping items between every two points.

Find the maximum number of items that can be shipped from S to T.


Capacity: For an edge (u, v), its maximum number of items (the maximum number of items that can be shipped) is referred to as capacity ),

Mark as C (u, v) (for nonexistent edges (u, v), C (u, v) = 0)

Traffic: the actual number of items delivered is called flow)

Rule: f (u, v) and F (v, u) can have at most one positive number (both can be 0), and f (u, v) =-F (v, U)

PS: The left side of the image indicates the actual Shipping item, and the right side indicates the maximum capacity.


Conclusion: For any node except S and T, U, Σ f (u, v) = 0 (some F is negative ).

(U, v) ε E


Among the largest stream problems: capacity C and traffic F meet three requirements

Capacity LimitF (u, v) <= C (u, v)

Oblique Symmetry: F (u, v) =-f (u, v)

Traffic balance: For any node except S and T, U, Σ f (u, v) = 0 (some F is negative ).

(U, v) ε E

Objective: To maximize | f | = Σ F (S, v) = Σ f (u, T) refers to the net traffic flowing out from point S (= the net traffic flowing into point T)

(S, v) ε E, (u, T) ε E


Augmented Path Algorithm:

Residual Volume: The capacity difference on each side (known as residual traffic, or residual volume ),

For example, the content of V2 to V4 In the second figure above is 14-11 = 3; the content of V4 to V2 is 0-(-11) = 11.

The algorithm is based on facts:

Any directed road from S to T in the residual network corresponds to the augmented path in the source image. [PS: It's okay if you don't understand this term ].

You only need to find the minimum D of all the residues in the road and increase the traffic on all the corresponding edges to D. This process is called augmented.

 

That is to say, as long as there is traffic from the start point S to the end point T, find the minimum residual traffic d

Then, this D must satisfy every edge of the path. Otherwise, such D cannot be found.

Then the traffic on each side of the path increases by d, and the total traffic increases by D.

Then, continue searching until it cannot be found.

It is not difficult to prove that if the traffic before the scale-up meets three conditions, then the scale-up will be satisfied.

Obviously, as long as there is an increasing path in the residual network, the traffic can increase.

Inverse Proposition: If there is no augmented path in the residual network, the current stream is the largest stream. This is the famous augmented path theorem.


Q: How do I find the path? Dfs ms is slow, with BFS

 

Queue <int> q; memset (flow, 0, sizeof (flow); // The initial traffic is 0 f = 0; // The total initial traffic is 0 (;;) // BFS find augmented path {memset (A, 0, sizeof (a); // A [I]: minimum residue from the starting point S to the I [For () each time () when a [] clears 0 again, the array vis can be used to mark at the same time.] a [s] = inf; // the starting point of the residual amount of wireless Q. push (s); // start point for the while (! Q. empty () // BFS find augmented path {int u = Q. front (); // gets the first Q. pop (); // For (INT v = 1; v <= N; V ++) if (! A [v] & Cap [u] [v]> flow [u] [v]) // find a new node v {P [v] = u; q. push (V); // record the Father's Day of V and add the FIFO queue a [v] = min (A [u], cap [u] [v]-flow [u] [v]); // The minimum residue on the S-V path [This guarantees the final result, each path meets a [T]} if (a [T] = 0) break; // cannot be found, the current stream is already the largest stream [T is the end point] For (INT u = T; u! = S; u = P [u]) // go back from the sink {flow [p [u] [u] + = A [T]; // update forward flow [u] [p [u]-= A [T]; // update Reverse Flow} f + = A [T]; // update the total outbound traffic from S}

 

Recommended entry questions:

HDU 3549 Flow Problem]


Maximum stream Template:

 

Const int maxn = 20010; // maximum number of points const int maxm = 880010; // maximum number of sides const int INF = 0x3f3f3f3f; struct node {int from, to, next; int cap;} edge [maxm]; int tol; int head [maxn]; int Dep [maxn]; int gap [maxn]; // gap [x] = Y: dep [I] = X in the residual network is y int N; // n is the total number of points, including the source point and sink point void Init () {Tol = 0; memset (Head,-1, sizeof (head);} void addedge (int u, int V, int W) {edge [tol]. from = u; edge [tol]. to = V; edge [tol]. CAP = W; edge [tol]. Next = head [u]; head [u] = tol ++; edge [tol]. from = V; edge [tol]. to = u; edge [tol]. CAP = 0; edge [tol]. next = head [v]; head [v] = tol ++;} void BFS (INT start, int end) {memset (DEP,-1, sizeof (DEP )); memset (GAP, 0, sizeof (GAP); Gap [0] = 1; int que [maxn]; int front, rear; front = rear = 0; dep [end] = 0; que [rear ++] = end; while (front! = Rear) {int u = que [Front ++]; If (front = maxn) Front = 0; For (INT I = head [u]; I! =-1; I = edge [I]. Next) {int v = edge [I]. To; If (edge [I]. Cap! = 0 | Dep [v]! =-1) continue; que [rear ++] = V; If (Rear = maxn) rear = 0; Dep [v] = Dep [u] + 1; ++ gap [Dep [v] ;}} int SAP (INT start, int end) {int res = 0; BFS (START, end ); int cur [maxn]; int s [maxn]; int Top = 0; memcpy (cur, Head, sizeof (head); int u = start; int I; while (DEP [start] <n) {If (u = END) {int temp = inf; int inser; for (I = 0; I <top; I ++) if (temp> edge [s [I]. CAP) {temp = edge [s [I]. CAP; inser = I ;}for (I = 0; I <top; I ++) {edge [s [I ]. Cap-= temp; edge [s [I] ^ 1]. cap + = temp;} res + = temp; Top = inser; u = edge [s [Top]. from;} If (u! = End & gap [Dep [u]-1] = 0) // when a fault occurs, the break is not added. For (I = cur [u]; I! =-1; I = edge [I]. Next) if (edge [I]. Cap! = 0 & Dep [u] = Dep [edge [I]. To] + 1) break; if (I! =-1) {cur [u] = I; s [top ++] = I; u = edge [I]. to;} else {int min = N; for (I = head [u]; I! =-1; I = edge [I]. next) {If (edge [I]. CAP = 0) continue; If (min> Dep [edge [I]. to]) {min = Dep [edge [I]. to]; cur [u] = I ;}-- gap [Dep [u]; Dep [u] = min + 1; ++ gap [Dep [u]; if (u! = Start) u = edge [s [-- top]. From ;}} return res ;}

 

 When assigning values to edges, you can use addition to avoid duplicate edges!

//************************************** * ************* // Maximum stream template // initialization: G [] [], start, end //************************************* * **************** const int maxn = 110; const int INF = 0x3fffffff; int G [maxn] [maxn]; // storage edge capacity. When no edge is initialized, the value 0 int path [maxn], flow [maxn], start, end; int N; // Number of vertices. The value 0-n.n contains the Source Vertex and sink vertex. Queue <int> q; int BFS () {int I, T; while (! Q. empty () Q. pop (); // clear the queue memset (path,-1, sizeof (PATH); // initialize the path to-1 path [start] = 0 before each search; flow [start] = inf; // Endless streams can flow into q At the source point. push (start); While (! Q. empty () {T = Q. front (); q. pop (); If (t = END) break; // enumerate all vertices. If the start point of the vertex number changes, you can change it here for (I = 0; I <= N; I ++) {if (I! = Start & path [I] =-1 & G [T] [I]) {flow [I] = flow [T] <G [T] [I]? Flow [T]: G [T] [I]; q. push (I); path [I] = T ;}} if (path [end] =-1) Return-1; // The vertex cannot be found. No augmented path found return flow [end];} int edmonds_karp () {int max_flow = 0; int step, now, pre; while (step = BFS ())! =-1) {max_flow + = step; now = end; while (now! = Start) {pre = path [now]; G [pre] [now]-= step; G [now] [pre] + = step; now = pre ;}} return max_flow ;}

 

 

 

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.