C 有一個宏,它被定義為一段程式碼或期望的值,並且每當使用者需要時,它將被重複使用。佛洛伊德-沃爾夏爾演算法是在給定的加權圖中找到所有頂點對之間最短路徑的過程。該演算法遵循動態規劃的方法來找到最小權重圖。
讓我們透過圖表來理解佛洛伊德-沃爾夏爾演算法的意義 -

以頂點1為來源,頂點4為目的地,求它們之間的最短路徑。
我們已經看到有兩條路徑可以連接到目標頂點4。
1 -> 4 – 邊的權重為5
1 -> 8 -> 3 -> 4 – 邊權重(1 2 1)為4。
在給定的圖 I 中,我們看到兩個頂點之間連接的最小邊。所以這裡頂點 8 和頂點 3 連接頂點 1 到頂點 4 的路徑以及邊是4。另一方面,頂點1到頂點4之間有一條有向邊,邊的權重為5。
現在我們比較兩個權重,即4和5。因此,這裡 4 是根據 Floyd Warshall 演算法計算的圖的最小權重。
在本文中,我們將使用 Floyd Warshall 演算法求解任兩個給定節點之間的最短路徑。
文法
以下語法用於程式中 -
data_type[][] array_name;
參數
data_type[][] - 使用者提到的資料類型。第一個陣列代表行,第二個陣列代表列。所以,這代表了二維數組。
array_name - 為陣列指定的名稱。
演算法
我們將使用頭檔'iostream' 啟動程序,並將巨集位置定義為'INF'(無限大),因為稍後它將滿足二維數組矩陣和if-else 語句。
接下來,我們建立名為'printPath' 的全域函數定義,並接受三個參數,分別是'parent[]'、'i' 和'j' 驗證路徑是否存在。
然後我們開始主函數,並將值‘4’儲存到變數‘V’中,該變數表示頂點的數量。其次,將值以鄰接矩陣的形式儲存到變數‘dist[V][V]’。如我們所知,dist[i][j]表示2D矩陣,它定義了從'i'到'j'的邊的權重,透過儲存鄰接矩陣。
接下來,我們正在為變數‘parent’初始化2D數組,並且大小為[V][V]。
透過使用兩個巢狀的for循環,我們將找到最短路徑。第一個for迴圈迭代矩陣中的行。然後,透過第二個for迴圈迭代矩陣中的列,然後我們將使用if-else語句檢查以下條件 -
-
#如果 (dist[i][j] != INF && i != j) { parent[i][j] = i;}
的中文翻譯為:parent[i][j] = i;}在if語句中,我們使用'and' (&&)運算子來表示兩個條件,如果這兩個條件都滿足,那麼'i'將被儲存到'parent[i ][j]'中,從而驗證路徑存在。
-
其他{ parent[i][j] = -1;}
的中文翻譯為:parent[i][j] = -1;}在 else 語句中,我們將「-1」初始化為「parent[i][j]」,以驗證路徑不存在。
現在我們從三個嵌套的for 迴圈開始,並在0 到V-1 的範圍內應用k 個頂點,在這個範圍的幫助下,我們的最短距離和路徑將被更新。在第三個巢狀循環中,我們使用以下條件,例如
在這個變數中,我們用來顯示每對頂點'i' 和'j' w.r.t 'parent[i][j]' .
if (dist[i][j] < dist[i][k] + dist[k][j]){ dist[i][j] = dist[i][k] + dist[k][j]; parent[i][j] = parent[k][j]; }
接下來,我們透過給定以下條件,列印出所有頂點對的最短距離和路徑
#透過使用兩個巢狀的 for 循環,我們列印最短距離和路徑。
透過在第二個for迴圈下使用if語句,我們將檢查dist[i][j]是否等於無限大。如果發現它是無窮大,則列印“INF”,否則列印最短路徑。
最後,我們呼叫名為'printPath()' 的函數,透過傳遞三個參數(parent[i]、i、和j。
這裡 K 正在更新二維數組矩陣中的行和列的部分,這驗證了新更新的最短路徑和距離。
範例
在這個程式中,我們將使用Floyd Warshall演算法計算任兩個節點之間的最短路徑。
#include <iostream> using namespace std; #define INF 1000000000 // Infinity void printPath(int parent[], int i, int j) { if (i == j) cout << i << " "; else if (parent[j] == -1) cout << "No path exists"; else { printPath(parent, i, parent[j]); cout << j << " "; } } int main() { int V = 4; // V represent number of vertices int dist[V][V] = {{0, 2, INF, 4}, {6, 0, 5, 3}, {INF, 10, 0, 1}, {7, 9, 8, 0}}; // Represent the graph using adjacency matrix // Apply the Floyd Warshall algorithm to find the shortest paths int parent[V][V]; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (dist[i][j] != INF && i != j) parent[i][j] = i; else parent[i][j] = -1; } } // update the path and distance using the k vertex range from 0 to V-1. for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (dist[i][j] > dist[i][k] + dist[k][j]) { dist[i][j] = dist[i][k] + dist[k][j]; parent[i][j] = parent[k][j]; } } } } // Print shortest distances and paths between all pairs of vertices for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { cout << "The Shortest distance between " << i << " and " << j << " is "; if (dist[i][j] == INF) cout << "INF "; else cout << dist[i][j] << " "; cout << "and the shortest path is:- "; printPath(parent[i], i, j); cout << endl; } } return 0; }
输出
The Shortest distance between 0 and 0 is 0 and the shortest path is:- 0 The Shortest distance between 0 and 1 is 2 and the shortest path is:- 0 1 The Shortest distance between 0 and 2 is 7 and the shortest path is:- 0 1 2 The Shortest distance between 0 and 3 is 4 and the shortest path is:- 0 3 The Shortest distance between 1 and 0 is 6 and the shortest path is:- 1 0 The Shortest distance between 1 and 1 is 0 and the shortest path is:- 1 The Shortest distance between 1 and 2 is 5 and the shortest path is:- 1 2 The Shortest distance between 1 and 3 is 3 and the shortest path is:- 1 3 The Shortest distance between 2 and 0 is 8 and the shortest path is:- 2 3 0 The Shortest distance between 2 and 1 is 10 and the shortest path is:- 2 1 The Shortest distance between 2 and 2 is 0 and the shortest path is:- 2 The Shortest distance between 2 and 3 is 1 and the shortest path is:- 2 3 The Shortest distance between 3 and 0 is 7 and the shortest path is:- 3 0 The Shortest distance between 3 and 1 is 9 and the shortest path is:- 3 1 The Shortest distance between 3 and 2 is 8 and the shortest path is:- 3 2 The Shortest distance between 3 and 3 is 0 and the shortest path is:- 3
结论
我们学习了使用Floyd Warshall算法找到任意两个节点之间的最短路径的概念。我们使用邻接矩阵作为程序的输入,通过它我们找到了最短路径和距离。此外,为了更新路径和距离,我们使用了k个顶点来更新行和列。在我们的日常生活中,我们在谷歌地图上搜索最短路线或路径,从一个起点到目的地。
以上是使用佛洛伊德-沃沙爾演算法找到任兩個節點之間的最短路徑的詳細內容。更多資訊請關注PHP中文網其他相關文章!

C#和C 在性能上的差異主要體現在執行速度和資源管理上:1)C 在數值計算和字符串操作上通常表現更好,因為它更接近硬件,沒有垃圾回收等額外開銷;2)C#在多線程編程上更為簡潔,但性能略遜於C ;3)選擇哪種語言應根據項目需求和團隊技術棧決定。

1)c relevantduetoItsAverity and效率和效果臨界。 2)theLanguageIsconTinuellyUped,withc 20introducingFeaturesFeaturesLikeTuresLikeSlikeModeLeslikeMeSandIntIneStoImproutiMimproutimprouteverusabilityandperformance.3)

