Warshall 算法
生活随笔
收集整理的這篇文章主要介紹了
Warshall 算法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Warshall算法
介紹:
Warshall在1962年提出了一個求關系的傳遞閉包的有效算法。其具體過程如下:
N—S圖
代碼實現:
import java.util.Scanner;public class Warshall {public static void main(String[] args) {System.out.println("請輸入矩陣階數:");Scanner scan = new Scanner(System.in);int n = scan.nextInt();// 行數int[][] array = new int[n][n];for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {array[i][j] = scan.nextInt();}}for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {if (array[i][j] == 1) {for (int k = 0; k < n; k++) {array[i][k] = array[i][k] | array[j][k];}}}}System.out.println("傳遞閉包的最終結果是:");for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {System.out.print(array[i][j]);}System.out.println();}}}運行結果:
應用:計算有窮集合上的二元關系的傳遞閉包和簡單方向圖的可到達矩陣。
總結
以上是生活随笔為你收集整理的Warshall 算法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux dig命令
- 下一篇: linux上dig命令,Linux中di