Ejemplo n.º 1
0
        public void EdmondKarp_Smoke_Test()
        {
            var graph = new WeightedDiGraph <char, int>();

            graph.AddVertex('S');
            graph.AddVertex('A');
            graph.AddVertex('B');
            graph.AddVertex('C');
            graph.AddVertex('D');
            graph.AddVertex('T');

            graph.AddEdge('S', 'A', 10);
            graph.AddEdge('S', 'C', 10);

            graph.AddEdge('A', 'B', 4);
            graph.AddEdge('A', 'C', 2);
            graph.AddEdge('A', 'D', 8);

            graph.AddEdge('B', 'T', 10);

            graph.AddEdge('C', 'D', 9);

            graph.AddEdge('D', 'B', 6);
            graph.AddEdge('D', 'T', 10);

            var algo = new EdmondKarpMaxFlow <char, int>(new EdmondKarpOperators());

            var result = algo.ComputeMaxFlow(graph, 'S', 'T');

            Assert.AreEqual(result, 19);
        }
Ejemplo n.º 2
0
        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(graph, 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);
        }
Ejemplo n.º 3
0
        public void EdmondKarp_AdjacencyMatrixGraph_Smoke_Test()
        {
            var graph = new Advanced.Algorithms.DataStructures.Graph.AdjacencyMatrix.WeightedDiGraph <char, int>();

            graph.AddVertex('S');
            graph.AddVertex('A');
            graph.AddVertex('B');
            graph.AddVertex('C');
            graph.AddVertex('D');
            graph.AddVertex('T');

            graph.AddEdge('S', 'A', 10);
            graph.AddEdge('S', 'C', 10);

            graph.AddEdge('A', 'B', 4);
            graph.AddEdge('A', 'C', 2);
            graph.AddEdge('A', 'D', 8);

            graph.AddEdge('B', 'T', 10);

            graph.AddEdge('C', 'D', 9);

            graph.AddEdge('D', 'B', 6);
            graph.AddEdge('D', 'T', 10);

            var algorithm = new EdmondKarpMaxFlow <char, int>(new EdmondKarpOperators());

            var result = algorithm.ComputeMaxFlow(graph, 'S', 'T');

            Assert.AreEqual(result, 19);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get Max Match from Given BiPartitioned Graph
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="partitions"></param>
        /// <returns></returns>
        private List <MatchEdge <T> > GetMaxBiPartiteMatching(Graph <T> graph,
                                                              Dictionary <int, List <T> > partitions)
        {
            //add unit edges from dymmy source to group 1 vertices
            var dummySource = operators.GetRandomUniqueVertex();

            if (graph.Vertices.ContainsKey(dummySource))
            {
                throw new Exception("Dummy vertex provided is not unique to given graph.");
            }

            //add unit edges from group 2 vertices to sink
            var dummySink = operators.GetRandomUniqueVertex();

            if (graph.Vertices.ContainsKey(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>(operators);

            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);
        }