Ejemplo n.º 1
0
        private IEnumerable <ICell> ShortestPathCells(ICell source, ICell destination)
        {
            IEnumerable <DirectedEdge> path;
            int sourceIndex = IndexFor(source);

            if (_sourceIndex.HasValue && _sourceIndex == sourceIndex && _dijkstraShortestPath != null)
            {
                path = _dijkstraShortestPath.PathTo(IndexFor(destination));
            }
            else
            {
                _sourceIndex          = sourceIndex;
                _dijkstraShortestPath = new DijkstraShortestPath(_graph, sourceIndex);
                path = _dijkstraShortestPath.PathTo(IndexFor(destination));
            }

            if (path == null)
            {
                yield return(null);
            }
            else
            {
                yield return(source);

                foreach (DirectedEdge edge in path)
                {
                    yield return(CellFor(edge.To));
                }
            }
        }
        public void AssertCanNotSearch()
        {
            IGraph graph = new GraphAdjMatrix(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });

            graph.AddEdge(0, 1);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 3);
            graph.AddEdge(3, 4);
            graph.AddEdge(4, 5);
            graph.AddEdge(5, 6);
            graph.AddEdge(5, 8);
            graph.AddEdge(6, 7);
            graph.AddEdge(7, 8);
            graph.AddEdge(8, 5);


            graph.AddEdge(0, 9);
            graph.AddEdge(9, 10);
            graph.AddEdge(10, 11);


            var bfs    = new DijkstraShortestPath(graph);
            var result = bfs.CanFind(0, 12);

            Assert.IsFalse(result);
        }
        public void AssertCanSearchShortestPathWithMultipleParents()
        {
            IGraph graph = new GraphAdjMatrix(new[] { 0, 1, 2, 3, 4, 5, 6 });

            graph.AddEdge(0, 1, 1);
            graph.AddEdge(0, 3, 3);
            graph.AddEdge(1, 2, 1);
            graph.AddEdge(2, 4, 1);
            graph.AddEdge(4, 6, 4);
            graph.AddEdge(3, 5, 3);
            graph.AddEdge(5, 6, 1);
            graph.AddEdge(3, 6, 5);

            var bfs    = new DijkstraShortestPath(graph);
            var result = bfs.CanFind(0, 6);

            Assert.IsTrue(result);

            var direction = bfs.PathToGoal();

            Assert.AreEqual(0, direction[0]);
            Assert.AreEqual(3, direction[1]);
            Assert.AreEqual(5, direction[2]);
            Assert.AreEqual(6, direction[3]);
        }
        public void FindShortestPath_FromSampleGraphNodeG_SampleGraphShortestPathResultG()
        {
            var actualShortestPathResult   = DijkstraShortestPath.FindShortestPath(_SampleData.NodeG, _SampleData.Graph);
            var expectedShortestPathResult = _SampleData.ShortestPathResultG;

            foreach (var n in actualShortestPathResult.DistanceToNodes.Keys)
            {
                var actual = actualShortestPathResult.DistanceToNodes[n];

                var expectedKey = expectedShortestPathResult.DistanceToNodes.Keys.FirstOrDefault(p => p.CompareTo(n) == 0);
                var expected    = expectedShortestPathResult.DistanceToNodes[expectedKey];
                Assert.AreEqual(expected, actual);
            }

            foreach (var n in actualShortestPathResult.PreviousNode.Keys)
            {
                var actual = actualShortestPathResult.PreviousNode[n];

                var expectedKey = expectedShortestPathResult.PreviousNode.Keys.FirstOrDefault(p => p.CompareTo(n) == 0);
                var expected    = expectedShortestPathResult.PreviousNode[expectedKey];

                if (actual == null)
                {
                    Assert.IsNull(expected);
                }
                else
                {
                    Assert.IsTrue(expected.CompareTo(actual) == 0);
                }
            }
        }
Ejemplo n.º 5
0
        public int SolveFourthLink()
        {
            var g = ParseWeightedGraphFromWeb(Link4);

            var reweightedGraph = new JohnsonReweighting().ReweightEdges(g);
            var res             = new DijkstraShortestPath().GetMinAllPairsShortestPaths(reweightedGraph);

            return(res);
        }
