Chapter 8 Dynamic Programming

Similar documents
Chapter 8 Dynamic Programming

Dynamic Programming. p. 1/43

All-Pairs Shortest Paths

6. DYNAMIC PROGRAMMING I

Partha Sarathi Mandal

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

CS Analysis of Recursive Algorithms and Brute Force

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

Analysis of Algorithms I: All-Pairs Shortest Paths

Lecture 7: Dynamic Programming I: Optimal BSTs

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

Single Source Shortest Paths

Greedy Algorithms My T. UF

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Lecture 2: Divide and conquer and Dynamic programming

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

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

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

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

Dynamic Programming: Shortest Paths and DFA to Reg Exps

CS 241 Analysis of Algorithms

Max-plus algebra. Max-plus algebra. Monika Molnárová. Technická univerzita Košice. Max-plus algebra.

Design and Analysis of Algorithms

Dynamic Programming. Cormen et. al. IV 15

Courses 12-13: Dynamic programming

(tree searching technique) (Boolean formulas) satisfying assignment: (X 1, X 2 )

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

Dynamic Programming (CLRS )

Combinatorial optimization problems

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

IS 2610: Data Structures

Dynamic Programming: Matrix chain multiplication (CLRS 15.2)

Dynamic Programming: Interval Scheduling and Knapsack

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

CS473 - Algorithms I

Dynamic Programming: Shortest Paths and DFA to Reg Exps

CSE 202 Dynamic Programming II

Question Paper Code :

Dynamic Programming. Prof. S.J. Soni

6. DYNAMIC PROGRAMMING I

Dynamic Programming( Weighted Interval Scheduling)

CS483 Design and Analysis of Algorithms

1 Assembly Line Scheduling in Manufacturing Sector

Relations Graphical View

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

CSE 421 Dynamic Programming

Algorithms Design & Analysis. Dynamic Programming

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

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

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

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

1 Review of Vertex Cover

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.

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

CHAPTER 1. Relations. 1. Relations and Their Properties. Discussion

CS173 Lecture B, November 3, 2015

Algorithm Design Strategies V

Advanced Counting Techniques. Chapter 8

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

General Methods for Algorithm Design

CS 350 Algorithms and Complexity

CS 350 Algorithms and Complexity

Shortest paths with negative lengths

Lecture 13: Dynamic Programming Part 2 10:00 AM, Feb 23, 2018

Discrete Structures May 13, (40 points) Define each of the following terms:

Fast Inference with Min-Sum Matrix Product

Mat 3770 Bin Packing or

CS481: Bioinformatics Algorithms

Technische Universität München, Zentrum Mathematik Lehrstuhl für Angewandte Geometrie und Diskrete Mathematik. Combinatorial Optimization (MA 4502)

Copyright 2000, Kevin Wayne 1

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.

Chapter 9: Relations Relations

Algorithms and Theory of Computation. Lecture 9: Dynamic Programming

Algorithm efficiency analysis

CS 580: Algorithm Design and Analysis

Activity selection. Goal: Select the largest possible set of nonoverlapping (mutually compatible) activities.

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

CS60007 Algorithm Design and Analysis 2018 Assignment 1

Lecture #8: We now take up the concept of dynamic programming again using examples.

CSE 421 Weighted Interval Scheduling, Knapsack, RNA Secondary Structure

University of the Virgin Islands, St. Thomas January 14, 2015 Algorithms and Programming for High Schoolers. Lecture 5

Unit 1A: Computational Complexity

Graduate Algorithms CS F-09 Dynamic Programming

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Dynamic Programming II Date: 10/12/17

CS Data Structures and Algorithm Analysis

Discrete (and Continuous) Optimization WI4 131

Chapter 7. Network Flow. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

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

Dynamic Programming ACM Seminar in Algorithmics

CPS 616 DIVIDE-AND-CONQUER 6-1

CS Algorithms and Complexity

Lecture 9. Greedy Algorithm

Examination paper for TDT4120 Algorithms and Data Structures

MATTHIAS GERDTS. COMBINATORIAL OPTIMISATION MSM 3M02b

Automata and Formal Languages - CM0081 Finite Automata and Regular Expressions

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

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

Outline. Computer Science 331. Cost of Binary Search Tree Operations. Bounds on Height: Worst- and Average-Case

Transcription:

Chapter 8 Dynamic Programming Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems Invented by American mathematician Richard Bellman in the 1950s to solve optimization problems and later assimilated by CS Programming here means planning Main idea: - set up a recurrence relating a solution to a larger instance to solutions of some smaller instances - solve smaller instances once - record solutions in a table - extract solution to the initial instance from that table 8-1

