CS 332: Algorithms: Dynamic Programming
CS 332: Algorithms: Dynamic Programming
Dynamic Programming
dynamic tables
1. Init table size m = 1
2. Insert elements until number n > m
3. Generate new table of size 2m
4. Reinsert old elements into new table
5. (back to step 2)
What is the worst-case cost of an insert?
What is the amortized cost of an insert?
Review:
Analysis Of Dynamic Tables
Let ci = cost of ith insert
ci = i if i-1 is exact power of 2, 1 otherwise
Example:
Operation
Insert(1)
Insert(2)
Insert(3)
Insert(4)
Insert(5)
Insert(6)
Insert(7)
Insert(8)
Insert(9)
Table Size
1
2
4
4
8
8
8
8
16
Cost
1
1
1
1
1
1
1
1
1
+ 1
+ 2
+ 4
+ 8
1
2
3
4
5
6
7
1
8
2
9
c
i 1
lg n
n 2 n (2n 1) 3n
j
j 0
Insert()s
Upshot: O(1) amortized cost per operation
Dynamic Programming
Another strategy for designing algorithms is
dynamic programming
A metatechnique, not an algorithm
small subproblems
if its a subsequence of y
How many subsequences of x are there?
What will be the running time of the brute-force alg?
LCS Algorithm
Brute-force algorithm: 2m subsequences of x to
Theorem:
if x[i ] y[ j ],
c[i 1, j 1] 1
c[i, j ]
max(c[i, j 1], c[i 1, j ]) otherwise
What is this really saying?
The End