Network maximum flow algorithm-EK algorithm, maximum flow ek Algorithm

Source: Internet
Author: User

Network maximum flow algorithm-EK algorithm, maximum flow ek Algorithm
Preface

The EK algorithm is the most basic algorithm for finding the largest stream in the network and a well-understood algorithm. It can be used to solve most of the largest stream problems.

However, due to time complexity constraints, such algorithms often have the risk of TLE.

Thoughts

Do you still remember the solution ideas we mentioned when introducing the largest stream?

For a network flow chart, find its smallest residual amount (the amount that can be extended) each time, and increase it.

That's right. The EK algorithm uses this idea to solve the problem.

Implementation

When implementing the EK algorithm, you need to traverse one side of the entire graph.

So how can we traverse it? BFS or DFS?

Because of the search order of DFS, some people with cancer problems will construct a data card for you. The specific method should be relatively simple, but I will not talk about it to prevent you from becoming such a person (# ^. ^ #)

So we choose BFS

When traversing an image, record the maximum value that can be extended, and record the edge through which the maximum value goes.

After traversal, we can expand the edge on the augmented Road.

Code

The question is here

In the code, I added some comments to some key points. If I do not understand them, please comment below: blush:

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <queue> using namespace std; const int MAXN = 2*1e6 + 10; const int INF = 1e8 + 10; inline char nc () {static char buf [MAXN], * p1 = buf, * p2 = buf; return p1 = p2 & (p2 = (p1 = buf) + fread (buf, 1, MAXN, stdin), p1 = p2 )? EOF: * p1 ++;} inline int read () {char c = nc (); int x = 0, f = 1; while (c <'0' | c> '9') {if (c = '-') f =-1; c = nc ();} while (c> = '0' & c <= '9') {x = x * 10 + c-'0'; c = nc ();} return x * f;} struct node {int u, v, flow, nxt;} edge [MAXN]; int head [MAXN]; int num = 0; // note that num must start from 0 inline void add_edge (int x, int y, int z) {edge [num]. u = x; edge [num]. v = y; edge [num]. flow = z; edge [num]. nxt = head [x]; head [x] = num ++;} inline void AddEdge (int x, int y, int Z) {add_edge (x, y, z); add_edge (y, x, 0); // do not forget to add the reverse edge} int N, M, S, T; int path [MAXN]; // The path through which int A [MAXN]; // The minimum traffic to this node is inline int EK () {int ans = 0; // The maximum stream while (true) // continuously seek the augmented path {memset (A, 0, sizeof (A); queue <int> q; // too lazy to write the queue... Q. push (S); A [S] = INF; while (q. size ()! = 0) {int p = q. front (); q. pop (); for (int I = head [p]; I! =-1; I = edge [I]. nxt) {if (! A [edge [I]. v] & edge [I]. flow) {path [edge [I]. v] = I; // record the path to facilitate the later expansion of A [edge [I]. v] = min (A [p], edge [I]. flow); // record the minimum traffic q. push (edge [I]. v) ;}} if (A [T]) break; // A small optimization} if (! A [T]) break; // there is no path that can be extended. Exit for (int I = T; I! = S; I = edge [path [I]. u) // move back to augmented {edge [path [I]. flow-= A [T]; edge [path [I] ^ 1]. flow + = A [T]; // use an exclusive OR operator to find the reverse edge. 0 ^ 1 = 1 1 ^ 1 = 0} ans + = A [T];} return ans;} int main () {# ifdef WIN32 freopen (". in "," r ", stdin); # else # endif memset (head,-1, sizeof (head); N = read (), M = read (), S = read (), T = read (); for (int I = 1; I <= M; I ++) {int x = read (), y = read (), z = read (); AddEdge (x, y, z);} printf ("% d", EK (); return 0 ;}

 

Performance Analysis

 

It is not difficult to see that the performance of this algorithm is still good,

But you can submit it here to know how fast this algorithm is (man ).

 

It can be proved that the time complexity of this algorithm is $ O (n * m ^ 2) $

General evidence:

In the worst case, we only need to increase one side each time, then we need to increase the number by $ S-1 $ times.

In BFS, due to the existence of reverse arc, the worst case is $ n * m $

The total time complexity is $ O (n * m ^ 2) $

 

Postscript

The EK algorithm ends here.

But how can I pass the loj question?

This will use the other algorithms we will talk about next.

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.