Пример #1
0
        public Edmonds_Karp(WeightGraph weightGraph, int s, int t)
        {
            _weightGraph = weightGraph;
            _s           = s;
            _t           = t;
            if (!weightGraph.Directed)
            {
                throw new Exception("directed graph can be supported");
            }
            if (_weightGraph.V < 2)
            {
                throw new Exception("vertex must be more than 2");
            }
            _weightGraph.ValidateNumber(s);
            _weightGraph.ValidateNumber(t);
            if (s == t)
            {
                throw new Exception("source point and sink point can't be same");
            }
            _residualQuantityGraph = new WeightGraph(_weightGraph.V, true);

            for (int v = 0; v < _weightGraph.V; v++)
            {
                foreach (var w in _weightGraph.GetAllContiguousEdge(v))
                {
                    int weight = _weightGraph.GetWeight(v, w);
                    _residualQuantityGraph.AddEdge(v, w, weight);
                    _residualQuantityGraph.AddEdge(w, v, 0);
                }
            }

            while (true)
            {
                List <int> path = GetArgumentingPath();
                if (path.Count == 0)
                {
                    break;
                }
                int f = int.MaxValue;
                for (int i = 1; i < path.Count; i++)
                {
                    int v = path[i - 1];
                    int w = path[i];
                    f = Math.Min(f, _residualQuantityGraph.GetWeight(v, w));
                }
                MaxFlow += f;
                for (int i = 1; i < path.Count; i++)
                {
                    int v                = path[i - 1];
                    int w                = path[i];
                    int weight           = _residualQuantityGraph.GetWeight(v, w);
                    int residualQuantity = _residualQuantityGraph.GetWeight(w, v);
                    _residualQuantityGraph.SetWeight(v, w, weight - f);
                    _residualQuantityGraph.SetWeight(w, v, residualQuantity + f);
                }
            }
        }