Dynamic Programming
Dynamic Programming
• Main idea:
- set up a recurrence relating a solution to a larger instance to
solutions of some smaller instances
- solve smaller instances once
- record solutions in a table
- extract solution to the initial instance from that table
Dynamic Programming
Algorithm
1. Characterize the structure of an optimal solution
2. Recursively define the value of an optimal solution
3. Compute the value of an optimal solution in a
bottom-up fashion
4. Construct an optimal solution from computed
information (not always necessary)
8
Examples of DP algorithms
• Multistage graph
Fibonacci:
function Fibonacci(n: integer) : integer;
var
i : index; sum, interm1, interm2: integer;
begin
interm1:= 0; {F0}
interm2:= 1; {F1}
for i:=3 to n do
sum :=interm1 + interm2;
interm1:=interm2;
interm2:= sum;
end {for}
Fibonacci := sum;
end {Fibonacci}
Dynamic Programming vs. Recursion
and Divide & Conquer