Dynamic Programming. p. 1/43

Similar documents
Chapter 8 Dynamic Programming

Chapter 8 Dynamic Programming

Dynamic Programming. Prof. S.J. Soni

Algorithms and Theory of Computation. Lecture 9: Dynamic Programming

Week 7 Solution. The two implementations are 1. Approach 1. int fib(int n) { if (n <= 1) return n; return fib(n 1) + fib(n 2); } 2.

CSE 431/531: Analysis of Algorithms. Dynamic Programming. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

Partha Sarathi Mandal

CS 470/570 Dynamic Programming. Format of Dynamic Programming algorithms:

Maximum sum contiguous subsequence Longest common subsequence Matrix chain multiplication All pair shortest path Kna. Dynamic Programming

CS473 - Algorithms I

Graduate Algorithms CS F-09 Dynamic Programming

Design and Analysis of Algorithms

Courses 12-13: Dynamic programming

All-Pairs Shortest Paths

CSC 1700 Analysis of Algorithms: Warshall s and Floyd s algorithms

Lecture 7: Dynamic Programming I: Optimal BSTs

Dynamic Programming. Shuang Zhao. Microsoft Research Asia September 5, Dynamic Programming. Shuang Zhao. Outline. Introduction.

Dynamic Programming ACM Seminar in Algorithmics

Dynamic Programming( Weighted Interval Scheduling)

Dynamic Programming. Reading: CLRS Chapter 15 & Section CSE 2331 Algorithms Steve Lai

Aside: Golden Ratio. Golden Ratio: A universal law. Golden ratio φ = lim n = 1+ b n = a n 1. a n+1 = a n + b n, a n+b n a n

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Single Source Shortest Paths

Dynamic Programming. Cormen et. al. IV 15

Dynamic Programming: Shortest Paths and DFA to Reg Exps

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

DAA Unit- II Greedy and Dynamic Programming. By Mrs. B.A. Khivsara Asst. Professor Department of Computer Engineering SNJB s KBJ COE, Chandwad

Dynamic Programming (CLRS )

Analysis of Algorithms I: All-Pairs Shortest Paths

Lecture 13. More dynamic programming! Longest Common Subsequences, Knapsack, and (if time) independent sets in trees.

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Lecture 13. More dynamic programming! Longest Common Subsequences, Knapsack, and (if time) independent sets in trees.

Chapter 4 Divide-and-Conquer

Dynamic Programming. Reading: CLRS Chapter 15 & Section CSE 6331: Algorithms Steve Lai

Introduction. I Dynamic programming is a technique for solving optimization problems. I Key element: Decompose a problem into subproblems, solve them

CSE 202 Homework 4 Matthias Springer, A

General Methods for Algorithm Design

CS173 Running Time and Big-O. Tandy Warnow

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Algorithms Design & Analysis. Dynamic Programming

Relations Graphical View

Section Summary. Sequences. Recurrence Relations. Summations Special Integer Sequences (optional)

CS60007 Algorithm Design and Analysis 2018 Assignment 1

Lecture 11. Single-Source Shortest Paths All-Pairs Shortest Paths

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

Question Paper Code :

Algorithm efficiency analysis

CS483 Design and Analysis of Algorithms

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

Introduction to Algorithms 6.046J/18.401J/SMA5503

Notes. Relations. Introduction. Notes. Relations. Notes. Definition. Example. Slides by Christopher M. Bourke Instructor: Berthe Y.

Dynamic Programming: Matrix chain multiplication (CLRS 15.2)

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

1 Closest Pair of Points on the Plane

Data Structures in Java

6. DYNAMIC PROGRAMMING I

Algorithm Design Strategies V

Outline. Similarity Search. Outline. Motivation. The String Edit Distance

Lecture 2: Divide and conquer and Dynamic programming

Note that M i,j depends on two entries in row (i 1). If we proceed in a row major order, these two entries will be available when we are ready to comp

CPSC 320 (Intermediate Algorithm Design and Analysis). Summer Instructor: Dr. Lior Malka Final Examination, July 24th, 2009

CS 246 Review of Linear Algebra 01/17/19

Similarity Search. The String Edit Distance. Nikolaus Augsten. Free University of Bozen-Bolzano Faculty of Computer Science DIS. Unit 2 March 8, 2012

Advanced Analysis of Algorithms - Midterm (Solutions)