Ejemplo n.º 6
0
        public void Execute(Graph graph)
        {
            System.Console.WriteLine("Je haalt het kompas uit je zak. Het trilt in je hand en projecteert in lichtgevende letters op de muur:");

            DijkstraShortestPath d = new DijkstraShortestPath();
            var path = d.Execute(graph);

            PrintLight(path, graph.EndPoint);
            System.Console.WriteLine("\n");
            PrintEnemies(path, graph.EndPoint);
        }
        public void DistanceTo_WhenPathExistsBetween0And4_WillBeSumOfWeightsInPath()
        {
            EdgeWeightedDigraph digraph = new EdgeWeightedDigraph(5);

            digraph.AddEdge(new DirectedEdge(0, 1, 2.5));
            digraph.AddEdge(new DirectedEdge(1, 2, 3.25));
            digraph.AddEdge(new DirectedEdge(2, 3, 1.25));
            digraph.AddEdge(new DirectedEdge(3, 4, 2));
            DijkstraShortestPath dijkstra = new DijkstraShortestPath(digraph, 0);

            Assert.AreEqual(9, dijkstra.DistanceTo(4));
        }
        public void HasPathTo_WhenPathDoesNotExistBetween4And0_WillBeFalse()
        {
            EdgeWeightedDigraph digraph = new EdgeWeightedDigraph(5);

            digraph.AddEdge(new DirectedEdge(0, 1, 2.5));
            digraph.AddEdge(new DirectedEdge(1, 2, 3.25));
            digraph.AddEdge(new DirectedEdge(2, 3, 1.25));
            digraph.AddEdge(new DirectedEdge(3, 4, 2));
            DijkstraShortestPath dijkstra = new DijkstraShortestPath(digraph, 4);

            Assert.IsFalse(dijkstra.HasPathTo(0));
        }
        public void HasPathTo_WhenPathExistsBetween0And4_WillBeTrue()
        {
            EdgeWeightedDigraph digraph = new EdgeWeightedDigraph(5);

            digraph.AddEdge(new DirectedEdge(0, 1, 2.5));
            digraph.AddEdge(new DirectedEdge(1, 2, 3.25));
            digraph.AddEdge(new DirectedEdge(2, 3, 1.25));
            digraph.AddEdge(new DirectedEdge(3, 4, 2));
            DijkstraShortestPath dijkstra = new DijkstraShortestPath(digraph, 0);

            Assert.IsTrue(dijkstra.HasPathTo(4));
        }
        public void Check_WhenGivenValidGraph_WillBeTrue()
        {
            EdgeWeightedDigraph digraph = new EdgeWeightedDigraph(5);

            digraph.AddEdge(new DirectedEdge(0, 1, 2.5));
            digraph.AddEdge(new DirectedEdge(1, 2, 3.25));
            digraph.AddEdge(new DirectedEdge(1, 4, 4.75));
            digraph.AddEdge(new DirectedEdge(2, 3, 1.25));
            digraph.AddEdge(new DirectedEdge(3, 4, 2));
            DijkstraShortestPath dijkstra = new DijkstraShortestPath(digraph, 0);

            Assert.IsTrue(dijkstra.Check(digraph, 0));
        }
        public void DistanceTo_WhenNoPathExists_WillBePositiveInfinity()
        {
            EdgeWeightedDigraph digraph = new EdgeWeightedDigraph(5);

            digraph.AddEdge(new DirectedEdge(0, 1, 2.5));
            digraph.AddEdge(new DirectedEdge(1, 2, 3.25));
            digraph.AddEdge(new DirectedEdge(1, 4, 4.75));
            digraph.AddEdge(new DirectedEdge(2, 3, 1.25));
            digraph.AddEdge(new DirectedEdge(3, 4, 2));
            DijkstraShortestPath dijkstra = new DijkstraShortestPath(digraph, 4);

            Assert.AreEqual(double.PositiveInfinity, dijkstra.DistanceTo(0));
        }
