Floyd-Warshall 演算法是一種用於計算圖中所有節點對之間最短路徑的演算法。這個演算法以其作者之一羅伯特·弗洛伊德(Robert W. Floyd)和另一位叫斯蒂芬·沃爾沃斯(Stephen Warshall)的計算機科學家的名字命名。 以下是 Floyd-Warshall 演算法的基本方法與特色:
using System; class ShortestPathAlgorithm { const int NoPath = int.MaxValue; // 定義表示無路徑的值 /// <summary> /// 輸出矩陣內容列表 /// </summary> /// <param name="dist">最短路徑地圖陣列</param> static void PrintSolution(int[,] dist) { int V = dist.GetLength(0); Console.WriteLine("最短路徑矩陣表:\n點\\點 | 0 1 2 3 4\n |-------------------------"); for (int i = 0; i < V; ++i) { Console.Write($" {i} |"); for (int j = 0; j < V; ++j) { if (dist[i, j] == NoPath) Console.Write(" ∞"); else Console.Write($"{(dist[i, j] == 0 ? " 0" : dist[i, j].ToString().PadLeft(5))}"); } Console.WriteLine(); } } /// <summary> /// Floyd-Warshall 演算法,求出所有點至各點最短路徑 /// </summary> /// <param name="graph">地圖陣列</param> static void ApplyFloydWarshall(int[,] graph) { int V = graph.GetLength(0); int[,] dist = new int[V, V]; Array.Copy(graph, dist, graph.Length);// 初始化最短路徑矩陣 // 遍歷所有節點,考慮每一個節點作為中繼節點的情況 for (int i = 0; i < V; ++i) { for (int j = 0; j < V; ++j) { for (int k = 0; k < V; ++k) { // 如果節點 k 可以縮短從 i 到 j 的距離,則更新最短距離 if (dist[i, k] != NoPath && dist[k, j] != NoPath && dist[i, k] + dist[k, j] < dist[i, j]) dist[i, j] = dist[i, k] + dist[k, j]; } } } PrintSolution(dist);// 輸出最短路徑矩陣 } static void Main() { int[,] path = { {0, NoPath, 14, 3, 12}, {6, 0, NoPath, 5, 6}, {NoPath, 4, 0, 4, 2}, {5, 8, 4, 0, NoPath}, {NoPath, NoPath, 4, NoPath, 0} }; ApplyFloydWarshall(path); } } ![]() ![]() ![]() |