First Ingredient of Dynamic Programming - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

First Ingredient of Dynamic Programming

Description:

First Ingredient of Dynamic Programming. 1. Optimal substructure. The optimal solution to the problem contains optimal ... Proof by contradiction (cut-and-paste) ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 10
Provided by: toshi155
Category:

less

Transcript and Presenter's Notes

Title: First Ingredient of Dynamic Programming


1
First Ingredient of Dynamic Programming
1. Optimal substructure
The optimal solution to the problem contains
optimal solutions to the subproblems.
Proof by contradiction (cut-and-paste).
2
Second Ingredient of DP
2. Overlapping subproblems.
Few subproblems but many recurring instances.
3
Memoization
1. Still recursive
2. After computing the solution to a subproblem,
store it in table.
3. Subsequent calls do table lookup.
4
Matrix-Chain Recursion Tree without Memoization
1..4
1..1 2..4 1..2
3..4 1..3 4..4
2..2 3..4 2..3 4..4 1..1 2..2 3..3
4..4 1..1 2..3 1..2 3..3
3..3 4..4 2..2 3..3
2..2 3..3 1..1 2..2
5
Matrix-Chain Recursion Tree with Memoization
1..4
1..1 2..4 1..2
3..4 1..3 4..4
2..2 3..4 2..3 4..4 1..1 2..2
1..1 2..3 1..2 3..3
3..3 4..4 2..2 3..3
6
Memoized-Matrix-Chain
Lookup-Chain(p, i, j) // chain product
A A dimensions in p if mi, j lt ?
// if cost already
computed then return mi, j // simply return
the cost // otherwise, its the first call
compute the cost recursively if i j then
mi, j 0 else for k i to j 1 do
q Lookup-Chain(p, i, k) Lookup-Chain(p,
k1, j) p p p if q lt mi, j
then mi, j q return mi, j
i j
i-1 k j
Invoke Lookup-Chain(p, 1, n) to compute the chain
product cost.
7
Analysis of Memoization
Lookup-Chain(p, 1, n)
LC(p, i, j)
8
Analysis (contd)
The first call to Lookup-Chain(p, i, j) requires
computation. // ?(j i) O(n) time //
(excluding time spent on recursively computing
other entries). The rest n i ? j ? 2 calls
result in table lookups // O(1) each call of
this kind
Total running time
9
Memoization vs DP
Write a Comment
User Comments (0)
About PowerShow.com