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

        var path = DijkstraWithoutQueue.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);
        }
    }
        public void FindPathBetween0And1()
        {
            var path   = DijkstraWithoutQueue.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);
        }
        public void FindPathBetween0And10()
        {
            var path = DijkstraWithoutQueue.DijkstraAlgorithm(Graph, 0, 10);

            Assert.IsNull(path);
        }