C 在現代世界中的應用廣泛且重要。 1)在遊戲開發中,C 因其高性能和多態性被廣泛使用,如UnrealEngine和Unity。 2)在金融交易系統中,C 的低延遲和高吞吐量使其成為首選,適用於高頻交易和實時數據分析。

C 中有四種常用的XML庫:TinyXML-2、PugiXML、Xerces-C 和RapidXML。 1.TinyXML-2適合資源有限的環境,輕量但功能有限。 2.PugiXML快速且支持XPath查詢,適用於復雜XML結構。 3.Xerces-C 功能強大,支持DOM和SAX解析,適用於復雜處理。 4.RapidXML專注於性能,解析速度極快,但不支持XPath查詢。

C 通過第三方庫(如TinyXML、Pugixml、Xerces-C )與XML交互。 1)使用庫解析XML文件,將其轉換為C 可處理的數據結構。 2)生成XML時,將C 數據結構轉換為XML格式。 3)在實際應用中,XML常用於配置文件和數據交換,提升開發效率。

C#和C 的主要區別在於語法、性能和應用場景。 1)C#語法更簡潔,支持垃圾回收,適用於.NET框架開發。 2)C 性能更高,需手動管理內存,常用於系統編程和遊戲開發。

C#和C 的歷史與演變各有特色,未來前景也不同。 1.C 由BjarneStroustrup在1983年發明,旨在將面向對象編程引入C語言,其演變歷程包括多次標準化,如C 11引入auto關鍵字和lambda表達式,C 20引入概念和協程,未來將專注於性能和系統級編程。 2.C#由微軟在2000年發布,結合C 和Java的優點,其演變注重簡潔性和生產力,如C#2.0引入泛型,C#5.0引入異步編程,未來將專注於開發者的生產力和雲計算。

C#和C 的学习曲线和开发者体验有显著差异。1)C#的学习曲线较平缓,适合快速开发和企业级应用。2)C 的学习曲线较陡峭,适用于高性能和低级控制的场景。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 Linux新版
SublimeText3 Linux最新版

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。