180 likes | 309 Views
Length of a Path. The weight ( length ) of a path is the sum of the weights of its edges. 7 2 1 5. Path p :. a. b. c. d. e. Edge weights: w ( a , b ) = 7, w ( b , c ) = 2, w ( c , d ) = 1, w ( d , e ) = 5
E N D
Length of a Path Theweight (length)of a path is the sum of the weights of its edges. 7 2 1 5 Path p: a b c d e Edge weights: w(a, b) = 7, w(b, c) = 2, w(c, d) = 1, w(d, e) = 5 Path weight w(p) = 7+2+1+5 = 15 A shortest path from vertex u to vertex v has the minimum weight among all paths connecting u to v.
3 d a 3 4 6 5 source s 1 c 1 f 2 2 5 3 e b 2 Single-Source Shortest Paths ProblemGiven a graph G = (V, E), where every edge e has a weight w(e), and a source vertex s in V, find the shortest path from s to every other vertex. s a b c d e f weight 0 3 4 6 6 6 9 path s s-a s-a-b s-a-b-c s-a-d s-a-b-e s-a-b-e-f
Applications • Transportation (shortest route from Ames to Phoenix?) • Network routing (how to direct packets to a destination across a network?) • Telecommunications • Speech interpretation (best interpretation of a spoken sentence) • Robot path planning • Medical imaging • Building block for network algorithms • ...
Shortest-Paths Tree The unique simple path from s to any vertex v in the tree is a shortest path from s to v. 3 d a 3 s 1 c f 2 3 e b 2 The vertices in the tree are exactly those reachable from s in G. Similar to the breadth-first tree (where all edges have weight 1).
negative-weight cycle Negative Weights The shortest path may have length either or – . a b -4 3 -1 4 h i 3 2 s 6 d g c 8 5 11 source 0 5 - -3 -8 3 2 f e 3 - - 7 j -6 unreachable from s
No Cycle in a Shortest Path A shortest path cannot contain a negative-weight cycle. A shortest path cannot contain a positive-weight cycle because otherwise the removal of such a cycle would reduce the path weight. Any zero-weight cycle in a shortest path can be removed. We only consider shortest path of at most |V| – 1 edges.
shortest path from u to v: u v x y shortest path from x to y Optimal Substructure Any subpath of a shortest path is the shortest of all paths between the two intermediate vertices. Greedy algorithm or dynamic programming? (Both are used in solving different versions of the shortest path problem.)
Representing Shortest Paths During the execution of a shortest-path algorithm, maintain two arrays: d(v)= length of the shortest path from s to v found so far (an upper bound on the weight of the eventual shortest path). (v)= the predecessor of v in the above path (used for backtracking the shortest path from s to v in the end). Initialization for each vertex v in G dod[v] [v] NIL d[s] 0
u u s s v v w w (v) (v) Relaxation Test if the shortest path to v found so far can be improved by going through u. Relax(u, v) ifd[v] > d[u] + w(u, v) thend[v] d[u] + w(u, v) [v] u d[v] > d[u] + w(u, v) Shortest path algorithms differ in the number of times the edges are relaxed and the order in which they are relaxed.
The Bellman-Ford Algorithm Edge weights may be negative. It detects any negative-weight cycle reachable from the source. Each edge is relaxed |V| – 1 times. Bellman-Ford(G, s) Initialize(G, s) fori 1 to |V| – 1 do for each edge (u, v) E do Relax(u, v) for each edge (u, v) E do ifd[v] > d[u] + w(u, v) then return false// negative-weight cycle found! return true// no negative-weight cycle Running time(VE)
x t 5 -2 6 8 -3 0 2 7 s -4 7 9 y z An Example Source: s Order of edge relaxation: (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)
x t 5 6 -2 6 8 -3 0 2 7 s -4 7 7 9 y z Round 1 d[t] = 6 [t] = s d[y] = 7 [y] = s
x t 5 11 6 -2 6 8 -3 0 2 7 s -4 7 2 7 9 y z Round 2 d[x] = 11 [x] = t d[z] = 2 [z] = t Relax (t, x), (t, y), (t, z), (x, t).
x t 5 4 6 -2 6 8 -3 0 2 7 s -4 7 2 7 9 y z Round 2 (cont’d) d[x] = 4 [x] = y Relax (y, x), (y, z), (z, x), (z, s), (z, s), (s, t), (s, y).
x t 5 4 2 -2 6 8 -3 0 2 7 s -4 7 2 7 9 y z Round 3 d[t] = 2 [t] = x
x t 5 4 2 -2 6 8 -3 0 2 7 s -4 7 -2 7 9 y z Round 4 d[z] = -2 [z] = t (unchanged)
a a a a a 2 2 2 2 2 5 5 5 5 5 1 3 5 -1 s s s s s b b b b b 0 0 0 0 0 3 1 5 7 -4 -4 -4 -4 -4 7 7 7 7 7 -1 -1 -1 -1 -1 6 4 2 7 c c c c c Detecting Negative-Weight Cycle Order of edge relaxation: (s, a), (b, c), (b, a), (a, b), (s, c) d[a] > d[b] + w(b, a): a negative cycle exists!
Correctness Assume G has no negative-weight cycles reachable from s. After |V| – 1 rounds of relaxation, d[v] is the length of a shortest path from s to v for every vertex v. For any vertex v, a path from s to v exists if and only if Bellman-Ford terminates with d[v] < . If G has no negative-weight cycles reachable from s, then Bellman-Ford returns true, d[v] records the shortest path length for every vertex v, and all [v] induce a shortest-paths tree. Otherwise, the algorithm returns false.