Search code examples
javaalgorithmgraph-algorithmpseudocode

Finding the set of MinCut edges in a Preflow Push Network Flow Algorithm


I have an implementation of the preflow push network flow algorithm that returns the maximum flow of a flow network. What I need is to identify the set of saturated edges that form the cut in the graph.

In my current implementation, I look for saturated forward edges with empty backward edges in the graph for which the distance from the edge source to the edge target is greater than or equal to one (ie, the source has a greater height than the target). In case there is only one possible set of edges in the graph that satisfies the min cut, the algorithm works ok, but if the algorithm can find more than one cut in the graph then it returns all the saturated edges in the graph. I have copied my code below. Any help is highly appreciated.

public Set<Edge> cutEdges(){
    for (String v_name : vertices.keySet()){
        int v_id = vertices.get(v_name);
        ArrayList<Edge> veList = residual_edges.get(v_id);
        for (Edge ve : veList){
            boolean reverseFound = false;
            EdgeData vinfo = (EdgeData) ve.getData();
            if (vinfo.getAvailable() == 0){
                Vertex temp1 = ve.getFirstEndpoint();
                Vertex temp2 = ve.getSecondEndpoint();
                int u_id = (v_id != vertices.get(temp1.getName().toString()) ? 
                        vertices.get(temp2.getName().toString()) : vertices.get(temp1.getName().toString()));
                ArrayList<Edge> ueList = residual_edges.get(u_id);
                for (Edge ue : ueList){
                    EdgeData uinfo = (EdgeData) ue.getData();
                    if (ue.getFirstEndpoint().getName().toString().equals(temp2.getName().toString()) && 
                            ue.getSecondEndpoint().getName().toString().equals(temp1.getName().toString())){                            
                        if (((VertexData)ue.getFirstEndpoint().getData()).getPreflowHeight() - 
                                ((VertexData)ue.getSecondEndpoint().getData()).getPreflowHeight() >= 1){
                            if (uinfo.getFlow() == 0)
                            continue;
                        }
                        reverseFound = true;
                        break;
                    }
                }
                if (!reverseFound){
                    if (((VertexData)temp1.getData()).getPreflowHeight() - 
                            ((VertexData)temp2.getData()).getPreflowHeight() >= 1)
                        cut_edges.add(ve);
                }
            }
        }
    }
    return cut_edges;
}

Solution

  • I'll copy my answer from the question How can I find the minimum cut on a graph using a maximum flow algorithm?:

    From the source vertex, do a depth-first search along edges that still have residual capacity (i.e., non-saturated edges). The cut consists of all edges that were "seen" (i.e., are incident on a visited vertex), but were not traversed since they are saturated. As you noted, there might be other saturated edges that are not part of the minimum cut.