zoukankan      html  css  js  c++  java
  • 无权图的Warshall算法

    Warshall算法 :将无权图的连接矩阵转化为联通表。

    public class Walshall {
    
        public static int[][] caculate(final int[][] linkedMatrix) {
            int M = linkedMatrix.length;
            int[][] connectTable = linkedMatrix.clone();
            for (int mid = 0; mid < M; mid++) {
                for (int left = 0; left < M; left++) {
                    if (connectTable[left][mid] > 0) {
                        for (int right = 0; right < M; right++) {
                            if (connectTable[mid][right] > 0) {
                                connectTable[left][right] = 1;
                            }
                        }
                    }
                }
            }
            return connectTable;
        }
    
        public static void main(String[] args) {
            int[][] linkedMatrix = new int[][]{
                {0, 0, 1, 0, 0},
                {1, 0, 0, 0, 1},
                {0, 0, 0, 0, 0},
                {0, 0, 0, 0, 1},
                {0, 0, 1, 0, 0}
            };
            int[][] connectTable = Walshall.caculate(linkedMatrix);
            int M = connectTable.length;
            for (int i = 0; i < M; i++) {
                for (int j = 0; j < M; j++) {
                    System.out.print(connectTable[i][j] + "	");
                }
                System.out.println();
            }
    
        }
    }
    

    输出:

    0	0	1	0	0	
    1	0	1	0	1	
    0	0	0	0	0	
    0	0	1	0	1	
    0	0	1	0	0	


  • 相关阅读:
    struts2上传下载
    git教程
    mysql触发器2
    mysql触发器
    mysql set sql_mode 1055 报错
    一些乱七八糟的话
    linux 命令2
    linux命令 mysql
    东南亚之行(越南篇)
    flume常见配置
  • 原文地址:https://www.cnblogs.com/leeeee/p/7276142.html
Copyright © 2011-2022 走看看