Esempio n. 1
0
        static void Main(string[] args)
        {
            // Intial Graph and add all node
            Graph graph = new Graph();

            graph.AddNode("A");
            graph.AddNode("B");
            graph.AddNode("C");
            graph.AddNode("D");
            graph.AddNode("E");

            // Bind path weight between node
            graph.AddConnection("A", "B", 6);
            graph.AddConnection("A", "D", 1);
            graph.AddConnection("B", "D", 2);
            graph.AddConnection("B", "E", 2);
            graph.AddConnection("B", "C", 5);
            graph.AddConnection("D", "E", 1);
            graph.AddConnection("E", "C", 5);

            DijkstraAlgo algo = new DijkstraAlgo();
            // Let A is start node
            IDictionary <string, NodeRecord> result = algo.CalculateDistances(graph, "A");

            // print all shortest path of each node
            foreach (var item in result)
            {
                Console.WriteLine(item.Key + " shortest path:" + item.Value.lowestWeight + " previous node:" + item.Value.previousNode?.name);
            }



            Console.ReadLine();
        }
        public void GetPath_ExistPath_Path()
        {
            var algo = new DijkstraAlgo();

            var pathToFile = "input.txt";

            var start = "Москва";

            var end = "Владивосток";

            var path = algo.GetPath(pathToFile, start, end);

            var coasts = new List <double>
            {
                20,
                20
            };

            var citiesName = new List <string>
            {
                "Владивосток",
                "Санкт-Петербург"
            };

            var expected = (citiesName, coasts);

            Assert.IsTrue(AreEqual(path, expected));
        }
Esempio n. 3
0
        public void Test_Exception_DuplicateNode()
        {
            Graph graph = new Graph();

            graph.AddNode("A");

            DijkstraAlgo      algo = new DijkstraAlgo();
            ArgumentException ex   = Assert.ThrowsException <ArgumentException>(() => graph.AddNode("A"));

            Assert.AreEqual("duplicate node", ex.Message);
        }
Esempio n. 4
0
        public void Test_Exception_Negative_Weight()
        {
            Graph graph = new Graph();

            graph.AddNode("A");
            graph.AddNode("B");

            DijkstraAlgo      algo = new DijkstraAlgo();
            ArgumentException ex   = Assert.ThrowsException <ArgumentException>(() => graph.AddConnection("A", "B", -1));

            Assert.AreEqual("weight cannot negative number", ex.Message);
        }
        public void GetPath_NotExistPath_Exception()
        {
            var algo = new DijkstraAlgo();

            var pathToFile = "input.txt";

            var start = "Владивосток";

            var end = "Москва";

            Assert.Throws <Exception>(() => algo.GetPath(pathToFile, start, end));
        }
        public void GetPath_InvalidDataFormat_Exception()
        {
            var algo = new DijkstraAlgo();

            var pathToFile = "invalid.txt";

            var start = "First";

            var end = "Second";

            Assert.Throws <Exception>(() => algo.GetPath(pathToFile, start, end));
        }
        public void GetPath_NotExistPathToFile_Exception()
        {
            var algo = new DijkstraAlgo();

            var pathToFile = "NotExist.txt";

            var start = "First";

            var end = "Second";

            Assert.Throws <Exception>(() => algo.GetPath(pathToFile, start, end));
        }
Esempio n. 8
0
        public void Test_Exception_Node_No_Connection()
        {
            Graph graph = new Graph();

            graph.AddNode("A");
            graph.AddNode("B");
            graph.AddNode("C");
            graph.AddConnection("A", "B", 6);

            DijkstraAlgo      algo = new DijkstraAlgo();
            ArgumentException ex   = Assert.ThrowsException <ArgumentException>(() => algo.CalculateDistances(graph, "A"));

            Assert.AreEqual("all node must have connection", ex.Message);
        }
Esempio n. 9
0
        public void Test_Calculation_Shortest_Path()
        {
            Graph graph = new Graph();

            graph.AddNode("A");
            graph.AddNode("B");
            graph.AddNode("C");
            graph.AddNode("D");
            graph.AddNode("E");

            graph.AddConnection("A", "B", 6);
            graph.AddConnection("A", "D", 1);
            graph.AddConnection("B", "D", 2);
            graph.AddConnection("B", "E", 2);
            graph.AddConnection("B", "C", 5);
            graph.AddConnection("D", "E", 1);
            graph.AddConnection("E", "C", 5);

            DijkstraAlgo algo = new DijkstraAlgo();
            IDictionary <string, NodeRecord> result = algo.CalculateDistances(graph, "A");

            Assert.AreEqual(7, result["C"].lowestWeight);
        }