Exemplo n.º 1
0
        public void BasicTest()
        {
            //Graph with three vertexes
            Graph graph = new Graph(3);

            graph.Add(0, 1, 2);
            graph.Add(1, 2, 1);
            graph.Add(0, 2, 5);

            // Min cost path from vertex (0) to vertex (2)
            int[] result = new int[]{0, 1, 2};

            Assert.That(graph.ShortestPath(0, 2), Is.EqualTo(result));
        }
Exemplo n.º 2
0
        public void ForestTest()
        {
            Graph graph = new Graph(6);

            graph.Add(0, 1, 1);
            graph.Add(0, 2, 1);
            graph.Add(1, 2, 1);
            graph.Add(3, 5, 1);
            graph.Add(3, 4, 1);
            graph.Add(4, 5, 1);

            int[] result = new int[0];

            Assert.That(graph.ShortestPath(0, 5), Is.EqualTo(result));
        }
Exemplo n.º 3
0
        public void GraphWithBridgeTest()
        {
            Graph graph = new Graph(7);

            graph.Add(0, 1, 4);
            graph.Add(0, 2, 5);
            graph.Add(1, 3, 3);
            graph.Add(2, 3, 1);
            graph.Add(3, 4, 2);
            graph.Add(4, 5, 4);
            graph.Add(4, 6, 3);
            graph.Add(5, 6, 2);

            int[] result = new int[]{0, 2, 3, 4, 5};

            Assert.That(graph.ShortestPath(0, 5), Is.EqualTo(result));
        }
Exemplo n.º 4
0
        public void CompleteGraphTest()
        {
            Graph graph = new Graph(5);

            graph.Add(0, 1, 3);
            graph.Add(0, 2, 7);
            graph.Add(0, 3, 9);
            graph.Add(0, 4, 1);
            graph.Add(1, 2, 5);
            graph.Add(1, 3, 5);
            graph.Add(1, 4, 3);
            graph.Add(2, 3, 2);
            graph.Add(2, 4, 9);
            graph.Add(3, 4, 8);

            int[] result = new int[]{0, 1, 3};

            Assert.That(graph.ShortestPath(0, 3), Is.EqualTo(result));
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            Graph  myGraph       = new Graph();                                             // Create graph object
            string startLocation = null;
            string destination   = null;

            Console.WriteLine("This is a Graph showing the implementation of Dijkstra's algorithm.");
            Console.WriteLine("The Graph consists of the entire alphabet (A to Z)\n");

            Boolean validInput;                                                             // Test if input is valid
            Boolean breakLoop = false;                                                      // Test if exit is requested

            do
            {
                validInput = false;

                do                                                                          // Loop until valid input is received
                {
                    try
                    {
                        Console.Write("Please enter the point you would like to start from? (enter \"XX\" to exit)\n > ");
                        startLocation = Console.ReadLine().ToUpper();

                        if (startLocation.Equals("XX"))                                     // If xx is entered, input is valid, but loop needs to be broken
                        {
                            breakLoop = true;
                        }
                        else
                        {
                            myGraph.ShortestPath(startLocation);                            // Run the algorithm with supplied input
                        }

                        validInput = true;
                    }
                    catch (IndexOutOfRangeException e)                                      // If the input doesn't exist in the list, handle exception
                    {
                        Console.WriteLine("\n" + e.Message + "\n");
                    }
                } while (!validInput);

                validInput = false;                                                         // Reset valid input property

                if (!startLocation.Equals("XX"))                                            // If an exit was not already requested, run the following
                {
                    do                                                                      // As for the destination
                    {
                        Console.Write("You are in \"" + startLocation.ToUpper() + "\". Where are you going? (enter \"XX\" to exit)\n > ");
                        destination = Console.ReadLine().ToUpper();

                        if ((Graph.GraphDictionary.ContainsKey(destination)) || (destination.Equals("XX")))
                        {
                            validInput = true;                                              // Mark input as valid if it is
                        }
                        else
                        {                                                                   // Handle input error without throwing exception
                            Console.WriteLine("\nThe value you entered does not exist in the graph, please try again");
                        }
                    } while (!validInput);                                                  // As for input until valid input is received

                    if (!destination.Equals("XX"))                                          // If exit was not requested, continue
                    {
                        Console.WriteLine("\nThe shortest path is: ");

                        try
                        {
                            Console.WriteLine(myGraph.FindPath(startLocation, destination));// Try to print the required path
                        }
                        catch (IndexOutOfRangeException InputException)
                        {
                            Console.WriteLine(InputException.Message);
                        }

                        Console.WriteLine("\nPress enter to continue...");
                        Console.ReadLine();
                    }
                    else
                    {
                        breakLoop = true;
                    }
                }
            } while (!breakLoop);                                                           // End the program
        }
Exemplo n.º 6
0
        public void RegularGraphTest()
        {
            Graph graph = new Graph(6);

            graph.Add(0, 1, 2);
            graph.Add(0, 2, 4);
            graph.Add(1, 2, 1);
            graph.Add(2, 3, 5);
            graph.Add(1, 3, 8);
            graph.Add(1, 4, 10);
            graph.Add(3, 4, 2);
            graph.Add(3, 5, 6);
            graph.Add(4, 5, 3);

            int[] result = new int[]{0, 1, 2, 3, 4, 5};

            Assert.That(graph.ShortestPath(0, 5), Is.EqualTo(result));
        }
Exemplo n.º 7
0
        public void WeirdGraphTest()
        {
            Graph graph = new Graph(8);

            graph.Add(0, 1, 2);
            graph.Add(1, 3, 1);
            graph.Add(1, 2, 4);
            graph.Add(3, 2, 2);
            graph.Add(2, 4, 2);
            graph.Add(2, 5, 2);
            graph.Add(5, 7, 3);
            graph.Add(5, 6, 5);
            graph.Add(6, 7, 1);

            int[] result = new int[]{6, 7, 5, 2, 3, 1, 0};

            Assert.That(graph.ShortestPath(6, 0), Is.EqualTo(result));
        }
Exemplo n.º 8
0
        public void TreeGraphTest()
        {
            Graph graph = new Graph(8);

            graph.Add(0, 1, 1);
            graph.Add(0, 2, 1);
            graph.Add(2, 3, 1);
            graph.Add(2, 4, 1);
            graph.Add(2, 5, 1);
            graph.Add(4, 6, 1);
            graph.Add(4, 7, 1);

            int[] result = new int[]{1, 0, 2, 4, 6};

            Assert.That(graph.ShortestPath(1, 6), Is.EqualTo(result));
        }