public void CheckOneVertice()
    {
        double[,] graph = { { 0.0 } };
        DijkstraAlgorithm.Dijkstra AlgorithmClass = new DijkstraAlgorithm.Dijkstra(graph);
        AlgorithmClass.CallDijkstraAlgorithm(0, 0);
        Assert.AreEqual(AlgorithmClass.GetMinimumDistance(0), 0.0);

        List <int> VerticesPath = new List <int> {
            0
        };

        Assert.AreEqual(AlgorithmClass.GetVerticesPath(), VerticesPath);
    }
    public void CheckOutOfRangeVertices()
    {
        double[,] graph = { { 0.0, 1.0 },
                            { 1.0, 0.0 } };
        DijkstraAlgorithm.Dijkstra AlgorithmClass = new DijkstraAlgorithm.Dijkstra(graph);

        Assert.Throws <System.ArgumentOutOfRangeException>(() => AlgorithmClass.GetMinimumDistance(-1));
        Assert.Throws <System.ArgumentOutOfRangeException>(() => AlgorithmClass.GetMinimumDistance(2));

        Assert.Throws <System.ArgumentOutOfRangeException>(() => AlgorithmClass.CallDijkstraAlgorithm(-1, 0));
        Assert.Throws <System.ArgumentOutOfRangeException>(() => AlgorithmClass.CallDijkstraAlgorithm(2, 0));
        Assert.Throws <System.ArgumentOutOfRangeException>(() => AlgorithmClass.CallDijkstraAlgorithm(0, -1));
        Assert.Throws <System.ArgumentOutOfRangeException>(() => AlgorithmClass.CallDijkstraAlgorithm(0, 2));
    }