Ejemplo n.º 12
0
        public void CanCalcShortestPath5()
        {
            var graph = new[]
            {
                "1 2,8 3,17",
                "2 1,7 3,10 4,5",
                "3 1,12",
                "4 3,3"
            };

            var paths = new DijkstraShortestPath().CalcShortestPaths(graph, 1);

            AssertHelper.Seq(new long[] { 0, 8, 16, 13 }, paths.Skip(1));
        }
Ejemplo n.º 13
0
        public void CanCalcShortestPath3()
        {
            var graph = new[]
            {
                "1 2,3 3,5",
                "2 3,1 4,2",
                "3 4,50	  ",
                "4 		  ",
            };

            var paths = new DijkstraShortestPath().CalcShortestPaths(graph, 1);

            AssertHelper.Seq(new long[] { 0, 3, 4, 5 }, paths.Skip(1));
        }
        public void FindShortestPath_FirstShortestNotTheBest_2levels_MatchExpectations()
        {
            #region setup
            var start = new DijkstraShortestPath.Vertex(1);
            var l11   = new DijkstraShortestPath.Vertex(2);
            var l12   = new DijkstraShortestPath.Vertex(3);
            var l21   = new DijkstraShortestPath.Vertex(4);
            var l22   = new DijkstraShortestPath.Vertex(5);
            var end   = new DijkstraShortestPath.Vertex(6);

            start.Edges.AddRange(new []
            {
                new DijkstraShortestPath.Edge(30)
                {
                    End = l11
                },
                new DijkstraShortestPath.Edge(20)
                {
                    End = l12
                },
            });

            l11.Edges.Add(new DijkstraShortestPath.Edge(60)
            {
                End = l21
            });
            l12.Edges.Add(new DijkstraShortestPath.Edge(50)
            {
                End = l22
            });

            l21.Edges.Add(new DijkstraShortestPath.Edge(10)
            {
                End = end
            });
            l22.Edges.Add(new DijkstraShortestPath.Edge(40)
            {
                End = end
            });
            #endregion setup

            var path = DijkstraShortestPath.FindShortestPath(start, end);

            using var scope = new AssertionScope();
            path.Select(p => p.Item2).Sum().Should().Be(100);
            path[0].Item1.Data.Should().Be(1);
            path[1].Item1.Data.Should().Be(2);
            path[2].Item1.Data.Should().Be(4);
            path[3].Item1.Data.Should().Be(6);
        }
Ejemplo n.º 15
0
        public void ShortestPath_LargeDataSet_RunsInExpectedTime()
        {
            // arrange
            RoadNetworkParser parser = new RoadNetworkParser(Resources.WashingtonRoadNetwork);
            DijkstraShortestPath d = new DijkstraShortestPath(parser.Vertices, parser.Edges);
            Stopwatch s = Stopwatch.StartNew();

            // act
            double distance = d.ShortestPath(456458, 456399);

            // assert
            Assert.IsTrue(s.Elapsed < TimeSpan.FromSeconds(10));
            Assert.IsTrue(distance < double.MaxValue);
        }
        public void FindPath_WhenNoPathExists_WillBeNull()
        {
            EdgeWeightedDigraph digraph = new EdgeWeightedDigraph(5);

            digraph.AddEdge(new DirectedEdge(0, 1, 2.5));
            digraph.AddEdge(new DirectedEdge(1, 2, 3.25));
            digraph.AddEdge(new DirectedEdge(1, 4, 4.75));
            digraph.AddEdge(new DirectedEdge(2, 3, 1.25));
            digraph.AddEdge(new DirectedEdge(3, 4, 2));

            IEnumerable <DirectedEdge> path = DijkstraShortestPath.FindPath(digraph, 4, 0);

            Assert.IsNull(path);
        }
