Ejemplo n.º 1
0
        public static float[,] AllPairsShortestPath(Graph g)
        {
            Output.WriteLine("[All Pairs Shortest Path Output]");

            g.UpdateMatrix();
            float[,] D = g.GetRawMatrix();
            int n = g.GetVertices().Count;

            //Set up a matrix called pred, tells you the predecessor for each vertex

            //Set labels
            //Where edges don't exist, values of positive infinity are already inserted
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (i == j)
                    {
                        D[i, j] = 0;
                    }
                }
            }

            for (int k = 0; k < n; k++)
            {
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        if (D[i, k] + D[k, j] < D[i, j])
                        {
                            D[i, j] = D[i, k] + D[k, j];
                        }
                    }
                }
            }

            PrintMatrix(D, g.GetVertices().Count);

            Output.WriteLine("[End All Pairs Shortest Path Output]");
            return D;
        }
Ejemplo n.º 2
0
        public static float[,] AllPairsShortestPath(Graph g)
        {
            Output.WriteLine("[All Pairs Shortest Path Output]");

            g.UpdateMatrix();
            float[,] D = g.GetRawMatrix();
            int n = g.GetVertices().Count;

            //Set up a matrix called pred, tells you the predecessor for each vertex

            //Set labels
            //Where edges don't exist, values of positive infinity are already inserted
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (i == j)
                    {
                        D[i, j] = 0;
                    }
                }
            }

            for (int k = 0; k < n; k++)
            {
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        if (D[i, k] + D[k, j] < D[i, j])
                        {
                            D[i, j] = D[i, k] + D[k, j];
                        }
                    }
                }
            }

            PrintMatrix(D, g.GetVertices().Count);

            Output.WriteLine("[End All Pairs Shortest Path Output]");
            return(D);
        }