Network flow에서 최대 유량을 찾는 알고리즘이 Maximum Flow이다. 네트워크(그래프)가 주어지고 이 그래프는 각 간선에 흐를 수 있는 최대 유량이 주어지는데, 이를 Capacity라 하자. 아래 그림은 Source에서 Sink까지 흐를 수 있는 최대 유량을 보여준다. 여기서 Source는 물을 흘려주는 시작점을 뜻하고(X에 해당), Sink는 물이 최종적으로 도착하는 도착점을 뜻한다(Y에 해당).
const int INF=987654321;
int V;
int capacity[MAX_V][MAX_V], flow[MAX_V][MAX_V];
int nf(int source, int sink) {
memset(flow, 0, sizeof(flow));
int totalFlow=0;
while(true) {
vector < int > parent(MAX_V, -1);
queue <int> q;
parent[source]=source;
q.push(source);
while(!q.empty()) {
int here=q.front(); q.pop();
for(int there=0; there<V; ++there)
if(capacity[here][there]-flow[here][there]>0 && parent[there]==-1) {
q.push(there);
parent[there]=here;
}
}
if(parent[sink]==-1) break;
int amount=INF;
for(int p=sink; p!=source; p=parent[p])
amount=min(capacity[parent[p]][p]-flow[parent[p]][p],amount);
for(int p=sink; p!=source; p=parent[p]) {
flow[parent[p]][p]+=amount;
flow[p][parent[p]]-=amount;
}
totalFlow+=amount;
}
return totalFlow;
}
// code by JM BOOK매칭으로 이어짐