algorithms 2il15 lecture 5 single source shortest paths
play

Algorithms (2IL15) Lecture 5 SINGLE-SOURCE SHORTEST PATHS 2 1 s - PowerPoint PPT Presentation

TU/e Algorithms (2IL15) Lecture 5 Algorithms (2IL15) Lecture 5 SINGLE-SOURCE SHORTEST PATHS 2 1 s 2 1 2 4 2.3 1 TU/e Algorithms (2IL15) Lecture 5 Part II of the course: optimization problems on graphs 2 1


  1. TU/e Algorithms (2IL15) – Lecture 5 Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS − 2 1 s 2 1 2 4 2.3 1

  2. TU/e Algorithms (2IL15) – Lecture 5 Part II of the course: optimization problems on graphs − 2 1 single-source shortest paths 2 1 Find shortest path from source vertex 2 to all other vertices 2.3 4 all-pairs shortest paths Find shortest paths between all pairs of vertices 4 maximum flow 3 1 5 Find maximum flow from source vertex 3 2 8 to target vertex 3 maximum bipartite matching Find maximum number of disjoint pairs of vertices with edge between them 2

  3. TU/e Algorithms (2IL15) – Lecture 5 Graph terminology graph G = ( V , E ) weighted, directed graph v 4 − 2  V = set of vertices (or: nodes) v 3 1 v 1 1 2  v 5 E = set of edges (or: arcs) 2 v 6 4  2.3 (edge-)weighted graph: v 2 every edge ( u,v ) has a weight w ( u , v ) v 7  directed graph vs. undirected graph  paths and cycles 3

  4. TU/e Algorithms (2IL15) – Lecture 5 Representation of graphs, I weighted, directed graph 1 2 3 4 5 6 7 1 0 2 v 4 − 2 v 3 2 0 4 1 v 1 3 0 -2 1 2 v 5 2 4 0 1 v 6 0 5 4 2.3 1 2 0 2.3 6 v 2 0 7 v 7 adjacency-matrix representation M [ i,j ] = 0 if i = j w(v i ,v j ) if i ≠ j and edge (v i ,v j ) exists; w(v i ,v j ) is weight of (v i ,v j ) 8 if edge (v i ,v j ) does not exist 4

  5. TU/e Algorithms (2IL15) – Lecture 5 Representation of graphs, II weighted, directed graph 1 6 2 v 4 − 2 2 6 4 v 3 3 4 -2 1 v 1 2 4 1 5 1 v 5 2 5 v 6 4 6 7 2.3 3 1 5 2 2.3 7 v 2 v 7 adjacency-list representation there is an edge from v 1 to v 6 with weight 2 today we assume adjacency-list representation 5

  6. TU/e Algorithms (2IL15) – Lecture 5 Shortest paths weighted, directed graph G = ( V , E ) weighted, directed graph v 4 − 2  weight (or: length) of a path v 3 1 = sum of edge weights v 1 2 1 v 5 2 v 6  δ (u,v) 4 2.3 = distance from u to v v 2 = min weight of any path from u to v v 7 weight = 4  shortest path from u to v = any path from u to v of weight δ (u,v) δ ( v 1 , v 5 ) = 2 weight = 2 Is δ (u,v) always well defined? No, not if there are negative-weight cycles. If δ (u,v) well defined, then there is a shortest path with at most | V |−1 edges 6

  7. TU/e Algorithms (2IL15) – Lecture 5 Shortest-paths problem single-pair find shortest path for single pair u,v of vertices can be solved using single-source problem with source = u and no better solutions are known single-source find shortest path from given source to all other vertices this week single-destination find shortest paths to given destination from all other vertices same as single-source for undirected graphs, for directed graphs just reverse edge directions all-pairs find shortest paths between all pairs of vertices can be solved by running single-source for each vertex, but better solutions are known: next week 7

  8. TU/e Algorithms (2IL15) – Lecture 5 The Single-Source Shortest-Paths Problem Input:  graph G = (V,E)  source vertex s V v 4 − 2 v 3 s 1 v 1 2 1 output v 5 2  for each v V : distance δ ( s,v ) v 6 store distance in attribute d(v) 4 2.3 if v not reachable: δ ( s,v ) = 8 v 2 v 7  8 a shortest-path tree that encodes all d(v 2 ) = d(v 7 ) = 4.3 shortest paths from s: π (v 2 ) = NIL π (v 7 ) = v 6 for v ≠ s the predecessor π (v) on a shortest path from s to v book (3rd ed) uses v.d and v. π instead of d(v) and π (v) NB if negative-weight cycle is reachable from s, then we only need to report this fact 8

  9. TU/e Algorithms (2IL15) – Lecture 5 The SSSP problem on unweighted graphs 2 Breadth-First Search (BFS) 2 running time: 1 Θ ( | V | + | E | ) 0 s 1 2 1 2 1 1 2 round i : for each vertex u at distance i-1 from s ( that is, with d(v)=i-1 ) do for all neighbors v of u do if v unvisited then set d(v)=i implementation: maintain queue Q initial part of Q: nodes with d(v)=i-1 that have not been handled yet rest of Q: nodes with d(v)=i with a neighbor that has been handled 9

  10. TU/e Algorithms (2IL15) – Lecture 5 The SSSP problem on weighted graphs Dijkstra’s algorithm  works only for non-negative edge weights  running time Θ ( |V| log |V| + |E| ) Bellman-Ford algorithm  can handle negative edge weights, works even for negative-weight cycles  running time Θ ( |V| ∙ |E| ) Special case: directed acyclic graph (DAG)  can handle negative edge weights running time Θ ( |V| + |E| )  10

  11. TU/e Algorithms (2IL15) – Lecture 5 Framework for solving SSSP problems For each vertex v , maintain value d ( v ) that is current “estimate” of δ ( s , v ) and pointer π ( v ) to predecessor in current shortest-path tree  Invariant: d ( v ) ≥ δ ( s,v ) 8 if d ( v ) < : there exists s -to- v path of weight d ( v ) in which π ( v ) is predecessor of v  at end of algorithm: d ( v ) = δ ( s,v ) and so pointers π ( v ) encode shortest-path tree Initialize-Single-Source ( G,s ) 1. for each vertex v ; π ( v ) ← NIL do d ( v ) ← 8 d ( s ) ← 0 2. 11

  12. TU/e Algorithms (2IL15) – Lecture 5 Framework for solving SSSP problems For each vertex v , maintain value d ( v ) that is current “estimate” of δ ( s , v ) and pointer π ( v ) to predecessor in current shortest-path tree  Invariant: d ( v ) ≥ δ ( s,v ) 8 if d ( v ) < : there exists s -to- v path of weight d ( v ) in which π ( v ) is predecessor of v  at end of algorithm: d ( v ) = δ ( s,v ) and so pointers π ( v ) encode shortest-path tree Estimates d ( v ) are updated using relaxation: Relax( u,v ) // ( u,v ) is an edge 5 1. if d ( u ) + w ( u,v ) < d ( v ) u v then d ( v ) ← d( u ) + w ( u,v ) 2. d ( u )=10 d ( v )=17 π ( v ) ← u 15 8 (if d ( u ) = then nothing happens) 12

  13. TU/e Algorithms (2IL15) – Lecture 5 All algorithms we discuss today use this framework  algorithms differ in how they determine which edge to relax next  NB: a single edge can be relaxed multiple times Lemma (path-relaxation property): Let s,v 1 ,v 2 ,…,v k be a shortest path. Suppose we relax the edges of the path in order, with possibly other relax-operations in between: …, Relax( s,v 1 ), …, Relax( v 1 ,v 2 ), … , Relax( v 2 ,v 3 ), …. , …, …, Relax( v k-1 ,v k ), … Then after these relax-operations we have d ( v k ) = δ ( s,v k ). Proof: Induction on k . Recall that we always have d ( v k ) ≥ δ ( s,v k ).  base case, k=1 : after Relax( s,v 1 ) we have d ( v 1 ) ≤ d ( s ) + w ( s , v 1 ) = δ ( s,v 1 )  k >1: just before Relax( v k-1 ,v k ) we have d ( v k-1 ) = δ ( s,v k-1 ) by induction. after Relax( v k-1 ,v k ) we thus have d ( v k ) ≤ d ( v k-1 ) + w ( v k-1 , v k ) = δ ( s,v k-1 ) + w ( v k-1 , v k ) = δ ( s,v k ) 13

  14. TU/e Algorithms (2IL15) – Lecture 5 Dijkstra’s algorithm Input:  weighted graph G(V,E) with non-negative edge weights  source node s Required output:  for each vertex v: − value d ( v ) = δ ( s,v ) − pointer π ( v ) to parent in shortest-path tree 14

  15. TU/e Algorithms (2IL15) – Lecture 5 Let’s see if we can adapt BFS 2 2 1 0 s 1 2 1 2 1 1 2 round i : for each vertex u at distance i-1 from s ( that is, with d(v)=i-1 ) do for all neighbors v of u do if v unvisited then set d(v)=i this is a Relax-operation when edges have weights, distances can “jump” and we cannot handle vertices in rounds as in BFS … … but we can still try to handle vertices in order of increasing distance to s 15

  16. TU/e Algorithms (2IL15) – Lecture 5 Dijkstra’s algorithm  handle vertices in increasing distance to s  handling a vertex = relax all outgoing edges 3 4 4 8 8 1 relax relax relax 4 3 1 1 relax relax s s d(s) = 0 2 4 8 8 6 2 5 Implementation: maintain priority queue Q  Q contains vertices that have not been handled yet; key = d ( v ) 16

  17. TU/e Algorithms (2IL15) – Lecture 5 Dijkstra ( G,s ) // G = ( V,E ) is a weighted directed graph with no negative edge weights 1. Initialize-Single-Source ( G,s ) S ← empty set // S = set of vertices that have been handled 2. 3. Put all vertices v in V in min-priority queue Q, with d(v) as key 4. while Q not empty do u ← Extract -Min( Q ) 5. S ← S U { u } 6. 7. for each outgoing edge ( u,v ) 8. do Relax( u,v ) if d(v) changes, we have to update Q: Relax( u,v ) // ( u,v ) is an edge 1. if d ( u ) + w ( u,v ) < d ( v ) then d ( v ) ← d( u ) + w ( u,v ) 2. π ( v ) ← u 3. 4. Decrease-Key ( Q , v , d ( v ) ) change key of v to (new, lower value of) d ( v ) 17

  18. TU/e Algorithms (2IL15) – Lecture 5 Dijkstra ( G,s ) // G = ( V,E ) is a weighted directed graph with no negative edge weights 1. Initialize-Single-Source ( G,s ) S ← empty set // S = set of vertices that have been handled 2. 3. Put all vertices v in V in min-priority queue Q, with d(v) as key 4. while Q not empty do u ← Extract -Min( Q ) 5. S ← S U { u } 6. 7. for each outgoing edge ( u,v ) 8. do Relax( u,v ) Invariant: at start of each iteration of while-loop: d ( v ) = δ ( s,v ) for all v in S . Intialization: S ← empty set, so Invariant holds before first iteration 18

Recommend


More recommend