Ford-Fulkerson Algorithm for Maximum Flow Problem

Maximum transfer volume

There are two computers s and t in the network, and now want to transfer data from s to t. There are a total of N computers in the network, some of which are connected with a unidirectional communication cable, and each communication cable has a corresponding maximum amount of data that can be transmitted within 1 second. How much data can s transfer to t in 1 second when there is no data transfer between other computers?

Think of the above network as a directed graph. Each edge e in the graph has a corresponding maximum possible data transfer amount. This turns the problem into the following form:

(1) Denote the actual data transmission amount corresponding to each edge as f(e).

(2) The amount of transmission should meet the following restrictions:

            0<=f(e)<=c(e)

(3) The data will neither increase nor decrease during the transmission process, and the amount of data received and the amount of data sent should be equal.

The goal is: to maximize the amount of data emitted from s.

We call the f that maximizes the transmission volume as the maximum flow, and the problem of solving the maximum flow is the maximum flow problem. Furthermore, we call c the capacity of the edge, f the flow of the edge, s the source, and t the sink.

(1) Greedy algorithm

(1) Find a path from s to t that only passes through the edges of f(e)<c(e);

(2) If there is no path satisfying the condition, end the algorithm. Otherwise, increase as much as possible along the path, returning to step (1).


But the results obtained are not optimal. Calculate the difference between the flow of the optimal result and the result of the greedy algorithm to get the difference graph. It can be found that if the flow with a difference of -1 is pushed back, a new flow is obtained.

So the greedy algorithm is improved as follows:

(1) Find a path from s to t using only the reverse edge rev(e) corresponding to e that satisfies f(e)<c(e) or f(e)>0.

(2) If there is no path that satisfies the condition, end. Otherwise, increase the flow as much as possible along the path and return to step (1).

Note: My understanding: You can first find the result according to the greedy algorithm, then add a reverse edge to the edge whose flow is not 0 in the result, and continue to use the algorithm to see if a new path can be found.

Residual network: the graph composed of the f(e)<c(e) considered and the reverse edge rev((e) corresponding to e that satisfies f(e)>0

The following is an example of an adjacency list implementation of the Ford-Fulkerson algorithm. Instead of saving the value of f(e), the value of c(e) is directly changed.

//The structure used to represent the edge (end point, capacity, reverse edge)

struct edge{int to,cap,rev};

vector<edge> G[MAX_V];//The adjacency list representation of the graph

bool used[MAX_V];//Access flag used in DFS

//Add an edge with capacity cap from s to t to the graph

void add_edge(int from,int to,int cap){

  G[from].push_back{(edge){to,cap,G[to].size()}};

  G[to].push_back{(edge){from,0,G[from].size-1}};

}

//find augmentation path through DFS

int dfs (int v, int t, int f) {

if(v==t) return f;

used[v]=true;

for(int i=0;i<G[v].size();i++){

edge & e = G [v] [i];

if(!used[G.to]&&e.cap>0){

int d=dfs(e.to,t,min(f,e.cap));

     if (d>0){

e.cap = e.cap-d;

G[e.to][e.rev].cap+=d;

return d;

}

}

}

return 0;

}

// Solve the maximum flow from s to t

int max_flow(int s,int t){

int flow=0;

for(; ;){

memset(used,0,sizeof(used));

int f=dfs(s,t,INF);

int(f==0) return flow;

flow+=f;

}

}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325684907&siteId=291194637