Dynamic Programming Code

Slides:



Advertisements
Similar presentations
Introduction to Algorithms 6.046J/18.401J/SMA5503
Advertisements

Multiplying Matrices Two matrices, A with (p x q) matrix and B with (q x r) matrix, can be multiplied to get C with dimensions p x r, using scalar multiplications.
Lecture 8: Dynamic Programming Shang-Hua Teng. Longest Common Subsequence Biologists need to measure how similar strands of DNA are to determine how closely.
15.Dynamic Programming Hsu, Lih-Hsing. Computer Theory Lab. Chapter 15P.2 Dynamic programming Dynamic programming is typically applied to optimization.
Unit 4: Dynamic Programming
CS 691 Computational Photography Instructor: Gianfranco Doretto Cutting Images.
Comp 122, Fall 2004 Dynamic Programming. dynprog - 2 Lin / Devi Comp 122, Spring 2004 Longest Common Subsequence  Problem: Given 2 sequences, X =  x.
Chapter 15 Dynamic Programming Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from.
1 Dynamic Programming (DP) Like divide-and-conquer, solve problem by combining the solutions to sub-problems. Differences between divide-and-conquer and.
Dynamic Programming (pro-gram)
Dynamic Programming Lecture 9 Asst. Prof. Dr. İlker Kocabaş 1.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2006 Lecture 1 (Part 3) Design Patterns for Optimization Problems.
Dynamic Programming Reading Material: Chapter 7..
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2009 Design Patterns for Optimization Problems Dynamic Programming.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Dynamic Programming - 1 Dynamic.
Dynamic Programming Optimization Problems Dynamic Programming Paradigm
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
1 Dynamic Programming Andreas Klappenecker [based on slides by Prof. Welch]
UNC Chapel Hill Lin/Manocha/Foskey Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject.
Analysis of Algorithms CS 477/677
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
Lecture 8: Dynamic Programming Shang-Hua Teng. First Example: n choose k Many combinatorial problems require the calculation of the binomial coefficient.
Analysis of Algorithms
11-1 Matrix-chain Multiplication Suppose we have a sequence or chain A 1, A 2, …, A n of n matrices to be multiplied –That is, we want to compute the product.
Lecture 7 Topics Dynamic Programming
Longest Common Subsequence
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems.
First Ingredient of Dynamic Programming
Dynamic Programming – Part 2 Introduction to Algorithms Dynamic Programming – Part 2 CSE 680 Prof. Roger Crawfis.
Algorithms and Data Structures Lecture X
Dynamic Programming Dynamic programming is a technique for solving problems with a recursive structure with the following characteristics: 1.optimal substructure.
Dynamic Programming UNC Chapel Hill Z. Guo.
October 21, Algorithms and Data Structures Lecture X Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CS 5243: Algorithms Dynamic Programming Dynamic Programming is applicable when sub-problems are dependent! In the case of Divide and Conquer they are.
COSC 3101A - Design and Analysis of Algorithms 7 Dynamic Programming Assembly-Line Scheduling Matrix-Chain Multiplication Elements of DP Many of these.
CS 8833 Algorithms Algorithms Dynamic Programming.
Dynamic Programming (Ch. 15) Not a specific algorithm, but a technique (like divide- and-conquer). Developed back in the day when “programming” meant “tabular.
COSC 3101A - Design and Analysis of Algorithms 8 Elements of DP Memoization Longest Common Subsequence Greedy Algorithms Many of these slides are taken.
Dynamic Programming Greed is not always good.. Jaruloj Chongstitvatana Design and Analysis of Algorithm2 Outline Elements of dynamic programming.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 4: Dynamic Programming Phan Th ị Hà D ươ ng 1.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 14.
Algorithmics - Lecture 121 LECTURE 11: Dynamic programming - II -
Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject to some constraints. (There may.
15.Dynamic Programming. Computer Theory Lab. Chapter 15P.2 Dynamic programming Dynamic programming is typically applied to optimization problems. In such.
Chapter 7 Dynamic Programming 7.1 Introduction 7.2 The Longest Common Subsequence Problem 7.3 Matrix Chain Multiplication 7.4 The dynamic Programming Paradigm.
Dynamic Programming 又稱 動態規劃 經常被用來解決最佳化問題. Dynamic Programming2 Rod cutting problem Given a rod of length N inches and a table of prices p[i] for i=1..N,
Chapter 15 Dynamic Programming Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from.
TU/e Algorithms (2IL15) – Lecture 4 1 DYNAMIC PROGRAMMING II
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Seminar on Dynamic Programming.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Dynamic Programming Several problems Principle of dynamic programming
Chapter 8 Dynamic Programming.
Dynamic Programming Comp 122, Fall 2004.
Dynamic Programming.
Data Structure and Algorithms
Dynamic Programming Comp 122, Fall 2004.
Lecture 8. Paradigm #6 Dynamic Programming
Algorithms CSCI 235, Spring 2019 Lecture 28 Dynamic Programming III
Dynamic Programming-- Longest Common Subsequence
Introduction to Algorithms: Dynamic Programming
Dynamic Programming II DP over Intervals
Algorithms and Data Structures Lecture X
Analysis of Algorithms CS 477/677
Matrix Chain Multiplication
Algorithms CSCI 235, Spring 2019 Lecture 27 Dynamic Programming II
Seminar on Dynamic Programming.
Data Structures and Algorithms Dynamic Programming
Presentation transcript:

Dynamic Programming Code

Straightforward top-down rod cutting CUT-ROD(p, n) If n == 0 Return 0 q = -1 For I = 1 to n q = max (q, p[i] + CUT-ROD(p, n-i)) Return q

Memoization in rod cutting MEMOIZED-CUT-ROD(p, n) let r[0..n] be a new array for I = 0 to n r[i] = -1 return MEMOIZED-CUT-ROD-AUX(p, n, r) MEMOIZED-CUT-ROD-AUX (p, n, r) if r[n] >= 0 return r[n] if n == 0 q = 0 else q = -1 For I = 1 to n q = max (q, p[i] + MEMOIZED-CUT-ROD-AUX(p, n-i, r)) r[n] = q return q

Bottom up memoization BOTTOM-UP-CUT-ROD(p, n) let r[0..n] be a new array r[0] = 0 for j = 1 to n q = -1 for i = 1 to j q = max (q, p[i] + r[j – i]) r[j] = q return r[n] How do we know where to make the cuts?

Bottom up memoization EXTENDED-BOTTOM-UP-CUT-ROD(p, n) let r[0..n] and s[0..n] be a new array r[0] = 0 for j = 1 to n q = -1 for i = 1 to j if q < p[i] + r[j – 1] q = p[i] + r[j – 1] s[j] = i r[j] = q return r[n] How do you print the solution?

Bottom up memoization EXTENDED-BOTTOM-UP-CUT-ROD(p, n) let r[0..n] and s[0..n] be a new array r[0] = 0 for j = 1 to n q = -1 for i = 1 to j if q < p[i] + r[j – 1] q = p[i] + r[j – 1] s[j] = i r[j] = q return r[n] PRINT-CUT-ROD-SOLUTION(p, n) (r, s) = EXT-BUCR(p, n) while n > 0 print s[n] n = n – s[n]

LCS Theorem Let Z = z1, . . . , zk be any LCS of X and Y . 1. If xm = yn, then zk = xm = yn and Zk-1 is an LCS of Xm-1 and Yn-1. 2. If xm  yn, then either zk  xm and Z is an LCS of Xm-1 and Y . 3. or zk  yn and Z is an LCS of X and Yn-1.

LCS-LENGTH (X, Y) m ← length[X] n ← length[Y] for i ← 1 to m do c[i, 0] ← 0 for j ← 0 to n do c[0, j ] ← 0 do for j ← 1 to n do if xi = yj then c[i, j ] ← c[i1, j1] + 1 else if c[i1, j ] ≥ c[i, j1] then c[i, j ] ← c[i 1, j ] else c[i, j ] ← c[i, j1] return c

LCS-LENGTH (X, Y) m ← length[X] n ← length[Y] for i ← 1 to m do c[i, 0] ← 0 for j ← 0 to n do c[0, j ] ← 0 do for j ← 1 to n do if xi = yj then c[i, j ] ← c[i1, j1] + 1 b[i, j ] ← “ ” else if c[i1, j ] ≥ c[i, j1] then c[i, j ] ← c[i 1, j ] b[i, j ] ← “↑” else c[i, j ] ← c[i, j1] b[i, j ] ← “←” return c and b

PRINT-LCS (b, X, i, j) if i = 0 or j = 0 then return if b[i, j ] = “ ” then PRINT-LCS(b, X, i1, j1) print xi elseif b[i, j ] = “↑” then PRINT-LCS(b, X, i1, j) else PRINT-LCS(b, X, i, j1)

Matrix-chain Multiplication coitweb.uncc.edu/~ras/courses/Matrix-Mult.ppt Suppose we have a sequence or chain A1, A2, …, An of n matrices to be multiplied That is, we want to compute the product A1A2…An There are many possible ways (parenthesizations) to compute the product

Matrix-chain Multiplication …contd Example: consider the chain A1, A2, A3, A4 of 4 matrices Let us compute the product A1A2A3A4 There are 5 possible ways: (A1(A2(A3A4))) (A1((A2A3)A4)) ((A1A2)(A3A4)) ((A1(A2A3))A4) (((A1A2)A3)A4)

Matrix-chain Multiplication …contd To compute the number of scalar multiplications necessary, we must know: Algorithm to multiply two matrices Matrix dimensions Can you write the algorithm to multiply two matrices?

Algorithm to Multiply 2 Matrices Input: Matrices Ap×q and Bq×r (with dimensions p×q and q×r) Result: Matrix Cp×r resulting from the product A·B MATRIX-MULTIPLY(Ap×q , Bq×r) 1. for i ← 1 to p 2. for j ← 1 to r 3. C[i, j] ← 0 4. for k ← 1 to q 5. C[i, j] ← C[i, j] + A[i, k] · B[k, j] 6. return C Scalar multiplication in line 5 dominates time to compute CNumber of scalar multiplications = pqr

Matrix-chain Multiplication …contd Example: Consider three matrices A10100, B1005, and C550 There are 2 ways to parenthesize ((AB)C) = D105 · C550 AB  10·100·5=5,000 scalar multiplications DC  10·5·50 =2,500 scalar multiplications (A(BC)) = A10100 · E10050 BC  100·5·50=25,000 scalar multiplications AE  10·100·50 =50,000 scalar multiplications Total: 7,500 Total: 75,000

Matrix-chain Multiplication …contd Matrix-chain multiplication problem Given a chain A1, A2, …, An of n matrices, where for i=1, 2, …, n, matrix Ai has dimension pi-1pi Parenthesize the product A1A2…An such that the total number of scalar multiplications is minimized Brute force method of exhaustive search takes time exponential in n

Dynamic Programming Approach The structure of an optimal solution Let us use the notation Ai..j for the matrix that results from the product Ai Ai+1 … Aj An optimal parenthesization of the product A1A2…An splits the product between Ak and Ak+1 for some integer k where1 ≤ k < n First compute matrices A1..k and Ak+1..n ; then multiply them to get the final matrix A1..n

Dynamic Programming Approach …contd Key observation: parenthesizations of the subchains A1A2…Ak and Ak+1Ak+2…An must also be optimal if the parenthesization of the chain A1A2…An is optimal (why?) That is, the optimal solution to the problem contains within it the optimal solution to subproblems

Dynamic Programming Approach …contd Recursive definition of the value of an optimal solution Let m[i, j] be the minimum number of scalar multiplications necessary to compute Ai..j Minimum cost to compute A1..n is m[1, n] Suppose the optimal parenthesization of Ai..j splits the product between Ak and Ak+1 for some integer k where i ≤ k < j

Dynamic Programming Approach …contd Ai..j = (Ai Ai+1…Ak)·(Ak+1Ak+2…Aj)= Ai..k · Ak+1..j Cost of computing Ai..j = cost of computing Ai..k + cost of computing Ak+1..j + cost of multiplying Ai..k and Ak+1..j Cost of multiplying Ai..k and Ak+1..j is pi-1pk pj m[i, j ] = m[i, k] + m[k+1, j ] + pi-1pk pj for i ≤ k < j m[i, i ] = 0 for i=1,2,…,n

Dynamic Programming Approach …contd But… optimal parenthesization occurs at one value of k among all possible i ≤ k < j Check all these and select the best one 0 if i=j m[i, j ] = min {m[i, k] + m[k+1, j ] + pi-1pk pj } if i<j i ≤ k< j

Dynamic Programming Approach …contd To keep track of how to construct an optimal solution, we use a table s s[i, j ] = value of k at which Ai Ai+1 … Aj is split for optimal parenthesization Algorithm: next slide First computes costs for chains of length l=1 Then for chains of length l=2,3, … and so on Computes the optimal cost bottom-up

Algorithm to Compute Optimal Cost Input: Array p[0…n] containing matrix dimensions and n Result: Minimum-cost table m and split table s MATRIX-CHAIN-ORDER(p[ ], n) for i ← 1 to n m[i, i] ← 0 for l ← 2 to n for i ← 1 to n-l+1 j ← i+l-1 m[i, j] ←  for k ← i to j-1 q ← m[i, k] + m[k+1, j] + p[i-1] p[k] p[j] if q < m[i, j] m[i, j] ← q s[i, j] ← k return m and s Takes O(n3) time Requires O(n2) space

Constructing Optimal Solution Our algorithm computes the minimum-cost table m and the split table s The optimal solution can be constructed from the split table s Each entry s[i, j ]=k shows where to split the product Ai Ai+1 … Aj for the minimum cost

Example Show how to multiply this matrix chain optimally Solution on the board Minimum cost 15,125 Optimal parenthesization ((A1(A2A3))((A4 A5)A6)) Matrix Dimension A1 30×35 A2 35×15 A3 15×5 A4 5×10 A5 10×20 A6 20×25

LPS (problem set 4) A palindrome is a nonempty string over some alphabet that reads the same forward and backward. Examples of palilndromes are all strings of length 1, "civic", "racecar", and "aibohphobia" (fear of palindromes). Give an efficient algorithm to find the longest palindrome that is a subsequence of a given input string. For example, given the input "character", your algorithm should return "carac". What is the running time of your algorithm?