Dynamic programming. Curs 2017

CSE 202 Dynamic Programming II

Conventional Matrix Operations

Lecture 7: Shortest Paths in Graphs with Negative Arc Lengths. Reading: AM&O Chapter 5

CSE 421 Dynamic Programming

Similarity Search. The String Edit Distance. Nikolaus Augsten.

CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015

CPSC 320 Sample Final Examination December 2013

Dynamic programming. Curs 2015

Find: a multiset M { 1,..., n } so that. i M w i W and. i M v i is maximized. Find: a set S { 1,..., n } so that. i S w i W and. i S v i is maximized.

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

Outline. Approximation: Theory and Algorithms. Motivation. Outline. The String Edit Distance. Nikolaus Augsten. Unit 2 March 6, 2009

a 11 x 1 + a 12 x a 1n x n = b 1 a 21 x 1 + a 22 x a 2n x n = b 2.

Today s Outline. CS 362, Lecture 13. Matrix Chain Multiplication. Paranthesizing Matrices. Matrix Multiplication. Jared Saia University of New Mexico

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

Design and Analysis of Algorithms

Dynamic Programming: Interval Scheduling and Knapsack

On the Fine-grained Complexity of One-Dimensional Dynamic Programming

1 Assembly Line Scheduling in Manufacturing Sector

1 Review of Vertex Cover

CSCI Honor seminar in algorithms Homework 2 Solution

ICS 6N Computational Linear Algebra Matrix Algebra

ICS141: Discrete Mathematics for Computer Science I

ELEMENTARY LINEAR ALGEBRA

Divide-and-Conquer Algorithms Part Two

CS3233 Competitive i Programming

The Divide-and-Conquer Design Paradigm

CS Analysis of Recursive Algorithms and Brute Force

Parallel Scientific Computing

Lecture 3: Matrix and Matrix Operations

Approximation: Theory and Algorithms

Algorithmic Approach to Counting of Certain Types m-ary Partitions

Introduction to Computer Science Lecture 5: Algorithms

IS 2610: Data Structures

Linear Algebra. Matrices Operations. Consider, for example, a system of equations such as x + 2y z + 4w = 0, 3x 4y + 2z 6w = 0, x 3y 2z + w = 0.

Transcription:

Dynamic Programming Formalized by Richard Bellman Programming relates to planning/use of tables, rather than computer programming. Solve smaller problems first, record solutions in a table; use solutions of smaller problems to get solutions for bigger problems. Differs from Divide and Conquer in that it stores the solutions, and subproblems are overlapping Fibonacci Numbers, Binomial Coefficients, Warshall s algorithm for transitive closure, Floyd s all pairs shortest path, Knapsack, optimal binary search tree p. 1/43

Fibonacci Numbers F 1 = F 2 = 1. F n = F n 1 +F n 2, for n 3. Fib(n) If n = 1 or n = 2, then return 1 Else return Fib(n 1)+Fib(n 2) End p. 2/43

Problem: Too time consuming. The sub problems Fib(n 1) and Fin(n 2) are overlapping Time complexity: Exponential T(n) = T(n 1)+T(n 2)+constant > 2T(n 2), Ω(2 n/2 ) Calculates Fib(n 2) twice, Fib(n 3) thrice, Fib(n 4) five times, and so on. We next consider a method which memorizes the values already computed. p. 3/43

Dynamic Programming Algorithm Fib(n): F(1) 1 F(2) 1 For i = 3 to n F(n) F(n 1)+F(n 2) EndFor Return F(n) End p. 4/43

T(n) O(n). Uses Extra Memory. p. 5/43

Using constant memory Fib(n): If n = 1 or n = 2, then return 1 Else prevprev 1 prev 1 For i = 3 to n { f prev +prevprev prevprev prev prev f } Return f End p. 6/43

Binomial Coefficients C(n,k) = n C k = ( n k) : number of ways to choose k out of n objects C(n,k) = C(n 1,k 1)+C(n 1,k), for n > k > 0. C(n,0) = 1 = C(n,n). So C(n,k) can be computed using smaller problems. p. 7/43

Binomial Coefficients 0 1 2 3 4 5 6 0 1 1 1 1 2 1 2 1 3 1 3 3 1 4 1 4 6 4 1 5 1 5 10 10 5 1 6 1 6 15 20 15 6 1 p. 8/43

