Exemplo n.º 1
0
        public void ShortestPathTo_FindShortestPath_WhenThereIsPossibleMultiplePaths(ShortestPathAlgorithm alg)
        {
            var graph = new DirectedWeightedSparseGraph <string>();

            graph.AddVertex("a");
            graph.AddVertex("b");
            graph.AddVertex("c");
            graph.AddVertex("d");

            graph.AddEdge("a", "b", 1);
            graph.AddEdge("b", "c", 1);
            graph.AddEdge("c", "a", 1);
            graph.AddEdge("c", "d", 1);
            graph.AddEdge("b", "d", 1);

            var algorithm = CreateAlgorithm(alg, graph, "a");
            var result    = algorithm.ShortestPathTo("d");

            Assert.NotNull(result);
            Assert.Equal(3, result.Count());
            Assert.Contains("a", result);
            Assert.Contains("b", result);
            Assert.Contains("d", result);
            Assert.Equal(2, algorithm.DistanceTo("d"));
        }
Exemplo n.º 2
0
        public void FindShortestPath_DeadNodes()
        {
            Dictionary <string, List <string> > graph = new Dictionary <string, List <string> >();

            graph.Add("a", new List <string>()
            {
                "c", "b", "g"
            });
            graph.Add("b", new List <string>()
            {
                "a", "g"
            });
            graph.Add("c", new List <string>()
            {
                "a", "d"
            });
            graph.Add("d", new List <string>()
            {
                "c", "e", "i"
            });
            graph.Add("e", new List <string>()
            {
                "d", "g", "h", "i"
            });
            graph.Add("f", new List <string>()
            {
                "g"
            });
            graph.Add("g", new List <string>()
            {
                "a", "f", "e"
            });
            graph.Add("h", new List <string>()
            {
                "e"
            });
            graph.Add("i", new List <string>()
            {
                "d", "e"
            });
            //should be ignored
            graph.Add("j", new List <string>());
            graph.Add("k", new List <string>());

            ShortestPathAlgorithm shortestPathAlgorithm = new ShortestPathAlgorithm();
            List <string>         result;
            bool found = shortestPathAlgorithm.TryFindShortestPath(graph, "a", "h", out result);

            string[] resultArray = result.ToArray();

            Assert.IsTrue(found);
            Assert.AreEqual(4, resultArray.Count());
            Assert.AreEqual("a", resultArray[0]);
            Assert.AreEqual("g", resultArray[1]);
            Assert.AreEqual("e", resultArray[2]);
            Assert.AreEqual("h", resultArray[3]);
        }
Exemplo n.º 3
0
        public void Constructor_Throw_WhenSourceIsNotPartOfGraph(ShortestPathAlgorithm alg)
        {
            var graph = new DirectedWeightedSparseGraph <string>();

            graph.AddVertex("a");
            graph.AddVertex("b");
            graph.AddVertex("c");
            graph.AddVertex("d");
            Assert.Throws <ArgumentException>(() => CreateAlgorithm(alg, graph, "x"));
        }
Exemplo n.º 4
0
        private static IShortestPath <string> CreateAlgorithm(ShortestPathAlgorithm algEnum, DirectedWeightedSparseGraph <string> Graph, string Source)
        {
            switch (algEnum)
            {
            case ShortestPathAlgorithm.DIJKSTRA: return(new DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string>(Graph, Source));

            case ShortestPathAlgorithm.BELLMAN_FORD: return(new BellmanFordShortestPaths <DirectedWeightedSparseGraph <string>, string>(Graph, Source));

            case ShortestPathAlgorithm.BREADTH_FIRST: return(new BreadthFirstShortestPaths <string>(Graph, Source));
            }
            throw new ArgumentException("unkown algorithm: " + algEnum);
        }
