コード例 #1
0
        /// <summary>
        /// We can get the min cut using the max-flow min-cut theorm, which states that the max flow in a n/w = min-cut of the network.
        /// Steps:
        /// 1. Run FordFulkerson algo to get the residual graph.
        /// 2. Find the set of vertices that can be reached from the source vertex.
        /// 3. Get all the edges from vertices in this set to the vertices which are not present in this set.
        /// this will give all the edges of the min cut.
        ///
        /// The running time of this algo is same as the ford fulkerson's max flow algo with edward karp optimization
        /// O(V(E^2))
        /// </summary>
        /// <param name="graph">graph object</param>
        /// <param name="startId">id of the source vertex</param>
        /// <param name="endId">id of the sink vertex</param>
        /// <returns></returns>
        public List <string> GetMinCut(GraphWithFwdEdges graph, string startId, string endId)
        {
            List <string> setOfEdges = new List <string>();

            // 1. Run FordFulkerson algo to get the residual graph.
            FordFulkersonEdmondKarp ffkp = new FordFulkersonEdmondKarp();

            ffkp.GetTheMaxFlow(graph, startId, endId);

            // 2. Find the set of vertices that can be reached from the source vertex.
            Dictionary <GraphVertex, bool> setOfVerticesFromSource = GetAllVerticesThatCanBeReachedFromSrc(graph, startId);

            // 3.Get all the forward edges from vertices in this set to the vertices which are not present in this set.
            foreach (GraphVertex vertex in setOfVerticesFromSource.Keys)
            {
                foreach (GraphVertex neighbour in vertex.Neighbours)
                {
                    string edgeId = vertex.Id + "#" + neighbour.Id;
                    if (graph.IsEdgeFwd[edgeId] && !setOfVerticesFromSource.ContainsKey(neighbour))
                    {
                        // the graph edge should not have 0 flow and also should not be present in the current set(found after the cut)
                        setOfEdges.Add(edgeId);
                    }
                }
            }

            return(setOfEdges);
        }
コード例 #2
0
        public static void TestFordFulkersonEdmondKarp()
        {
            FordFulkersonEdmondKarp ffek = new FordFulkersonEdmondKarp();

            /*
             * (A)<------3--------(B)
             | X               /x
             |  \             / |
             |    3         4   |
             |      \      /    |
             | 3        \  x      |
             |         (C)      1
             |       /   \      |
             |     /       \    |
             |    1          2  |
             |  x             x |
             | (D)-------2------>(E)
             |                  |
             |                  |
             |                  |
             | 6                  1
             |                  |
             | x                  X
             | (F)-------9------->(G)
             */
            Graph graph = new Graph();

            graph.AddEdge("A", "B", 3);
            graph.AddEdge("B", "A", 0);
            graph.AddEdge("A", "D", 3);
            graph.AddEdge("D", "A", 0);
            graph.AddEdge("D", "E", 2);
            graph.AddEdge("E", "D", 0);
            graph.AddEdge("E", "B", 1);
            graph.AddEdge("B", "E", 0);
            graph.AddEdge("C", "A", 3);
            graph.AddEdge("A", "C", 0);
            graph.AddEdge("B", "C", 4);
            graph.AddEdge("C", "B", 0);
            graph.AddEdge("C", "D", 1);
            graph.AddEdge("D", "C", 0);
            graph.AddEdge("C", "E", 2);
            graph.AddEdge("E", "C", 0);
            graph.AddEdge("D", "F", 6);
            graph.AddEdge("F", "D", 0);
            graph.AddEdge("F", "G", 9);
            graph.AddEdge("G", "F", 0);
            graph.AddEdge("E", "G", 1);
            graph.AddEdge("G", "E", 0);

            int maxFlow = ffek.GetTheMaxFlow(graph, "A", "G");

            Console.WriteLine("The max flow in the graph is {0}", maxFlow);
        }