同步操作将从 Gitee 极速下载/javascript-algorithms 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
In computer science, the Floyd–Warshall algorithm is an algorithm for finding shortest paths in a weighted graph with positive or negative edge weights (but with no negative cycles). A single execution of the algorithm will find the lengths (summed weights) of shortest paths between all pairs of vertices. Although it does not return details of the paths themselves, it is possible to reconstruct the paths with simple modifications to the algorithm.
The Floyd–Warshall algorithm compares all possible paths through the graph between
each pair of vertices. It is able to do this with O(|V|^3)
comparisons in a graph.
This is remarkable considering that there may be up to |V|^2
edges in the graph,
and every combination of edges is tested. It does so by incrementally improving an
estimate on the shortest path between two vertices, until the estimate is optimal.
Consider a graph G
with vertices V
numbered 1
through N
. Further consider
a function shortestPath(i, j, k)
that returns the shortest possible path
from i
to j
using vertices only from the set {1, 2, ..., k}
as
intermediate points along the way. Now, given this function, our goal is to
find the shortest path from each i
to each j
using only vertices
in {1, 2, ..., N}
.
This formula is the heart of the Floyd–Warshall algorithm.
The algorithm above is executed on the graph on the left below:
In the tables below i
is row numbers and j
is column numbers.
k = 0
1 | 2 | 3 | 4 | |
---|---|---|---|---|
1 | 0 | ∞ | −2 | ∞ |
2 | 4 | 0 | 3 | ∞ |
3 | ∞ | ∞ | 0 | 2 |
4 | ∞ | −1 | ∞ | 0 |
k = 1
1 | 2 | 3 | 4 | |
---|---|---|---|---|
1 | 0 | ∞ | −2 | ∞ |
2 | 4 | 0 | 2 | ∞ |
3 | ∞ | ∞ | 0 | 2 |
4 | ∞ | − | ∞ | 0 |
k = 2
1 | 2 | 3 | 4 | |
---|---|---|---|---|
1 | 0 | ∞ | −2 | ∞ |
2 | 4 | 0 | 2 | ∞ |
3 | ∞ | ∞ | 0 | 2 |
4 | 3 | −1 | 1 | 0 |
k = 3
1 | 2 | 3 | 4 | |
---|---|---|---|---|
1 | 0 | ∞ | −2 | 0 |
2 | 4 | 0 | 2 | 4 |
3 | ∞ | ∞ | 0 | 2 |
4 | 3 | −1 | 1 | 0 |
k = 4
1 | 2 | 3 | 4 | |
---|---|---|---|---|
1 | 0 | −1 | −2 | 0 |
2 | 4 | 0 | 2 | 4 |
3 | 5 | 1 | 0 | 2 |
4 | 3 | −1 | 1 | 0 |
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。