Download presentation
Presentation is loading. Please wait.
1
Lecture 9 Dynamic programming
What is Dynamic Programming How to write Dynamic Programming paradigm When to apply Dynamic Programming
2
Roadmap What is Dynamic Programming How to write DP paradigm
Knapsack problem Longest common subsequence Matrix chain multiplication When to apply Dynamic Programming Practice
3
Techniques Based on Recursion
Induction or Tail recursion Non-overlapping subproblems Overlapping subproblems
4
An Introductory Example
\begin{eqnarray*} Fib(0) & = & 0 \\ Fib(1) & = & 1 \\ Fib(n) & = & Fib(n-1) + Fib(n-2) \end{eqnarray*} 19th century statue of Fibonacci in Camposanto, Pisa. (Source from Wikipedia) Problem: Fibonacci Input: A positive integer n Output: Fib(n)
5
Quiz Problem: Fibonacci Input: A positive integer n Output: Fib(n) Give your solution of Fibonacci and explain the time/space complexity.
6
Recursion implementation
int fib_rec (int n){ if (n==1 || n==2) { return 1; } else { return fib_rec(n-1) + fib_rec(n-2); \begin{eqnarray*} T(n) & = & T(n-1) + T(n-2) + 2,\ \ T(1),T(2) < 3 \\ T(n) & > & Fib_n \\ S(n) & = & O(n) \end{eqnarray*} O
7
O(n) time, O(n) space int fib (int n){ int* array = new int[n];
for (int i = 2; i <= n; i++) array[i] = array[i-1] + array[i-2]; return array[n]; } \begin{eqnarray*} T(n) & = & n + 1\\ S(n) & = & n + 1 \end{eqnarray*}
8
O(n) time, O(1) space We can do even better: int fib (int n){
if (n < 2) return n; int f0 = 0; int f1 = 1; int i = 2; while (i <= n) { f1 = f1 + f0; f0 = f1 - f0; i++; } return f1; \begin{eqnarray*} T(n) & < & 3n\\ S(n) & = & 3 \end{eqnarray*} We can do even better:
9
Use Divide-and-conquer
O(log n) time, O(1) space \left(\begin{array}{cc} f_n & f_{n-1}\\ f_{n-1} & f_{n-2} \end{array}\right) = 1 & 1\\ 1 & 0 \end{array}\right) \times \left(\begin{array}{cc} f_{n-1} & f_{n-2}\\ f_{n-2} & f_{n-3} \end{array}\right) \[T(n) = T(\frac{n}{2}) + O(1)\] \[T(n) = O(\log n)\] Use Divide-and-conquer
10
(Source from Wikipedia)
Fibonacci \begin{eqnarray*} Fib(0) & = & 0 \\ Fib(1) & = & 1 \\ Fib(n) & = & Fib(n-1) + Fib(n-2) \end{eqnarray*} Overlapping subproblems 19th century statue of Fibonacci in Camposanto, Pisa. (Source from Wikipedia) Recursion? No, thanks.
11
Non-recursively handle recursive problems
Dynamic programming Non-recursively handle recursive problems Time-Memory tradeoff Find smaller subproblems Write the relation expression Use an array to save the intermediate results Update the array iteratively.
12
All-pairs shortest path
Define $d_{i,j}^{k}$ to be the length of a shortest path from $i$ to $j$ that does not pass any vertex in $\{k+1,k+2,\ldots,n\}$. Clearly \[d_{ij}^{k} = \left\{\begin{array}{ll} l[i,j] & {\rm if}\ k=0 \\ min\{d_{i,j}^{k-1},d_{i,k}^{k-1}+d_{k,j}^{k-1}\} & {\rm if}\ 1\le k\le n \end{array}\right.\]
13
Floyd-Warshall algorithm
Θ(n3)
14
Where are we? What is Dynamic Programming How to write DP paradigm
Knapsack problem Longest common subsequence Matrix chain multiplication When to apply Dynamic Programming Practice
15
Knapsack problem Problem: Knapsack
Input: A set of items U = {u1,...,un} with sizes s1,s2,...,sn and values v1,v2,...,vn and a knapsack capacity C Output: The maximum value that can be put into the knapsack
16
Knapsack problem V[i,j]: the maximum value obtained by filling a knapsack of size j with items taken from the first i items {u1,...,ui}. \[V[i,j] = \left\{ \begin{array}{ll} 0 & {\rm if}\ i=0\ {\rm or}\ j=0 \\ V[i-1,j] & {\rm if}\ j<s_{i} \\ max\{V[i-1,j],V[i-1,j-s_{i}]+v_{i}\} & {\rm if}\ i>0\ {\rm and}\ j\ge s_{i} \end{array} \right.\] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1, 1 1, 2 2, 2 4, 10 12, 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 2 3 4 10 12 13 14 15 15 15 15 15 15 15 15 2 3 4 10 12 13 14 15 15 15 15 15 15 15 15
17
Psuedo-polynomial algorithm
Knapsack problem Time: Θ(nC) Space: Θ(nC) Psuedo-polynomial algorithm
18
How to construct the solution?
1 2 3 4 5 10 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1, 1 1, 2 2, 2 4, 10 12, 4 \[V[i,j] = \left\{ \begin{array}{ll} 0 & {\rm if}\ i=0\ {\rm or}\ j=0 \\ V[i-1,j] & {\rm if}\ j<s_{i} \\ max\{V[i-1,j],V[i-1,j-s_{i}]+v_{i}\} & {\rm if}\ i>0\ {\rm and}\ j\ge s_{i} \end{array} \right.\] V
19
What if the thief robs a super-market? Knapsack with repetition.
Knapsack problem What if the thief robs a super-market? Knapsack with repetition. V[i,j] = 0 if i = 0 or j = 0 = V[i-1, j] if si > j = max{V[i,j-si] + vi, V[i-1, j]} o.w. or V[j] = max_{i, si <= j}{V[j-si]+vi}
20
Longest common subsequence
Problem: LCS Input: Two strings A=a1a2...an and B=b1b2...bm Output: The longest common subsequence of A and B L[i,j]: length of the longest common subsequence of A[1..i] and B[1..j] ababe ababe Solution: L[n,m] abcabc abcabc
21
LCS a b c a b e \[ L[i,j] = \left\{ \begin{array}{ll} 0 & {\rm if}\ i=0\ {\rm or}\ j=0 \\ L[i-1,j-1]+1 & {\rm if}\ i>0,j>0\ {\rm and}\ a_{i}=b_{j} \\ max\{L[i-1,j],L[i,j-1]\} & {\rm if}\ i>0,j>0\ {\rm and}\ a_{i}\not=b_{j} \end{array}\right.\] 1 1 1 1 1 1 1 2 2 2 2 2 1 2 2 3 3 3 1 2 2 3 4 4 1 2 2 3 4 4
22
LCS Time: Θ(mn) Space: Θ(max{m,n})
23
Longest common substring?
\[ L[i,j] = \left\{ \begin{array}{ll} 0 & {\rm if}\ i=0\ {\rm or}\ j=0 \\ L[i-1,j-1]+1 & {\rm if}\ i>0,j>0\ {\rm and}\ a_{i}=b_{j} \\ 0 & {\rm if}\ i>0,j>0\ {\rm and}\ a_{i}\not=b_{j} \end{array}\right.\] L[i,j]: length of longest common sub-postfix of a[1..i] and b[1..j]. Solution: the maximum L[i,j] for all i>0 and j>0
24
Matrix chain multiplication
What is the most efficient way of computing the following multiplication? n matrix, how many different way of multiplications? Answer: catalan number C_{n-1} C_n = C0Cn-1 + C1 C Cn-1C0 = 2n\choose n - 2n \choose n+1 = (2n\choose n+1) / n+1 Catalan number: 1. How many different ways to pair n-pair parenthesis? Fix one (), then 0n-1, 1 n-2, 2 n-3,... n-1 0 2. How many different ways to completely parenthesize n+1 factors? 3. How many different full binary trees with n+1 leaves? View intermediate node as multiplication 4. How many different ways to cut a convex n+2 polygon into triangles? \[\left( \begin{array}{c} a_{11}\ldots a_{15} \\ \vdots \\ a_{61}\ldots a_{65} \end{array} \right) \left( \begin{array}{c} b_{11}\ldots b_{14} \\ b_{51}\ldots b_{54} c_{11}\ldots c_{13} \\ c_{41}\ldots c_{43} \end{array} \right)\] The order of multiplications matters.
25
Quiz M1 * M2 * … * Mn How many different ways to compute
Catalan number
26
Matrix chain multiplication
Problem: MCM Input: A matrix chain M1M2...Mn with rank r1r2..rnrn+1 Output: Minimal cost of the multiplication ABCD 50,20,1,10,100
27
C[i,j]: the minimal cost of the multiplication MiMi +1 ... Mj
Dynamic programming C[i,j]: the minimal cost of the multiplication MiMi Mj A B C D Memoization A B C D 1000 1500 7000 ABCD 50,20,1,10,100 200 3000 1000
28
MCM 1+2^ n^2 = n(n+1)(2n+1)/6
29
C[i,j]: the minimal cost of the multiplication MiMi +1 ... Mj
Dynamic programming C[i,j]: the minimal cost of the multiplication MiMi Mj ABCD 50,20,1,10,100 1000 200 3000 1500 7000 A B C D 4 3 2 A B C D A B C D A B C D 1 2 3 4
30
Where are we? What is Dynamic Programming How to write DP paradigm
Knapsack problem Longest common subsequence Matrix chain multiplication When to apply Dynamic Programming Practice
31
Elements of DP Optimal substructure
A problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems. Overlapping subproblems
32
Optimal Substructure Given a graph G=<V,E>, which problem has optimal substructure, longest simple path problem or shortest simple path problem?
33
Practice Subset sum Longest palindrome sequence Editing distance
Longest increasing subsequence Planning a company party
34
Subset sum problem Problem: SubsetSum
Input: A set of numbers A={a1,a2,...,an}, and a sum s Output: Yes, if there exists a subset B⊆A such that the sum of B equals to s; No, otherwise. \[ L[i,j] = \left\{ \begin{array}{ll} 0 & {\rm if}\ i=0\ \\ 1 & {\rm if}\ j=0\\ L[i-1,j] & {\rm if}\ i>0, j>0,\ j<a_i\\ L[i-1,j-a_i] \vee L[i-1,j] & {\rm if}\ i>0,j>0,\ j\geq a_i \end{array}\right.\] L[i,j]: Does there exists a subset B of {a1,...,ai} such that the sum of B equals to j Solution: L[n,s]
35
Longest palindrome subsequence
A palindrome is a nonempty string over some alphabet that reads the same for- ward and backward. civic, racecar Problem: PalindromeSubsequence Input: A string A=a1a2...an Output: Length of the longest subsequence of A that is a palindrome We can also reduce this problem to the Longest Common Subsequence problem of a1a2..an and an...a2a1. (by Qiu Zhe.) L[i,j]: length of the longest palindrome subsequence of ai...aj L[i,j] = 1 if i = j L[i,j] = L[i+1, j-1] if ai = aj L[i,j] = max{L[i+1,j], L[i,j-1]} o.w
36
E[i,j]: edit distance of a1...ai and b1...bj
Editing distance snowy --> sunny The edit distance between two strings is the cost of their best possible alignment. E[i,j]: edit distance of a1...ai and b1...bj
37
Planning a company party
2 We like convivial friends. And we will not invite both the employee and his/her direct supervisor. How to maximize the conviviality? 4 9
38
Single-source longest path in DAG
D[s] = 0 D[v] = max(u,v)\in E{D[u]+w(u,v)}
39
Conclusion What is Dynamic Programming How to write DP paradigm
Knapsack problem Longest common subsequence Matrix chain multiplication When to apply Dynamic Programming Practice
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.