/
Introduction to Algorithms: Introduction to Algorithms:

Introduction to Algorithms: - PowerPoint Presentation

marina-yarberry
marina-yarberry . @marina-yarberry
Follow
344 views
Uploaded On 2019-11-06

Introduction to Algorithms: - PPT Presentation

Introduction to Algorithms Shortest Paths I Introductio n to Algorithms Shortes t Paths I Properties of shortest paths Dijkstras A lgorithm Correctness Analysis Breadth F irst S earch ID: 763951

computer 421 path science 421 computer science path shortest dijkstra

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Introduction to Algorithms:" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Introduction to Algorithms:Shortest Paths I

Introductio n to Algorithms Shortes t Paths IProperties of shortest pathsDijkstra’s AlgorithmCorrectnessAnalysisBreadth-First Search CS 421 - Computer Science II 2

Paths in Graphs Consider a digraph G = (V, E) with edge-weight function w : E  . The weight of path p = v1  v2   vk is defined to bek 1w( p)   w(vi , vi1) .i1   CS 421 - Computer Science II 3

Paths in Graphs 4 –2 –5 1Example:w(p) = 4 + (-2) + (-5) + 1 = -2CS 421 - Computer Science II 4          p:

Shortes t Paths A shortest path from u to v is a path of minimum weight from u to v. The shortest- path weight from u to v is defined as(u, v) = min{w(p) : p is a path from u to v}.Note: (u, v) = , if no path from u to v exists.CS 421 - Computer Science II 5

Well- D efinedness of Shortest Paths If a graph G contains a negative-weight cycle, then some shortest paths may not exist.CS 421 - Computer Science II 6Example:u v … < 0 Then, (u, v) = -.

Optimal Substructure Theorem. A sub-path of a shortest path is a shortest path.CS 421 - Computer Science II 7

Optimal Substructure Theorem. A sub-path of a shortest path is a shortest path.Proof. Suppose that p is the shortest path between the vertices u and v. And that x and y lie on that shortest path. CS 421 - Computer Science II 8 u v x yp:

Optimal Substructure A ssume there’s another sub-path between x and y with a lower weight. Then that path would sub-path of a shortest path between u and v. But p is the shortest path. Which is a contradiction. Therefore, a sub-path of a shortest path is also a shortest path. CS 421 - Computer Science II 9 u x y v p:

Triangle I nequality Theorem. For all u, v, x  V, we have(u, v)  (u, x) +  ( x, v).CS 421 - Computer Science II 10Proof. u x v  ( u , v )  ( u , x )  ( x , v )

Single- S ource S hortest Paths Problem. From a given source vertex s  V, find the shortest-path weights (s, v) for all v  V . If all edge weights w(u, v) are non-negative, all shortest-path weights must exist. (May be ).IDEA: Greedy.Maintain a set S of vertices whose shortest- path distances from s are known.At each step add to S the vertex v  V – Swhose distance estimate from s is minimal. Update the distance estimates of vertices adjacent to v . CS 421 - Computer Science II 11

Dijkstra’ s Algorithm d [s]  0for each v  V – {s}do d[v]   S  Q  Vwhile Q  ⊳ Q is a priority queue with V – SCS 421 - Computer Science II 12do u  EXTRACT-MIN(Q)S  S  {u}for each v  Adj [ u ] do if d [ v ] > d [ u ] + w ( u , v ) then d [ v ]  d [ u ] + w ( u , v )

Dijkstra’ s A lgorithmd[s]  0for each v  V – {s} do d [ v ]  S  Q  Vwhile Q  ⊳ Q is a priority queue with V – S13do u  EXTRACT-MIN(Q)S  S  {u} for eac h v  Adj [ u ] do if d [ v ] > d [ u ] + w ( u , v ) then d [ v ]  d [ u ] + w ( u , v ) Implici t D ECREAS E -K EY relaxation step

Example of Dijkstra’s Algorithm A BDCE1031 4 7 9 8 2 2 Graph with non-negative edge weights:CS 421 - Computer Science II 14

Example of Dijkstra’s Algorithm A BDCE1031 4 7 9 8 2 2 Graph with non-negative edge weights:CS 421 - Computer Science II 150Q: A B C D Ed: 0      S : {}

Example of Dijkstra’s Algorithm A BDCE1031 4 7 9 8 2 2 CS 421 - Computer Science II 160Q: A B C D Ed: 0     “A”  EXTRACT - M IN ( Q ) : S : { A }