Exemplo n.º 5
0
        public void FindShortestPath_ImpossiblePath_startAtDeadEnd()
        {
            Dictionary <string, List <string> > graph = new Dictionary <string, List <string> >();

            graph.Add("a", new List <string>()
            {
                "c", "b", "g"
            });
            graph.Add("b", new List <string>()
            {
                "a", "g"
            });
            graph.Add("c", new List <string>()
            {
                "a", "d"
            });
            graph.Add("d", new List <string>()
            {
                "c", "e", "i"
            });
            graph.Add("e", new List <string>()
            {
                "d", "g", "h", "i"
            });
            graph.Add("f", new List <string>()
            {
                "g"
            });
            graph.Add("g", new List <string>()
            {
                "a", "f", "e"
            });
            graph.Add("h", new List <string>()
            {
                "e"
            });
            graph.Add("i", new List <string>()
            {
                "d", "e"
            });
            //should be ignored
            graph.Add("j", new List <string>());
            graph.Add("k", new List <string>());

            ShortestPathAlgorithm shortestPathAlgorithm = new ShortestPathAlgorithm();
            List <string>         result;
            bool found = shortestPathAlgorithm.TryFindShortestPath(graph, "k", "a", out result);

            string[] resultArray = result.ToArray();

            Assert.IsFalse(found);
        }
Exemplo n.º 6
0
        public void ShortestPathTo_Throw_WhenDestinationIsNotInGraph(ShortestPathAlgorithm alg)
        {
            var graph = new DirectedWeightedSparseGraph <string>();

            graph.AddVertex("a");
            graph.AddVertex("b");
            graph.AddVertex("c");
            graph.AddVertex("d");

            var algorithm = CreateAlgorithm(alg, graph, "a");

            Assert.Throws <ArgumentException>(() => algorithm.ShortestPathTo("z"));
        }
Exemplo n.º 7
0
        public void DistanceTo_Throw_WhenVertexIsNotInGraph(ShortestPathAlgorithm alg)
        {
            var graph = new DirectedWeightedSparseGraph <string>();

            graph.AddVertex("a");
            graph.AddVertex("b");
            graph.AddVertex("c");

            graph.AddEdge("a", "b", 1);

            var algorithm = CreateAlgorithm(alg, graph, "a");

            Assert.Throws <ArgumentException>(() => algorithm.DistanceTo("z"));
        }
Exemplo n.º 8
0
        public void DistanceTo_ReturnInfinity_WhenVertexIsNotAchievable(ShortestPathAlgorithm alg)
        {
            var graph = new DirectedWeightedSparseGraph <string>();

            graph.AddVertex("a");
            graph.AddVertex("b");
            graph.AddVertex("c");

            graph.AddEdge("a", "b", 1);

            var algorithm = CreateAlgorithm(alg, graph, "a");

            Assert.Equal(long.MaxValue, algorithm.DistanceTo("c"));
        }
Exemplo n.º 9
0
        public void HasPathTo_ReturnFalse_WhenVertexIsNotAchievable(ShortestPathAlgorithm alg)
        {
            var graph = new DirectedWeightedSparseGraph <string>();

            graph.AddVertex("a");
            graph.AddVertex("b");
            graph.AddVertex("c");

            graph.AddEdge("a", "b", 1);

            var algorithm = CreateAlgorithm(alg, graph, "a");

            Assert.False(algorithm.HasPathTo("c"));
        }
        public void Finds_shortest_paths_in_complex_maps(Map map, string startId, string endId, string expectedPath, int expectedDistance)
        {
            var  shortestPath = new ShortestPathAlgorithm();
            Path path         = shortestPath.Find(map, startId, endId);

            if (expectedPath == null)
            {
                Assert.Null(path);
            }
            else
            {
                Assert.Equal(expectedPath, string.Join(',', path.NodeIds));
                Assert.Equal(expectedDistance, path.Distance);
            }
        }
        public void Finds_shortest_path_between_two_points_with_one_edge()
        {
            Map map = new Map("t1",
                              new Node("a"),
                              new Node("b"));

            map.AddEdge("a", "b", 1);

            var  shortestPath = new ShortestPathAlgorithm();
            Path p            = shortestPath.Find(map, "a", "b");

            Assert.Equal(2, p.NodeIds.Length);
            Assert.Equal("a", p.NodeIds.ElementAt(0));
            Assert.Equal("b", p.NodeIds.ElementAt(1));
            Assert.Equal(1, p.Distance);
        }
Exemplo n.º 12
0
        public void TestWith8Nodes()
        {
            // https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-greedy-algo-7/
            var node0 = new Node("0");
            var node1 = new Node("1");
            var node2 = new Node("2");
            var node3 = new Node("3");
            var node4 = new Node("4");
            var node5 = new Node("5");
            var node6 = new Node("6");
            var node7 = new Node("7");
            var node8 = new Node("8");

            AddTwoWayConnection(node0, node1, 4);
            AddTwoWayConnection(node0, node7, 8);

            AddTwoWayConnection(node1, node7, 11);
            AddTwoWayConnection(node1, node2, 8);

            AddTwoWayConnection(node2, node5, 4);
            AddTwoWayConnection(node2, node8, 2);
            AddTwoWayConnection(node2, node3, 7);

            AddTwoWayConnection(node3, node5, 14);
            AddTwoWayConnection(node3, node4, 9);

            AddTwoWayConnection(node4, node5, 10);

            AddTwoWayConnection(node5, node6, 2);

            AddTwoWayConnection(node6, node8, 6);
            AddTwoWayConnection(node6, node7, 1);

            AddTwoWayConnection(node7, node8, 7);

            var nodes = new List <Node> {
                node0, node1, node2,
                node3, node4, node5,
                node6, node7, node8
            };
            var algorithm = new ShortestPathAlgorithm(nodes);

            var result = algorithm.Run(node0);

            result.Should().NotBeNull();
            Print(result);
        }
Exemplo n.º 13
0
        public void ShortestPathTo_ReturnNull_WhenDestinationIsNotAchievable(ShortestPathAlgorithm alg)
        {
            var graph = new DirectedWeightedSparseGraph <string>();

            graph.AddVertex("a");
            graph.AddVertex("b");
            graph.AddVertex("c");
            graph.AddVertex("d");

            graph.AddEdge("a", "b", 1);
            graph.AddEdge("b", "c", 1);
            graph.AddEdge("c", "a", 1);

            var algorithm = CreateAlgorithm(alg, graph, "a");

            Assert.Null(algorithm.ShortestPathTo("d"));
        }
Exemplo n.º 14
0
        public void ShortestPathTo_ReturnSingleVertex_WhenDestinationIsSameAsSource(ShortestPathAlgorithm alg)
        {
            var graph = new DirectedWeightedSparseGraph <string>();

            graph.AddVertex("a");
            graph.AddVertex("b");
            graph.AddVertex("c");
            graph.AddVertex("d");

            graph.AddEdge("a", "b", 1);
            graph.AddEdge("b", "c", 1);
            graph.AddEdge("c", "a", 1);

            var algorithm = CreateAlgorithm(alg, graph, "a");
            var result    = algorithm.ShortestPathTo("a");

            Assert.NotNull(result);
            Assert.Single(result);
            Assert.Equal("a", result.Single());
        }
Exemplo n.º 15
0
        public void TestFromAtoBtoCtoD()
        {
            var nodeA = new Node("A");
            var nodeB = new Node("B");
            var nodeC = new Node("C");
            var nodeD = new Node("D");

            nodeA.AddConnection(nodeB, 10);
            nodeB.AddConnection(nodeC, 10);
            nodeC.AddConnection(nodeD, 10);

            var nodes = new List <Node> {
                nodeA, nodeB, nodeC, nodeD
            };
            var algorithm = new ShortestPathAlgorithm(nodes);

            var result = algorithm.Run(nodeA);

            result.Should().NotBeNull();
            Print(result);
        }
