Graphs/Ford-Fulkerson.cpp
// Try to find a valid S->T path that increases the flow
Try to find a valid S->T path that increases the flow
Try to find a valid S - > T path that increases the flow
int DFS(int curr, int T, int upMin)
{
    if(curr == T)
    {
        return upMin;
    }
    g[curr].visited = true;
    int sz = g[curr].adj.size();
    for(int i = 0; i < sz; i++)
    {
        int nxt = g[curr].adj[i];
        if(!g[nxt].visited && cap[curr][nxt] > 0)
        {
            int flowReached = DFS(nxt, T, min(upMin, cap[curr][nxt]));
            if(flowReached > 0)
            {
                , update capacities
                cap[curr][nxt] -= flowReached;
                cap[nxt][curr] += flowReached;
                g[curr].visited = false;
                return flowReached;
            }
        }
    }
    g[curr].visited = false;
    return 0;
}
int DFS ( int curr , int T , int upMin ) { if ( curr == T ) { return upMin ; } g [ curr ] . visited = true ; int sz = g [ curr ] . adj . size ( ) ; for ( int i = 0 ; i < sz ; i ++ ) { int nxt = g [ curr ] . adj [ i ] ; if ( ! g [ nxt ] . visited && cap [ curr ] [ nxt ] > 0 ) { int flowReached = DFS ( nxt , T , min ( upMin , cap [ curr ] [ nxt ] ) ) ; if ( flowReached > 0 ) { cap [ curr ] [ nxt ] -= flowReached ; cap [ nxt ] [ curr ] += flowReached ; g [ curr ] . visited = false ; return flowReached ; } } } g [ curr ] . visited = false ; return 0 ; }
Try to find a valid S->T path that increases the flow
Try to find a valid S - > T path that increases the flow
// Reached the sink // Reached the sink, update capacities