예제 #1
0
        public static void PrintPath(int[,] graph, int sourceNode, int destinationNode)
        {
            Console.Write(
                "Shortest path [{0} -> {1}]: ",
                sourceNode,
                destinationNode);

            var path = DijkstraWithoutPriorityQueue.DijkstraAlgorithm(graph, sourceNode, destinationNode);

            if (path == null)
            {
                Console.WriteLine("no path");
            }
            else
            {
                int pathLength = 0;
                for (int i = 0; i < path.Count - 1; i++)
                {
                    pathLength += graph[path[i], path[i + 1]];
                }

                var formattedPath = string.Join("->", path);
                Console.WriteLine("{0} (length {1})", formattedPath, pathLength);
            }
        }
예제 #2
0
        public void FindPathBetween0And1()
        {
            var path   = DijkstraWithoutPriorityQueue.DijkstraAlgorithm(Graph, 0, 1);
            var length = 0;

            for (int i = 1; i < path.Count; i++)
            {
                length += Graph[path[i - 1], path[i]];
            }

            var expectedPath   = new[] { 0, 8, 5, 4, 11, 1 };
            var expectedLength = 37;

            Assert.AreEqual(expectedLength, length);
            CollectionAssert.AreEqual(path, expectedPath);
        }
예제 #3
0
        public void FindPathBetween0And10()
        {
            var path = DijkstraWithoutPriorityQueue.DijkstraAlgorithm(Graph, 0, 10);

            Assert.IsNull(path);
        }