Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Stopwatch stopWatch          = new Stopwatch();
            var       exampleNetworkPath = Directory.GetCurrentDirectory() + "/" + TEST_EXAMPLES_DIRECTOR;
            var       filesPath          = Directory.GetFiles(exampleNetworkPath);

            foreach (string filePath in filesPath)
            {
                List <string> fileLines = TextFile.ReadFileLines(filePath);
                FlowGraph     flowGraph = new FlowGraph(fileLines);


                stopWatch.Start();
                var maxFlow = flowGraph.FordFulkerson();
                stopWatch.Stop();


                TimeSpan ts          = stopWatch.Elapsed;
                var      elapsedTime = (decimal)ts.Ticks * 100 / frequency;

                Console.WriteLine($"{maxFlow}; " + elapsedTime.ToString("F10"));
                stopWatch.Restart();
            }

            Console.ReadLine();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Lädt den Graphen aus der gegebenen Datei und führt auf diesem den FordFulkerson Algorithmus aus.
        /// <paramref name="fileName">Der Name der Datei welche den Graphen enthält.</paramref>
        /// </summary>
        public static void CallFordFulkerson(string fileName)
        {
            // Den Graphen aus der Datei laden.
            FlowGraph <string> graph = (FlowGraph <string>)ApplicationHelper.LoadGraph(fileName);

            // Ford-Fulkerson ausführen.
            FlowGraph <string> result = graph.FordFulkerson();

            // Das Ergebnis in eine Datei schreiben.
            string baseFileName = fileName.Split('.')[0];

            ApplicationHelper.WriteResult(baseFileName, "fordfulkersonResult_", "", result.Edges.Select((edge) => edge.ToString()));
        }
        public void FordFulkerson_must_work()
        {
            // Quelle und Senke erstellen.
            Vertex <string> source = new Vertex <string>("s");
            Vertex <string> target = new Vertex <string>("t");

            // alle weiteren Knoten des Graphens erstellen.
            Vertex <string> vA = new Vertex <string>("a");
            Vertex <string> vB = new Vertex <string>("b");
            Vertex <string> vC = new Vertex <string>("c");
            Vertex <string> vD = new Vertex <string>("d");

            // Den Graphen erstellen.
            FlowGraph <string> flowGraph = new FlowGraph <string>(source, target, new List <Vertex <string> >()
            {
                source, vA, vB, vC, vD, target
            });

            // Die Kanten des Graphens erstellen.
            flowGraph.AddEdge("s", "a", capacity: 5, flow: 5);
            flowGraph.AddEdge("s", "b", capacity: 7, flow: 3);
            flowGraph.AddEdge("a", "b", capacity: 7, flow: 3);
            flowGraph.AddEdge("a", "c", capacity: 4, flow: 2);
            flowGraph.AddEdge("b", "c", capacity: 3, flow: 3);
            flowGraph.AddEdge("b", "d", capacity: 3, flow: 3);
            flowGraph.AddEdge("c", "d", capacity: 4, flow: 0);
            flowGraph.AddEdge("c", "t", capacity: 5, flow: 5);
            flowGraph.AddEdge("d", "t", capacity: 6, flow: 3);

            FlowGraph <string> maxFlowGraph = flowGraph.FordFulkerson();

            // Die Flüsse über die Kanten überprüfen.
            Assert.AreEqual(5, maxFlowGraph.Edges[0].Flow);
            Assert.AreEqual(5, maxFlowGraph.Edges[1].Flow);
            Assert.AreEqual(1, maxFlowGraph.Edges[2].Flow);
            Assert.AreEqual(4, maxFlowGraph.Edges[3].Flow);
            Assert.AreEqual(3, maxFlowGraph.Edges[4].Flow);
            Assert.AreEqual(3, maxFlowGraph.Edges[5].Flow);
            Assert.AreEqual(2, maxFlowGraph.Edges[6].Flow);
            Assert.AreEqual(5, maxFlowGraph.Edges[7].Flow);
            Assert.AreEqual(5, maxFlowGraph.Edges[8].Flow);

            // Die Kantenflüsse für den nächsten Test auf 0 setzen.
            flowGraph.Edges.ForEach(e =>
            {
                e.Flow = 0;
            });

            maxFlowGraph = flowGraph.FordFulkerson();

            // Die Flüsse über die Kanten überprüfen.
            Assert.AreEqual(4, maxFlowGraph.Edges[0].Flow);
            Assert.AreEqual(6, maxFlowGraph.Edges[1].Flow);
            Assert.AreEqual(0, maxFlowGraph.Edges[2].Flow);
            Assert.AreEqual(4, maxFlowGraph.Edges[3].Flow);
            Assert.AreEqual(3, maxFlowGraph.Edges[4].Flow);
            Assert.AreEqual(3, maxFlowGraph.Edges[5].Flow);
            Assert.AreEqual(2, maxFlowGraph.Edges[6].Flow);
            Assert.AreEqual(5, maxFlowGraph.Edges[7].Flow);
            Assert.AreEqual(5, maxFlowGraph.Edges[8].Flow);
        }