1 / 30

Algorithms

Algorithms. Shortest Path Problems. G = (V, E) weighted directed graph w: E-> R weight function Weight of a path p = <v 0 , v 1 ,. . ., v n > Shortest path weight from u to v Shortest path from u to v: Any path from u to v with w(p) =  (u,v)  [v] predecessor of v on a path. .

Download Presentation

Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithms Shortest Path Problems

  2. G = (V, E) weighted directed graph w: E-> R weight function Weight of a path p = <v0, v1,. . ., vn> Shortest path weight from u to v Shortest path from u to v: Any path from u to v with w(p) = (u,v) [v] predecessor of v on a path

  3.            

  4. Variants • Single-source shortest paths: • find shortest paths from source vertex to every other vertex • Single-destination shortest paths: • find shortest paths to a destination from every vertex • Single-pair shortest-path • find shortest path from u to v • All pairs shortest paths

  5. Lemma 25.1 • Subpaths of shortest paths are shortest paths. Given G=(G,E) w: E -> R Let p = p = <v1, v2,. . ., vk> be a shortest path from v1 to vk For any i,j such that 1 i j k, let pij be a subpath from vi to vj.. Then pij is a shortest path from vi to vj.

  6. p 1 i j k

  7. Corollary 25.2 Let G = (V,E) w: E -> R Suppose shortest path p from a source s to vertex v can be decomposed into p’ s u v for vertex u and path p’. Then weight of the shortest path from s to v is (s,v) = (s,u) + w(u,v)

  8. Lemma 25.3 Let G = (V,E) w: E -> R Source vertex s For all edges (u,v)E (s,v) (s,u) + w(u,v)

  9. u1 u2 u3 s v u4 un

  10. Relaxation • Shortest path estimate d[v] is an attribute of each vertex which is an upper bound on the weight of the shortest path from s to v • Relaxation is the process of incrementally reducing d[v] until is is an exact weight of the shortest path from s to v

  11. INITIALIZE-SINGLE-SOURCE(G, s) 1. for each vertex v V(G) 2. do d[v] 3. [v]  nil 4. d[s]  0

  12.            

  13. Relaxing an Edge (u,v) • Question: Can we improve the shortest path to v found so far by going through u? • If yes, update d[v] and [v]

  14. RELAX(u,v,w) 1. if d[v] > d[u] + w(u,v) 2. then d[v]  d[u] + w(u,v) 3. [v]  u

  15. EXAMPLE 1 s  s  Relax       v u v u

  16. EXAMPLE 2 s  s  Relax       v u v u

  17. Dijkstra’s Algorithm • Problem: • Solve the single source shortest-path problem on a weighted, directed graph G(V,E) for the cases in which edge weights are non-negative

  18. Dijkstra’s Algorithm • Approach • maintain a set S of vertices whose final shortest path weights from the source s have been determined. • repeat • select vertex from V-S with the minimum shortest path estimate • insert u in S • relax all edges leaving u

  19. DIJKSTRA(G,w,s) 1. INITIALIZE-SINGLE-SOURCE(G,s) 2. S 3. Q  V[G] 4. while Q  5. do u  EXTRACT-MIN(Q) 6. S  S {u} 7. for each vertex v Adj[u] 8. do RELAX(u,v,w)

  20.            

  21. Analysis of Dijkstra’s Algorithm • Suppose priority Q is: • an ordered (by d) linked list • Building the Q O(V lg V) • Each EXTRACT-MIN O(V) • This is done V times O(V2) • Each edge is relaxed one time O(E) • Total time O(V2 + E) = O(V2)

  22. Analysis of Dijkstra’s Algorithm • Suppose priority Q is: • a binary heap • BUILD-HEAP O(V) • Each EXTRACT-MIN O(lg V) • This is done V times O(V lg V) • Each edge is relaxation O(lg V) • Each edge relaxed one time O(E lg V) • Total time O(Vlg V + E lg V))

  23. Properties of Relaxation • Lemma 25.4 G=(V,E) w: E -> R (u,v)  E After relaxing edge (u,v) by executing RELAX(u,v,w) we have d[v]  d[u] + w(u,v)

  24. Lemma 25.5 • Given: G=(V,E) w: E -> R source s  V Graph initialized by INITIALIZE-SINGLE-SOURCE(G,s) • then d[v] (s,v) for all v V and this invariant is maintained over all relaxation steps Once d[v] achieves a lower bound (s,v), it never changes

  25. Corollary 25.6 • Given: • G=(V,E) w: E -> R source s  V • No path connects s to given v • then • after initialization • d[v] (s,v) • and this inequality is maintained over all relaxation steps.

  26. Lemma 25.7 • Given: • G=(V,E) w: E -> R source s  V • Let s - - ->u ->v be the shortest path in G for all vertices u and v. • Suppose G initialized by INITIALIZE-SINGLE-SOURCE is followed by a sequence of relaxations including RELAX(u,v,w) • Then d[u] = (s,u) prior to call implies that d[u] = (s,u) after the call

  27. Bottom Line • Therefore, relaxation causes the shortest path estimates to descend monotonically toward the actual shortest-path weights.

  28. Shortest-Paths Tree of G(V,E) • The shortest-paths tree at S of G(V,E) is a directed subgraph G’-(V’,E’), where V’ V, E’E, such that • V’ is the set of vertices reachable from S in G • G’ forms a rooted tree with root s, and • for all v V’, the unique simple path from s to v in G’ is a shortest path from s to v in G

  29. Goal • We want to show that successive relaxations will yield a shortest-path tree

  30. Lemma 25.8 • Given: • G=(V,E) w: E -> R source s  V • Assume that G contains no negative-weight cycles reachable from s. • Then after the graph is initialized with INITIALIZE-SINGLE-SOURCE • • the predecessor subgraph G forms a rooted tree with root s, and • • any sequence of relaxation steps on edges in G maintains this property as an invariant.

More Related