Dynamic Programming
Dynamic Programming
LECTURE 16
Dynamic Programming
(plus FFT Recap)
Adam Smith
9/24/2008
2 = 1 = i
3
4 = 2 = -1
1
n=8
7
5
6 = 3 = -i
Fourier matrix Fn
0 = 0 = 1
k+n/2 = -k
k = (k)2 = (k+n/2)2
When n is even,
there are only
n/2 possible
values of 2k
FFT Algorithm
fft(n, a0,a1,,an-1) {
if (n == 1) return a0
(e0,e1,,en/2-1) FFT(n/2, a0,a2,a4,,an-2)
(d0,d1,,dn/2-1) FFT(n/2, a1,a3,a5,,an-1)
for k =
k
yk+n/2
yk+n/2
}
0 to n/2 - 1 {
e2ik/n
e k + k d k
e k - k d k
return (y0,y1,,yn-1)
}
FFT Summary
Theorem. FFT algorithm evaluates a degree n-1 polynomial at each of
the nth roots of unity in O(n log n) steps.
assumes n is a power of 2
O(n log n)
coefficient
representation
point-value
representation
Inverse DFT
Inverse FFT
Claim. Inverse of Fourier matrix is given by following formula.
Polynomial Multiplication
Theorem. Can multiply two degree n-1 polynomials in O(n log n) steps.
coefficient
representation
FFT
coefficient
representation
O(n log n)
inverse FFT
point-value multiplication
O(n)
O(n log n)
Integer Multiplication
Integer multiplication. Given two n bit integers a = an-1 a1a0 and
b = bn-1 b1b0, compute their product c = a b.
Convolution algorithm.
Form two polynomials.
Note: a = A(2), b = B(2).
Compute C(x) = A(x) B(x).
Evaluate C(2) = a b.
Running time: O(n log n) complex arithmetic steps.
Chapter 5
Dynamic Programming
Fibonacci Sequence
Sequence defined by
a1 = 1
a2 = 1
an = an-1 + an-2
Running Time?
Fib(n)
1. If n =1 or n=2, then
2.
return 1
3. Else
4.
a = Fib(n-1)
5.
b = Fib(n-2)
6.
return a+b
Review Question
Prove that the solution to the recurrence T(n)=T(n-1)+T(n-2) + (1) is
exponential in n.
a
b
c
d
e
f
g
h
0
Time
10
weight = 999
weight = 1
a
Time
0
10
11
1
2
3
4
5
6
7
8
0
Time
10
11
Input: n, s1,,sn
f1,,fn
v1,,vn
2
3
3
4
5
p(1) = 0, p(j) = j-2
Input: n, s1,,sn
f1,,fn
v1,,vn
global array
M-Compute-Opt(j) {
if (M[j] is empty)
M[j] = max(wj + M-Compute-Opt(p(j)), M-Compute-Opt(j-1))
return M[j]
}
Run M-Compute-Opt(n)
Run Find-Solution(n)
Find-Solution(j) {
if (j = 0)
output nothing
else if (vj + M[p(j)] > M[j-1])
print j
Find-Solution(p(j))
else
Find-Solution(j-1)
}
Input: n, s1,,sn
f1,,fn
v1,,vn