Ejemplo n.º 17
0
        public void CanCalcShortestPath1()
        {
            var graph = new[]
            {
                "1		2,7		6,14		3,9",
                "2		1,7		3,10		4,15",
                "3		1,9		2,10		6,2		4,11",
                "4		2,15	3,11		5,6",
                "5		4,6		6,9",
                "6		1,14	3,2			5,9"
            };

            var paths = new DijkstraShortestPath().CalcShortestPaths(graph, 1);

            AssertHelper.Seq(new long[] { 0, 7, 9, 20, 20, 11 }, paths.Skip(1));
        }
        public void FindPath_WhenMultiplePathsExistBetween0And4_WillBeShortestPath()
        {
            EdgeWeightedDigraph digraph = new EdgeWeightedDigraph(5);

            digraph.AddEdge(new DirectedEdge(0, 1, 2.5));
            digraph.AddEdge(new DirectedEdge(1, 2, 3.25));
            digraph.AddEdge(new DirectedEdge(1, 4, 4.75));
            digraph.AddEdge(new DirectedEdge(2, 3, 1.25));
            digraph.AddEdge(new DirectedEdge(3, 4, 2));

            DirectedEdge[] path = DijkstraShortestPath.FindPath(digraph, 0, 4).ToArray();

            Assert.AreEqual("From: 0, To: 1, Weight: 2.5", path[0].ToString());
            Assert.AreEqual("From: 1, To: 4, Weight: 4.75", path[1].ToString());
            Assert.AreEqual(2, path.Length);
        }
Ejemplo n.º 19
0
        public void ShortestPath_AdjacentNodes_FindsPath()
        {
            // arrange
            HashSet<int> v = new HashSet<int>();
            Dictionary<int, Dictionary<int, double>> e = new Dictionary<int, Dictionary<int, double>>();
            v.Add(1);
            v.Add(2);
            e[1] = new Dictionary<int, double>();
            e[2] = new Dictionary<int, double>();
            e[1][2] = e[2][1] = 1;
            DijkstraShortestPath d = new DijkstraShortestPath(v, e);

            // act
            // assert
            Assert.AreEqual(1, d.ShortestPath(1, 2));
        }
