Download presentation
Presentation is loading. Please wait.
Published byClyde Wilkins Modified over 9 years ago
1
Length of a Path The weight (length) of a path is the sum of the weights of its edges. adcbe Path p: 7 2 1 5 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.
2
Single-Source Shortest Paths path s s-a s-a-b s-a-b-c s-a-d s-a-b-e s-a-b-e-f weight 0 3 4 6 6 6 9 s a b c d e f Problem Given 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. a s b e f d c 3 3 5 1 2 2 2 4 1 6 3 5 source
3
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...
4
Shortest-Paths Tree a s b e f d c 3 3 1 2 2 3 The unique simple path from s to any vertex v in the tree is a shortest path from s to v. 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).
5
Negative Weights 0 3 5 11 - -- 3 5 2 3 -6 7 8 4 -4 6 -3 2 -83 s a c e b d f g h j i unreachable from s negative-weight cycle source The shortest path may have length either or – .
6
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.
7
Optimal Substructure Any subpath of a shortest path is the shortest of all paths between the two intermediate vertices. u y x v shortest path from u to v: shortest path from x to y Greedy algorithm or dynamic programming? (Both are used in solving different versions of the shortest path problem.)
8
Representing Shortest Paths 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). During the execution of a shortest-path algorithm, maintain two arrays: Initialization for each vertex v in G do d[v] [v] NIL d[s] 0
9
Relaxation Test if the shortest path to v found so far can be improved by going through u. s w v u (v)(v) s w v u (v)(v) d[v] > d[u] + w(u, v) Relax(u, v) if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) [v] u Shortest path algorithms differ in the number of times the edges are relaxed and the order in which they are relaxed.
10
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) for i 1 to |V| – 1 do for each edge (u, v) E do Relax(u, v) for each edge (u, v) E do if d[v] > d[u] + w(u, v) then return false // negative-weight cycle found! return true // no negative-weight cycle Running time (VE)
11
An Example 0 6 8 2 7 -3 -4 7 9 5 -2 s t yz x 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)
12
Round 1 0 7 6 6 8 2 7 -3 -4 7 9 5 -2 s t yz x d[y] = 7 [y] = s d[t] = 6 [t] = s
13
Round 2 0 2 7 11 6 6 8 2 7 -3 -4 7 9 5 -2 s t yz x Relax (t, x), (t, y), (t, z), (x, t). d[x] = 11 [x] = t d[z] = 2 [z] = t
14
Round 2 (cont’d) 0 2 7 4 6 6 8 2 7 -3 -4 7 9 5 -2 s t yz x Relax (y, x), (y, z), (z, x), (z, s), (z, s), (s, t), (s, y). d[x] = 4 [x] = y
15
Round 3 0 2 7 4 2 6 8 2 7 -3 -4 7 9 5 -2 s t yz x d[t] = 2 [t] = x
16
Round 4 0 -2 7 4 2 6 8 2 7 -3 -4 7 9 5 -2 s t yz x d[z] = -2 [z] = t (unchanged)
17
Detecting Negative-Weight Cycle 0 5 2 -4 7 s a b c Order of edge relaxation: (s, a), (b, c), (b, a), (a, b), (s, c) 0 5 7 7 5 2 -4 7 s a b c 0 3 5 6 5 2 -4 7 s a b c 0 1 3 4 5 2 -4 7 s a b c 0 1 2 5 2 -4 7 s a b c d[a] > d[b] + w(b, a): a negative cycle exists!
18
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.