Example of Dijkstra’s Algorithm A BDCE1031 4 7 9 8 2 2 CS 421 - Computer Science II 17010Q: A B C D Ed: 0 10 3  3 S: { A } Relax all edges leaving A :

Example of Dijkstra’s Algorithm A BDCE1031 4 7 9 8 2 2 CS 421 - Computer Science II 18010Q: A B C D Ed: 0 10 3  3 “C”  E XTRACT - M IN ( Q ) : S : { A, C }

Example of Dijkstra’s Algorithm A BDCE1031 4 7 9 8 2 2 CS 421 - Computer Science II 190 711Q: A B C D Ed: 0 7 3 11 53 5 S: { A, C } Relax all edges leaving C :

Example of Dijkstra’s Algorithm A BDCE1031 4 7 9 8 2 2 CS 421 - Computer Science II 200 711Q: A B C D Ed: 0 7 3 11 53 5 “E”  E XTRACT - M IN ( Q ) : S : { A, C , E }

Example of Dijkstra’s Algorithm A BDCE1031 4 7 9 8 2 2 CS 421 - Computer Science II 210 711Q: A B C D Ed: 0 7 3 11 53 5 S : { A, C , E } Relax all edges leaving E :

Example of Dijkstra’s Algorithm A BDCE1031 4 7 9 8 2 2 CS 421 - Computer Science II 220 711Q: A B C D Ed: 0 7 3 11 53 5 “B”  E XTRACT - M IN ( Q ) : S : { A, C , E, B }

Example of Dijkstra’s Algorithm A BDCE1031 4 7 9 8 2 2 CS 421 - Computer Science II 230 7 9 Q: A B C D Ed: 0 7 3 9 53 5 S : { A, C , E, B } Relax all edges leaving B :

Example of Dijkstra’s Algorithm A BDCE1031 4 7 9 8 2 2 CS 421 - Computer Science II 240 7 9 Q: A B C D Ed: 0 7 3 9 53 5 S : { A, C , E, B } “D”  E XTRACT - M IN ( Q ) :

Example of Dijkstra’s Algorithm A BDCE1031 4 7 9 8 2 2 CS 421 - Computer Science II 250 7 9 Q: A B C D Ed: 0 7 3 9 53 5 S : { A, C , E, B }

Dijkstra’s Algorithm – Finding Shortest Path Algorithm determines weight of shortest path between source vertex s but how find the actual shortest path?Shortest Path Tree: union of all shortest paths. For each vertex v  V and the last edge into that vertex that was relaxed (u, v), the union of all of those edges is a shortest path tree. Has the property that there’s a unique path between s and all v  V and it’s the shortest path. CS 421 - Computer Science II 26

Analysis of Dijkstra’ s Algorithm d [s]  0for each v  V – {s}do d[v]   S  Q  Vwhile Q  CS 421 - Computer Science II 27do u  EXTRACT-MIN(Q)S  S  {u}for each v  Adj[u]do if d[v] > d[ u] + w ( u , v ) then d [ v ]  d [ u ] + w ( u , v ) | V | times

Analysis of Dijkstra’ s Algorithmwhile Q  CS 421 - Computer Science II 28do u  E XTRACT-M I N ( Q)S  S  {u}for each v  Adj[u]do if d[v] > d[u] + w(u, v)then d[v]  d[u] + w(u, v) | V | times degree ( u ) times Handshakin g Lemm a   ( E ) implici t D ECREASE - K EY ’s.

Analysis of Dijkstra’ s Algorithm whil e Q  CS 421 - Computer Science II 29do u  EXTRACT-MIN(Q) S  S  {u}for each v  Adj[u]do if d[v] > d[u] + w(u, v)then d[v]  d[u] + w(u, v) | V | times degree ( u ) times Tim e =  ( V · T E XTRAC T - M IN + E · T D ECREAS E - K EY ) Note: Same formula as in the analysis of Prim’s minimum spannin g tree algorithm.

