Slide 1

Slide 1 text

Chapter 24 Single-Source Shortest Paths Sun-Yuan Hsieh 謝孫源 教授 成功大學資訊工程學系

Slide 2

Slide 2 text

2 Shortest paths How to find the shortest route between two points on a map. Input: Directed graph G = (V, E) Weight function w : E → R Weight of path p = = = sum of edge weights on path p.   k v v v ,..., 1 , 0    k i i i v v w 1 1 ) , (

Slide 3

Slide 3 text

3 Shortest-path weight u to v : Shortest path u to v is any path p such that w(p) = δ(u, v). min{ ( ): }, if there exists a path ( , ) , otherwise. w p u v u v u v       p

Slide 4

Slide 4 text

4 Example: shortest paths from s [d values appear inside vertices. Shaded edges show shortest paths.] This example shows that the shortest path might not be unique. It also shows that when we look at shortest paths from one vertex to all other vertices, the shortest paths are organized as tree.

Slide 5

Slide 5 text

5 Can think of weights as representing any measure that accumulates linearly along a path. we want to minimize. Example: time, cost, penalties, loss. Generalization of breadth-first search to weighted graphs.

Slide 6

Slide 6 text

6 Variants Single-source Find shortest paths from a given source vertex s ∈ V to every vertex v ∈ V. Single-destination Find shortest paths to a given destination vertex. Single-pair Find shortest path from u to v. No way known that’s better in worst case than solving single-source. All-pairs Find shortest path from u to v for all u , v ∈ V. We’ll see algorithms for all-pairs in the next chapter.

Slide 7

Slide 7 text

7 Negative-weight edges OK, as long as no negative-weight cycles are reachable from the source. If we have a negative-weight cycle, we can just keep going around it, and get w(s, v) = -∞ for all v on the cycle. But OK if the negative-weight cycle is not reachable from the source. Some algorithms work only if there are no negative-weight edges in the graph. We’ll be clear when they’re allowed and not allowed.

Slide 8

Slide 8 text

8 Optimal substructure Lemma Any subpath of a shortest path is a shortest path. Proof: Cut - and - paste. Suppose this path p is a shortest path from u to v. Then δ(u ,v) = w(p) = w(pux ) + w(pxy ) + w(pyv ).

Slide 9

Slide 9 text

9 Now suppose there exists a shorter path x y. Then w(p’xy ) < w(pxy ) Construct p’ : Then So p wasn’t shortest path after all ! ■ (lemma) P’xy ( ') ( ) ( ' ) ( ) ( ) ( ) ( ) ( ) ux xy yv ux xy yv w p w p w p w p w p w p w p w p       

Slide 10

Slide 10 text

10 Cycles Shortest paths can’t contain cycles : Already ruled out negative-weight cycles. Positive-weight ⇒ we can get a shorter path by omitting the cycle. Zero-weight: no reason to use them ⇒ assume that our solutions won’t use them.

Slide 11

Slide 11 text

11 Output of single-source shortest-path algorithm For each vertex v ∈ V : d [v] = δ(s, v). Initially, d [v] = ∞. Reduces as algorithms progress. But always maintain d [v] ≥ δ(s, v). Call d [v] a shortest-path estimate. π [v] = predecessor of v on a shortest path from s. If no predecessor, π[v] = NIL. π induces a tree ------ shortest-path tree. We won’t prove properties of π in lecture ------ see text.

Slide 12

Slide 12 text

12 Initialization All the shortest-paths algorithms start with INIT-SINGLE-SOURCE. INIT-SINGLE-SOURCE (V, s) For each v ∈ V do d [v] ← ∞ π[v] ← NIL d [s] ← 0

Slide 13

Slide 13 text

13 Relaxing an edge (u, v) Can we improve the shortest-path estimate for v by going through u and taking (u, v) ? RELAX (u , v , w ) If d [v] > d [u]+ w(u, v) then d [v] ← d [u] + w ( u, v ) π [v] ← u

Slide 14

Slide 14 text

14 For all the single-source shortest-paths algorithms we’ll look at, start by calling INIT-SINGLE-SOURCE, then relax edges. The algorithms differ in the order and how many times they relax each edge.

Slide 15

Slide 15 text

15 Shortest-paths properties Based on calling INIT-SINGLE-SOURCE once and then calling RELAX zero or more times. Triangle inequality For all (u, v) ∈ E, we have δ(s, v) ≤ δ(s, u) + w(u, v). Proof: Weight of shortest path s v is ≤ weight of any path s v. Path s u → v is a path s v, and if we use a shortest path s u, its weight isδ(s, u) + w(u, v). ■ s u v w(u,v) (s,u) (s,v)

Slide 16

Slide 16 text

16 Upper-bound property Always have d [v] ≥ δ(s, v) for all v. Once d [v] = δ(s, v), it never changes. Proof: Initially true. Suppose there exists a vertex such that d [v] < δ(s, v). Without loss of generality, v is first vertex for which this happens. Let u be the vertex that causes d [v] to change. Then d [v] = d [u] + w(u, v). So, d [v] < δ(s, v) ≤ δ(s, u) + w(u, v) (triangle inequality) ≤ d [u] + w(u, v) (v is first violation) ⇒ d [v] < d [u] + w(u, v). (Contradicts d [v] = d [u] + w(u, v)) Once d [v] reaches δ(s, v), it never goes lower. It never goes up, since relaxations only lower shortest-path estimates. ■

Slide 17

Slide 17 text

17 No-path property If δ(s, v) = ∞, then d [v] = ∞ always. Proof: d [v] ≥ δ(s, v) = ∞ ⇒ d [v] = ∞. ■

Slide 18

Slide 18 text

18 Convergence property If s u → v is a shortest path, d [u] =δ(s, u), and we call RELAX(u, v, w), then d [v] = δ(s, v) afterward. Proof: After relaxation: d [v] ≤ d [u] + w(u, v) (RELAX code) = δ(s, u) + w(u, v) = δ(s, v) (lemma ----- optimal substructure) Since d [v] ≥ δ(s, v), must have d [v] =δ(s, v). ■

Slide 19

Slide 19 text

19 Path relaxation property Let p = be a shortest path from . If we relax, in order, , even intermixed with other relaxations, then . Proof: Induction to show that after is relaxed. Basis: i = 0. Initially, . Inductive step: Assume . Relax . By Convergence Property, afterward and never changes. 0 1 , ,..., k v v v   0 to k s v v  ) , ( ),..., , ( ), , ( 1 2 1 1 0 k k v v v v v v  [ ] ( , ) k k d v s v   [ ] ( , ) i i d v s v   1 ( , ) i i v v  0 0 [ ] 0 ( , ) ( , ) d v s v s s      1 1 [ ] ( , ) i i d v s v     1 ( , ) i i v v  [ ] ( , ) i i d v s v   [ ] i d v

Slide 20

Slide 20 text

20 d(v1 ) = (s,v0 ) + w(v0 ,v1 ) = (s,v1 ) d(v2 ) = (s,v1 ) + w(v1 ,v2 ) = (s,v2 ) d(v3 ) = (s,v2 ) + w(v2 ,v3 ) = (s,v3 ) d(vk ) = (s,vk-1 ) + w(vk-1 ,vk ) = (s,vk ) v0 v1 v2 v3 vk-1 vk

Slide 21

Slide 21 text

21 The Bellman-Ford algorithm Allows negative-weight edges. Computes d [v] and π[v] for all v ∈ V. Returns TRUE if no negative-weight cycles reachable from s, FALSE otherwise.

Slide 22

Slide 22 text

22 BELLMAN-FORD (V, E, w, s) INIT-SINGLE-SOURCE (V, s) for i ← 1 to |V | - 1 do for each edge (u, v) ∈ E do RELAX (u, v, w) for each edge (u , v) ∈ E do if d [v] > d [u] + w(u, v) then return FALSE return TRUE Core: The first for loop relaxes all edges |V | - 1 times. Time: Θ(VE).

Slide 23

Slide 23 text

23 i = 1 6 7 8 9 2 -4 -3 5 -2 7 0     s t y x z

Slide 24

Slide 24 text

23 i = 1 6 7 8 9 2 -4 -3 5 -2 7 0     s t y x z

Slide 25

Slide 25 text

23 i = 1 6 7 8 9 2 -4 -3 5 -2 7 0     s t y x z

Slide 26

Slide 26 text

23 i = 1 6 7 8 9 2 -4 -3 5 -2 7 0     s t y x z

Slide 27

Slide 27 text

23 i = 1 6 7 8 9 2 -4 -3 5 -2 7 0     s t y x z

Slide 28

Slide 28 text

23 i = 1 6 7 8 9 2 -4 -3 5 -2 7 0     s t y x z

Slide 29

Slide 29 text

23 i = 1 6 7 8 9 2 -4 -3 5 -2 7 0     s t y x z

Slide 30

Slide 30 text

23 i = 1 6 7 8 9 2 -4 -3 5 -2 7 0     s t y x z

Slide 31

Slide 31 text

23 i = 1 6 7 8 9 2 -4 -3 5 -2 7 0     s t y x z

Slide 32

Slide 32 text

23 i = 1 6 7 8 9 2 -4 -3 5 -2 7 0    s t y x z 6

Slide 33

Slide 33 text

23 i = 1 6 7 8 9 2 -4 -3 5 -2 7 0   s t y x z 6 7

Slide 34

Slide 34 text

24 i = 2 6 7 8 9 2 -4 -3 5 -2 7 0   s t y x z 6 7

Slide 35

Slide 35 text

24 i = 2 6 7 8 9 2 -4 -3 5 -2 7 0  s t y x z 6 7 11

Slide 36

Slide 36 text

24 i = 2 6 7 8 9 2 -4 -3 5 -2 7 0  s t y x z 6 7 11

Slide 37

Slide 37 text

24 i = 2 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 6 7 11 2

Slide 38

Slide 38 text

24 i = 2 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 6 7 11 2

Slide 39

Slide 39 text

24 i = 2 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 6 7 2 4

Slide 40

Slide 40 text

24 i = 2 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 6 7 2 4

Slide 41

Slide 41 text

24 i = 2 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 6 7 2 4

Slide 42

Slide 42 text

24 i = 2 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 6 7 2 4

Slide 43

Slide 43 text

24 i = 2 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 6 7 2 4

Slide 44

Slide 44 text

24 i = 2 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 6 7 2 4

Slide 45

Slide 45 text

i = 3 25 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 6 7 2 4

Slide 46

Slide 46 text

i = 3 25 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 6 7 2 4

Slide 47

Slide 47 text

i = 3 25 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 6 7 2 4

Slide 48

Slide 48 text

i = 3 25 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 6 7 2 4

Slide 49

Slide 49 text

i = 3 25 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 2 4 2

Slide 50

Slide 50 text

i = 3 25 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 2 4 2

Slide 51

Slide 51 text

i = 3 25 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 2 4 2

Slide 52

Slide 52 text

i = 3 25 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 2 4 2

Slide 53

Slide 53 text

i = 3 25 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 2 4 2

Slide 54

Slide 54 text

i = 3 25 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 2 4 2

Slide 55

Slide 55 text

i = 3 25 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 2 4 2

Slide 56

Slide 56 text

26 i = 4 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 2 4 2

Slide 57

Slide 57 text

26 i = 4 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 2 4 2

Slide 58

Slide 58 text

26 i = 4 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 2 4 2

Slide 59

Slide 59 text

26 i = 4 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 4 2 -2

Slide 60

Slide 60 text

26 i = 4 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 4 2 -2

Slide 61

Slide 61 text

26 i = 4 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 4 2 -2

Slide 62

Slide 62 text

26 i = 4 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 4 2 -2

Slide 63

Slide 63 text

26 i = 4 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 4 2 -2

Slide 64

Slide 64 text

26 i = 4 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 4 2 -2

Slide 65

Slide 65 text

26 i = 4 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 4 2 -2

Slide 66

Slide 66 text

26 i = 4 6 7 8 9 2 -4 -3 5 -2 7 0 s t y x z 7 4 2 -2

Slide 67

Slide 67 text

27 Example: Values you get on each pass and how quickly it converges depends on order of relaxation. But guaranteed to converge after |V | - 1 passes, assuming no negative-weight cycles.

Slide 68

Slide 68 text

28 Proof: Use path-relaxation property. Let v be reachable from s, and let p = be a shortest path from s to v, where and . Since p is acyclic, it has ≤ |V | - 1edges, so k ≤ |V | - 1. Each iteration of the for loop relaxes all edges: First iteration relaxes . Second iteration relaxes . kth iteration relaxes . By the path-relaxation property, . ■ 0 1 , ,..., k v v v   0 v s  k v v  0 1 ( , ) v v 1 2 ( , ) v v 1 ( , ) k k v v  [ ] [ ] ( , ) ( , ) k k d v d v s v s v     

Slide 69

Slide 69 text

29 How about the TRUE/FALSE return value? Suppose there is no negative-weight cycle reachable from s. At termination, for all (u, v) ∈ E, d [v] = δ(s, v) ≤ δ(s, u) + w(u, v) (triangle inequality) = d [u] + w(u, v) So BELLMAN-FORD returns TRUE.

Slide 70

Slide 70 text

30 0 1 , ,..., k v v v   0 k v v  1 1, 1 1 1 1, 1 1 [ ] ( [ ] ( )) [ ] ( ) k k i i i i i i k k i i i i i d v d v w v v d v w v v                 1 1 [ ] [ ] ( , ) i i i i d v d v w v v    

Slide 71

Slide 71 text

31 Each vertex appears once in each summation and . ⇒ . This contradicts C being a negative-weight cycle! ■ 1 [ ] k i i d v   1 1 [ ] k i i d v    1 1 0 ( , ) k i i i w v v    

Slide 72

Slide 72 text

32 Single-source shortest paths in a directed acyclic graph Since a dag, we’re guaranteed no negative-weight cycles. DAG-SHORTEST-PATHS (V, E, w, s) topologically sort the vertices INIT-SINGLE-SOURCE (V, s) for each vertex u, taken in topologically sorted order do for each vertex v ∈ Adj [u] do RELAX (u, v, w)

Slide 73

Slide 73 text

33 Example: Time:Θ(V + E ).

Slide 74

Slide 74 text

34 ∞ 0 ∞ ∞ ∞ ∞ 5 2 7 -1 -2 6 1 3 4 2 r s t x y z Figure 24.5 The execution of the algorithm for shortest paths in a directed acyclic graph. The vertices are topologically sorted from left to right. The source vertex is s. The d values are shown within the vertices, and shaded edges indicate the π values.

Slide 75

Slide 75 text

34 ∞ 0 ∞ ∞ ∞ ∞ 5 2 7 -1 -2 6 1 3 4 2 ∞ r s t x y z Figure 24.5 The execution of the algorithm for shortest paths in a directed acyclic graph. The vertices are topologically sorted from left to right. The source vertex is s. The d values are shown within the vertices, and shaded edges indicate the π values.

Slide 76

Slide 76 text

34 ∞ 0 ∞ ∞ ∞ ∞ 5 2 7 -1 -2 6 1 3 4 2 ∞ r s t x y z 0 Figure 24.5 The execution of the algorithm for shortest paths in a directed acyclic graph. The vertices are topologically sorted from left to right. The source vertex is s. The d values are shown within the vertices, and shaded edges indicate the π values.

Slide 77

Slide 77 text

34 ∞ 0 ∞ ∞ ∞ ∞ 5 2 7 -1 -2 6 1 3 4 2 ∞ r s t x y z 0 6 2 Figure 24.5 The execution of the algorithm for shortest paths in a directed acyclic graph. The vertices are topologically sorted from left to right. The source vertex is s. The d values are shown within the vertices, and shaded edges indicate the π values.

Slide 78

Slide 78 text

34 ∞ 0 ∞ ∞ ∞ ∞ 5 2 7 -1 -2 6 1 3 4 2 ∞ r s t x y z 0 6 2 2 Figure 24.5 The execution of the algorithm for shortest paths in a directed acyclic graph. The vertices are topologically sorted from left to right. The source vertex is s. The d values are shown within the vertices, and shaded edges indicate the π values.

Slide 79

Slide 79 text

34 ∞ 0 ∞ ∞ ∞ ∞ 5 2 7 -1 -2 6 1 3 4 2 ∞ r s t x y z 0 6 2 2 6 4 Figure 24.5 The execution of the algorithm for shortest paths in a directed acyclic graph. The vertices are topologically sorted from left to right. The source vertex is s. The d values are shown within the vertices, and shaded edges indicate the π values.

Slide 80

Slide 80 text

34 ∞ 0 ∞ ∞ ∞ ∞ 5 2 7 -1 -2 6 1 3 4 2 ∞ r s t x y z 0 6 2 2 6 4 6 Figure 24.5 The execution of the algorithm for shortest paths in a directed acyclic graph. The vertices are topologically sorted from left to right. The source vertex is s. The d values are shown within the vertices, and shaded edges indicate the π values.

Slide 81

Slide 81 text

34 ∞ 0 ∞ ∞ ∞ ∞ 5 2 7 -1 -2 6 1 3 4 2 ∞ r s t x y z 0 6 2 2 6 4 6 5 Figure 24.5 The execution of the algorithm for shortest paths in a directed acyclic graph. The vertices are topologically sorted from left to right. The source vertex is s. The d values are shown within the vertices, and shaded edges indicate the π values.

Slide 82

Slide 82 text

34 ∞ 0 ∞ ∞ ∞ ∞ 5 2 7 -1 -2 6 1 3 4 2 ∞ r s t x y z 0 6 2 2 6 4 6 5 5 Figure 24.5 The execution of the algorithm for shortest paths in a directed acyclic graph. The vertices are topologically sorted from left to right. The source vertex is s. The d values are shown within the vertices, and shaded edges indicate the π values.

Slide 83

Slide 83 text

34 ∞ 0 ∞ ∞ ∞ ∞ 5 2 7 -1 -2 6 1 3 4 2 ∞ r s t x y z 0 6 2 2 6 4 6 5 5 3 Figure 24.5 The execution of the algorithm for shortest paths in a directed acyclic graph. The vertices are topologically sorted from left to right. The source vertex is s. The d values are shown within the vertices, and shaded edges indicate the π values.

Slide 84

Slide 84 text

34 ∞ 0 ∞ ∞ ∞ ∞ 5 2 7 -1 -2 6 1 3 4 2 ∞ r s t x y z 0 6 2 2 6 4 6 5 5 3 3 Figure 24.5 The execution of the algorithm for shortest paths in a directed acyclic graph. The vertices are topologically sorted from left to right. The source vertex is s. The d values are shown within the vertices, and shaded edges indicate the π values.

Slide 85

Slide 85 text

35 Correctness: Because we process vertices in topologically sorted order, edges of any path must be relaxed in order of appearance in the path. ⇒ Edges on any shortest path are relaxed in order. ⇒ By path-relaxation property, correct. ■

Slide 86

Slide 86 text

36 Dijkstra’s algorithm No negative-weight edges. Essentially a weighted version of breadth-first search. Instead of a FIFO queue, uses a priority queue. Keys are shortest-path weights ( d [v] ). Have two sets of vertices: S = vertices whose final shortest-path weights are determined. Q = priority queue = V – S.

Slide 87

Slide 87 text

37 DIJKSTRA (V, E, w, s) INIT-SINGLE-SOURCE (V, s) S ← Ø Q ← V // i.e., insert all vertices into Q While Q ≠ Ø do u ← EXTRACT-MIN(Q) S ← S U {u} for each vertex v ∈ Adj [u] do RELAX (u, v, w) Like Prim’s algorithm, but computing d [v], and using shortest- path weights as keys. Dijkstra’s algorithm can be viewed as greedy, since it always chooses the “lightest” (“closest”?) vertex in V – S to add to S.

Slide 88

Slide 88 text

38 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0

Slide 89

Slide 89 text

38 ∞ ∞ ∞ ∞ 0 s u v x y 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0

Slide 90

Slide 90 text

38 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0 ∞ ∞ ∞ ∞ 0 s u v x y

Slide 91

Slide 91 text

38 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0 ∞ ∞ ∞ ∞ 0 s u v x y 10 1 5 2 3 2 4 6 9 7 s x y u v 10 ∞ ∞ 5 0

Slide 92

Slide 92 text

38 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0 10 1 5 2 3 2 4 6 9 7 s x y u v 10 ∞ ∞ 5 0 ∞ 10 5 ∞ y u v x

Slide 93

Slide 93 text

38 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0 10 1 5 2 3 2 4 6 9 7 s x y u v 10 ∞ ∞ 5 0 ∞ 10 ∞ 5 x u v y

Slide 94

Slide 94 text

38 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0 10 1 5 2 3 2 4 6 9 7 s x y u v 10 ∞ ∞ 5 0 ∞ 10 ∞ 5 x u v y

Slide 95

Slide 95 text

38 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0 10 1 5 2 3 2 4 6 9 7 s x y u v 10 ∞ ∞ 5 0 ∞ 10 ∞ 5 x u v y 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 14 5 0

Slide 96

Slide 96 text

38 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0 10 1 5 2 3 2 4 6 9 7 s x y u v 10 ∞ ∞ 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 14 5 0 14 8 7 y u v y

Slide 97

Slide 97 text

38 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0 10 1 5 2 3 2 4 6 9 7 s x y u v 10 ∞ ∞ 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 14 5 0 14 8 7 y u v y

Slide 98

Slide 98 text

38 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0 10 1 5 2 3 2 4 6 9 7 s x y u v 10 ∞ ∞ 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 14 5 0 14 8 7 y u v y 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 13 5 0

Slide 99

Slide 99 text

38 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0 10 1 5 2 3 2 4 6 9 7 s x y u v 10 ∞ ∞ 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 14 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 13 5 0 8 13 v u v y

Slide 100

Slide 100 text

38 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0 10 1 5 2 3 2 4 6 9 7 s x y u v 10 ∞ ∞ 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 14 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 13 5 0 13 8 u v v y

Slide 101

Slide 101 text

38 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0 10 1 5 2 3 2 4 6 9 7 s x y u v 10 ∞ ∞ 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 14 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 13 5 0 13 8 u v v y

Slide 102

Slide 102 text

38 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0 10 1 5 2 3 2 4 6 9 7 s x y u v 10 ∞ ∞ 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 14 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 13 5 0 13 8 u v v y 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 9 5 0

Slide 103

Slide 103 text

38 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0 10 1 5 2 3 2 4 6 9 7 s x y u v 10 ∞ ∞ 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 14 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 13 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 9 5 0 9 v v y

Slide 104

Slide 104 text

38 10 1 5 2 3 2 4 6 9 7 s x y u v ∞ ∞ ∞ ∞ 0 10 1 5 2 3 2 4 6 9 7 s x y u v 10 ∞ ∞ 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 14 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 13 5 0 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 9 5 0 9 v v y 10 1 5 2 3 2 4 6 9 7 s x y u v 8 7 9 5 0

Slide 105

Slide 105 text

39 Example: Order of adding to S: s, y, z, x.

Slide 106

Slide 106 text

40 Correctness: Loop invariant : At the start of each iteration of the while loop, d [v] = δ(s, v) for all v ∈ S. Initialization: Initially, S = Ø , so trivially true. Termination: At end, Q= Ø ⇒ S = V ⇒ d [v] = δ(s, v) for all v ∈ V.

Slide 107

Slide 107 text

41 Maintenance: Need to show that d [u] = δ(s, u) when u is added to S in each iteration. Suppose there exists u such that d [u] ≠ δ(s, u). Without loss of generality, let u be the first vertex for which d [u] ≠ δ(s, u) when u is added to S. Observations: u ≠ s, since d [s] = δ(s, s) = 0. Therefore, s ∈ S , so S ≠ Ø . There must be some path s u, since otherwise d [u]= δ(s, u) = ∞ by no-path property.

Slide 108

Slide 108 text

42 So, there’s a path s u. This means there’s a shortest path s u. Just before u is added to S, path p connects a vertex in S ( i.e., s ) to a vertex in V - S ( i.e., u ). Let y be first vertex along p that’s in V - S, and let x ∈ S be y’s predecessor. p

Slide 109

Slide 109 text

43 Decompose p into s x → y u. (Could have x = s or y = u, so that or may have no edges.) p1 p2 1 p 2 p

Slide 110

Slide 110 text

44 Claim d [y] = δ(s, y) when u is added to S. Proof x ∈ S and u is the first vertex such that d [u] ≠ δ(s, u) when u is added to S ⇒ d [x] = δ(s, x) when x is added to S. Relaxed (x , y) at that time, so by the convergence property, d [y] = δ(s, y). ■ (claim)

Slide 111

Slide 111 text

45 Now can get a contradiction to d [u] ≠ δ(s, u): y is on shortest path s u, and all edge weights are nonnegative ⇒ δ(s, y) ≤ δ(s, u) ⇒ d [y] = δ(s, y) ≤ δ(s, u) ≤ d [u] (upper-bound property). Also, both y and u were in Q when we chose u, so d [u] ≤ d [y] ⇒ d [u] = d [y]. Therefore, d [y] = δ(s, y) = δ(s, u) = d [u]. Contradicts assumption that d [u] ≠ δ(s, u). Hence, Dijkstra’s algorithm is correct. ■

Slide 112

Slide 112 text

46 Analysis: Like Prim’s algorithm, depends on implementation of priority queue. If binary heap, each operation takes O (lg V) time ⇒ O (E lg V). If a Fibonacci heap: Each EXTRACT-MIN takes O(1) amortized time. There are O (V) other operations, taking O (lg V) amortized time each. Therefore, time is O ( V lg V + E ).

Slide 113

Slide 113 text

47 Difference constraints Given a set of inequalities of the form . x’s are variables, 1≤ i, j ≤ n, b’s are constants, 1≤ k ≤ m. Want to find a set of values for the x’s that satisfy all m inequalities, or determine that no such values exist. Call such a set of values a feasible solution. j i k x x b  

Slide 114

Slide 114 text

48 Example: Solution: x = (0, -4, -5, -3) Also: x = (5, 1, 0, 2) = [above solution] + 5 1 2 1 3 2 4 3 4 4 1 5 6 1 2 3 x x x x x x x x x x             

Slide 115

Slide 115 text

49 Lemma If x is a feasible solution, then so is x + d for any constant d. Proof x is a feasible solution ⇒ for all i, j ,k. ⇒ . ■ (lemma) j i k x x b   ( ) ( ) j i k x d x d b    

Slide 116

Slide 116 text

50 Constraint graph G = (V , E), weighted, directed. V = : one vertex per variable + E = { is a constraint} U for all j if 0 1 2 , , ,..., n v v v v   0 v ( , ): i j j i k v v x x b   0 1 0 2 0 {( , ),( , ),...,( , )} n v v v v v v 0 ( , ) 0 j w v v  ( , ) i j k w v v b  j i k x x b  

Slide 117

Slide 117 text

51 Theorem Given a system of difference constraints, let G = (V, E) be the corresponding Constraint graph. 1. If G has no negative-weight cycles, then is a feasible solution. 2. If G has a negative-weight cycle, then there is no feasible solution. 0 1 0 2 0 ( ( , ), ( , ),..., ( , )) n x v v v v v v    

Slide 118

Slide 118 text

52 Proof 1. Show no negative-weight cycles ⇒ feasible solution. Need to show that for all constraints. Use By the triangle inequality, Therefore, feasible. j i k x x b   0 0 ( , ) ( , ) ( , ) j j i i k i j x v v x v v b w v v      0 0 ( , ) ( , ) ( , ) j i i j j i k j i k v v v v w v v x x b x x b        

Slide 119

Slide 119 text

53 2. Show negative-weight cycles ⇒ no feasible solution. Without loss of generality, let a negative-weight cycle be C = where ( can’t be on C, since has no entering edges.) C corresponds to the constraints (The last two inequalities above are incorrect in the first three printings of the book. They were corrected in the fourth printing.) 2 1 1 2 3 2 2 3 1 2 2 1 1 1 ( , ) ( , ) ( , ) ( , ) k k k k k k k k x x w v v x x w v v x x w v v x x w v v               1 2 , ,..., k v v v   1 k v v  0 v 0 v

Slide 120

Slide 120 text

54 If x is a solution satisfying these inequalities, it must satisfy their sum. So add them up. Each is added once and subtracted once. We get 0 ≤ w (C). But w (C) < 0 , since C is a negative-weight cycle. Contradiction ⇒ no such feasible solution x exists. ■ (theorem) 1 1 ( ) k k v v x x    i x

Slide 121

Slide 121 text

55 How to find a feasible solution 1. Form constraint graph. n + 1 vertices. m + n edges. Θ (m + n) time. 2. Run BELLMAN-FORD from . O ((n + 1)(m + n)) = O ( + nm) time. 3. If BELLMAN-FORD returns FALSE ⇒ no feasible solution. If BELLMAN-FORD returns TRUE ⇒ set for all i. 0 v 2 n 0 ( , ) i i x v v  