Ejemplo n.º 20
0
        private IEnumerable <Cell> ShortestPathCells(Cell source, Cell destination)
        {
            IEnumerable <DirectedEdge> path = DijkstraShortestPath
                                              .FindPath(_graph, IndexFor(source.Point), IndexFor(destination.Point));

            if (path == null)
            {
                yield break;
            }

            yield return(source);

            foreach (DirectedEdge edge in path)
            {
                yield return(CellFor(edge.To));
            }
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Returns an ordered IEnumerable of Cells representing the shortest path from a specified source Cell to a destination Cell
        /// </summary>
        /// <param name="source">The Cell which is at the start of the path</param>
        /// <param name="destination">The Cell which is at the end of the path</param>
        /// <returns>Returns an ordered IEnumerable of Cells representing the shortest path from a specified source Cell to a destination Cell</returns>
        public IEnumerable <Cell> ShortestPath(Cell source, Cell destination)
        {
            var dsp = new DijkstraShortestPath(_graph, IndexFor(source));
            IEnumerable <DirectedEdge> path = dsp.PathTo(IndexFor(destination));

            if (path == null)
            {
                yield return(null);
            }
            else
            {
                foreach (DirectedEdge edge in path)
                {
                    yield return(CellFor(edge.To));
                }
            }
        }
Ejemplo n.º 22
0
        public void Test()
        {
            // orientovaný ohodnocený.
            var graph1 = new DirectedWeightedGraph(false);

            for (int i = 'a'; i < 'h'; i++)
            {
                graph1.AddVertex(Convert.ToString(Convert.ToChar(i)));
            }

            graph1.AddEdge("a", "b", 10);
            graph1.AddEdge("b", "c", 20);
            graph1.AddEdge("c", "d", 3);
            graph1.AddEdge("d", "e", 4);
            graph1.AddEdge("f", "c", 2);
            graph1.AddEdge("b", "f", 3);
            graph1.AddEdge("e", "b", 1);

            DijkstraShortestPath <DirectedWeightedGraph> .Search(graph1, "a");

            Assert.AreEqual(0, graph1.GetVertex("a").distance);
            Assert.AreEqual(10, graph1.GetVertex("b").distance);
            Assert.AreEqual(15, graph1.GetVertex("c").distance);
            Assert.AreEqual(18, graph1.GetVertex("d").distance);
            Assert.AreEqual(22, graph1.GetVertex("e").distance);
            Assert.AreEqual(13, graph1.GetVertex("f").distance);
            Assert.AreEqual(long.MaxValue, graph1.GetVertex("g").distance);


            Stack <Vertex> stack = new Stack <Vertex>();

            stack.Push(graph1.GetVertex("e"));
            stack.Push(graph1.GetVertex("d"));
            stack.Push(graph1.GetVertex("c"));
            stack.Push(graph1.GetVertex("f"));
            stack.Push(graph1.GetVertex("b"));
            stack.Push(graph1.GetVertex("a"));

            var stackTest = DijkstraShortestPath <DirectedWeightedGraph> .GetShortestPath(graph1, "e");

            while (stack.Count > 0)
            {
                Assert.AreEqual(stack.Pop(), stackTest.Pop());
            }
        }
Ejemplo n.º 23
0
        public List <Point> ShortestPathList(Point source, Point destination)
        {
            var list = new List <Point>();
            IEnumerable <DirectedEdge> path = DijkstraShortestPath
                                              .FindPath(_graph, IndexFor(source), IndexFor(destination));

            if (path == null)
            {
                return(list);
            }

            foreach (DirectedEdge edge in path)
            {
                list.Add(CoordFor(edge.To));
            }

            return(list);
        }
Ejemplo n.º 24
0
        private IEnumerable <ICell> ShortestPathCells(ICell source, ICell destination)
        {
            var path = DijkstraShortestPath.FindPath(_graph, IndexFor(source), IndexFor(destination));

            if (path == null)
            {
                yield return(null);
            }
            else
            {
                yield return(source);

                foreach (var edge in path)
                {
                    yield return(CellFor(edge.To));
                }
            }
        }
Ejemplo n.º 25
0
        public void ShortestPath_ConsecutiveNodesSinglePathSameWeight_FindsPath()
        {
            // arrange
            HashSet<int> v = new HashSet<int>();
            Dictionary<int, Dictionary<int, double>> e = new Dictionary<int, Dictionary<int, double>>();
            v.Add(1);
            v.Add(2);
            v.Add(3);
            e[1] = new Dictionary<int, double>();
            e[2] = new Dictionary<int, double>();
            e[3] = new Dictionary<int, double>();
            e[1][2] = e[2][1] = e[3][2] = e[2][3] = 1;
            DijkstraShortestPath d = new DijkstraShortestPath(v, e);

            // act
            // assert
            Assert.AreEqual(2, d.ShortestPath(1, 3));
        }
        public void testConstructor()
        {
            GraphPath <String, DefaultWeightedEdge> path;
            Graph <String, DefaultWeightedEdge>     g = create();

            path = new DijkstraShortestPath <string, DefaultWeightedEdge>(g, Double.PositiveInfinity).getPath(V1, V5);
            Assert.AreEqual(new List <DefaultWeightedEdge>()
            {
                e13, e12, e24
            }, path.getEdgeList());
            //assertEquals(Arrays.asList(e13, e12, e24), path.getEdgeList());
            Assert.AreEqual(10.0, path.getWeight(), 0);
//            assertEquals(10.0, path.getWeight(), 0);

            path = new DijkstraShortestPath <string, DefaultWeightedEdge>(g, 7.0).getPath(V3, V4);
            Assert.AreEqual(null, path);
//            assertNull(path);
        }
Ejemplo n.º 27
0
        public void ShortestPath_KeysNotInGraph_ThrowsException()
        {
            // arrange
            HashSet<int> v = new HashSet<int>();
            Dictionary<int, Dictionary<int, double>> e = new Dictionary<int, Dictionary<int, double>>();
            v.Add(1);
            v.Add(2);
            v.Add(3);
            e[1] = new Dictionary<int, double>();
            e[2] = new Dictionary<int, double>();
            e[3] = new Dictionary<int, double>();
            e[1][2] = e[2][1] = e[3][2] = e[2][3] = 1;
            DijkstraShortestPath d = new DijkstraShortestPath(v, e);

            // act
            // assert
            Assert.AreEqual(double.MaxValue, d.ShortestPath(1, 4));
        }
Ejemplo n.º 28
0
        private IEnumerable <ITile> ShortestPathCells(ITile source, ITile destination)
        {
            IEnumerable <DirectedEdge> path = DijkstraShortestPath.FindPath(_graph, IndexFor(source), IndexFor(destination));

            if (path == null)
            {
                yield return(null);
            }
            else
            {
                yield return(source);

                foreach (DirectedEdge edge in path)
                {
                    yield return(CellFor(edge.To));
                }
            }
        }
Ejemplo n.º 29
0
        public void DijkstraShortestPathTest()
        {
            var dg = EdgeWeightedDigraphSample();
            var sp = new DijkstraShortestPath(dg, 0);
            var es = new DirectedEdge[] {
                new DirectedEdge(0, 2, 0.26),
                new DirectedEdge(2, 7, 0.34),
                new DirectedEdge(7, 3, 0.39),
                new DirectedEdge(3, 6, 0.52),
            };
            int i = 0;

            foreach (var e in sp.PathTo(6))
            {
                Assert.Equal(e, es[i]);
                i++;
            }
            Assert.Equal(1.51, sp.DistTo(6), 2);
        }
        public void TestGraphOne()
        {
            var graph = new Dictionary <int, List <WeightedEdge> >();

            graph.Add(0, new List <WeightedEdge>()
            {
                new WeightedEdge()
                {
                    Destination = 1, Weight = 2
                },
                new WeightedEdge()
                {
                    Destination = 2, Weight = 4
                }
            });
            graph.Add(1, new List <WeightedEdge>()
            {
                new WeightedEdge()
                {
                    Destination = 2, Weight = 1
                },
                new WeightedEdge()
                {
                    Destination = 3, Weight = 3
                }
            });

            graph.Add(2, new List <WeightedEdge>()
            {
                new WeightedEdge()
                {
                    Destination = 3, Weight = 1
                }
            });

            var path = new DijkstraShortestPath(graph, 4, 0).Compute();

            Assert.That(path[0], Is.EqualTo(0));
            Assert.That(path[1], Is.EqualTo(2));
            Assert.That(path[2], Is.EqualTo(3));
            Assert.That(path[3], Is.EqualTo(4));
        }
        public void PathTo_WhenCalledOnMediumSizedGraphForEachNode_WillFindExpectedPaths()
        {
            string path = @"Algorithms\TestSetup\mediumEWD.txt";
            EdgeWeightedDigraph graph = TestSetup.TestHelpers.CreateEdgeWeightedDigraphFromFile(path);

            int longestPathLength    = 0;
            int longestPathStartNode = 0;
            int longestPathEndNode   = 0;

            for (int i = 0; i < graph.NumberOfVertices; i++)
            {
                DijkstraShortestPath dsp = new DijkstraShortestPath(graph, i);
                for (int j = 0; j < graph.NumberOfVertices; j++)
                {
                    var dspPath = dsp.PathTo(j);
                    int length  = dspPath.Count();
                    if (length > longestPathLength)
                    {
                        longestPathLength    = length;
                        longestPathStartNode = i;
                        longestPathEndNode   = j;
                        Console.WriteLine($"Path from {i} -> {j} = {length}");
                        /////// Console Output /////////
                        // Path from 0-> 1 = 9
                        // Path from 0-> 6 = 10
                        // Path from 0-> 22 = 11
                        // Path from 0-> 29 = 12
                        // Path from 0-> 38 = 13
                        // Path from 8-> 237 = 14
                        // Path from 10-> 63 = 15
                        // Path from 10-> 237 = 16
                        ///////////////////////////////
                    }
                }
            }

            Assert.AreEqual(16, longestPathLength);
            Assert.AreEqual(10, longestPathStartNode);
            Assert.AreEqual(237, longestPathEndNode);
            Assert.AreEqual(250, graph.NumberOfVertices);
            Assert.AreEqual(2546, graph.NumberOfEdges);
        }
 private void btnCreate_Click(object sender, EventArgs e)
 {
     noOfNodes = Convert.ToInt32(txtNoOfNodes.Text);
     if (noOfNodes <= 0 || noOfNodes > DijkstraShortestPath.MAXNODE)
     {
         MessageBox.Show("Wrong Input! Please, Read in correct digit from 1 to 100:", "MobileAgentBasedIDS", MessageBoxButtons.OK, MessageBoxIcon.Error);
         //txtNoOfNodes.
     }
     else
     {
         for (int i = 1; i <= noOfNodes; i++)
         {
             lstNodes.Items.Add("Node " + i);
         }
         dijkstraShortestPath = new DijkstraShortestPath(noOfNodes);
         //int[,] Weight = dijkstraShortestPath.GenerateWeight();
         dijkstraShortestPath.GenerateWeight();
         Console.WriteLine("Nodes and Distance(Weight) Table");
         displayListOfNodesAndWeights(dijkstraShortestPath.Weight, 1);
     }
 }
        public void FindShortestPath_Straightforward_MatchExpectations()
        {
            #region setup
            var start = new DijkstraShortestPath.Vertex(1);
            var l11   = new DijkstraShortestPath.Vertex(2);
            var l12   = new DijkstraShortestPath.Vertex(3);
            var end   = new DijkstraShortestPath.Vertex(4);

            start.Edges.AddRange(new []
            {
                new DijkstraShortestPath.Edge(10)
                {
                    End = l11
                },
                new DijkstraShortestPath.Edge(20)
                {
                    End = l12
                },
            });

            l11.Edges.Add(new DijkstraShortestPath.Edge(30)
            {
                End = end
            });
            l12.Edges.Add(new DijkstraShortestPath.Edge(40)
            {
                End = end
            });
            #endregion setup

            var path = DijkstraShortestPath.FindShortestPath(start, end);

            using var scope = new AssertionScope();
            path.Select(p => p.Item2).Sum().Should().Be(40);
            path[0].Item1.Data.Should().Be(1);
            path[1].Item1.Data.Should().Be(2);
            path[2].Item1.Data.Should().Be(4);
        }
Ejemplo n.º 34
0
        public void FindPathes_FromTestData_FindAllPathesCorrect(string input, string output)
        {
            FindShortPathTestData testData = FindShortPathesTestDataBuilder.CreateTestData(
                input,
                output);

            DijkstraShortestPath dijkstra = new DijkstraShortestPath(testData.Graph);

            foreach (int vertex in testData.Graph.Vertexes)
            {
                Dictionary <int, Result> pathes = dijkstra.FindShortestPath(vertex);

                int[] distances = pathes
                                  .OrderBy(p => p.Key)
                                  .Select(p => p.Value.Distance)
                                  .ToArray();

                Assert.That(
                    distances,
                    Is.EqualTo(testData.Distances[vertex]),
                    string.Format("Vertex = {0}", vertex));
            }
        }
Ejemplo n.º 35
0
        public void ShortestPath_NodesNotConnected_ReturnsNotFound()
        {
            // arrange
            HashSet<int> v = new HashSet<int>();
            Dictionary<int, Dictionary<int, double>> e = new Dictionary<int, Dictionary<int, double>>();
            v.Add(1);
            v.Add(2);
            v.Add(3);
            v.Add(4);
            e[1] = new Dictionary<int, double>();
            e[2] = new Dictionary<int, double>();
            e[3] = new Dictionary<int, double>();
            e[4] = new Dictionary<int, double>();
            e[1][2] = e[2][1] = 1;
            e[3][4] = e[4][3] = 1;
            DijkstraShortestPath d = new DijkstraShortestPath(v, e);

            // act
            // assert
            Assert.AreEqual(double.MaxValue, d.ShortestPath(1, 4));
        }
Ejemplo n.º 36
0
        public void ShortestPath_ShorterPathIsMoreNodes_ReturnsNotFound()
        {
            // arrange
            HashSet<int> v = new HashSet<int>();
            v.Add(1);
            v.Add(2);
            v.Add(3);
            v.Add(4);

            Dictionary<int, Dictionary<int, double>> e = new Dictionary<int, Dictionary<int, double>>();
            e[1] = new Dictionary<int, double>();
            e[2] = new Dictionary<int, double>();
            e[3] = new Dictionary<int, double>();
            e[4] = new Dictionary<int, double>();

            e[1][2] = e[2][1] = 1;
            e[1][4] = e[4][1] = 7;
            e[2][3] = e[3][2] = 2;
            e[3][4] = e[4][3] = 3;

            DijkstraShortestPath d = new DijkstraShortestPath(v, e);

            // act
            // assert
            Assert.AreEqual(6, d.ShortestPath(1, 4));
        }