Beispiel #1
0
        private void btnFindMaxFlow_Click(object sender, EventArgs e)
        {
            ParseData();
            FindAugmentingPathMethod Method = (FindAugmentingPathMethod)Enum.Parse(typeof(FindAugmentingPathMethod), cbMethod.SelectedIndex.ToString());

            var Sourse = Nodes[0];
            var Sink   = Nodes[Nodes.Count - 1];

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            ford = new FordFulkerson(Nodes, Edges, Sourse, Sink);
            float maxFlow = ford.Run(Method);

            stopWatch.Stop();

            // Get the elapsed time as a TimeSpan value.
            TimeSpan ts = stopWatch.Elapsed;

            // Format and display the TimeSpan value.
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}",
                                               ts.Hours, ts.Minutes, ts.Seconds,
                                               ts.Milliseconds);

            tbMaxFlow.Text = maxFlow.ToString() + " # " + ts.Ticks;

            if (Trials > 0)
            {
                Times[cbMethod.SelectedIndex].Add(ts.Ticks);
            }
            tbAvgTime.Text = Times[cbMethod.SelectedIndex].Count > 0 ? Times[cbMethod.SelectedIndex].Average().ToString() : "0";
            ShowAugmentingPaths();
            Trials++;
        }
Beispiel #2
0
        List <Edge> FindAugmentingPath(Node root, Node target, FindAugmentingPathMethod Method, ref int totalTrials)
        {
            switch (Method)
            {
            case FindAugmentingPathMethod.BFS:
                return(Bfs(root, target, ref totalTrials));

            case FindAugmentingPathMethod.DFS:
                return(DFS(root, target, ref totalTrials));

            case FindAugmentingPathMethod.Fatest:
                return(FAT(root, target, true, ref totalTrials));

            case FindAugmentingPathMethod.Thinnest:
                return(FAT(root, target, false, ref totalTrials));

            default:
                return(Bfs(root, target, ref totalTrials));
            }
        }
Beispiel #3
0
        private void btnGlobal_Click(object sender, EventArgs e)
        {
            ParseData();
            FindAugmentingPathMethod Method = (FindAugmentingPathMethod)Enum.Parse(typeof(FindAugmentingPathMethod), cbMethod.SelectedIndex.ToString());

            var                    Sourse    = Nodes[0];
            List <string>          resutls   = new List <string>();
            Dictionary <int, Node> NodesCopy = new Dictionary <int, Node>(Nodes);

            foreach (Node Sink in NodesCopy.Values)
            {
                Sourse = Nodes[0];
                var t = Nodes[Sink.Id];
                ford = new FordFulkerson(Nodes, Edges, Sourse, t);
                float maxFlow = ford.Run(Method);

                resutls.Add(Sourse.Name + "--" + maxFlow + "-->" + Sink.Name);
                ParseData();
            }
        }
Beispiel #4
0
        public float Run(FindAugmentingPathMethod Method)
        {
            var flow           = 0f;
            int totalTrials    = 0;
            var AugmentingPath = FindAugmentingPath(Source, Sink, Method, ref totalTrials);
            var loops          = 0;

            while (AugmentingPath != null && AugmentingPath.Count > 0)
            {
                ++loops;
                var minCapacity = MaxValue;
                foreach (var edge in AugmentingPath)
                {
                    if (edge.Capacity < minCapacity)
                    {
                        minCapacity = edge.Capacity; // update
                    }
                }

                if (minCapacity == MaxValue || minCapacity < 0)
                {
                    throw new Exception("minCapacity " + minCapacity);
                }

                AugmentingPaths.Add(new Path(AugmentingPath, minCapacity).ToString());

                ComputeResidualNetwork(AugmentingPath, minCapacity);
                flow += minCapacity;

                AugmentingPath = FindAugmentingPath(Source, Sink, Method, ref totalTrials);
            }

            // max flow
            //PrintLn("\n** Max flow = " + flow);

            // min cut
            //PrintLn("\n** Min cut");
            //FindMinCut(Source);

            return(flow);
        }