public void TestCase1()
        {
            var vertices = new ExtendedVertex[]
                {
                    new ExtendedVertex(1, new AdjacentEdge[]
                        {
                            new AdjacentEdge(2,10),
                            new AdjacentEdge(4,30),
                            new AdjacentEdge(5,100),
                        }),
                    new ExtendedVertex(2, new AdjacentEdge[]
                    {
                        new AdjacentEdge(3,50),
                    }),
                    new ExtendedVertex(3, new AdjacentEdge[]
                        {
                            new AdjacentEdge(5,10),
                        }),
                    new ExtendedVertex(4, new AdjacentEdge[]
                    {
                        new AdjacentEdge(3,20),
                        new AdjacentEdge(5,50),
                    }),
                    new ExtendedVertex(5, new AdjacentEdge[0]),
                };

            var alg = new DijkstraAlgorithm(vertices);
            var minPaths = alg.CalculateMinPath();
            Assert.AreEqual(5, minPaths.Length);
            Assert.AreEqual("0,10,50,30,60", string.Join(",", minPaths));
        }
Пример #2
0
        static void Main(string[] args)
        {
            string filePath = args[0];
            var stopWatch = new Stopwatch();
            stopWatch.Start();

            var nodesEnumerable = File.ReadLines(filePath);
            var vertices = nodesEnumerable.Select(node => node.Split(new[] { ' ', '\t', ',' }, StringSplitOptions.RemoveEmptyEntries))
                               .Select(nodeArray =>
                                   {
                                       int nodeId = int.Parse(nodeArray[0]);
                                       var adjacentList = new List<AdjacentEdge>();
                                       for (int i = 1; i < nodeArray.Length; i +=2)
                                       {
                                           adjacentList.Add(new AdjacentEdge(int.Parse(nodeArray[i]), int.Parse(nodeArray[i+1])));
                                       }
                                       return new ExtendedVertex(nodeId, adjacentList.ToArray());
                                   })
                               .ToArray();

            stopWatch.Stop();
            var initTime = stopWatch.ElapsedMilliseconds;
            Console.WriteLine("read and init time={0}", initTime);
            stopWatch.Restart();
            var alg = new DijkstraAlgorithm(vertices);
            var result = alg.CalculateMinPath();
            stopWatch.Stop();
            Console.WriteLine("running time = {0}, result={1}", stopWatch.ElapsedMilliseconds, string.Join(
                ",",
                result[6],
                result[36],
                result[58],
                result[81],
                result[98],
                result[114],
                result[132],
                result[164],
                result[187],
                result[196]));
            Console.WriteLine("press any key");
            Console.ReadLine();
        }
        public void TestCase2()
        {
            var vertices = new ExtendedVertex[]
                {
                    new ExtendedVertex(1, new AdjacentEdge[]
                        {
                            new AdjacentEdge(2,50),
                            new AdjacentEdge(3,10),
                            new AdjacentEdge(5,45),
                        }),
                    new ExtendedVertex(2, new AdjacentEdge[]
                    {
                        new AdjacentEdge(5,10),
                    }),
                    new ExtendedVertex(3, new AdjacentEdge[]
                        {
                            new AdjacentEdge(1,20),
                            new AdjacentEdge(4,15),
                        }),
                    new ExtendedVertex(4, new AdjacentEdge[]
                    {
                        new AdjacentEdge(2,20),
                        new AdjacentEdge(5,35),
                    }),
                    new ExtendedVertex(5, new AdjacentEdge[]
                        {
                            new AdjacentEdge(4,30),
                        }),
                    new ExtendedVertex(6, new AdjacentEdge[]
                        {
                            new AdjacentEdge(4,3),
                        }),
                };

            var alg = new DijkstraAlgorithm(vertices);
            Assert.AreEqual("0,45,10,25,45,1000000", string.Join(",", alg.CalculateMinPath()));
            Assert.AreEqual("1000000,50,1000000,30,0,1000000", string.Join(",", alg.CalculateMinPath(5)));
            Assert.AreEqual("1000000,23,1000000,3,33,0", string.Join(",", alg.CalculateMinPath(6)));
        }