資源簡(jiǎn)介
解決任意兩點(diǎn)間的最短路徑的一種算法,可以正確處理有向圖或負(fù)權(quán)的最短路徑問題,同時(shí)也被用于計(jì)算有向圖的傳遞閉包。Floyd-Warshall算法的時(shí)間復(fù)雜度為O(N3),空間復(fù)雜度為O(N2)。
java代碼實(shí)現(xiàn)。算法詳解,參考技術(shù)文檔
https://www.jianshu.com/p/db0df9197073
代碼片段和文件信息
package?a;
/**
?*? ┏┓ ┏┓+?+
?*? ┏┛┻━━━┛┻┓?+?+
?*? ┃ ┃
?*? ┃ ━ ┃?++?+?+?+
?*? ?████━████?┃+
?*? ┃ ┃?+
?*? ┃ ┻ ┃
?*? ┃ ┃?+?+
?*? ┗━┓ ┏━┛
?*? ┃ ┃
?*? ┃ ┃?+?+?+?+
?*? ┃ ┃ Code?is?far?away?from?bug?with?the?animal?protecting
?*? ┃ ┃?+? 神獸保佑代碼無bug
?*? ┃ ┃
?*? ┃ ┃ +
?*? ┃ ? ┗━━━┓?+?+
?*? ┃? ┣┓
?*? ┃? ┏┛
?*? ┗┓┓┏━┳┓┏┛?+?+?+?+
?*? ┃┫┫ ┃┫┫
?*? ┗┻┛ ┗┻┛+?+?+?+
?*
?*?@Author:Halburt
?*?@Date:2019-04-23?下午?1:52
?*?@Description:?Floyd算法demo
?*/
public?class?FloydDemo?{
????//????表示無窮大?即不可達(dá)
????public?static?int?MAX?=?Integer.MAX_VALUE;
????//????距離矩陣
????public?int[][]?dist;
????//????路徑Path矩陣
????public?int[][]?path;
????/**
?????*?按點(diǎn)初始化
?????*
?????*?@param?size
?????*/
????public?FloydDemo(int?size)?{
????????this.path?=?new?int[size][size];
????????this.dist?=?new?int[size][size];
????}
????public?static?void?print(int[][]?arrs)?{
????????System.out.println(“------------------------“);
????????for?(int[]?arr?:?arrs)?{
????????????for?(int?a?:?arr)?{
????????????????if?(a?==?FloydDemo.MAX)?{
????????????????????System.out.print(“∞?“);
????????????????}?else?{
????????????????????System.out.print(a?+?“?“);
????????????????}
????????????}
????????????System.out.println();
????????}
????????System.out.println(“------------------------“);
????}
????/**核心算法
?????*?構(gòu)建距離矩陣和路徑矩陣
?????*?@param?matrix
?????*/
????public?void?floyd(int[][]?matrix)?{
//????????matrix和path?length不一致可處理異常
????????int?size?=?matrix.length;
????????//初始化?dist?和?path
????????for(int?i?=?0;i????????
評(píng)論
共有 條評(píng)論