public void UpdateShortestPath()
        {
            // Arrange
            var shortestPath = new DijkstrasShortestPath(Graph());

            shortestPath.Reset();

            var index_u = 1;
            var index_v = 0;

            // Pre-act 1
            this.AssertResults1(shortestPath);

            // Manipulate data for testing purposes.
            // These values will be set in the loop
            shortestPath.ResetDistanceOfSourceVertex(sourceVertexIndex: index_u);
            shortestPath.SetShortestDistance(index_u, value: 2);
            shortestPath.SetShortestDistance(index_v, value: 9);

            // Pre-act 2
            this.AssertResults2(shortestPath);

            // Act
            shortestPath.Update(index_u, index_v);

            // Assert
            this.AssertResults3(shortestPath);
            this.AssertVerticesThatAreInShortestDistance(shortestPath);
        }
        public void DoesNotUpdatePath()
        {
            // Arrange
            var shortestPath = new DijkstrasShortestPath(Graph());

            shortestPath.Reset();

            var index_u = 1;
            var index_v = 0;

            // Pre-act 1
            this.AssertResults1(shortestPath);

            // Manipulate data for testing purposes.
            // These values will be set in the loop
            shortestPath.ResetDistanceOfSourceVertex(sourceVertexIndex: index_u);
            shortestPath.SetShortestDistance(index_u, value: 2);
            shortestPath.SetShortestDistance(index_v, value: 4); // v=4 is the reason to not to update

            // Pre-act 2
            Assert.Equal(4, shortestPath.shortestDistances[0]);
            Assert.Equal(2, shortestPath.shortestDistances[1]);
            Assert.Equal(2147483647, shortestPath.shortestDistances[2]);

            // Act
            shortestPath.Update(index_u, index_v);

            // Assert
            Assert.Equal(4, shortestPath.shortestDistances[0]);
            Assert.Equal(2, shortestPath.shortestDistances[1]);
            Assert.Equal(2147483647, shortestPath.shortestDistances[2]);
        }
        private void AssertResults2(DijkstrasShortestPath shortestPath)
        {
            Assert.Equal(9, shortestPath.shortestDistances[0]);
            Assert.Equal(2, shortestPath.shortestDistances[1]);
            Assert.Equal(2147483647, shortestPath.shortestDistances[2]);

            Assert.False(shortestPath.verticesThatAreInShortestDistance[0]);
            Assert.False(shortestPath.verticesThatAreInShortestDistance[1]);
            Assert.False(shortestPath.verticesThatAreInShortestDistance[2]);
        }
        public void ResetAllDistancesToInfinite_And_ResetVisitedVertices()
        {
            // Arrange
            var shortestPath = new DijkstrasShortestPath(Graph());

            // Act
            shortestPath.Reset();

            // Assert
            this.AssertResults1(shortestPath);
        }
        public void GetMinimumDistancesIndexTest2()
        {
            // Arrange
            var shortestPath = new DijkstrasShortestPath(Graph());

            shortestPath.Reset();
            shortestPath.ResetDistanceOfSourceVertex(2);

            // Act
            var minimumDistancesIndex = shortestPath.GetMinimumDistancesIndex();

            // Assert
            Assert.Equal(2, minimumDistancesIndex);
        }
        public void ResetSourceVertexDistance()
        {
            // Arrange
            var shortestPath = new DijkstrasShortestPath(Graph());

            // Act
            shortestPath.Reset();
            shortestPath.ResetDistanceOfSourceVertex(1);

            // Assert
            this.AssertVerticesThatAreInShortestDistance(shortestPath);
            Assert.Equal(2147483647, shortestPath.shortestDistances[0]);
            Assert.Equal(0, shortestPath.shortestDistances[1]);
            Assert.Equal(2147483647, shortestPath.shortestDistances[2]);
        }