Exemplo n.º 16
0
        public void ShortestPathTo_FindShortestPath_WhenEdgeHaveDifferentWeight(ShortestPathAlgorithm alg)
        {
            var vertices = new[] { "r", "s", "t", "x", "y", "z" };
            var graph    = new DirectedWeightedSparseGraph <string>();

            graph.AddVertices(vertices);

            graph.AddEdge("r", "s", 7);
            graph.AddEdge("r", "t", 6);
            graph.AddEdge("s", "t", 5);
            graph.AddEdge("s", "x", 9);
            graph.AddEdge("t", "x", 10);
            graph.AddEdge("t", "y", 7);
            graph.AddEdge("t", "z", 5);
            graph.AddEdge("x", "y", 2);
            graph.AddEdge("x", "z", 4);
            graph.AddEdge("y", "z", 1);

            var algorithm   = CreateAlgorithm(alg, graph, "s");
            var shortestToZ = algorithm.ShortestPathTo("z");

            Assert.NotNull(shortestToZ);
            Assert.Equal(3, shortestToZ.Count());
            Assert.Contains("s", shortestToZ);
            Assert.Contains("t", shortestToZ);
            Assert.Contains("z", shortestToZ);
            Assert.Equal(10, algorithm.DistanceTo("z"));

            var shortestToY = algorithm.ShortestPathTo("y");

            Assert.NotNull(shortestToY);
            Assert.Equal(3, shortestToY.Count());
            Assert.Contains("s", shortestToY);
            Assert.Contains("x", shortestToY);
            Assert.Contains("y", shortestToY);
            Assert.Equal(11, algorithm.DistanceTo("y"));
        }
Exemplo n.º 17
0
        private void TestShortestPathsAlgorithm(IWeightedGraph <int, double> graph,
                                                IVertex <int> source,
                                                IVertex <int> target,
                                                double pathCosts,
                                                ShortestPathAlgorithm algorithm,
                                                double precision = 0.1)
        {
            IWeightedGraph <int, double> shortestPath = null;

            switch (algorithm)
            {
            case ShortestPathAlgorithm.Dijkstra:
                shortestPath = Dijkstra.FindShortestPath(graph, source, target, 0.0, double.MaxValue, (x, y) => x + y);
                break;

            case ShortestPathAlgorithm.BellmanFordMoore:
                shortestPath = BellmanFordMoore.FindShortestPath(graph, source, target, 0.0, double.MaxValue, (x, y) => x + y);
                break;

            default:
                throw new NotSupportedException($"Testing shortest path for the {algorithm} algorithm is currently not supported.");
            }
            AssertDoublesNearlyEqual(pathCosts, shortestPath.GetAllEdges().Sum(e => e.Weight), precision);
        }
Exemplo n.º 18
0
        public void Constructor_Throw_WhenAnyEdgeWeightIsLessThanZeroShortestPathAlgorithm(ShortestPathAlgorithm alg)
        {
            var graph = new DirectedWeightedSparseGraph <string>();

            graph.AddVertex("a");
            graph.AddVertex("b");

            graph.AddEdge("a", "b", -1);

            Assert.Throws <ArgumentException>(() => CreateAlgorithm(alg, graph, "a"));
        }
Exemplo n.º 19
0
        public void Constructor_Throw_WhenSourceVertexIsNull(ShortestPathAlgorithm alg)
        {
            var graph = new DirectedWeightedSparseGraph <string>();

            Assert.Throws <ArgumentNullException>(() => CreateAlgorithm(alg, graph, null));
        }
Exemplo n.º 20
0
 public void Constructor_Throw_WhenGraphInNull(ShortestPathAlgorithm alg)
 {
     Assert.Throws <ArgumentNullException>(() => CreateAlgorithm(alg, null, "vertex"));
 }