public void HasUnvisitedTest_ReturnFalseIf_AllIsVisitedIsTrue_Test()
        {
            // Arrange
            var graph = new DijkstraGraph()
            {
                Nodes = new[]
                {
                    Mock.Of<DijkstraNode>(t => t.IsVisited == true)
                }
            };

            // Assert
            Assert.False(graph.HasUnvisited());
        }
        public void HasUnvisited_ReturnsTrue_Test()
        {
            // Arrange
            var graph = new DijkstraGraph()
            {
                Nodes = new[]
                {
                    Mock.Of<DijkstraNode>(t => t.IsVisited),
                    Mock.Of<DijkstraNode>(t => t.IsVisited == false && t.TotalWeigth == 1)
                }
            };

            // Assert
            Assert.True(graph.HasUnvisited());
        }
        public void HasUnvisitedTest_ReturnFalseIf_AllIsVisitedTrue_Or_MarkIsInfinity_Test()
        {
            // Arrange
            var graph = new DijkstraGraph()
            {
                Nodes = new[]
                {
                    Mock.Of<DijkstraNode>(t => t.IsVisited),
                    Mock.Of<DijkstraNode>(t => t.IsVisited == false && t.TotalWeigth == null)
                }
            };

            // Assert
            Assert.False(graph.HasUnvisited());
        }
Пример #4
0
        /// <summary>
        /// Computes total wieghts from start node for all nodes in graph
        /// </summary>
        public Dijkstra Compute(DijkstraGraph graph)
        {
            while (graph.HasUnvisited())
            {
                var current = graph.NearestUnvisited();
                foreach (var node in graph.Linked(current))
                {
                    var weightFromCurrent = current.TotalWeigth + current.LinkWeight(node);

                    // if this way shorter
                    if (!node.TotalWeigth.HasValue || weightFromCurrent < node.TotalWeigth)
                    {
                        node.TotalWeigth = weightFromCurrent;
                        node.Previous = current;
                    }
                }

                current.IsVisited = true;
            }

            return this;
        }