Analysi s of Dijkstra ( cont'd) Time = (V)·TEXTRACT-MIN + (E)·TDECREASE- KEY Total Q T EXTRACT-MIN TDECREASE-KEYCS 421 - Computer Science II 30O(V2)O(V) O(1)O(lg V) O(lg V)O((V+E )l g V ) array binary heap Fibonacci heap O (l g V ) amortized O (1) amortized O ( E + V l g V ) worst case

Unweighte d Graphs Suppose that w(u, v) = 1 for all (u, v)  E. Can Dijkstra’s algorithm be improved?Use a simple FIFO queue instead of a priority queue.CS 421 - Computer Science II 31

Unweighte d Graphs CS 421 - Computer Science II 32Breadth-first searchwhile Q  do u  DEQUEUE(Q)for each v  Adj[u]do if d[v] = then d[v]  d[u] + 1 ENQUEUE(Q, v)Analysis: Time = O(V + E).

Example of B readth-First Search a f h d b ge icQ:CS 421 - Computer Science II 33

Example of B readth-First Search a f h d b ge icCS 421 - Computer Science II 340c0Q: a

Example of B readth-First Search a f h db ge icCS 421 - Computer Science II 3501c1 1Q: a b d 1

Example of B readth-First Search a f h db ge icCS 421 - Computer Science II 360112Q: a b d c e 2 1 2 2

Example of B readth-First Search a f h db ge icCS 421 - Computer Science II 3701122 2Q: a b d c e 2

Example of B readth-First Search a f h db ge icCS 421 - Computer Science II 38011222Q : a b d c e

Example of B readth-First Search a f h db ge icCS 421 - Computer Science II 39011223 3 Q: a b d c e g i 3 3

Example of B readth-First Search a f h db ge icCS 421 - Computer Science II 40011223 3 3 4 Q : a b d c e g i f 4

Example of B readth-First Search a f h db ge icCS 421 - Computer Science II 41011223 3 4 4 4 Q : a b d c e g i f h 4

Example of B readth-First Search a f h db ge icCS 421 - Computer Science II 42011223 3 4 4 4 Q : a b d c e g i f h

Example of B readth-First Search a f h db ge icCS 421 - Computer Science II 43011223 3 4 4 Q : a b d c e g i f h

CS 421 - Computer Science II 44

Correctnes s Dijkstra’s — P art I Lemma. Initializing d[s]  0 and d[v]   for all v  V – { s} establishes d[v]  (s, v) for all v  V, and this invariant is maintained over any sequence of relaxation steps.CS 421 - Computer Science II 45

Correctnes s Dijkstra’s — Part I Contradiction. CS 421 - Computer Science II 46 Proof. Suppos e not. Let v be the first vertex forwhich d[v] < (s, v), and let u be the vertex that caused d[v] to change: d[v] = d[u] + w(u , v) . Then, d [ v ] <  ( s , v ) supposition   ( s , u ) +  ( u , v ) triangle inequality   ( s , u ) + w ( u , v ) sh . path  specific path  d [ u ] + w ( u , v ) v is first violation

Correctnes s Dijkstra’s — P art II Lemma. Let u be v’s predecessor on a shortestpath from s to v. Then, if d[u] =  (s , u ) and edge (u, v) is relaxed, we have d[v]  (s, v) after the relaxation.CS 421 - Computer Science II 47

Correctnes s Dijkstra’s — Part II Proof . Observe that (s, v) = (s, u) + w(u, v). Suppose that d [ v ] > (s, v) before the relaxation. (Otherwise, we’re done.) Then, the test d[v] > d[u] + w(u, v) succeeds, because d[v] > (s, v) = (s, u) + w(u, v) = d [ u ] + w ( u , v ) , and the algorithm sets d [ v ] = d [ u ] + w ( u , v ) =  ( s , v ) . CS 421 - Computer Science II 48

Correctnes s Dijkstra’s — P art III Theorem. Dijkstra’s algorithm terminates withd[v] = (s, v) for all v  V.CS 421 - Computer Science II 49

Correctnes s Dijkstra’s — P art III sxyProof. It suffices to show that d[v] =  ( s , v) for every v  V when v is added to S. Suppose u is the first vertex added to S for which d[u]  (s, u). Let y be the first vertex in V – S along a shortest path fro m s to u , an d le t x b e it s predecessor : S , just before adding u . CS 421 - Computer Science II 50 u

Correctnes s Dijkstra’s — P art III sxyuSSince u is the first vertex violating the claimed invariant, we have d [x] = (s, x). When x was added to S, the edge (x, y) was relaxed, which implies that d[y] = (s, y)  (s, u)  d[u]. But, d[u]  d[y] by our choice of u. Contradiction. CS 421 - Computer Science II 51

Correctnes s of BFS whil e Q  do u  DEQUEUE(Q)for each v  Adj[u] do if d [ v] = then d[v]  d[u] + 1 ENQUEUE(Q, v)Key idea:The FIFO Q in breadth-first search mimics the priority queue Q in Dijkstra.Invariant: v comes after u in Q implies thatd[v] = d[u] or d [v] = d [u ] + 1 . CS 421 - Computer Science II 52