Complexity for calculating C(n,k) (besides the initial C(i,0),C(i,i)). Σ k i=1 Σi 1 j=1 1+Σn i=k+1 Σk j=1 1 = (k 1)(k)/2+k(n k) Θ(nk). p. 9/43

Transitive Closure Intuitively, a relation is transtive if arb and brc implies arc. Think of a directed graph. Then, transitive closure can be used to determine if there is a non-trivial directed path from node i to node j. p. 10/43

Warshall s Algorithm Given adjacency matrix A R k (i,j) denotes if there is a path from i to j which has intermediate vertices only among {1, 2,..., k}. Thus, R 0 (i,j) = A(i,j). R k+1 (i,j) from R k (i,j)? If R k (i,j) = 1, then R k+1 (i,j) = 1 If R k (i,k+1) = 1 and R k (k+1,j) = 1, then R k+1 (i,j) = 1 Thus, we compute R 0,R 1,... one by one. Can consider this as 3D matrix (involving the parameters i, j, k) Takes time Θ(n 3 ). p. 11/43

Warshall s Algorithm Input A[1..n,1..n]. R[0,i,j] = A[i,j]. For k = 1 to n do For i = 1 to n do For j = 1 to n do If R[k 1,i,j] = 1 or (R[k 1,i,k] = 1 and R[k 1,k,j] = 1), then R[k,i,j] 1 Else R[k,i,j] 0 Endif Endfor Endfor Endfor Complexity: Θ(n 3 ). p. 12/43

All pairs shortest path algorithm Suppose A is the adjacency matrix of a weighted graph, that is, A[i,j] gives the weight of the edge (i,j). Here the vertices are numbered 1 to n. Here we assume that A[i,j] is non-negative. One can handle negative weights also as long as there is no negative circuits (because going through negative circuits repeatedly can reduce the weight of the path by arbitrary amount). If edge does not exist then the weight is taken to be. We want to find shortest path between all pairs of vertices. p. 13/43

Floyd s algorithm Idea similar to Warshall s algorithm. D k [i,j] denote the length of the shortest path from i to j where the intermediate vertices (except for the end vertices i and j) are all k. D 0 [i,j] = A[i,j]. Here we assume A[i,i] = 0 for all i. If initially not so, then update A[i,i] to be so. To find, D k [i,j], for 1 k n: D k [i,j] = min(d k 1 [i,j],d k 1 [i,k]+d k 1 [k,j]). p. 14/43

In the algorithm, next[i,j] denotes the vertex which appears just after i in the shortest (known) path from i to j. Note: D k [i,k] = D k 1 [i,k] and D k [k,j] = D k 1 [k,j]. So, we can do the computation in place! (using D itself to compute D 0,D 1,D 2,...). p. 15/43

Floyds algorithm Input A[1..n,1..n] For i = 1 to n do, For j = 1 to n do next[i,j] = j; D[i,j] = A[i,j] EndFor EndFor For i = 1 to n do D[i,i] = 0 EndFor For k = 1 to n do For i = 1 to n For j = 1 to n If D[i,j] > D[i,k]+D[k,j], then D[i,j] = D[i,k]+D[k,j] next[i,j] = next[i,k] Endif EndFor EndFor EndFor p. 16/43

Path(i,j) current = i; Print(i); While current j do current = next(current, j); Print(current) EndWhile p. 17/43

0/1 Knapsack problem Some objects O 1,O 2,...,O n Their weights W 1,W 2,...,W n (assumed to be integral values) Their values V 1,V 2,...,V n Capacity C To find a set S {1,2,...,n} such that Σ i S W i C (capacity constraint) and Σ i S V i is maximsed Note that here we cannot use fractional items! We either take the whole or nothing of each item. p. 18/43

Let F(C,j) denote the maximum value one can obtain using capacity C such that objects chosen are only from O 1,...,O j. Then, F(C,0) = 0. If W j C, then F(C,j) = max(f(c,j 1),F(C W j,j 1)+V j ). If W j > C, then F(C,j) = F(C,j 1). p. 19/43

For s = 0 to C do F(s,0) = 0 EndFor. For s = 1 to C do For j = 1 to n do If W j s, then F(s,j) = max(f(s,j 1),F(s W j,j 1)+V j ); Else F(s,j) = F(s,j 1) Endif EndFor EndFor Complexity: O(C n) p. 20/43

