1. Problem Denition
-- Input: Directed graph G = (V , E) with edge costs ce for each edge e in E, [No distinguished source vertex.]
-- Goal: Either
(A) Compute the length of a shortest u --> v path for all pairs of vertices u, v in V
(B) Correctly report that G contains a negative cycle.
2. Invocations of a single-source shortest-path subroutine
-- n Dijkstra = O(nm log n) =
-- O(n^2 log n) if m = O(n)
-- O(n^3 log n) if m = O(n^2)
-- n Bellman-Ford = O(n^2m) =
-- O(n^3) if m = O(n)
-- O(n^4) if m = O(n^2)
3. Floyd-Warshall algorithm
-- Running time : O(n^3) algorithm for APSP. Works even with graphs with negative edge lengths.
-- At least as good as n Bellman-Fords, better in dense graphs.
-- In graphs with nonnegative edge costs, competitive with n Dijkstra's in dense graphs.
4. Optimal Substructure
-- Recall: Can be tricky to define ordering on subproblems in graph problems.
-- Key idea: Order the vertices V = {1, 2, ... , n} arbitrarily. Let V(k) = {1, 2, ... , k}.
-- Lemma: Suppose G has no negative cycle. Fix source i in V, destination j in V, and k in {1, 2, ... , n}. Let P = shortest (cycle-free) i -j path with all internal nodes in V(k).
-- Optimal substructure lemma:
Suppose G has no negative cost cycle. Let P be a shortest (cycle-free) i -j path with all internal
nodes in V(k). Then:
Case 1: If k not internal to P, then P is a shortest (cycle-free) i -j path with all internal vertices in V(k-1).
Case 2: If k is internal to P, then: P1 = shortest (cycle-free) i -k path with all internal nodes in
V(k-1) and P2 = shortest (cycle-free) k-j path with all internal nodes in V(k-1)
5. The Floyd-Warshall Algorithm :
-- Let A = 3-D array (indexed by i , j , k)
-- Base cases: For all i , j in V:
A[i , j , 0] = 0 if i = j ; cij if (i , j) in E ; +Infinity if i <> j and (i , j) not in E
For k = 1 to n
For i = 1 to n
For j = 1 to n
A[i , j , k] = min { A[i , j , k - 1] [Case 1] , A[i , k , k - 1] + A[k , j , k - 1] [Case 2]
-- Correctness: From optimal substructure + induction, as usual.
-- Running time: O(1) per subproblem, O(n^3) overall.
6. Handling Negative Cycle :
-- Will have A[i , i , n] < 0 for at least one i in V at end of algorithm if the input graph has negative cost cycle.
-- Reconstruction : Have Floyd-Warshall compute B[i , j ] = max label of an internal node on a shortest i -j path for all i , j in V. ( Reset B[i , j ] = k if 2nd case of recurrence used to compute
A[i , j , k] ) ==> Can use the B[i , j ]'s to recursively reconstruct shortest paths.
7. Johnson's algorithm
-- Reduces APSP to :
-- 1 invocation of Bellman-Ford (O(mn) )
-- n invocations of Dijkstra (O(nm log n))
-- Running time: O(mn) + O(mn log n) = O(mn log n) (As good as with nonnegative edge lengths)
8. Reweighting :
-- G = (V , E) is a directed graph with general edge lengths ce. Fix a real number pv for each vertex v in V.
-- For every edge e = (u , v) of G, ce' = ce + pu - pv , If the s-t path P has length L with the original edge lengths {ce}, P's length with the new edge length {ce'} is :
Sum(e in P) { ce' } = Sum( e = (u,v) in P ) { ce + pu - pv } = (Sum ( e in P ) {ce} ) + ps - pt
-- Summary: Reweighting using vertex weights {pv} adds the same amount (namely, ps - pt ) to every s-t path. Reweighting always leaves the shortest path unchanged.
-- After reweighting by some {pv}, all edge lengths become nonnegative!
9. Johnson's Algorithm:
-- Input: Directed graph G = (V , E), general edge lengths ce .
-- (1) Form G' by adding a new vertex s and a new edge (s , v) with length 0 for each v in G.
-- (2) Run Bellman-Ford on G' with source vertex s. [If B-F detects a negative-cost cycle in G' (which must lie in G), halt + report this.]
-- (3) For each v in G, define pv = length of a shortest s --> v path in G'.
For each edge e = (u , v) in G, define ce' = ce + pu - pv.
-- (4) For each vertex u of G: Run Dijkstra's algorithm in G, with edge lengths {ce'}, with source vertex u, to compute the shortest-path distance d'(u , v) for each v in G.
-- (5) For each pair u , v in G, return the shortest-path distance d(u , v) := d'(u , v) - pu + pv
-- Runing time : O(nm log n)
-- Step (1) , form G' --> O(n)
-- Step (2) , run BF --> O(mn)
-- Step (3), form c' --> O(m)
-- Step (4), n Dijkstra --> O(nm log n)
-- Step (5), O(1) work per u-v pair --> O(n2)
10. Correctness of Johnson's Algorithm
-- Claim: For every edge e = (u , v) of G, the reweighted length ce' = ce + pu - pv is nonnegative.
-- Proof: Fix an edge (u , v). By construction,
pu = length of a shortest s-u path in G'
pv = length of a shortest s-v path in G'
Let P = a shortest s-u path in G' (with length pu --> exists, by construction of G')
==> P + (u , v) = an s-v path with length pu + cuv
==> Shortest s-v path only shorter, so pv <= pu + cuv
==> c'uv = cuv + pu - pv >= 0.
相关推荐
在图论中,All-Pairs-Shortest-Paths(APSP)问题是一个经典的问题,它旨在找出给定加权无向图或有向图中所有节点对之间的最短路径。这个问题在许多领域都有应用,包括网络设计、交通规划、社交网络分析等。Floyd-...
在集中式算法方面,解决图的直径和半径问题通常采用计算所有点对之间最短路径的方法,即All-Pairs Shortest Paths(APSP)。针对无权图的APSP问题,可以利用宽度优先搜索(BFS),其时间复杂度为O(nm+n^2logn),其中...
Floyd算法适用于APSP(All Pairs Shortest Paths),是一种动态规划算法,稠密图效果最佳,边权可正可负。此算法简单有效,由于三重循环结构紧凑,对于稠密图,效率要高于执行|V|次Dijkstra算法。
8. **All Pairs Shortest Paths (APSP)**: APSP是指寻找图中所有顶点对的最短路径,可以使用Floyd-Warshall或Johnson's algorithm等方法解决。 9. **01背包不可分学号**: 这可能是针对特定的01背包问题的一个变种,...
5. **最短路径树**:在具有多个源点和目标点的情况下,All Pairs Shortest Paths(APSP)问题可以通过Floyd-Warshall算法解决。 6. **强连通分量**:检测图中是否存在强连通分量,即图中每个顶点都可以通过一系列边...
5. **近似算法**:对于一些难以精确解决的问题,如最近点对查找,可能需要采用近似算法,如APSP(All Pairs Shortest Paths)的近似算法。 6. **应用领域**:计算几何的应用广泛,包括计算机图形学(游戏开发、动画...
Floyd 算法适用于 APSP(All Pairs Shortest Paths),稠密图效果最佳,边权可正可负。此算法简单有效,由于三重循环结构紧凑,对于稠密图,效率要高于执行|V|次 Dijkstra 算法。 Floyd 算法的优点是容易理解,可以算...