Ejemplo n.º 1
0
        public FordFulkersonAlgorithm(Graph Graph)
        {
            InnerGraph = (Graph)Graph.Clone();

            BreadthFirstSearch = new BreadthFirstSearchAlgorithm(InnerGraph);

            DepthFirstSearch = new DepthFirstSearchAlgorithm(InnerGraph);

            FoundPath = new Node[InnerGraph.NumberOfNodes];

            CapacityOfFlow = 0.0;
        }
Ejemplo n.º 2
0
        public bool FindMaximumFlowWithBFSRealization(Node Start, Node Target)
        {
            if (Start.Equals(Target))
            {
                CapacityOfFlow = 0.0;

                return(true);
            }

            Node CurrentNode = new Node();

            for (; BreadthFirstSearch.BreadthFirstSearch(Start, Target, FoundPath);)
            {
                double CurrentStreamCapacity = Infinity;

                for (int Index = Target.Index; Index != Start.Index; Index = FoundPath[Index].Index)
                {
                    CurrentNode = FoundPath[Index];

                    CurrentStreamCapacity = Min(CurrentStreamCapacity, InnerGraph.FindEdge(CurrentNode, InnerGraph.SetOfNodes[Index]).Weight);
                }

                for (int Index = Target.Index; Index != Start.Index; Index = FoundPath[Index].Index)
                {
                    InnerGraph.FindEdge(CurrentNode, InnerGraph.SetOfNodes[Index]).Weight -= CurrentStreamCapacity;

                    InnerGraph.FindEdge(InnerGraph.SetOfNodes[Index], CurrentNode).Weight += CurrentStreamCapacity;
                }

                CapacityOfFlow += CurrentStreamCapacity;

                BreadthFirstSearch = new BreadthFirstSearchAlgorithm(InnerGraph);
            }

            return(true);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Graph.AddNode(new Node(7, "0", new Coordinate(0, 0)));
            Graph.AddNode(new Node(7, "1", new Coordinate(50, 100)));
            Graph.AddNode(new Node(7, "2", new Coordinate(100, 0)));
            Graph.AddNode(new Node(7, "3", new Coordinate(0, 200)));
            Graph.AddNode(new Node(7, "4", new Coordinate(250, 200)));
            Graph.AddNode(new Node(7, "5", new Coordinate(50, 250)));
            Graph.AddNode(new Node(7, "6", new Coordinate(150, 250)));

            Graph.AddTwoWayEdge(new Edge(7, Graph.SetOfNodes[0], Graph.SetOfNodes[1]));
            Graph.AddTwoWayEdge(new Edge(5, Graph.SetOfNodes[0], Graph.SetOfNodes[3]));
            Graph.AddTwoWayEdge(new Edge(8, Graph.SetOfNodes[1], Graph.SetOfNodes[2]));
            Graph.AddTwoWayEdge(new Edge(7, Graph.SetOfNodes[1], Graph.SetOfNodes[4]));
            Graph.AddTwoWayEdge(new Edge(9, Graph.SetOfNodes[1], Graph.SetOfNodes[3]));
            Graph.AddTwoWayEdge(new Edge(5, Graph.SetOfNodes[2], Graph.SetOfNodes[4]));
            Graph.AddTwoWayEdge(new Edge(15, Graph.SetOfNodes[3], Graph.SetOfNodes[4]));
            Graph.AddTwoWayEdge(new Edge(6, Graph.SetOfNodes[3], Graph.SetOfNodes[5]));
            Graph.AddTwoWayEdge(new Edge(8, Graph.SetOfNodes[5], Graph.SetOfNodes[4]));
            Graph.AddTwoWayEdge(new Edge(11, Graph.SetOfNodes[5], Graph.SetOfNodes[6]));
            Graph.AddTwoWayEdge(new Edge(9, Graph.SetOfNodes[6], Graph.SetOfNodes[4]));

            NewGraph.AddNode(new Node(7, "0", new Coordinate(0, 0)));
            NewGraph.AddNode(new Node(7, "1", new Coordinate(50, 100)));
            NewGraph.AddNode(new Node(7, "2", new Coordinate(100, 0)));
            NewGraph.AddNode(new Node(7, "3", new Coordinate(0, 200)));
            NewGraph.AddNode(new Node(7, "4", new Coordinate(500, 100)));
            NewGraph.AddNode(new Node(7, "5", new Coordinate(50, 250)));
            NewGraph.AddNode(new Node(7, "6", new Coordinate(150, 250)));

            NewGraph.AddOneWayEdge(new Edge(7, NewGraph.SetOfNodes[0], NewGraph.SetOfNodes[1]));
            NewGraph.AddOneWayEdge(new Edge(5, NewGraph.SetOfNodes[0], NewGraph.SetOfNodes[3]));
            NewGraph.AddOneWayEdge(new Edge(8, NewGraph.SetOfNodes[1], NewGraph.SetOfNodes[2]));
            NewGraph.AddOneWayEdge(new Edge(7, NewGraph.SetOfNodes[1], NewGraph.SetOfNodes[4]));
            NewGraph.AddOneWayEdge(new Edge(9, NewGraph.SetOfNodes[1], NewGraph.SetOfNodes[3]));
            NewGraph.AddOneWayEdge(new Edge(5, NewGraph.SetOfNodes[2], NewGraph.SetOfNodes[4]));
            NewGraph.AddOneWayEdge(new Edge(15, NewGraph.SetOfNodes[3], NewGraph.SetOfNodes[4]));
            NewGraph.AddOneWayEdge(new Edge(6, NewGraph.SetOfNodes[3], NewGraph.SetOfNodes[5]));
            NewGraph.AddOneWayEdge(new Edge(8, NewGraph.SetOfNodes[5], NewGraph.SetOfNodes[4]));
            NewGraph.AddOneWayEdge(new Edge(11, NewGraph.SetOfNodes[5], NewGraph.SetOfNodes[6]));
            NewGraph.AddOneWayEdge(new Edge(9, NewGraph.SetOfNodes[4], NewGraph.SetOfNodes[6]));

            DepthFirstSearchAlgorithm   DFS = new DepthFirstSearchAlgorithm(Graph);
            BreadthFirstSearchAlgorithm BFS = new BreadthFirstSearchAlgorithm(Graph);
            KruscalAlgorithm            KruskalTreeSearch  = new KruscalAlgorithm(Graph);
            PrimAlgorithm          PrimTreeSearch          = new PrimAlgorithm(Graph);
            BellmanFordAlgorithm   BellmanFordPathSearch   = new BellmanFordAlgorithm(Graph);
            DijkstraAlgorithm      DijkstraPathSearch      = new DijkstraAlgorithm(Graph);
            FloydWarshallAlgorithm FloydWarshallPathSearch = new FloydWarshallAlgorithm(Graph);
            JohnsonAlgorithm       JohnsonPathSearch       = new JohnsonAlgorithm(Graph);
            AStarAlgorithm         AStarPathSearch         = new AStarAlgorithm(Graph);

            Console.WriteLine("[Current Graph]\n", Color.Green);

            Console.WriteLine("[0]        [2]");
            Console.WriteLine("|\\         /|");
            Console.WriteLine("| \\ 7    8/ |");
            Console.WriteLine("|  \\     /  |5");
            Console.WriteLine("|5  \\   /   |");
            Console.WriteLine("|    [1]    |");
            Console.WriteLine("| 9 /   \\7  |");
            Console.WriteLine("|  /     \\  |");
            Console.WriteLine("| /   15  \\ |");
            Console.WriteLine("[3]--------[4]");
            Console.WriteLine(" \\        /  \\ ");
            Console.WriteLine("  \\     8/    \\9");
            Console.WriteLine(" 6 \\    /      \\");
            Console.WriteLine("    \\  /        \\");
            Console.WriteLine("    [5]---------[6]");
            Console.WriteLine("           11\n");

            Console.WriteLine("[1 : Depth First Search]\n", Color.Green);

            DFS.DepthFirstSearch(Graph.SetOfNodes[0]);

            Utilities.PrintResult(DFS.ResultOfSearching);

            Console.WriteLine();

            Console.WriteLine("[2 : Breadth First Search]\n", Color.Green);

            BFS.BreadthFirstSearch(Graph.SetOfNodes[0]);

            Utilities.PrintResult(BFS.ResultOfSearching);

            Console.WriteLine();

            FordFulkersonAlgorithm FordFulkersonStreamSearch = new FordFulkersonAlgorithm(NewGraph);

            Console.WriteLine($"[3 : Kruskal Tree Search]\n", Color.Green);

            KruskalTreeSearch.FindMinimumSpanningTree();

            Utilities.PrintResult(KruskalTreeSearch.MinimumSpanningTree);

            Console.WriteLine();

            Console.WriteLine($"[4 : Prim Tree Search]\n", Color.Green);

            PrimTreeSearch.FindMinimumSpanningTree();

            Utilities.PrintResult(PrimTreeSearch.MinimumSpanningTree);

            Console.WriteLine();

            Console.WriteLine("[5 : Bellman-Ford Path Search]\n", Color.Green);

            BellmanFordPathSearch.FindTheShortestPathes(Graph.SetOfNodes[0]);

            Utilities.PrintResult(BellmanFordPathSearch.ShortestPathes, Graph.SetOfNodes[0]);

            Console.WriteLine("[6 : Dijkstra Path Search]\n", Color.Green);

            DijkstraPathSearch.FindTheShortestPathes(Graph.SetOfNodes[0]);

            Utilities.PrintResult(DijkstraPathSearch.ShortestPathes, Graph.SetOfNodes[0]);

            FloydWarshallPathSearch.FindAllTheShortestPathes();

            Console.WriteLine("[7 : Floyd-Warshall Path Search]\n", Color.Green);

            Utilities.PrintResult(FloydWarshallPathSearch.MatrixOfTheShortesPathes);

            Console.WriteLine();

            JohnsonPathSearch.FindAllTheShortestPathes();

            Console.WriteLine("[8 : Johnson Path Search]\n", Color.Green);

            Utilities.PrintResult(JohnsonPathSearch.MatrixOfTheShortesPathes);

            Console.WriteLine();

            Console.WriteLine("[9 : Ford-Fulkerson Stream Search]\n", Color.Green);

            FordFulkersonStreamSearch.FindMaximumFlowWithDFSRealization(NewGraph.SetOfNodes[0], NewGraph.SetOfNodes[6]);

            Console.WriteLine($"Maximum stream capacity is : {FordFulkersonStreamSearch.CapacityOfFlow}");

            Console.WriteLine();

            Console.WriteLine("[10 : A* Path Search]\n", Color.Green);

            AStarPathSearch.FindTheShortesPath(Graph.SetOfNodes[0], Graph.SetOfNodes[6]);

            Utilities.PrintResult(AStarPathSearch.ShortesPath);

            Console.ReadKey();
        }