CS252 Algorithms Wednesday, 8 May 2024 + Questions? + Today - How I think about dynamic programming + "Programming" is not our usual programming - Optimization problem solving techniques - Linear programming, nonlinear, integer, ... - Search space, an "objective function" that you want to maximize or minimize, and constraints + Fibonacci numbers - recursion is the natural formulation - recursion is trouble - memoization: I remember what it means by thinking of Fibonacci + My general approach to DP problems - State the problem - Inputs - Outputs - What are we optimizing? - Try a couple small instances by hand to get a feel for the problem - Search space - What is it? - How big is it? (and thus, how long will brute force take?) - Pause for notation - S = search space - V : S --> R+ (a "value" for each element of the search space) - s_opt and v_opt = V(s_opt) -- the optimal value and an/the element s_opt in S that yields it - Come up with a parameterized characterization of the problem of finding v_opt recursively. (This takes practice.) Here, we're trying to find just v_opt, not s_opt. - Show a recursive decomposition of the parameterized problem; include base case(s). Here, you're expressing your full solution in terms of the solutions of smaller but similar problems. - Note that solving by straight-up recursion will typically have the Fibonacci exponential runtime problem - Put together a table and solve for v_opt from the bottom up - Analyze the runtime of your algorithm - Now that you can compute v_opt, can you enhance the algorithm to help find s_opt? (This is often called "back-tracing", and you have seen a similar issue when we looked at BFS/DFS and Dijkstra.) + Wire-cutting (also called rod-cutting) - The problem - Given a price p[j] you can charge for a piece of wire of length j, for j = 1,...,m - Find the maximum revenue you can earn by cutting up (or not) a piece of wire of length n (which we assume is <= m) - Secondary goal: where should I make the cuts? - Play around p[0...6] = 0 2 3 8 9 12 13 - Search space? Search space size? - What are we optimizing? (v_opt & s_opt) - Characterize problem as a function to be optimized - Write a recursive decomposition of the function - Build up subproblem solutions - we could do it top-down (memoized) - but we'll generally do it bottom-up (dynamic programming) - Complexity? - Backtracing - Retrieving the cuts that maximize revenue + Other problems - Minimum edit distance (Levenshtein) - Weighted interval scheduling - Knapsack -