Dynamic Programming: Solving Problems Optimally
Gain insights into dynamic programming techniques for solving problems optimally, including iterative methods, divide-and-conquer strategies, and overcoming challenges such as overlapping subproblems and redundant work. Explore the concept of identifying dynamic programming problems and learn the steps involved in implementing dynamic programming solutions effectively.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
DYNAMIC PROGRAMMING David Kauchak CS 140 Fall 2024
Admin Assignment 3 Assignment 4
Algorithmic "techniques" Iterative/incremental: solve problem of size n by first solving problem of size n-1. Divide-and-conquer: divide problem into independent subproblems. Solve each subproblem independently. Combine solutions to subproblem to create solution to the original problem.
Dynamic programming Method for solving problems where optimal solutions can be defined in terms of optimal solutions to subproblems AND the subproblems are overlapping
Running time Each call creates two recursive calls Each call reduces the size of the problem by 1 or 2: ? ? = ? ? 1 + ? ? 2 + ?(1) Creates a full binary of depth n (where the shortest leaf is at depth n/2) O(2n)
Can we do better? Fib(n) Fib(n-2) Fib(n-1) Fib(n-4) Fib(n-2) Fib(n-3) Fib(n-3) Fib(n-4) Fib(n-4) Fib(n-5) Fib(n-5) Fib(n-6) Fib(n-3) Fib(n-4) Fib(n-5)
A lot of repeated work! Fib(n) Fib(n-2) Fib(n-1) Fib(n-4) Fib(n-2) Fib(n-3) Fib(n-3) Fib(n-3) Fib(n-4) Fib(n-4) Fib(n-5) Fib(n-5) Fib(n-6) Fib(n-4) Fib(n-5)
Identifying a dynamic programming problem The solution can be defined with respect to solutions to subproblems The subproblems created are overlapping, that is we see the same subproblems repeated
Overlapping sub-problems divide and conquer dynamic programming
Dynamic programming: steps recursive definition: use this to recursively define the value of an optimal solution DP solution: describe the dynamic programming table: size, initial values, order in which it s filled in, location of solution Analysis: analyze space requirements, running time
Recursive definition Define a function and clearly define the inputs to the function The function definition should be recursive with respect to multiple subproblems pretend like you have a working function, but it only works on smaller problems Key: subproblems will be overlapping, i.e., inputs to subproblems will not be disjoint
Recursive definition Fibonacci: F(n) = ?
Recursive definition Fibonacci: F(n) = F(n-1) + F(n-2)
DP solution The recursive solution will generally be top-down, i.e., working from larger problems to smaller DP solution: work bottom-up, from the smallest versions of the problem to the largest store the answers to subproblems in a table (often an array or matrix) to build bigger problems, lookup solutions in the table to subproblems
DP solution F(n) = F(n-1) + F(n-2) What are the smallest possible values (subproblems)? To calculate F(n), what are all the subproblems we need to calculate? This is the table . How should we fill in the table?
DP solution F(n) = F(n-1) + F(n-2) What are the smallest possible values (subproblems)? F(1) = 1, F(2) = 1 To calculate F(n), what are all the subproblems we need to calculate? This is the table . F(1) F(n-1) How should we fill in the table? F(1) F(n)
DP solution Store the intermediary values in an array (fib)
Analysis Space requirements? Running time?
Analysis Space requirements: (n) Running time: (n)
Counting binary search trees How many unique binary search trees can be created using the numbers 1 through n? 4 2 5 3 6 1
Step 1: What is the subproblem? Assume we have a working function (call it T) that can give us the answer to smaller subproblems How can we use the answer from this to answer our question? How many options for the root are there? 1 2 3 n
Subproblems i How many trees have i as the root?
Subproblems i 1, 2, , i-1 i+1, i+2, , n ?
Subproblems i 1, 2, , i-1 i+1, i+2, , n ? T(i-1) subproblem of size i-1
Subproblems i 1, 2, , i-1 i+1, i+2, , n T(i-1) Number of trees for i+1, i+2, , i+n is the same as the number of trees from 1, 2, , n-i subproblem of size i-1
Subproblems i 1, 2, , i-1 i+1, i+2, , n T(i-1) T(n-i) subproblem of size i-1 subproblem of size n-i
Recursive definition i 1, 2, , i-1 i+1, i+2, , n T(i-1) T(n-i) Given solutions for T(i-1) and T(n-i) how many trees are there with i as the root?
Recursive definition i 1, 2, , i-1 i+1, i+2, , n T(i-1) T(n-i) Trees with i as root = T(i-1) * T(n-i)
Recursive definition Trees with i as root = T(i-1) * T(n-i) How many trees total? 1 2 3 n
Recursive definition Trees with i as root = T(i-1) * T(n-i) = n = ( ) ( ) 1 * ( ) T n T i T n i 1 i 1 2 3 n
A recursive implementation = n = ( ) ( ) 1 * ( ) T n T i T n i 1 i Like with Fibonacci, we re repeating a lot of work
DP solution (from the bottom-up) = n = ( ) ( ) 1 * ( ) T n T i T n i 1 i What are the smallest possible subproblems? To calculate T(n), what are all the subproblems we need to calculate? This is the table . How should we fill in the table?
DP solution (from the bottom-up) = n = ( ) ( ) 1 * ( ) T n T i T n i 1 i What are the smallest possible subproblems? T(0)=1, T(1) = 1 Why do we need T(0) and why is it 1?
DP solution (from the bottom-up) = n = ( ) ( ) 1 * ( ) T n T i T n i 1 i What are the smallest possible subproblems? T(0)=1, T(1) = 1 1 Need to think carefully about base cases/edge cases
DP solution (from the bottom-up) = n = ( ) ( ) 1 * ( ) T n T i T n i 1 i What are the smallest possible subproblems? T(0)=1, T(1) = 1 To calculate T(n), what are all the subproblems we need to calculate? This is the table . T(0) T(n-1) How should we fill in the table? T(0) T(n)
Fill in the first 4 values 0 1 2 3 4 5 n
1 1 0 1 2 3 4 5 n
c[0]*c[1] + c[1]*c[0] 1 1 0 1 2 3 4 5 n
1 2 2 1 c[0]*c[1] + c[1]*c[0] 1 1 0 1 2 3 4 5 n
1 1 2 0 1 2 3 4 5 n
2 3 1 c[0]*c[2] + c[1]*c[1] + c[2]*c[0] 1 1 2 0 1 2 3 4 5 n
1 1 2 5 0 1 2 3 4 5 n
1 1 2 5 0 1 2 3 4 5 n
3: Analysis Space requirements? Running time?
3: Analysis Space requirements: (n) Running time: (n2)
Subsequences For a sequence X = x1, x2, , xn, a subsequence is a subset of the sequence defined by a set of increasing indices (i1, i2, , ik) where 1 i1 < i2< < ik n X = A B A C D A B A B ABA?
Subsequences For a sequence X = x1, x2, , xn, a subsequence is a subset of the sequence defined by a set of increasing indices (i1, i2, , ik) where 1 i1 < i2< < ik n X = A B A C D A B A B ABA
Subsequences For a sequence X = x1, x2, , xn, a subsequence is a subset of the sequence defined by a set of increasing indices (i1, i2, , ik) where 1 i1 < i2< < ik n X = A B A C D A B A B ACA?