public List <MinCutEdge <T> > ComputeMinCut(WeightedDiGraph <T, W> graph, T source, T sink) { var edmondsKarpMaxFlow = new EdmondKarpMaxFlow <T, W>(operators); var maxFlowResidualGraph = edmondsKarpMaxFlow .computeMaxFlowAndReturnResidualGraph(graph, source, sink); //according to Min Max theory //the Min Cut can be obtained by Finding edges //from Reachable Vertices from Source //to unreachable vertices in residual graph var reachableVertices = getReachable(maxFlowResidualGraph, source); var result = new List <MinCutEdge <T> >(); foreach (var vertex in reachableVertices) { foreach (var edge in graph.Vertices[vertex].OutEdges) { //if unreachable if (!reachableVertices.Contains(edge.Key.Value)) { result.Add(new MinCutEdge <T>(vertex, edge.Key.Value)); } } } return(result); }
public List <MinCutEdge <T> > ComputeMinCut(IDiGraph <T> graph, T source, T sink) { if (this.@operator == null) { throw new ArgumentException("Provide an operator implementation for generic type W during initialization."); } if (!graph.IsWeightedGraph) { if ([email protected]() != typeof(int)) { throw new ArgumentException("Edges of unweighted graphs are assigned an imaginary weight of one (1)." + "Provide an appropriate IFlowOperators<int> operator implementation during initialization."); } } var edmondsKarpMaxFlow = new EdmondKarpMaxFlow <T, W>(@operator); var maxFlowResidualGraph = edmondsKarpMaxFlow .computeMaxFlowAndReturnResidualGraph(graph, source, sink); //according to Min Max theory //the Min Cut can be obtained by Finding edges //from Reachable Vertices from Source //to unreachable vertices in residual graph var reachableVertices = getReachable(maxFlowResidualGraph, source); var result = new List <MinCutEdge <T> >(); foreach (var vertex in reachableVertices) { foreach (var edge in graph.GetVertex(vertex).OutEdges) { //if unreachable if (!reachableVertices.Contains(edge.TargetVertexKey)) { result.Add(new MinCutEdge <T>(vertex, edge.TargetVertexKey)); } } } return(result); }
/// <summary> /// Get Max Match from Given BiPartitioned Graph. /// </summary> private List <MatchEdge <T> > getMaxBiPartiteMatching(IGraph <T> graph, Dictionary <int, List <T> > partitions) { //add unit edges from dymmy source to group 1 vertices var dummySource = @operator.GetRandomUniqueVertex(); if (graph.ContainsVertex(dummySource)) { throw new Exception("Dummy vertex provided is not unique to given graph."); } //add unit edges from group 2 vertices to sink var dummySink = @operator.GetRandomUniqueVertex(); if (graph.ContainsVertex(dummySink)) { throw new Exception("Dummy vertex provided is not unique to given graph."); } var workGraph = createFlowGraph(graph, dummySource, dummySink, partitions); //run ford fulkerson using edmon karp method var fordFulkerson = new EdmondKarpMaxFlow <T, int>(@operator); var flowPaths = fordFulkerson .ComputeMaxFlowAndReturnFlowPath(workGraph, dummySource, dummySink); //now gather all group1 to group 2 edges in residual graph with positive flow var result = new List <MatchEdge <T> >(); foreach (var path in flowPaths) { result.Add(new MatchEdge <T>(path[1], path[2])); } return(result); }