java圖論弗洛伊德和迪杰斯特拉算法解決最短路徑問題
弗洛伊德算法
算法介紹

算法圖解分析?


?
?第一輪循環(huán)中,以A(下標(biāo)為:0)作為中間頂點
【即把作為中間頂點的所有情況都進(jìn)行遍歷,就會得到更新距離表和前驅(qū)關(guān)系】,距離表和前驅(qū)關(guān)系更新為:

弗洛伊德算法和迪杰斯特拉算法的最大區(qū)別是:
弗洛伊德算法是從各個頂點出發(fā),求最短路徑;
迪杰斯特拉算法是從某個頂點開始,求最短路徑。
/**
* 弗洛伊德算法
* 容易理解,容易實現(xiàn)
*/
public void floyd() {
int len = 0;//變量保存距離
//對中間頂點遍歷,k就是中間頂點的下標(biāo)[A,B,C,D,E,F,G]
for(int k = 0;k < dis.length;k++) {
//從i頂點開始出發(fā)[A,B,C,D,E,F,G]
for(int i = 0;i < dis.length;i++) {
//到達(dá)j頂點 //[A,B,C,D,E,F,G]
for(int j = 0;j < dis.length;j++) {
len = dis[i][k] + dis[k][j];//=>求出從i頂點出發(fā),經(jīng)過k中間頂點,到達(dá)j頂點距離
if(len < dis[i][j]) {//如果len小于dis[i][j]
dis[i][j] = len;//更新距離
pre[i][j] = pre[k][j];//更新前驅(qū)頂點
}
}
}
}
}
?迪杰斯特拉算法
算法介紹

算法過程?

