Professional Documents
Culture Documents
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 1 / 28
Dynamic Programming
Fibonacci Numbers
Fib(n)
1 if n = 0
2 return 0
3 if n = 1
4 return 1
5 return Fib(n − 1) + Fib(n − 2)
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 2 / 28
Dynamic Programming
Fibonacci Numbers
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 3 / 28
Dynamic Programming
Fibonacci Numbers
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 4 / 28
Dynamic Programming
Fibonacci Numbers
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 5 / 28
Dynamic Programming
Fibonacci Numbers
Fib(n)
1 if F [n] = −1
2 F [n] = Fib(n − 1) + Fib(n − 2)
3 return F [n]
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 6 / 28
Dynamic Programming
Fibonacci Numbers
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 7 / 28
Dynamic Programming
Fibonacci Numbers
Efficiency
Time - O(n)
Space - O(n)
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 8 / 28
Dynamic Programming
Fibonacci Numbers
Fib(n)
1 F [0] = 0
2 F [1] = 1
3 for i = 2 to n
4 F [i] = F [i − 1] + F [i − 2]
5
6 return F [n]
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 9 / 28
Dynamic Programming
Dynamic Programming
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 10 / 28
Dynamic Programming
Dynamic Programming
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 11 / 28
Dynamic Programming
Dynamic Programming
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 12 / 28
Dynamic Programming
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 13 / 28
Dynamic Programming
Greedy approach:
Select the length that has the maximum price/length
Reduce to problem to a smaller sub-problem
Does it work?
Use the greedy approach for length = 4
Greedy solution is 3, 1 with revenue 9
Optimal solution is 2, 2 with revenue 10
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 14 / 28
Dynamic Programming
24−1 = 23 = 8
Let us find a way to solve the problem recursively (we might be able
to modify the solution so that the maximum can be actually
computed):
Assume we have cut a rod of length n into 0 ≤ k ≤ n pieces of length
i1 , i2 ..., ik ,
n = i1 + i2 + ... + ik
with revenue
rn = pi1 + pi2 + ... + pik
Assume further that this solution is optimal.
How can we construct it?
Advice: when you don’t know what to do next, start with a simple
example and hope something will occur to you...
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 16 / 28
Dynamic Programming
Begin by constructing (by hand) the optimal solutions for i = 1, 2, ..., 10:
r1 = 1 from solution 1 = 1 (no cuts)
r2 = 5 from solution 2 = 2 (no cuts)
r3 = 8 from solution 3 = 3 (no cuts)
r4 = 10 from solution 4 = 2 + 2
r5 = 13 from solution 5 = 2 + 3
r6 = 17 from solution 6 = 6 (no cuts)
r7 = 18 from solution 7 = 1 + 6 or 7 = 2 + 2 + 3
r8 = 22 from solution 8 = 2 + 6
r9 = 25 from solution 9 = 3 + 6
r10 = 30 from solution 10 = 10 (no cuts)
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 17 / 28
Dynamic Programming
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 18 / 28
Dynamic Programming
Cut-Rod(p, n)
1 if n == 0
2 return 0
3
4 q = −∞
5
6 for i = 1 to n
7 q = max (q, p[i] + Cut-Rod(p, n − i))
8
9 return q
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 19 / 28
Dynamic Programming
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 20 / 28
Dynamic Programming
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 21 / 28
Dynamic Programming
Cut-Rod(p, 4)
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 22 / 28
Dynamic Programming
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 23 / 28
Dynamic Programming
Bottom-Up-Cut-Rod(p, n)
1 Let r [0..n] be a new array
2 r [0] = 0
3 for j = 1 to n
4 q = −∞
5 for i = 1 to j
6 q = max (q, p[i] + r [j − i])
7 r [j] = q
8 return r [n]
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 25 / 28
Dynamic Programming
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 26 / 28
Dynamic Programming
Extended-Bottom-Up-Cut-Rod(p, n)
1 Let r [0..n] and s[0..n] be a new arrays
2 r [0] = 0
3 for j = 1 to n
4 q = −∞
5 for i = 1 to j
6 if q < p[i] + r [j − i]
7 q = p[i] + r [j − i]
8 s[j] = i
9 r [j] = q
10 return r and s
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 27 / 28
Dynamic Programming
Print-Cut-Rod-Solution(p, n)
1 (r , s) = Extended-Bottom-Up-Cut-Rod(p, n)
2 while n > 0
3 print s[n]
4 n = n − s[n]
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 28 / 28