Пример #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);
                }
            }
        }
Пример #2
0
        public BinaryPartGraphMatch(IAdjacency iAdjacency)
        {
            _iAdjacency = iAdjacency;
            BinaryPartitionDetection binaryPartitionDetection = new BinaryPartitionDetection(iAdjacency);

            if (!binaryPartitionDetection.IsBinaryPartition)
            {
                throw new Exception("you must use two partite graph");
            }
            int[]       colors      = binaryPartitionDetection.Colors;
            WeightGraph weightGraph = new WeightGraph(iAdjacency.V + 2, true);

            for (int v = 0; v < iAdjacency.V; v++)
            {
                if (colors[v] == 0)
                {
                    weightGraph.AddEdge(iAdjacency.V, v, 1);
                }
                if (colors[v] == 1)
                {
                    weightGraph.AddEdge(v, iAdjacency.V + 1, 1);
                }
                foreach (var w in iAdjacency.GetAllContiguousEdge(v))
                {
                    if (colors[v] == 0)
                    {
                        weightGraph.AddEdge(v, w, 1);
                    }
                    else if (colors[v] == 1)
                    {
                        weightGraph.AddEdge(w, v, 1);
                    }
                }
            }
            Edmonds_Karp edmondsKarp = new Edmonds_Karp(weightGraph, iAdjacency.V, iAdjacency.V + 1);

            MaxMatch       = edmondsKarp.MaxFlow;
            IsPerfectMatch = MaxMatch * 2 == iAdjacency.V;
        }