public class DijkstraAlgorithm {
public static void main(String[] args) {
char[] vertex = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' };
// 鄰接矩陣
int[][] matrix = new int[vertex.length][vertex.length];
final int N = 65535;// 表示不可以連接
matrix[0] = new int[] { N, 5, 7, N, N, N, 2 };
matrix[1] = new int[] { 5, N, N, 9, N, N, 3 };
matrix[2] = new int[] { 7, N, N, N, 8, N, N };
matrix[3] = new int[] { N, 9, N, N, N, 4, N };
matrix[4] = new int[] { N, N, 8, N, N, 5, 4 };
matrix[5] = new int[] { N, N, N, 4, 5, N, 6 };
matrix[6] = new int[] { 2, 3, N, N, 4, 6, N };
// 創(chuàng)建Graph對象
Graph graph = new Graph(vertex, matrix);
// 測試,圖的鄰接矩陣是否ok
graph.showGraph();
// 測試迪杰斯特拉算法
graph.dsj(6);
graph.showDijkstra();
}
}
class Graph {
private char[] vertex;// 頂點數(shù)組
private int[][] matrix;// 鄰接矩陣
private VisitedVertex vv;// 已經(jīng)訪問過的頂點的集合
// 構(gòu)造器
public Graph(char[] vertex, int[][] matrix) {
this.vertex = vertex;
this.matrix = matrix;
}
// 顯示結(jié)果
public void showDijkstra() {
vv.show();
}
// 顯示圖
public void showGraph() {
for (int[] link : matrix) {
System.out.println(Arrays.toString(link));
}
}
/**
* 迪杰斯特拉算法實現(xiàn)
*
* @param index 表示出發(fā)頂點對應(yīng)的下標(biāo)
*/
public void dsj(int index) {
vv = new VisitedVertex(vertex.length, index);
update(index);// 更新index頂點到周圍頂點的距離和前驅(qū)頂點
for (int j = 1; j < vertex.length; j++) {
index = vv.updateArr();// 選擇并返回新的訪問頂點
update(index);// 更新index頂點到周圍頂點的距離和前驅(qū)頂點
}
}
/**
* 更新index下標(biāo)頂點到周圍頂點的距離和周圍頂點的前驅(qū)頂點
*/
private void update(int index) {
int len = 0;
// 根據(jù)遍歷鄰接矩陣的 matrix[index]行
for (int j = 0; j < matrix[index].length; j++) {
// len含義是:出發(fā)頂點到index頂點的距離 + 從index頂點到j(luò)頂點的距離的和
len = vv.getDis(index) + matrix[index][j];
// 如果j頂點沒有被訪問過,并且len小于出發(fā)頂點到j(luò)頂點的距離,就需要更新
if (!vv.in(j) && len < vv.getDis(j)) {
vv.updateDis(j, index);// 更新j頂點的前驅(qū)為index頂點
vv.updateDis(j, len);// 更新出發(fā)頂點到j(luò)頂點的距離
}
}
}
}
//已訪問頂點集合
class VisitedVertex {
// 記錄各個頂點是否訪問過; 1表示訪問過,0未訪問,會動態(tài)更新
public int[] already_arr;
// 每個下標(biāo)對應(yīng)的值為前一個頂點下標(biāo),會動態(tài)更新
public int[] pre_visited;
// 記錄出發(fā)頂點到其他所有頂點的距離,比如G為出發(fā)頂點,就會記錄G到其他頂點的距離,動態(tài)更新,求的最短距離放到dis
public int[] dis;
/**
* 構(gòu)造器
*
* @param length 表示頂點的個數(shù)
* @param index 出發(fā)頂點對應(yīng)的下標(biāo),比如G頂點,下標(biāo)就是6
*/
public VisitedVertex(int length, int index) {
this.already_arr = new int[length];
this.pre_visited = new int[length];
this.dis = new int[length];
// 初始化dis數(shù)組
Arrays.fill(dis, 65535);
this.already_arr[index] = 1;// 設(shè)置出發(fā)頂點被訪問過
this.dis[index] = 0;// 設(shè)置出發(fā)頂點的訪問距離為0
}
/**
* 判斷index頂點是否被訪問過
*
* @param index
* @return 如果訪問過,就返回true,否則返回false
*/
public boolean in(int index) {
return already_arr[index] == 1;
}
/**
* 更新出發(fā)頂點到index頂點的距離
*
* @param index
* @param len
*/
public void updateDis(int index, int len) {
dis[index] = len;
}
/**
* 更新pre這個頂點的前驅(qū)頂點為index頂點
*
* @param pre
* @param index
*/
public void updatePre(int pre, int index) {
pre_visited[pre] = index;
}
/**
* @return 返回出發(fā)頂點到index頂點的距離
*/
public int getDis(int index) {
return dis[index];
}
public int updateArr() {
int min = 65535, index = 0;
for (int i = 0; i < already_arr.length; i++) {
if (already_arr[i] == 0 && dis[i] < min) {
min = dis[i];
index = i;
}
}
// 更新index頂點被訪問過
already_arr[index] = 1;
return index;
}
// 顯示最后的結(jié)果
// 即將三個數(shù)組的情況輸出
public void show() {
System.out.println("==========================");
// 輸出already_arr
for (int i : already_arr) {
System.out.print(i + " ");
}
// 輸出pre_visited
for (int i : pre_visited) {
System.out.print(i + " ");
}
// 輸出dis
for (int i : dis) {
System.out.print(i + " ");
}
System.out.println();
// 為了好看最后的最短距離,如下處理
char[] vertex = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' };
int count = 0;
for (int i : dis) {
if (i != 65535) {
System.out.print(vertex[count] + "(" + i + ")");
} else {
System.out.println("N");
}
count++;
}
System.out.println();
}
}
以上就是java圖論弗洛伊德和迪杰斯特拉算法解決最短路徑問題的詳細(xì)內(nèi)容,更多關(guān)于弗洛伊德和迪杰斯特拉算法解決最短路徑的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot分布式文件存儲數(shù)據(jù)庫mongod
MongoDB是一個基于分布式文件存儲的NoSQL數(shù)據(jù)庫,由C++語言編寫,旨在為Web應(yīng)用提供可擴展的高性能數(shù)據(jù)存儲解決方案。MongoDB是一個介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫中功能最豐富最像關(guān)系數(shù)據(jù)庫的2023-02-02
springboot整合Excel填充數(shù)據(jù)代碼示例
這篇文章主要給大家介紹了關(guān)于springboot整合Excel填充數(shù)據(jù)的相關(guān)資料,文中通過代碼示例介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用springboot具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08
springboot配置文件屬性變量引用方式${}和@@用法及區(qū)別說明
這篇文章主要介紹了springboot配置文件屬性變量引用方式${}和@@用法及區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot集成tensorflow實現(xiàn)圖片檢測功能
TensorFlow名字的由來就是張量(Tensor)在計算圖(Computational?Graph)里的流動(Flow),它的基礎(chǔ)就是前面介紹的基于計算圖的自動微分,本文將給大家介紹Spring?Boot集成tensorflow實現(xiàn)圖片檢測功能,需要的朋友可以參考下2024-06-06
Spring MVC 更靈活的控制 json 返回問題(自定義過濾字段)
本篇文章主要介紹了Spring MVC 更靈活的控制 json 返回問題(自定義過濾字段),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02
SpringBoot中@EnableAutoConfiguration和@Configuration的區(qū)別
這篇文章主要介紹了SpringBoot中@EnableAutoConfiguration和@Configuration的區(qū)別,@SpringBootApplication相當(dāng)于@EnableAutoConfiguration,@ComponentScan,@Configuration三者的集合,需要的朋友可以參考下2023-08-08

