Floyd algorithm (Floyd)

package com.rao.graph;


/**
 * @Author shat
 * @className Floyd
 * @date 2019/12/11 18:43
 * @package com.rao.graph
 * @Description Floyd algorithm
 */
public class Floyd {

    final static int INF = Integer.MAX_VALUE;

    /**
     * Floyd algorithm
     * @param matrix
     * / 
    Public  static  void Floyd ( int [] [] Matrix) {
         // updates the value of circulant matrices, matrix.length returns the number of rows 
        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]);
                }
            }
        }

        // print result of the shortest path floyd 
        the System. OUT .println ( " Shortest Path Matrix: " );
         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);
    }
}

Guess you like

Origin www.cnblogs.com/rao11/p/12024503.html