Example: Fibonacci numbers Recall definition of Fibonacci numbers: F(n) = F(n-1) + F(n-2) F(0) = 0 F(1) = 1 Computing the n th Fibonacci number recursively (top-down): F(n) F(n-1) + F(n-2) F(n-2) + F(n-3) F(n-3) + F(n-4)... 8-2

Example: Fibonacci numbers (cont.) Computing the n th Fibonacci number using bottom-up iteration and recording results: F(0) = 0 F(1) = 1 F(2) = 1+0 = 1 F(n-2) = F(n-1) = F(n) = F(n-1) + F(n-2) Efficiency: - time -space 0 1 1... F(n-2) F(n-1) F(n) 8-3

Examples of DP algorithms Computing a binomial coefficient Warshall s algorithm for transitive closure Floyd s algorithm for all-pairs shortest paths Constructing an optimal binary search tree Some instances of difficult discrete optimization problems: - traveling salesman - knapsack 8-4

Computing a binomial coefficient by DP Binomial coefficients are coefficients of the binomial formula: (a + b) n = C(n,0)a n b 0 +... + C(n,k)a n-k b k +... + C(n,n)a 0 b n Recurrence: C(n,k) = C(n-1,k) + C(n-1,k-1) for n > k > 0 C(n,0) = 1, C(n,n) = 1 for n 0 Value of C(n,k) can be computed by filling a table: 0 1 2... k-1 k 0 1 1 1 1... n-1 C(n-1,k-1) C(n-1,k) n C(n,k) 8-5

Computing C(n,k): pseudocode and analysis Time efficiency: Θ(nk) Space efficiency: Θ(nk) 8-6

Warshall s Algorithm: Transitive Closure Computes the transitive closure of a relation Alternatively: existence of all nontrivial paths in a digraph Example of transitive closure: 1 3 1 3 2 4 0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0 2 4 0 0 1 0 1 1 1 1 0 0 0 0 1 1 1 1 8-7

1 Warshall s Algorithm Constructs transitive closure T as the last matrix in the sequence of n-by-n matrices R (0),, R (k),, R (n) where R (k) [i,j] = 1 iff there is nontrivial path from i to j with only first k vertices allowed as intermediate Note that R (0) = A (adjacency matrix), R (n) = T (transitive closure) 3 1 3 1 3 1 3 1 3 2 4 2 4 2 4 2 4 2 4 R (0) 0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0 R (1) 0 0 1 0 1 0 1 1 0 0 0 0 0 1 0 0 R (2) 0 0 1 0 1 0 1 1 0 0 0 0 1 1 1 1 R (3) 0 0 1 0 1 0 1 1 0 0 0 0 1 1 1 1 R (4) 0 0 1 0 1 1 1 1 0 0 0 0 1 1 1 1 8-8

