cse 101
play

CSE 101 Algorithm Design and Analysis Sanjoy Dasgupta Russell - PowerPoint PPT Presentation

CSE 101 Algorithm Design and Analysis Sanjoy Dasgupta Russell Impagliazzo Ragesh Jaiswal (Thanks Miles Jones) Lecture 29: Dynamic Programming (Shortest Paths) SHORTEST PATHS Given a graph with edge weights, find paths that minimize total


  1. CSE 101 Algorithm Design and Analysis Sanjoy Dasgupta Russell Impagliazzo Ragesh Jaiswal (Thanks Miles Jones) Lecture 29: Dynamic Programming (Shortest Paths)

  2. SHORTEST PATHS Given a graph with edge weights, find paths that minimize total weight of edges used Dijkstra’s shortest path algorithm assumed non-negative edge weights Another easy case: DAG

  3. WHAT IF THERE ARE CYCLES? What if the graph has both negative weights and cycles? If the graph has negative cycles, some distances are negative infinity. Otherwise, 𝐸𝑗𝑑𝑒 𝐷 ≑ !,# $% &' ()*(} {𝐸𝑗𝑑𝑒 𝑀 + π‘₯ min 𝑀, 𝐷 } ! But this doesn’t give a consistent recursion, because the recursion graph is not a DAG, so recursion loops around cycles…… How can we make recursion acyclic when the graph is cyclic?

  4. LESSONS Generalizing problems to allow recursion and hence DP Keeping recursions simple Trying multiple recursive algorithms based on different types of case analysis leading to different DP algorithms Using reductions (again)

  5. WHY WE MIGHT WANT TO DO THIS: ARBITRAGE

  6. SOME POSSIBILITIES What vertex do we visit first (after 𝑑 )? What vertex do we visit last (before 𝑒 )? What is the middle vertex along the path? Do we ever visit vertex 𝑀 ?

  7. FIRST VERTEX Attempt 1: BTSP( 𝑑, 𝑒, 𝐻 ) β–ͺ If 𝑑 = 𝑒 return 0 β–ͺ 𝑁 := ∞ β–ͺ For each 𝑀 ∈ 𝑂(𝑑) do: β–ͺ 𝑁 := min( 𝑁 , ( π‘₯(𝑑, 𝑀) + BTSP( 𝑀, 𝑒, 𝐻 ))) β–ͺ return 𝑁

  8. PROBLEM: LOOPING Cycles in graph = cycles in recursive calls

  9. FIRST VERTEX Attempt 2: BTSP( 𝑑, 𝑒, 𝐻 ) β–ͺ If 𝑑 = 𝑒 return 0 β–ͺ 𝑁 := ∞ β–ͺ For each 𝑀 ∈ 𝑂(𝑑) do: β–ͺ 𝑁 := min( 𝑁 , ( π‘₯(𝑑, 𝑀) + BTSP( 𝑀, 𝑒, 𝐻 βˆ’ {𝑑} ))) β–ͺ return 𝑁

  10. PROBLEM Changing G in recursive calls: exponentially many sub-graphs. Usable for longest simple path = TSP = NP-complete, so approach seems inherently exponential.

  11. PREVENTING INFINITE RECURSION Let’s put a budget on how deep we’ll recurse, π‘ˆ That corresponds to looking at which paths? How large should π‘ˆ be?

  12. PREVENTING INFINITE RECURSION Let’s put a budget on how deep we’ll recurse, π‘ˆ That corresponds to looking at paths with at most T edges How large should π‘ˆ be: π‘ˆ = π‘œ : search all simple paths. Not all paths we search are simple, but that’s OK

  13. MINIMUM LENGTH NEGATIVE CYCLE If there is a negative cycle, we can keep going around it. But we can break up any cycle into a set of simple cycles.

  14. MINIMUM LENGTH NEGATIVE CYCLE If there is a negative cycle, we can keep going around it. But we can break up any cycle into a set of simple cycles. If the overall cycle is negative, at least one simple cycle is negative. So if there is any negative cycle, there is one of size at most π‘œ

  15. PREVENTING INFINITE RECURSION Let’s put a budget on how deep we’ll recurse, π‘ˆ That corresponds to looking at paths of length at most π‘ˆ If there’s no negative cycle, then the shortest paths are simple, don’t repeat vertices. So the shortest path between any two vertices is of length at most |π‘Š| = π‘œ

  16. FIRST VERTEX

  17. BELLMAN-FORD SUBPROBLEMS BF[ 𝑑, 𝑒, π‘ˆ ]: shortest weight of a path from 𝑑 to 𝑒 with at most π‘ˆ edges Note: G is also an input to recursion, but G doesn’t change. Only need changing parts as parameters in matrix

  18. BASE CASES: Base case : π‘ˆ = ? BF[ 𝑑, 𝑒 ,?] = 0 if ?? ?? otherwise

  19. BASE CASES: Base case : π‘ˆ = 0 BF[ 𝑑, 𝑒 , 0] = 0 if 𝑑 = 𝑒 ∞ , otherwise

  20. RECURSIVE PART: Check all first steps along path BF[ 𝑑, 𝑒, π‘ˆ ] := min (BF[ 𝑑, 𝑒, π‘ˆ βˆ’ 1 ], min 𝐢𝐺 𝑣, 𝑒, π‘ˆ βˆ’ 1 + π‘₯ 𝑑, 𝑣 ) -∈/ %

  21. BELLMAN-FORD METHOD

  22. BELLMAN-FORD ALGORITHM

  23. BELLMAN-FORD ALGORITHM TIME ANALYSIS

  24. SAVING MEMORY We never use BF( 𝑣, 𝑀, π‘ˆ ) beyond step π‘ˆ + 1 . It’s OK if we’ve made BF( 𝑣, 𝑀, π‘ˆ ) smaller as long as we never make it less than the shortest path length. So we can just use one matrix BF( 𝑣, 𝑀 ) rather than π‘œ different matrices, and make both read and write steps from the same Matrix.

  25. ALTERNATIVE RECURSION Bellman-Ford’s recursion based on " What is the first vertex on the path from 𝑣 to 𝑀 ?" What other question could we ask about the path? What is the last vertex on the path? Symmetric

  26. ALTERNATIVE RECURSION Bellman-Ford’s recursion based on " What is the first vertex on the path from 𝑣 to 𝑀 ? " What other question could we ask about the path? What is the last vertex ? (BF in reverse) What is the middle vertex of the path? (Min plus matrix multiply method) Is 𝑀 ' ever used in the path? (Floyd-Warshall algorithm)

  27. MIN-PLUS METHOD

  28. MIN PLUS ALGORITHM

  29. MIN PLUS ALGORITHM

  30. WHICH IS BETTER?

  31. FLOYD WARSHALL Is 𝑀 used at all? Need to eliminate vertices in order, so that remaining vertices are consecutive. Subtlety one: need to distinguish between endpoints and set of intermediate points Subtlety two: implicitly assumes no negative cycles, so that shortest paths can be simple.

  32. FLOYD WARSHALL GENERALIZATION Assuming the shortest paths are simple, find the shortest path in G from 𝑑 to 𝑒 using intermediate nodes 𝑉 = {𝑣 ! , … , 𝑣 " } , where 𝑑 and 𝑒 may or may not be in 𝑉 . BTFW( 𝑑, 𝑒, 𝐻, 𝑉 ) IF 𝑉 is empty, and 𝑑 = 𝑒 return 0 IF 𝑉 is empty and ( 𝑑, 𝑒 ) is an edge return w( 𝑑, 𝑒 ) IF 𝑉 is empty, return infinity Usecase:= BTFW( 𝑑, 𝑣 " , 𝐻, 𝑉 βˆ’ {𝑣 " } ) + BTFW( 𝑣 " , 𝑒, 𝐻, 𝑉 βˆ’ {𝑣 " } ) // Use u_k at most once Dontusecase:=BTFW( 𝑑, 𝑒, 𝐻, 𝑉 βˆ’ {𝑣 " } ) // Don’t use 𝑣 " Return max(Usecase, Dontusecase)

  33. REACHABLE SUBPROBLEMS 𝑑 can be arbitrary, 𝑒 can be arbitrary, 𝑉 = {𝑀 0 , … 𝑀 1 } for some 0 ≀ 𝑙 ≀ π‘œ FW[ 𝑑, 𝑒, 𝑙 ] : Best path from 𝑑 to 𝑒 using only 𝑀 0 , … , 𝑀 1 as intermediate vertices Base case: 𝑉 is empty, 𝑙 = 0 , FW[ 𝑑, 𝑑, 0 ]=0, FW( 𝑑, 𝑒 )=w( 𝑑, 𝑒 ) for edge ( 𝑑, 𝑒 ), FW[ 𝑑, 𝑒, 0 ]= ∞ Recursion: FW[ 𝑑, 𝑒, 𝑙 ] = min ( , )

  34. REACHABLE SUBPROBLEMS 𝑑 can be arbitrary, 𝑒 can be arbitrary, 𝑉 = {𝑀 0 , … 𝑀 1 } for some 0 ≀ 𝑙 ≀ π‘œ FW[ 𝑑, 𝑒, 𝑙 ] : Best path from 𝑑 to 𝑒 using only 𝑀 0 , … , 𝑀 1 as intermediate vertices Base case: 𝑉 is empty, 𝑙 = 0 , FW[ 𝑑, 𝑑, 0 ]=0, FW( 𝑑, 𝑒 )=w( 𝑑, 𝑒 ) for edge ( 𝑑, 𝑒 ), FW[ 𝑑, 𝑒, 0 ]= ∞ Recursion: FW[ 𝑑, 𝑒, 𝑙 ] = min(F[ 𝑑, 𝑀 1 , 𝑙 βˆ’ 1 ]+F[ 𝑀 1 , 𝑒, 𝑙 βˆ’ 1 ], F[ 𝑑, 𝑒, 𝑙 βˆ’ 1 ])) Top down: k decreases, bottom-up : k increases

  35. FW ALGORITHM DPFW( 𝐻) Initialize array FW[ π‘Š, π‘Š, 1 … |π‘Š| ] For 𝑑 in π‘Š do: FW[ 𝑑, 𝑑, 0 ]:= 0 For edges 𝑑, 𝑒 ∈ E do: FW[ 𝑑, 𝑒, 0 ] = w( 𝑑, 𝑒 ) For 𝑑 in π‘Š do: For 𝑒 in π‘Š βˆ’ {𝑑} do: F[ 𝑑, 𝑒, 0 ]:= ∞ For 𝑙 = 1 to |π‘Š| do: For 𝑑 in π‘Š do: For 𝑒 in π‘Š do: FW[ 𝑑, 𝑒, 𝑙 ] = min(FW[ 𝑑, 𝑀 ! , 𝑙 βˆ’ 1 ]+FW[ 𝑀 ! , 𝑒, 𝑙 βˆ’ 1 ],FW[ 𝑑, 𝑒, 𝑙 βˆ’ 1 ])) Return the matrix FW[ 𝑑, 𝑒, |π‘Š| ] (Note: can save space by reusing single matrix)

  36. WHICH IS BETTER?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend