Dynamic Programming Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. August 31, 2015
Outline 1 Maximum sum contiguous subsequence 2 Longest common subsequence 3 Matrix chain multiplication 4 All pair shortest path 5 Knapsack
Outline 1 Maximum sum contiguous subsequence 2 Longest common subsequence 3 Matrix chain multiplication 4 All pair shortest path 5 Knapsack
Maximum sum contiguous subsequence Let S be an array, such that S[i] R. We want to find a contiguous subsequence of S with the maximum sum. The problem can be solved with the following recurrence. Exercise S[i] = max {S[i 1] + a[i], a[i]} i, 1 i n 1 Find the indices k and l, 1 k l n, such that l maximum. i=k S[i] is
Outline 1 Maximum sum contiguous subsequence 2 Longest common subsequence 3 Matrix chain multiplication 4 All pair shortest path 5 Knapsack
Longest common subsequence (LCS) Let A = {a 1, a 2,..., a n } and B = {b 1, b 2,..., b m } be two strings over an alphabet set Σ. A subsequence of A is of the form a i1, a i2,..., a ik, where each i j is between 1 and n and 1 i 1 < i 2 < < i k n. Let L[i, j] denote the length of the LCS of {a 1, a 2,..., a i } and {b 1, b 2,..., b j }. Then, L[i, j] can be expressed as 0 if i = 0 or j = 0; L[i, j] = L[i 1, j 1] + 1 if a i = b j, i, j > 0 max{l[i 1, j], L[i, j 1]} if a i b j, i, j > 0 The final answer is L[n, m]. Exercise Find the subsequences of A and B that achieve the LCS.
Outline 1 Maximum sum contiguous subsequence 2 Longest common subsequence 3 Matrix chain multiplication 4 All pair shortest path 5 Knapsack
Matrix chain multiplication We have to find the matrix product M = M 1 M 2 M k M k+1 M k+2 M n, where M k is of size r k r k+1. Let C[i, j] be the minimum cost of multiplying the matrices M i M j in terms of scalar multiplications. We denote M i M j as M i,j. We can write M ij = M i,k 1 M k,j for i < k < j. C[i, j] = min i k j {C[i, k 1] + C[k, j] + r i r k r j+1 } C[1, n] is the final answer. Exercise Find out how to compute C[i, j].
Outline 1 Maximum sum contiguous subsequence 2 Longest common subsequence 3 Matrix chain multiplication 4 All pair shortest path 5 Knapsack
All pair shortest path Let G = (V, E) be a directed graph with weights on each edge. Let V = {1,..., n}. We want to find the distance among all vertices of G. Let i, j V and dij k be the length of the shortest path from i to j that does not pass through {k + 1, k + 2,..., n}. So, dij n is the final answer. { l[i, j] if k = 0 d k ij = { min d k 1 ij, d k 1 ik + d k 1 kj } if 1 k n
Outline 1 Maximum sum contiguous subsequence 2 Longest common subsequence 3 Matrix chain multiplication 4 All pair shortest path 5 Knapsack
Knapsack Let U = {u 1,..., u n } be a set of n items to be packed inside a knapsack of capacity C, C Z +. For 1 i n, let s i and v i be the size and value of the i-th item, where s i, v i Z +. We want to fill the knapsack with some items from U whose total size is at most C and the sum of the values of the items in the knapsack is maximized. Let V [i, j] denote the value obtained by filling a knapsack of size j with items taken from the first i items, {u 1,..., u i } in an optimal way. 0 if i = 0 or j = 0 V [i, j] = V [i 1, j] if j < s i max {V [i 1, j], V [i 1, j s i ] + v i } if i > 0 and j s i V [n, C] is the final answer. Exercise Find the subset of U that gives the optimal answer.