Floyd algorithm
Algorithm
The Floyed algorithm is an algorithm for efficiently and simultaneously finding the shortest paths betweeen every pair of vertices in a weighted and potentially directed graph.
Consider a graph G with vertices V,each numbered 1 through N. Further consider a function shortestPath(i,j,k) that return the shortest possible path from i to j using vertices only from the set {1,2,....k} as intermediate point along the way. Now ,given this function ,our goal is to find the shorttest path from each i to each j using only vertices 1 to k+1.And the fuction what we want is called Floyed algorithm.
For each of these pairs of vertices,the true shortest path could be either (1) a path that only uses vertices in the set {1,....,k} or (2) a path that goes from i to k+1 and then from k+1 to j.We know that the best path from i to j that uses vertices 1 through k is defined by shortestPath(i,j,k),and it is clear that if there were a better path from i to k+1 to j,then the length of this path would be the concatenation of the shortest path from i to k+1(uing vertices in {1,....,k} and shortest path from k+1 to j (also using vertices in {1,...,k}).
If w(i,j) is the weight of the edge between vertices i and j,we can define shortestPath(i,j,k) in terms of the following recursive formula :the base case is
shortestPath(i,j,0)=w(i,j)
and the recursive case is
shortestPath(i,j,k)=min(shortestPath(i,j,k-1),shortestPath(i,k,k-1)+shortestPath(k,j,k-1)):
This formula is the heart of the Floyed algorithm. The algorithm works by first computer shortestPath(i,j,k) for all (i,j) pairs for k=1,then k=2,etc.This process continue until k=n,and we have found the shortest path for all (i,j) pairs using any intermediate vertices.
code :
1 procedure FloydWarshallWithPathReconstruction () 2 for k := 1 to n 3 for i := 1 to n 4 for j := 1 to n 5 if path[i][k] + path[k][j] < path[i][j] then { 6 path[i][j] := path[i][k]+path[k][j]; 7 next[i][j] := k; } 8 9 function Path (i,j) 10 if path[i][j] equals infinity then 11 return "no path"; 12 int intermediate := next[i][j]; 13 if intermediate equals 'null' then 14 return " "; /* there is an edge from i to j, with no vertices between */ 15 else 16 return Path(i,intermediate) + intermediate + Path(intermediate,j);
Applications and generalizations
The Floyd–Warshall algorithm can be used to solve the following problems, among others:
- Shortest paths in directed graphs (Floyd's algorithm).
- Transitive closure of directed graphs (Warshall's algorithm). In Warshall's original formulation of the algorithm, the graph is unweighted and represented by a Boolean adjacency matrix. Then the addition operation is replaced by logical conjunction (AND) and the minimum operation by logical disjunction (OR).
- Finding a regular expression denoting the regular language accepted by a finite automaton (Kleene's algorithm)
- Inversion of real matrices (Gauss–Jordan algorithm) [3]
- Optimal routing. In this application one is interested in finding the path with the maximum flow between two vertices. This means that, rather than taking minima as in the pseudocode above, one instead takes maxima. The edge weights represent fixed constraints on flow. Path weights represent bottlenecks; so the addition operation above is replaced by the minimum operation.
- Testing whether an undirected graph is bipartite.
- Fast computation of Pathfinder networks.
- Widest paths/Maximum bandwidth paths
Case problem:
Now ,give you a problem and use the Floyd algorithm to slove it.Maybe it's helpful for you further understanding the algorithm!
http://www.cnblogs.com/heat-man/articles/2743401.html
Refefience:
http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?