Warshall s Algorithm (recurrence) On the k-th iteration, the algorithm determines for every pair of vertices i, j if a path exists from i and j with just vertices 1,,k allowed as intermediate { R (k-1) [i,j] (path using just 1,,k-1) R (k) [i,j] = or R (k-1) [i,k] and R (k-1) [k,j] (path from i to k k and from k to i using just 1,,k-1) i j 8-9

Warshall s Algorithm (matrix generation) Recurrence relating elements R (k) to elements of R (k-1) is: R (k) [i,j] = R (k-1) [i,j] or (R (k-1) [i,k] and R (k-1) [k,j]) It implies the following rules for generating R (k) from R (k-1) : Rule 1 If an element in row i and column j is 1 in R (k-1), it remains 1 in R (k) Rule 2 If an element in row i and column j is 0 in R (k-1), it has to be changed to 1 in R (k) if and only if the element in its row i and column k and the element in its column j and row k are both 1 s in R (k-1) 8-10

Warshall s Algorithm (example) 3 1 0 0 1 0 1 0 0 1 R (0) = 0 0 0 0 2 4 0 1 0 0 R (1) = 0 0 1 0 1 0 1 1 0 0 0 0 0 1 0 0 R (2) = 0 0 1 0 1 0 1 1 0 0 0 0 1 1 1 1 R (3) = 0 0 1 0 1 0 1 1 0 0 0 0 1 1 1 1 R (4) = 0 0 1 0 1 1 1 1 0 0 0 0 1 1 1 1 8-11

Warshall s Algorithm (pseudocode and analysis) Time efficiency: Θ(n 3 ) Space efficiency: Matrices can be written over their predecessors 8-12

Floyd s Algorithm: All pairs shortest paths Problem: In a weighted (di)graph, find shortest paths between every pair of vertices Same idea: construct solution through series of matrices D (0),, D (n) using increasing subsets of the vertices allowed as intermediate Example: 4 3 1 6 1 5 2 3 1 4 8-13

Floyd s Algorithm (matrix generation) On the k-th iteration, the algorithm determines shortest paths between every pair of vertices i, j that use only vertices among 1,,k as intermediate D (k) [i,j] = min {D (k-1) [i,j], D (k-1) [i,k] + D (k-1) [k,j]} i D (k-1) [i,j] D (k-1) [i,k] j k D (k-1) [k,j] 8-14

Floyd s Algorithm (example) 3 3 2 1 2 6 7 1 4 D (0) = 0 3 2 0 7 0 1 6 0 D (1) = 0 3 2 0 5 7 0 1 6 9 0 D (2) = 0 3 2 0 5 9 7 0 1 6 9 0 D (3) = 0 10 3 4 2 0 5 6 9 7 0 1 6 16 9 0 D (4) = 0 10 3 4 2 0 5 6 7 7 0 1 6 16 9 0 8-15

Floyd s Algorithm (pseudocode and analysis) Time efficiency: Θ(n 3 ) Space efficiency: Matrices can be written over their predecessors Note: Shortest paths themselves can be found, too 8-16

Optimal Binary Search Trees Problem: Given n keys a 1 < < a n and probabilities p 1 p n searching for them, find a BST with a minimum average number of comparisons in successful search. Since total number of BSTs with n nodes is given by C(2n,n)/(n+1), which grows exponentially, brute force is hopeless. Example: What is an optimal BST for keys A, B, C, and D with search probabilities 0.1, 0.2, 0.4, and 0.3, respectively? 8-17

DP for Optimal BST Problem Let C[i,j] be minimum average number of comparisons made in T[i,j], optimal BST for keys a i < < a j,where 1 i j n. Consider optimal BST among all BSTs with some a k (i k j ) as their root; T[i,j] is the best among them. a k C[i,j] = min {p k 1 + i k j Optimal BST for a,..., a i Optimal BST for a,..., a k-1 k+1 j p s (level a s in T[i,k-1] +1) + p s (level a s in T[k+1,j] +1)} k-1 s = i j s =k+1 8-18

DP for Optimal BST Problem (cont.) After simplifications, we obtain the recurrence for C[i,j]: C[i,j] = min {C[i,k-1] + C[k+1,j]} + p s i k j C[i,i] = p i for 1 i j n 0 1 j n j s = i for 1 i j n 1 0 p 1 goal 0 p 2 i C[i,j] p n n+1 0 8-19

Example: key A B C D probability 0.1 0.2 0.4 0.3 The tables below are filled diagonal by diagonal: the left one is filled using the recurrence j C[i,j] = min {C[i,k-1] + C[k+1,j]} + p s, C[i,i] = p i ; the right one, for trees roots, records k s values giving the minima i j i k j 0 1 2 3 4 1 0.1.4 1.1 1.7 2 0.2.8 1.4 i j 0 1 2 3 4 s = i 1 1 2 3 3 2 2 3 3 B C D 3 0.4 1.0 4 0.3 5 0 3 3 3 4 4 5 A optimal BST

Optimal Binary Search Trees 8-21

Analysis DP for Optimal BST Problem Time efficiency: Θ(n 3 ) but can be reduced to Θ(n 2 ) by taking advantage of monotonicity of entries in the root table, i.e., R[i,j] is always in the range between R[i,j-1] and R[i+1,j] Space efficiency: Θ(n 2 ) Method can be expended to include unsuccessful searches 8-22

Knapsack Problem by DP Given n items of integer weights: w 1 w 2 w n values: v 1 v 2 v n a knapsack of integer capacity W find most valuable subset of the items that fit into the knapsack Consider instance defined by first i items and capacity j (j W). Let V[i,j] be optimal value of such instance. Then max {V[i-1,j], v i + V[i-1,j- w i ]} if j- w i 0 V[i,j] = V[i-1,j] if j- w i < 0 Initial conditions: V[0,j] = 0 and V[i,0] = 0 8-23

Visualizing this Relationship V[i,j] = max {V[i-1,j], v i + V[i-1,j- w i ]} if j- w i 0 V[i-1,j] if j- w i < 0 So we can build up the table, left to right by repeatedly applying the result of this expression The initial conditions are shown by the first row and first column with 0 values 8-24

Knapsack Problem by DP (example) Example: Knapsack of capacity W = 5 item weight value 1 2 $12 We know the solution is 37; the 2 1 $10 next question is how we find 3 3 $20 the items actually involved. 4 2 $15 w 1 = 2, v 1 = 12 w 2 = 1, v 2 = 10 w 3 = 3, v 3 = 20 w 4 = 2, v 4 = 15 8-25

Top Down Approach with Memorization We can use a top down approach by storing all results When we need a value we first ask have we stored it 8-26

Example of Memorization Notice that not all values need to be calculated Only eleven out of twenty of the nontrivial values (the zeros) need to be computed 8-27