Example #3
0
        static void Main(string[] args)
        {
            Matrix mtr = new Matrix("data.txt");//заполнение массива элементами из filename

            /*
             * Console.WriteLine("Write the start and end vertex from 1");
             * string input;
             * string [] buf;
             * int start;
             * int end;
             *
             * input = Console.ReadLine();
             * buf = input.Split(' ');
             * start = Convert.ToInt32(buf[0]);
             * end = Convert.ToInt32(buf[1]);
             */

            Dijkstra dijkstra = new Dijkstra(mtr.Data, mtr.Size);

            try
            {
                Console.WriteLine(dijkstra.CalcShortesWay(3, 0));
            }
            catch (IndexOutOfRangeException e)
            {
                Console.Write($"{e.GetType().Name} : {3} or {0} is out of graph or equals");
            }

            try
            {
                Console.WriteLine(dijkstra.CalcShortesWay(0, 2));
            }
            catch (IndexOutOfRangeException e)
            {
                Console.Write($"{e.GetType().Name} : {0} or {2} is out of graph or equals");
            }
            Console.ReadKey();
        }
    public void TestWikipediaExample()
    {
        double[,] graph = { {  0.0,  7.0,  9.0,  0.0, 0.0, 14.0 },
                            {  7.0,  0.0, 10.0, 15.0, 0.0,  0.0 },
                            {  9.0, 10.0,  0.0, 11.0, 0.0,  2.0 },
                            {  0.0, 15.0, 11.0,  0.0, 6.0,  0.0 },
                            {  0.0,  0.0,  0.0,  6.0, 0.0,  9.0 },
                            { 14.0,  0.0,  2.0,  0.0, 9.0,  0.0 } };
        DijkstraAlgorithm.Dijkstra AlgorithmClass = new DijkstraAlgorithm.Dijkstra(graph);
        AlgorithmClass.CallDijkstraAlgorithm(0, 5);
        Assert.AreEqual(AlgorithmClass.GetMinimumDistance(0), 0.0);
        Assert.AreEqual(AlgorithmClass.GetMinimumDistance(1), 7.0);
        Assert.AreEqual(AlgorithmClass.GetMinimumDistance(2), 9.0);
        Assert.AreEqual(AlgorithmClass.GetMinimumDistance(3), 20.0);
        Assert.AreEqual(AlgorithmClass.GetMinimumDistance(4), 20.0);
        Assert.AreEqual(AlgorithmClass.GetMinimumDistance(5), 11.0);

        List <int> VerticesPath = new List <int> {
            0, 2, 5
        };

        Assert.AreEqual(AlgorithmClass.GetVerticesPath(), VerticesPath);
    }
        // Use this for initialization
        void Start()
        {
            // Исходные данные по сетке
            tgs = TerrainGridSystem.instance;
            List <Cell> myCells = tgs.cells;
            int         Nx      = tgs.rowCount;
            int         Nz      = tgs.columnCount;

            // Задать исходную высоту
            Cell    zeroCell  = myCells[0];
            Vector3 zeroCoord = tgs.CellGetPosition(0);

            using (var file = new StreamWriter(@"D:\Vec_grid.txt", true))
            {
                for (int row = 0; row < Nx; row++)
                {
                    for (int column = 0; column < Nz; column++)
                    {
                        Vector3 centerCoord = tgs.CellGetPosition(myCells[column + Nz * row]);
                        String  str         = centerCoord.x.ToString() + "\t" + centerCoord.z.ToString() + "\t" + centerCoord.y.ToString();
                        file.WriteLine(str);
                    }
                    file.WriteLine("\n");
                }
            }

            // Заполнить весы графа
            double[,] dijk_grid = new double[Nx * Nz, Nx *Nz];
            for (int row = 0; row < Nx; row++)
            {
                for (int column = 0; column < Nz; column++)
                {
                    int     currentIndex = column + Nz * row;
                    Vector3 centerCoord  = tgs.CellGetPosition(myCells[currentIndex]);

                    if (row != 0)
                    {
                        int     leftIndex = column + Nz * (row - 1);
                        Vector3 leftCoord = tgs.CellGetPosition(myCells[leftIndex]);
                        dijk_grid[currentIndex, leftIndex] = Vector3.Distance(centerCoord, leftCoord);
                    }

                    if (row != (Nx - 1))
                    {
                        int     rightIndex = column + Nz * (row + 1);
                        Vector3 rightCoord = tgs.CellGetPosition(myCells[rightIndex]);
                        dijk_grid[currentIndex, rightIndex] = Vector3.Distance(centerCoord, rightCoord);
                    }

                    if (column != (Nz - 1))
                    {
                        int     upIndex = (column + 1) + Nz * row;
                        Vector3 upCoord = tgs.CellGetPosition(myCells[upIndex]);
                        dijk_grid[currentIndex, upIndex] = Vector3.Distance(centerCoord, upCoord);
                    }

                    if (column != 0)
                    {
                        int     downIndex = (column - 1) + Nz * row;
                        Vector3 downCoord = tgs.CellGetPosition(myCells[downIndex]);
                        dijk_grid[currentIndex, downIndex] = Vector3.Distance(centerCoord, downCoord);
                    }

                    // ----------------

                    if ((row != 0) && (column != 0))
                    {
                        int     leftDownIndex = (column - 1) + Nz * (row - 1);
                        Vector3 leftDownCoord = tgs.CellGetPosition(myCells[leftDownIndex]);
                        dijk_grid[currentIndex, leftDownIndex] = Vector3.Distance(centerCoord, leftDownCoord);
                    }

                    if ((row != (Nx - 1)) && (column != 0))
                    {
                        int     rightDownIndex = (column - 1) + Nz * (row + 1);
                        Vector3 rightDownCoord = tgs.CellGetPosition(myCells[rightDownIndex]);
                        dijk_grid[currentIndex, rightDownIndex] = Vector3.Distance(centerCoord, rightDownCoord);
                    }

                    if ((row != 0) && (column != (Nz - 1)))
                    {
                        int     leftUpIndex = (column + 1) + Nz * (row - 1);
                        Vector3 leftUpCoord = tgs.CellGetPosition(myCells[leftUpIndex]);
                        dijk_grid[currentIndex, leftUpIndex] = Vector3.Distance(centerCoord, leftUpCoord);
                    }

                    if ((row != (Nx - 1)) && (column != (Nz - 1)))
                    {
                        int     rightUpIndex = (column + 1) + Nz * (row + 1);
                        Vector3 rightUpCoord = tgs.CellGetPosition(myCells[rightUpIndex]);
                        dijk_grid[currentIndex, rightUpIndex] = Vector3.Distance(centerCoord, rightUpCoord);
                    }
                }
            }

            DijkstraAlgorithm.Dijkstra AlgorithmClass = new DijkstraAlgorithm.Dijkstra(dijk_grid);
            AlgorithmClass.CallDijkstraAlgorithm(0, Nx * Nz - 1);
            double     distance = AlgorithmClass.GetMinimumDistance(Nx * Nz - 1);
            List <int> a        = AlgorithmClass.GetVerticesPath();

            using (var file = new StreamWriter(@"D:\Dijk_grid.txt", true))
            {
                for (int row = 0; row < Nx * Nz; row++)
                {
                    for (int column = 0; column < Nx * Nz; column++)
                    {
                        String str = dijk_grid[row, column] + "\t";
                        file.Write(str);
                    }
                    file.WriteLine("\n");
                }
            }
        }