弗洛伊德算法(Floyd )

package com.rao.graph;


/**
 * @author Srao
 * @className Floyd
 * @date 2019/12/11 18:43
 * @package com.rao.graph
 * @Description 弗洛伊德算法
 */
public class Floyd {

    final static int INF = Integer.MAX_VALUE;

    /**
     * 弗洛伊德算法
     * @param matrix
     */
    public static void floyd(int[][] matrix){
        //更新循环矩阵的值,matrix.length返回的是行数
        for (int k = 0; k < matrix.length; k++) {
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix.length; j++) {
                    if (matrix[i][k] == INF || matrix[k][j] == INF){
                        continue;
                    }
                    matrix[i][j] = Math.min(matrix[i][j], matrix[i][k] + matrix[k][j]);
                }
            }
        }

        //打印floyd最短路径的结果
        System.out.println("最短路径矩阵:");
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix.length; j++) {
                System.out.printf("%3d ", matrix[i][j]);
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int[][] matrix = {
                {0, 5, 2, INF, INF, INF, INF},
                {5, 0, INF, 1, 6, INF, INF},
                {2, INF, 0, 6, INF, 8, INF},
                {INF, 1, 6, 0, 1, 2, INF},
                {INF, 6, INF, 1, 0, INF, 7},
                {INF, INF, 8, 2, INF, 0, 3},
                {INF, INF, INF, INF, 7, 3, 0}
        };

        floyd(matrix);
    }
}

你可能感兴趣的:(弗洛伊德算法(Floyd ))