Professional Documents
Culture Documents
Dyn Prog
Dyn Prog
Fibonacci numbers
Fibonacci recurrence: Fn = Fn1 + Fn2 with F0 = 0, F1 = 1
function Fibonacci(n)
if n = 0
then
return (0)
elseif
n=1
then return (1)
else return (Fibonacci(n 1) + Fibonacci(n 2))
end if
end
As Fn+1 /Fn (1 + 5)/2 1.61803 then Fn > 1.6n , and to compute Fn we need
1.6n recursive calls.
To compute F6 :
0 1 1 2
To get Fn need O(n) iterations.
3 5 7
9 16
2 3
13 18 23
2 3 4
3 4
= 18 25 32
3 4 5
4 5
23 32 41
Recall that matrix multiplication is NOT commutative, so we can not permute the
order of the matrices without changing the result,
but it is associative, so we can parenthesise as we wish.
Consider A1 A2 A3 , where dim(A1 ) = 10 100 dim(A2 ) = 100 5 and
dim(A3 ) = 5 50.
(A1 A2 )A3 needs (10 100 5) + (10 5 50) = 7500 operations,
A1 (A2 A3 ) needs (100 5 50) + (10 100 50) = 75000 operations.
The order makes a big difference in real computations time
(A1 (A2 (A3 A4 ))), ((A1 A2 )(A3 A4 )), (((A1 (A2 A3 ))A4 ), (A1 ((A2 A3 )A4 ))), (((A1 A2 )A3 )A4 ))
Let P (m) be the number of ways to put parenthesis correctly in A1 , . . . An . Then,
with solution
1
if n = 1
P (n) = Pn1
k=1 P (k)P (n k) si n 2
2n
1
= (4n /n3/2 )
P (n) =
n1 n
The Catalan numbers!
Therefore, brute force will take too long!
But we got a recursive definition. Lets try a recursive solution.
Recursive solution
Let m(i, j) be the minimum nomber of operations needed to compute
Aij = Ai . . . Aj .
m(i, j) is given by choosing the value k, i k j that minimizes
m(i, k) + m(k + 1, j) + cost (A1k Ak+1n ).
That is,
0
m(i, j) =
min1kj {m(i, k) + m(k + 1, j) + pi1 pk pj }
if i = j
otherwise
n1
X
i=1
T (i) + n (2n ).
An exponential function.
How many subproblems?
there are only O(n2 ) Aij !
We are repeating the computation of too many identical subproblems
Use dynamic programming to compute the optimal cost by a bottom-up approach.
0
if i = j
m[i, j] =
min1kj {m[i, k] + m[k + 1, j] + pi1 pk pj } otherwise
This algorithm has time complexity T (n) = (n3 ), and uses space (n2 ).
Therefore after computing table s we can multiply the matrices in an optimal way:
A1n = A1s[1,n] As[1,n]+1n .
0-1 Knapsack
We have a set I of n items, item i has weight wi and worth vi . We can carry at
most weight W in our knapsack. Considering that we can NOT take fractions of
items, what items should we carry to maximize the profit?
Let v[i, j] be the maximum value we can get from objects {1, 2, . . . , i} and taking a
maximum weight of 0 j W .
We wish to compute v[n, W ].
To compute v[i, j] we have two possibilities: The i-th element is or is not part of
the solution.
This gives the recurrence,
v[i 1, j w ] + v
i
i
v[i, j] =
v[i 1, j]
Example.
Let I = {1, 2, 3, 4, 5} with v(1) = 1; v(2) = 6; v(3) = 18; v(4) = 22; v(5) = 28,
w(1) = 1; w(2) = 2; w(3) = 5; w(4) = 6; w(5) = 7 and W = 11.
0
10
11
18
19
24
25
25
25
25
18
22
24
28
29
29
40
18
22
28
29
34
35
40
Question
As you already know 0-1 Knapsack is NP-hard.
But the previous algorithm has time complexity O(nW ). Therefore =NP!
Is something wrong?
Exercise
Modify the 0-1 Knapsack algorithm so that in addition to computing the optimal
cost it computes an optimal solution.
d
if S = {1, k}
1,m
C(S, k) =
minm6=k,mS [C(S {k}, m) + d(m, k)] otherwise
Complexity:
Pn3
Time: (n 1)
Pn1
Space: k=1 k
k=1
n1
k
n2
k
= (n 1)2n2 O(n2n )
k grandchild j
Complexity:
Obvious time and space complexity: O(n2 ).
But, at each vertex, the algorithm only looks at its children and granchildren,
therefore each j N is looked only 3 times:
1.- when the algorithm computes I(j),
2.- when the algorithm computes the MIS for the father of j,
3.- when the algorithm computes the MIS for the grandfather of j.
Since each j is used a constant number of times, the total number of steps is O(n)