Esempio n. 1
0
        /// <summary>
        /// Получает матрицу кротчайших расстояний по алгоритму Флойда-Уоршелла.
        /// </summary>
        /// <returns>Матрица кротчайших расстояний по алгоритму Флойда-Уоршелла.</returns>
        public Matrix GetFloydWarshallShortestPath()
        {
            Matrix current = AdjacencyMatrix.Copy();

            //Обнуляем главную диагональ, чтобы убрать петли, так как алгоритм Флойда-Уоршалла применим только для таких графов.
            for (int k = 0; k < current.LengthY; k++)
            {
                current[k, k] = 0;
            }
            for (int aloghorithmStepsCount = 0; aloghorithmStepsCount < VertexesCount; aloghorithmStepsCount++)
            {
                Matrix previous = current.Copy();
                for (int i = 0; i < VertexesCount; i++)
                {
                    for (int j = i + 1; j < VertexesCount; j++)
                    {
                        current[i, j] = Math.Min(previous[i, j], previous[i, aloghorithmStepsCount] + previous[aloghorithmStepsCount, j]);
                        current[j, i] = Math.Min(previous[j, i], previous[aloghorithmStepsCount, i] + previous[j, aloghorithmStepsCount]);
                    }
                }
            }

            return(current);
        }