dynamic programming shortest paths and dfa to reg exps
play

Dynamic Programming: Shortest Paths and DFA to Reg Exps Lecture 17 - PowerPoint PPT Presentation

CS 374: Algorithms & Models of Computation, Fall 2015 Dynamic Programming: Shortest Paths and DFA to Reg Exps Lecture 17 October 22, 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 54 Part I Shortest Paths with Negative Length


  1. CS 374: Algorithms & Models of Computation, Fall 2015 Dynamic Programming: Shortest Paths and DFA to Reg Exps Lecture 17 October 22, 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 54

  2. Part I Shortest Paths with Negative Length Edges Chandra & Manoj (UIUC) CS374 2 Fall 2015 2 / 54

  3. Single-Source Shortest Paths with Negative Edge Lengths Single-Source Shortest Path Problems Input : A directed graph 10 6 G = (V , E) with arbitrary 9 2 3 18 s (including negative) edge 6 -16 lengths. For edge e = (u , v) , 6 4 30 11 15 5 ℓ (e) = ℓ (u , v) is its length. 19 6 -8 20 16 Given nodes s , t find 44 1 t 7 shortest path from s to t . Given node s find shortest 2 path from s to all other nodes. Chandra & Manoj (UIUC) CS374 3 Fall 2015 3 / 54

  4. Single-Source Shortest Paths with Negative Edge Lengths Single-Source Shortest Path Problems Input : A directed graph 10 6 G = (V , E) with arbitrary 9 2 3 18 s (including negative) edge 6 -16 lengths. For edge e = (u , v) , 6 4 30 11 15 19 5 ℓ (e) = ℓ (u , v) is its length. -8 20 6 16 Given nodes s , t find 44 1 t 7 shortest path from s to t . Given node s find shortest 2 path from s to all other nodes. Chandra & Manoj (UIUC) CS374 3 Fall 2015 3 / 54

  5. What are the distances computed by Dijkstra’s algorithm? The distance as computed by Dijkstra algorithm start- 5 ing from s : x − 5 s (A) s = 0 , x = 5 , y = 1 , z = 0 . 1 w z 1 (B) s = 0 , x = 1 , y = 2 , 1 z = 5 . y (C) s = 0 , x = 5 , y = 1 , z = 2 . (D) IDK. Chandra & Manoj (UIUC) CS374 4 Fall 2015 4 / 54

  6. Dijkstra’s Algorithm and Negative Lengths With negative length edges, Dijkstra’s algorithm can fail 5 x − 5 s 1 z w 1 1 y Chandra & Manoj (UIUC) CS374 5 Fall 2015 5 / 54

  7. Dijkstra’s Algorithm and Negative Lengths With negative length edges, Dijkstra’s algorithm can fail 5 x − 5 s 5 x − 5 s 1 z w 1 w z 1 1 1 1 y 1 y Chandra & Manoj (UIUC) CS374 5 Fall 2015 5 / 54

  8. Dijkstra’s Algorithm and Negative Lengths With negative length edges, Dijkstra’s algorithm can fail 5 5 x − 5 x − 5 s s 1 1 z w z w 1 1 1 1 2 y y 1 Chandra & Manoj (UIUC) CS374 5 Fall 2015 5 / 54

  9. Dijkstra’s Algorithm and Negative Lengths With negative length edges, Dijkstra’s algorithm can fail 5 5 x − 5 x − 5 s s 1 1 z w z w 1 1 1 1 2 3 y y 1 Chandra & Manoj (UIUC) CS374 5 Fall 2015 5 / 54

  10. Dijkstra’s Algorithm and Negative Lengths With negative length edges, Dijkstra’s algorithm can fail 5 5 5 x − 5 x − 5 s s 1 1 z w z w 1 1 1 1 2 3 y y 1 Chandra & Manoj (UIUC) CS374 5 Fall 2015 5 / 54

  11. Dijkstra’s Algorithm and Negative Lengths With negative length edges, Dijkstra’s algorithm can fail 5 5 5 x − 5 x − 5 s s 0 1 1 z w z w 1 1 1 1 2 3 y y 1 Chandra & Manoj (UIUC) CS374 5 Fall 2015 5 / 54

  12. Dijkstra’s Algorithm and Negative Lengths With negative length edges, Dijkstra’s algorithm can fail 5 5 5 x − 5 x − 5 s s 1 0 1 1 z w z w 1 1 1 1 2 3 y y 1 Shortest path Chandra & Manoj (UIUC) CS374 5 Fall 2015 5 / 54

  13. Dijkstra’s Algorithm and Negative Lengths With negative length edges, Dijkstra’s algorithm can fail 5 5 5 x − 5 x − 5 s s 1 0 1 1 z w z w 1 1 1 1 2 3 y y 1 Shortest path False assumption: Dijkstra’s algorithm is based on the assumption that if s = v 0 → v 1 → v 2 . . . → v k is a shortest path from s to v k then dist(s , v i ) ≤ dist(s , v i+1 ) for 0 ≤ i < k . Holds true only for non-negative edge lengths. Chandra & Manoj (UIUC) CS374 5 Fall 2015 5 / 54

  14. Negative Length Cycles Definition A cycle C is a negative length cycle if the sum of the edge lengths of C is negative. 10 9 b c 18 3 s 6 -16 f d 30 15 e 11 19 -8 20 6 16 g t 44 Chandra & Manoj (UIUC) CS374 6 Fall 2015 6 / 54

  15. Negative Length Cycles Definition A cycle C is a negative length cycle if the sum of the edge lengths of C is negative. 10 9 b c 18 3 s 6 -16 f d 30 15 e 11 19 -8 20 6 16 g t 44 Chandra & Manoj (UIUC) CS374 6 Fall 2015 6 / 54

  16. Shortest Paths and Negative Cycles Given G = (V , E) with edge lengths and s , t . Suppose G has a negative length cycle C , and 1 s can reach C and C can reach t . 2 Question: What is the shortest distance from s to t ? Possible answers: Define shortest distance to be: undefined, that is −∞ , OR 1 the length of a shortest simple path from s to t . 2 Chandra & Manoj (UIUC) CS374 7 Fall 2015 7 / 54

  17. Shortest Paths and Negative Cycles Given G = (V , E) with edge lengths and s , t . Suppose G has a negative length cycle C , and 1 s can reach C and C can reach t . 2 Question: What is the shortest distance from s to t ? Possible answers: Define shortest distance to be: undefined, that is −∞ , OR 1 the length of a shortest simple path from s to t . 2 Lemma If there is an efficient algorithm to find a shortest simple s → t path in a graph with negative edge lengths, then there is an efficient algorithm to find the longest simple s → t path in a graph with positive edge lengths. Finding the s → t longest path is difficult. NP-Hard ! Chandra & Manoj (UIUC) CS374 7 Fall 2015 7 / 54

  18. Alterantively: Finding Shortest Walks Given a graph G = (V , E) : A path is a sequence of distinct vertices v 1 , v 2 , . . . , v k such that 1 (v i , v i+1 ) ∈ E for 1 ≤ i ≤ k − 1 . A walk is a sequence of vertices v 1 , v 2 , . . . , v k such that 2 (v i , v i+1 ) ∈ E for 1 ≤ i ≤ k − 1 . Vertices are allowed to repeat. Define dist(u , v) to be the length of a shortest walk from u to v . If there is a walk from u to v that contains negative length cycle 1 then dist(u , v) = −∞ Else there is a path with at most n − 1 edges whose length is 2 equal to the length of a shortest walk and dist(u , v) is finite Helpful to think about walks Chandra & Manoj (UIUC) CS374 8 Fall 2015 8 / 54

  19. Shortest Paths with Negative Edge Lengths Problems Algorithmic Problems Input : A directed graph G = (V , E) with edge lengths (could be negative). For edge e = (u , v) , ℓ (e) = ℓ (u , v) is its length. Questions : Given nodes s , t , either find a negative length cycle C that s can 1 reach or find a shortest path from s to t . Given node s , either find a negative length cycle C that s can 2 reach or find shortest path distances from s to all reachable nodes. Check if G has a negative length cycle or not. 3 Chandra & Manoj (UIUC) CS374 9 Fall 2015 9 / 54

  20. Shortest Paths with Negative Edge Lengths In Undirected Graphs Note : With negative lengths, shortest path problems and negative cycle detection in undirected graphs cannot be reduced to directed graphs by bi-directing each undirected edge. Why? Problem can be solved efficiently in undirected graphs but algorithms are different and more involved than those for directed graphs. Beyond the scope of this class. If interested, ask instructor for references. Chandra & Manoj (UIUC) CS374 10 Fall 2015 10 / 54

  21. Why Negative Lengths? Several Applications Shortest path problems useful in modeling many situations — in 1 some negative lenths are natural Negative length cycle can be used to find arbitrage opportunities 2 in currency trading Important sub-routine in algorithms for more general problem: 3 minimum-cost flow Chandra & Manoj (UIUC) CS374 11 Fall 2015 11 / 54

  22. Negative cycles Application to Currency Trading Currency Trading Input : n currencies and for each ordered pair (a , b) the exchange rate for converting one unit of a into one unit of b . Questions : Is there an arbitrage opportunity? 1 Given currencies s , t what is the best way to convert s to t 2 (perhaps via other intermediate currencies)? Concrete example: Thus, if exchanging 1 $ → 1 Chinese Yuan = 0 . 1116 Euro 1 Yuan → Euro → $ , we get: 1 Euro = 1 . 3617 US dollar 2 0 . 1116 ∗ 1 . 3617 ∗ 7 . 1 = 1 US Dollar = 7 . 1 Chinese Yuan. 3 1 . 07896$ . Chandra & Manoj (UIUC) CS374 12 Fall 2015 12 / 54

  23. Reducing Currency Trading to Shortest Paths Observation: If we convert currency i to j via intermediate currencies k 1 , k 2 , . . . , k h then one unit of i yields exch(i , k 1 ) × exch(k 1 , k 2 ) . . . × exch(k h , j) units of j . Chandra & Manoj (UIUC) CS374 13 Fall 2015 13 / 54

  24. Reducing Currency Trading to Shortest Paths Observation: If we convert currency i to j via intermediate currencies k 1 , k 2 , . . . , k h then one unit of i yields exch(i , k 1 ) × exch(k 1 , k 2 ) . . . × exch(k h , j) units of j . Create currency trading directed graph G = (V , E) : For each currency i there is a node v i ∈ V 1 E = V × V : an edge for each pair of currencies 2 edge length ℓ (v i , v j ) = 3 Chandra & Manoj (UIUC) CS374 13 Fall 2015 13 / 54

  25. Reducing Currency Trading to Shortest Paths Observation: If we convert currency i to j via intermediate currencies k 1 , k 2 , . . . , k h then one unit of i yields exch(i , k 1 ) × exch(k 1 , k 2 ) . . . × exch(k h , j) units of j . Create currency trading directed graph G = (V , E) : For each currency i there is a node v i ∈ V 1 E = V × V : an edge for each pair of currencies 2 edge length ℓ (v i , v j ) = − log(exch(i , j)) can be negative 3 Chandra & Manoj (UIUC) CS374 13 Fall 2015 13 / 54

Recommend


More recommend