Example #7
0
    public static void Main(String[] args)
    {
        int[,] graph = new int[, ] {
            { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
            { 4, 0, 8, 0, 0, 0, 0, 11, 0 },
            { 0, 8, 0, 7, 0, 4, 0, 0, 2 },
            { 0, 0, 7, 0, 9, 14, 0, 0, 0 },
            { 0, 0, 0, 9, 0, 10, 0, 0, 0 },
            { 0, 0, 4, 14, 10, 0, 2, 0, 0 },
            { 0, 0, 0, 0, 0, 2, 0, 1, 6 },
            { 8, 11, 0, 0, 0, 0, 1, 0, 7 },
            { 0, 0, 2, 0, 0, 0, 6, 7, 0 }
        };
        DijkstrasShortestPath dsp = new DijkstrasShortestPath();
        var result = dsp.ShortestPath(graph, 0, 9);

        dsp.printResult(result);
    }
        public void Test()
        {
            // Arrange
            var shortestPath = new DijkstrasShortestPath(Graph());

            var source = 1;

            // Act
            shortestPath.Find(source);

            // Assert
            Assert.Equal(3, shortestPath.shortestDistances[0]);
            Assert.Equal(0, shortestPath.shortestDistances[1]);
            Assert.Equal(2147483647, shortestPath.shortestDistances[2]);

            Assert.True(shortestPath.verticesThatAreInShortestDistance[0]);
            Assert.True(shortestPath.verticesThatAreInShortestDistance[1]);
            Assert.False(shortestPath.verticesThatAreInShortestDistance[2]);
        }
Example #9
0
        static void Main(string[] args)
        {
            var graph = new Graph <int>(5, true);

            graph.AddEdges(0, 1);
            graph.AddEdges(0, 2);
            graph.AddEdges(1, 2);
            graph.AddEdges(2, 3);
            graph.AddEdges(1, 4);
            graph.Display();
            Console.WriteLine("------------------");
            BreathFirstSearch <int> .Search(graph, 0);

            Console.WriteLine("------------------");
            DepthFirstSearch <int> .Search(graph, graph.FindEdge(0));

            Console.WriteLine("------------------");

            Console.WriteLine("---- Topological Search----");
            var graph2 = new Graph <int>(9, true);

            graph2.AddEdges(0, 1);
            graph2.AddEdges(1, 2);
            graph2.AddEdges(2, 7);
            graph2.AddEdges(2, 4);
            graph2.AddEdges(2, 3);
            graph2.AddEdges(1, 5);
            graph2.AddEdges(5, 6);
            graph2.AddEdges(3, 6);
            graph2.AddEdges(3, 4);
            graph2.AddEdges(6, 8);
            Console.WriteLine("------------------");
            TopologicalSearch <int> .Search(graph2, 0);

            Console.WriteLine("------------------");

            Console.WriteLine("---- Shortest Path Undirected----");
            var pathGraph = new Graph <int>(8);

            pathGraph.AddEdges(0, 1);
            pathGraph.AddEdges(1, 2);
            pathGraph.AddEdges(1, 3);
            pathGraph.AddEdges(2, 3);
            pathGraph.AddEdges(1, 4);
            pathGraph.AddEdges(3, 5);
            pathGraph.AddEdges(5, 4);
            pathGraph.AddEdges(3, 6);
            pathGraph.AddEdges(6, 7);
            pathGraph.AddEdges(0, 7);
            UnweightedShortestPath <int> .ShortestPath(pathGraph, 0, 5);

            UnweightedShortestPath <int> .ShortestPath(pathGraph, 0, 6);

            UnweightedShortestPath <int> .ShortestPath(pathGraph, 7, 4);

            Console.WriteLine("------------------");

            Console.WriteLine("---- Shortest Path Directed----");
            var directedPathGraph = new Graph <int>(8, true);

            directedPathGraph.AddEdges(0, 1);
            directedPathGraph.AddEdges(1, 2);
            directedPathGraph.AddEdges(1, 3);
            directedPathGraph.AddEdges(2, 3);
            directedPathGraph.AddEdges(1, 4);
            directedPathGraph.AddEdges(3, 5);
            directedPathGraph.AddEdges(5, 4);
            directedPathGraph.AddEdges(3, 6);
            directedPathGraph.AddEdges(6, 7);
            directedPathGraph.AddEdges(0, 7);
            UnweightedShortestPath <int> .ShortestPath(directedPathGraph, 0, 5);

            UnweightedShortestPath <int> .ShortestPath(directedPathGraph, 0, 6);

            UnweightedShortestPath <int> .ShortestPath(directedPathGraph, 7, 4);

            Console.WriteLine("------------------");

            Console.WriteLine("---- Dijkstra ----");
            var weightedUnDirectedGraph = new Graph <int>(8, true);

            weightedUnDirectedGraph.AddEdges(0, 1, 1);
            weightedUnDirectedGraph.AddEdges(1, 2, 2);
            weightedUnDirectedGraph.AddEdges(1, 3, 6);
            weightedUnDirectedGraph.AddEdges(2, 3, 2);
            weightedUnDirectedGraph.AddEdges(1, 4, 3);
            weightedUnDirectedGraph.AddEdges(3, 5, 1);
            weightedUnDirectedGraph.AddEdges(5, 4, 5);
            weightedUnDirectedGraph.AddEdges(3, 6, 1);
            weightedUnDirectedGraph.AddEdges(6, 7, 1);
            weightedUnDirectedGraph.AddEdges(0, 7, 8);
            DijkstrasShortestPath <int> .DijkstrasPath(weightedUnDirectedGraph, 0, 5);

            DijkstrasShortestPath <int> .DijkstrasPath(weightedUnDirectedGraph, 4, 7);

            DijkstrasShortestPath <int> .DijkstrasPath(weightedUnDirectedGraph, 7, 0);

            Console.WriteLine("------------------");

            Console.ReadLine();
        }
 private void AssertVerticesThatAreInShortestDistance(DijkstrasShortestPath shortestPath)
 {
     Assert.False(shortestPath.verticesThatAreInShortestDistance[0]);
     Assert.False(shortestPath.verticesThatAreInShortestDistance[1]);
     Assert.False(shortestPath.verticesThatAreInShortestDistance[2]);
 }
 private void AssertResults3(DijkstrasShortestPath shortestPath)
 {
     Assert.Equal(5, shortestPath.shortestDistances[0]);
     Assert.Equal(2, shortestPath.shortestDistances[1]);
     Assert.Equal(2147483647, shortestPath.shortestDistances[2]);
 }