4. Dynamic Programming, like the divide – and – conquer method, solves
problems by combining the solutions to subsproblems.
Partitioning the problems into smaller part ( can be called as
subproblems ) solving them recursively then combining the solution to
solve the original Problems.
This method is usually applied for solving optimization problems.
Definition
5. Characterize the structure of an Optimal Solution.
Recursively define the value of an Optimal Solution.
Compute the value of an Optimal Solution, typically in a bottom-up
fashion.
Construct an optimal solution from computed information.
Sequence of Four Steps
7. Problems that having Optimal Substructure Property, if it is Optimum
Solution from that problems can be structured using Optimal Structure
from that sub-problems.
Optimal Substructure
8. Says that a stick that have length of n, divided into smaller part and have
variety of price depending on the length. Like below :
Optimal Substructure
Determine the Maximum revenue that can be reach from selling all parts of the
stick!
9. .
Lets pretend that value of n is 4 so, the probability of the revenue that can be
get is :
Optimal Substructure
4 Parts with length of 1. Revenue = 1 x 4 = 4
2 Parts with length of 1 and 1 Part with length of 2. Revenue = 2 x 1 + 5 = 7.
1 Part with length of 1 and 1 Part with length of 3. Revenue = 1 x 1 + 1 x 8 = 9.
2 Parts with length of 2. Revenue = 2 x 5 = 10.
1 Part with length of 4. Revenue = 1 x 9 = 9.
10. If Recursive subproblem is revisited multiple times, it can be optimized
by remembering the solution of that subproblem.
Overlapping Subproblems
12. Runs like a normal recursion problem.
But checks a lookup table before
computing its solutions.
Saves its solution on the lookup table
after its computation.
Only has a solution to a problem that it
has faced.
Overlapping Subproblems
Memoization ( Top – Down )
Method
Runs the problem from the bottom of the
recursion.
Checks the lookup table before creating a
solution.
Save every solution along the way to the
lookup table.
Has every solution below the specified
subproblem.
Tabulation ( Bottom – Up )
Method
13. Use the table s[ i, j ] saves a significant amount of work when reconstructing an optimal solution
for matrix-chain multiplication.
Suppose that we did not maintain the s[ i, j ] table, having filled in only the table m[ i, j ]
containing optimal subproblem costs. It would take θ ( j – i ) = ω ( 1 ) time to reconstruct.
By storing in s[ i, j ] the index of the matrix at which we split the product Ai, Ai+1, …..Aj , we
can reconstruct each choice in O ( 1 ) time.
Reconstructing an optimal solution
14. oized version of RECURSIVE-MATRIX-CHAIN with top-down method.
A memoized recursive algorithm maintains an entry in a table for the solution to each
subproblem. Each table entry initially contains a special value to indicate that the entry has yet
to be filled in. When the subproblem is first encountered as the recursive algorithm unfolds, its
solution is computed and then stored in the table.
Memoization
Memoized version of RECURSIVE-
MATRIX-CHAIN with top-down method.
15. Like the bottom-up dynamic-programming algorithm MATRIX-CHAIN-ORDER, the procedure
MEMOIZED-MATRIX-CHAIN runs in O(n3)time. Line 5 of MEMOIZED-MATRIX-CHAIN
executes θ (n2) times.
In general practice, if all subproblems must be solved at least once, a bottom-up dynamic-
programming algorithm usually outperforms the corresponding top-down memoized algorithm
by a constant factor, because the bottom-up algorithm has no overhead for recursion and less
overhead for maintaining the table.
Memoization