예제 #1
0
        private bool AreSameResult(DijkstraResult expected, DijkstraResult actual)
        {
            // Edge cases
            if (expected.Parents.Count != actual.Parents.Count)
            {
                return(false);
            }
            if (expected.Distances.Count != actual.Distances.Count)
            {
                return(false);
            }

            // Compare parents
            if (expected.Parents.Any(expectedParent =>
                                     expectedParent.Value != actual.Parents[expectedParent.Key]))
            {
                return(false);
            }

            // Compare distances
            if (expected.Distances.Any(expectedDistance =>
                                       expectedDistance.Value != actual.Distances[expectedDistance.Key]))
            {
                return(false);
            }

            return(true);
        }
예제 #2
0
        public void TestDijkstraHappyPath()
        {
            var g = new Dictionary <char, List <DijkstraEdge> >
            {
                {
                    'a', new List <DijkstraEdge>
                    {
                        new DijkstraEdge('a', 'b', 5),
                        new DijkstraEdge('a', 'd', 9),
                        new DijkstraEdge('a', 'e', 2),
                    }
                },
                {
                    'b', new List <DijkstraEdge>
                    {
                        new DijkstraEdge('b', 'a', 5),
                        new DijkstraEdge('b', 'c', 2),
                    }
                },
                {
                    'c', new List <DijkstraEdge>
                    {
                        new DijkstraEdge('c', 'b', 2),
                        new DijkstraEdge('c', 'd', 3),
                    }
                },
                {
                    'd', new List <DijkstraEdge>
                    {
                        new DijkstraEdge('d', 'a', 9),
                        new DijkstraEdge('d', 'c', 3),
                        new DijkstraEdge('d', 'f', 2),
                    }
                },
                {
                    'e', new List <DijkstraEdge>
                    {
                        new DijkstraEdge('e', 'a', 2),
                        new DijkstraEdge('e', 'f', 3),
                    }
                },
                {
                    'f', new List <DijkstraEdge>
                    {
                        new DijkstraEdge('f', 'e', 3),
                        new DijkstraEdge('f', 'd', 2),
                    }
                },
            };

            var parents = new Dictionary <char, char?>
            {
                { 'a', null },
                { 'b', 'a' },
                { 'c', 'b' },
                { 'd', 'f' },
                { 'e', 'a' },
                { 'f', 'e' },
            };
            var distances = new Dictionary <char, int>()
            {
                { 'a', 0 },
                { 'b', 5 },
                { 'c', 7 },
                { 'd', 7 },
                { 'e', 2 },
                { 'f', 5 },
            };
            var expected = new DijkstraResult(parents, distances);

            var actual = GetShortestPaths(g);

            Assert.True(AreSameResult(expected, actual));

            var actual2 = GetShortestPaths2ndTime(g);

            Assert.True(AreSameResult(expected.Parents, expected.Distances, actual2.Parents, actual2.Distances));

            var actual3 = GetShortestPaths3rdTime(g);

            Assert.True(AreSameResult(expected.Parents, expected.Distances, actual3.Parents, actual3.Distances));

            var actual4 = GetShortestPaths4thTime(g);

            Assert.True(AreSameResult(expected.Parents, expected.Distances, actual4.Parents, actual4.Distances));
        }