finding shortest paths shortest path problem shortest
play

Finding Shortest Paths Shortest Path Problem Shortest Path Problem - PowerPoint PPT Presentation

Finding Shortest Paths Shortest Path Problem Shortest Path Problem Given a graph G = ( V , E ) and an edge weight function : V R . Length of a Path The length or weight ( P ) of a path P = { v 1 , v 2 , . . . , v l } with at least two


  1. Finding Shortest Paths

  2. Shortest Path Problem

  3. Shortest Path Problem Given a graph G = ( V , E ) and an edge weight function ω : V → R . Length of a Path The length or weight ω ( P ) of a path P = { v 1 , v 2 , . . . , v l } with at least two vertices is l − 1 � ω ( P ) = ω ( v i v i + 1 ) i = 1 If | P | = 1 , ω ( P ) = 0 . Shortest Path Problem For two vertices u and v , the shortest path from u to v is the path P for which ω ( P ) is minimal. The distance d ( u , v ) from u to v is the length of a shortest path from u to v . 3 / 24

  4. Shortest Path Problem Variants ◮ Single Pair Shortest Path (SPSP) Find a shortest path from a vertex u to some vertex v . ◮ Single Source Shortest Path (SSSP) Find shortest paths from a source vertex v to all other vertices in the graph. ◮ All Pairs Shortest Path (APSP) Find shortest paths fall vertex pairs u and v . There is no algorithm for SPSP which is in general better than a SSSP algorithm. 4 / 24

  5. Shortest Path Properties Theorem Each subpath of a shortest path is a shortest path. (Optimal Substructure Property) Theorem For all vertices u , v , and w , d ( u , v ) ≤ d ( u , w ) + d ( w , v ) . (Triangle Inequality) 5 / 24

  6. Negative Weight Edges and Cycles Negative Cycles ◮ Natural in some application ◮ Makes finding a shortest path harder Theorem If there is a path from u to v containing a vertex w and w is in a cycle C with ω ( C ) < 0 , then there is no shortest path from u to v . Avoiding Cycles ◮ Only permit simple cycles, i. e., no vertex twice ◮ Follows if graph has no negative cycles ◮ With negative cycles, shortest path problem equal to longest path problem ◮ Optimal Substructure Property no longer given 6 / 24

  7. General Approach

  8. General Approach Store for each vertex v ◮ dist s ( v ) , length of currently best known path P from start vertex s to v ◮ par s ( v ) , parent of v in P Relaxation ◮ Updates best known distance. 1 Procedure Relax( u , v ) If dist s ( v ) > dist s ( u ) + ω ( uv ) Then 2 Set par ( v ) := u and dist s ( v ) := dist s ( u ) + ω ( uv ) . 3 8 / 24

  9. General Approach Initialization ◮ Set par ( v ) := null and dist s ( v ) := ∞ for each vertex v . ◮ Set dist s ( s ) := 0 for start vertex s . Iteration ◮ Pick vertex pair u , v . ◮ Call Relax( u , v ) ◮ Repeat Open questions ◮ How do we pick u and v ? ◮ When do we stop the iteration? 9 / 24

  10. Single Source Shortest Path

  11. Shortest Path for DAGs Directed Acyclic Graphs ◮ No (negative) cycles ◮ Topological order Algorithm Idea ◮ Find a topological order � v 1 , v 2 , . . . , v n � . ◮ For i := 1 to n , relax all outgoing edges of v i . Properties ◮ Invariant: For all v j with j ≤ i , dist ( v j ) is optimal. ◮ Runtime: linear ◮ Works with negative edges, i. e., can be used to compute longest path. 11 / 24

  12. Bellman-Ford Observation ◮ A shortest path has at most | V | − 1 edges. ◮ If we know all shortest path with k edges, we can compute all shortest with k + 1 edges by relaxing all edges once. 1 For Each v ∈ V Set dist ( v ) := ∞ and par ( v ) = null . 2 3 Set dist ( s ) := 0 . 4 For i := 1 To | V | − 1 For Each ( u , v ) ∈ E 5 Relax( u , v ) 6 12 / 24

  13. Bellman-Ford Properties ◮ Runtime: O ( | V || E | ) ◮ Works with negative weight edges ◮ Can detect negative cycles Detecting negative cycles ◮ Negative cycle → There is always an edge ( u , v ) for which Relax( u , v ) updates dist ( v ) . ◮ If Relax( u , v ) still updates dist ( v ) for i ≥ | V | , then ( u , v ) is part of a negative cycle. 13 / 24

  14. Dijkstra’s Algorithm Idea ◮ Let S be set of vertices where shortest path is known. ◮ Relax all outgoing edges ( u , v ) , i. e., u ∈ S and v / ∈ S . ◮ If dist ( v ) is minimal for all vertices not in S , then dist ( v ) is optimal. ◮ Add v to S and repeat. 1 Initialize( G , s ) 2 Create priority Q and add all vertices in V . 3 While Q is not empty Remove v with minimal dist ( v ) from Q . 4 For Each ( v , w ) ∈ E 5 Relax( v , w ) 6 14 / 24

  15. Dijkstra’s Algorithm Properties ◮ Runtime: O ( | E | log | V | ) with binary heaps and O ( | V | log | V | + | E | ) with Fibonacci-Heaps ◮ Invariant: For all vertices in S , dist ( s ) is optimal. ◮ Requirement: No negative edges. The algorithm assumes that distances are always increasing. What happens if there are negative edges? 1 -1 1 -3 1 -3 0 2 2 0 2 1 Dijkstra Bellman-Ford 15 / 24

  16. All Pairs Shortest Path

  17. Floyd-Warshall Idea ◮ Assume we know the shortest path from v i to v j using only the (additional) vertices � v 1 , v 2 , . . . , v k − 1 � . Let d ( k − 1 ) be this distance. ij ◮ Then, we can add v k in the next iteration and get � � d ( k ) d ( k − 1 ) , d ( k − 1 ) + d ( k − 1 ) = min ij ij ik kj ◮ If k = | V | , then d ( k ) = d ( v i , v j ) for all i and j . ij ◮ Initial values  0 if i = j   d ( 0 ) = ω ( v i v j ) if v i v j ∈ E ij  ∞ else  17 / 24

  18. Floyd-Warshall 1 For Each pair i , j with 1 ≤ i , j ≤ | V | Set d ( 0 ) := 0 if i = j , ω ( v i v j ) if v i v j ∈ E , and ∞ otherwise. 2 ij 3 For k := 1 To | V | For Each pair i , j with 1 ≤ i , j ≤ | V | 4 � � Set d ( k ) d ( k − 1 ) , d ( k − 1 ) + d ( k − 1 ) = min . 5 ij ij ik kj To represent d ij , use two | V | × | V | arrays. Detecting negative cycles ◮ Check if, for some i and some k , d ( k ) < 0 . ii Runtime: O ( | V | 3 ) 18 / 24

  19. Dijkstra vs. Floyd-Warshall Runtime for APSP ◮ Dijkstra: O ( | V | 2 log | V | + | V || E | ) ◮ Floyd-Warshall: O ( | V | 3 ) Observation ◮ Since | E | ≤ | V | 2 , Dijkstra would be better, especially for sparse graphs. ◮ Problem: negative weight edges. Question ◮ Is there a way to avoid these negative edges? 19 / 24

  20. Johnson’s Algorithm Algorithm ◮ Add a new vertex q and add, for each v ∈ V , the directed edge qv with weight 0 . ◮ Run Bellman-Ford with start vertex q . Let h ( v ) be the length of a shortest path from q to v . ◮ For each edge uv , set ˜ ω ( uv ) := ω ( uv ) + h ( u ) − h ( v ) . ◮ Remove q and run Dijkstra’s algorithm on each vertex using ˜ ω as edge weights. Properties ◮ Runtime O ( | V | 2 log | V | + | V || E | ) ◮ Works with negative weight edges and can detect negative cycles. 20 / 24

  21. A* and Branch and Bound

  22. Single Pair Shortest Path Single Pair Shortest Path ◮ Weighted graph ◮ Find shortest path from s to t . Dijkstra ◮ Explores all in distance d ( s , t ) before terminating. (Can be improved to d ( s , t ) / 2 with bidirectional search) ◮ Next vertex is selected by distance from s . Problem ◮ Some vertices go in the wrong direction. 22 / 24

  23. A* Idea ◮ For a vertex v , make an estimation h t ( v ) of d ( v , t ) ◮ Important: h t ( v ) ≤ d ( v , t ) v dist s ( v ) h t ( v ) s t Algorithm ◮ Basically Dijkstra ◮ For next iteration, pick vertex v for which dist s ( v ) + h t ( v ) is minimal. 23 / 24

  24. Generalised A* Idea ◮ Take decision tree. ◮ Find a shortest path from root to leaf. ◮ Important: Do not construct whole tree. Only construct explored parts. Branch and Bound ◮ Start at root. ◮ Branch: Determine the children of a node. ◮ Bound: Compute for every node a lower bound for the cost of the solutions in this subtree. ◮ Select next node where estimated lower bound is minimal. Note ◮ Finding the optimal lower bound (i. e., h t ( v ) = d ( v , t ) ) is as hard as solving the original problem. 24 / 24

Recommend


More recommend