Ejemplo n.º 1
0
        public void DijkstraShortestPath_EmptyGraphTest()
        {
            var graph = new AdjacencyListGraph <int>();
            var path  = graph.DijkstraShortestPath(1, 10);

            Assert.AreEqual(0, path.Count());
        }
Ejemplo n.º 2
0
        public void DijkstraShortestPath_NegativeEdgeTest()
        {
            var graph = new AdjacencyListGraph <int>();

            graph.AddEdge(1, 2, -7);
            graph.AddEdge(2, 1, -7);

            var path = graph.DijkstraShortestPath(1, 2);
        }
Ejemplo n.º 3
0
        private static void DijkstraShortestPath_SmallGraphTest(int start, int finish, int[] targetPath, double weight)
        {
            var graph = new AdjacencyListGraph <int>();

            graph.AddEdge(1, 2, 7);
            graph.AddEdge(2, 1, 7);
            graph.AddEdge(1, 3, 9);
            graph.AddEdge(3, 1, 9);
            graph.AddEdge(1, 6, 14);
            graph.AddEdge(6, 1, 14);
            graph.AddEdge(2, 4, 15);
            graph.AddEdge(4, 2, 15);
            graph.AddEdge(3, 4, 11);
            graph.AddEdge(4, 3, 11);
            graph.AddEdge(3, 2, 10);
            graph.AddEdge(2, 3, 10);
            graph.AddEdge(6, 3, 2);
            graph.AddEdge(3, 6, 2);
            graph.AddEdge(6, 5, 9);
            graph.AddEdge(5, 6, 9);
            graph.AddEdge(5, 4, 6);
            graph.AddEdge(4, 5, 6);

            var path = graph.DijkstraShortestPath(start, finish);

            var idx           = 0;
            var segmentsCount = 0;
            var pathWeight    = 0.0;

            foreach (var edge in path)
            {
                segmentsCount++;
                pathWeight += edge.Weight;
                Assert.AreEqual(targetPath[idx++], edge.Target);
            }
            Assert.AreEqual(targetPath.Length, segmentsCount);
            Assert.AreEqual(weight, pathWeight);
        }