Want to see which objects are chosen For s = 0 to C do F(s,0) = 0;Used(s,0) = false EndFor For s = 1 to C do For j = 1 to n do If W j s, then F(s,j) = max(f(s,j 1),F(s W j,j 1)+V j ); If F(s,j) = F(s,j 1), then Used(s,j) = false Else Used(s,j) = true Endif Else F(s,j) = F(s,j 1); Used(s,j) = false Endif EndFor EndFor p. 21/43

Left = C For j = n down to 1 do { If Used(Left,j) = true, then Print( Pick item j); Left = Left W j ; EndIf EndFor p. 22/43

Coin changing using DP Given some denominations d[1] > d[2] >... > d[n] = 1. To find the minimal number of coins needed to make change for certain amount S. We will give a dynamic programming algorithm which is optimal for all denominations. p. 23/43

Intuition Let C[i,j] denote the number of coins needed to obtain value j, when one is only allowed to use coins d[i],d[i+1],...,d[n]. Then, C[n,j] = j, for 0 j S. Computing: C[i 1,j]: If we use at least one coin of denomination d[i 1]: 1+C[i 1,j d[i 1]] If we do not use any coins of denomination d[i 1]: C[i, j] Taking minimum of above, we get: C[i 1,j] = min(1+c[i 1,j d[i 1]],C[i,j]) p. 24/43

CoinChange For j = 0 to S do C[n,j] = j Endfor For i = n down to 2 do For j = 0 to S do If j d[i 1], then C[i 1,j] = min(1+c[i 1,j d[i 1]],C[i,j]) Else C[i 1,j] = C[i,j] EndFor EndFor Complexity: Θ(S n) p. 25/43

To give coins used: For j = 0 to S do C[n,j] j; used[n,j] true EndFor For i = n down to 2 do For j = 0 to S do If j d[i 1] and 1+C[i 1,j d[i 1]] < C[i,j], then C[i 1,j] 1+C[i 1,j d[i 1]]; used[i 1,j] true Else C[i 1,j] C[i,j]; used[i 1,j] false EndFor EndFor p. 26/43

For i = 1 to n do U[i] 0 EndFor i 1 val S While val 0 do If used(i,val) = true, then U[i] U[i]+1, val val d[i] Else i i+1 EndWhile Now U[i] gives the number of coins with denomination d[i] used. p. 27/43

Matrix Multiplication Suppose we have matrices M j with r j rows and c j columns, for 1 j k, where c j = r j+1, for 1 j < k We want to compute M 1 M 2... M k. Note that matrix multiplication is associative (though not commutative). So whether we do the multplications (for k = 3) as (M 1 M 2 ) M 3 M 1 (M 2 M 3 ) does not matter for final answer. Note that multiplying matrix of size r c with matrix of size c c takes time r c c (for standard method). p. 28/43

Different orders of multiplication can give different time complexity for matrix chain multiplication! If r 1 = 1, c 1 = r 2 = n, c 2 = r 3 = 1 and c 3 = n, then Doing it first way ((M 1 M 2 ) M 3 ) will give complexity r 1 c 1 c 2 +r 1 c 2 c 3 = 2n Doing it second way (M 1 (M 2 M 3 )) will give complexity r 2 c 2 c 3 +r 1 c 1 c 3 = 2n 2 p. 29/43

Given n martices M 1,M 2,...,M n, we want to find M 1 M 2... M n but minimize the number of multiplications (using standard method of multipliation). Let F(i,j) denote the minimum number of operations needed to compute M i M i+1... M j. Then, F(i,j), for i < j, is minimal over k (for i k < j) of F(i,k)+F(k+1,j)+cost(M i... M k,m k+1... M j ) Here cost(m i... M k,m k+1... M j ) = r i c k c j p. 30/43

To compute F(i,j) for 1 i j n, F(i,i) = 0, for 1 i n. F(i,j), for 1 i < j n, by using formula given earlier. Need appropriate order of computing F(i,j) so that we do not duplicate work: what is needed is available at the time of use. Do it in increasing order of j i. p. 31/43

For i = 1 to n do F(i,i) = 0 EndFor For r = 1 to n 1 do 3. For i = 1 to n r do 4. j = i+r. 5. F(i,j) = min j 1 k=i [F(i,k)+F(k +1,j)+r ic k c j ]. EndFor EndFor p. 32/43

Complexity Complexity of finding the optimal order: O(n 3 ) (O(n 2 ) values to be computed, and computation at step 5 takes time O(n)). Note: This is complexity of finding the optimal solution, not the complexity of matrix multiplication! To determine the exact order of multiplication needed, one can do it by keeping track of the k which was used for F(i,j). That will give us the order of multiplication. p. 33/43

Dyn Prog Algorithms in general Define sub problems use optimal solutions for sub problems to give optimal solutions for the larger problem. using optimal solutions to smaller problems, one should be able to determine an optimal solution for a larger problem. Note: We do not just combine solutions for arbitrary subproblems. For example, if we use coin denominations 1, 5 and 50, and find optimal solution for S = 6 and S = 4, then respectively, we will get U 6 [1] = 1, U 6 [5] = 1 and U 4 [1] = 4 respectively. However, just combining them for U 10 will give U 10 [1] = 5 and U 10 [5] = 1, which is not the optimal solution. p. 34/43

So, we use optimal solutions for some specific subprobelms to obtain an optimal solution for the larger problem. The subproblems are often generated by reducing some parts of the original problem ordering among subproblems, so that result of smaller subproblems are available when solving larger subproblem. Optimal substructure Property: We always need that for the optimal solution, the reduced part is optimal for the smaller problem. p. 35/43

Example: If S is optimal solution to coin changing problem for value S, then if remove one coin, with denomination d, from S, then it is optimal solution for S d. Example: Given a weighted graph consider the problem of finding the longest simple path. Then, this does not satisfy the optimal substructure property. If path v 0,v 1,v 2,...,v k is longest simple path from v 0 to v k, then it does not mean that v 0,v 1,...,v k 1 is the longest simple path from v 0 to v k 1! p. 36/43

When do we use dynamic programming? When, the sub-problems are overlapping, dynamic programming allows one to avoid duplication of work as compared to recursion. p. 37/43

Longest Common Subsequence Motivated by problem of whether two proteins are similar. A subsequence of a sequence a[1]a[2]... a[n] is a sequence of the form a[i 1 ]a[i 2 ]...a[i k ] such that 1 i 1 < i 2 <... < i k n. Example: ACAE, AB, BA, AE, E are subsequences of ABCADE. DA is not a subsequence of ABCADE. Longest common subsequence of two sequences ABC and BDC is BC. p. 38/43

Input: x[1]x[2]...x[n] and y[1]y[2]...y[m] Find the longest common subsequence of the above two sequences (strings). If x[n] = y[m], then the longest common subsequence is: (the longest common subsequence of x[1]...x[n 1] and y[1]...y[m 1]) followed by x[n] If x[n] y[m], then the longest common subsequence is: (the longest common subsequence of x[1]... x[n 1] and y[1]...y[m]) or (the longest common subsequence of x[1]... x[n] and y[1]...y[m 1]) p. 39/43

F(i,j) length of the longest common subsequence of x[1]x[2]...x[i] and y[1]y[2]...y[j], where 0 i n and 0 j m Base Case: F[i,j] = 0, for i = 0 or j = 0 Solve the recurrence equation given earlier to obtain other F[i,j]. p. 40/43

LCS(x[1]...x[n],y[1]...y[m]) For i = 0 to n do F(i,0) = 0 EndFor For j = 0 to m do F(0,j) = 0 EndFor For i = 1 to n For j = 1 to m If x[i] = y[j], then F(i,j) = 1+F(i 1,j 1) Endif If x[i] y[j], then F(i,j) = max(f(i 1,j),F(i,j 1)) Endif EndFor EndFor End Complexity: Θ(mn) p. 41/43

LCSP(x[1]...x[n],n,m,F) If F(n, m) = 0, then return. ElseIf F(n,m) = F(n,m 1), then return LCSP(x,n,m 1,F) ElseIf F(n,m) = F(n 1,m), then return LCSP(x,n 1,m,F) Else return LCSP(x,n 1,m 1,F) x(n) End p. 42/43

The main idea of dynamic programming is: Solve several smaller problems Store these solutions (in an array) and use them to solve large problems Have a recurrence; solve in particular order Subproblems have overlaps (i.e., some of the subsubproblems they solve are same) In Divide an conquer usually do not have overlaps, and do not store the solutions. p. 43/43