Dynamic Programming. CIS 110, Fall University of Pennsylvania

Similar documents
CS50 Dynamic Programming

Module 27: Chained Matrix Multiplication and Bellman-Ford Shortest Path Algorithm

1 More on the Bellman-Ford Algorithm

Dynamic Programming II

We augment RBTs to support operations on dynamic sets of intervals A closed interval is an ordered pair of real

1 Dynamic Programming

15-451/651: Design & Analysis of Algorithms January 26, 2015 Dynamic Programming I last changed: January 28, 2015

Algorithmic Paradigms. Chapter 6 Dynamic Programming. Steps in Dynamic Programming. Dynamic Programming. Dynamic Programming Applications

Dynamic Programming Group Exercises

Dynamic Programming. Lecture Overview Introduction

Dynamic Programming (Part #2)

Dynamic Programming Matrix-chain Multiplication

Algorithms: COMP3121/3821/9101/9801

CS141: Intermediate Data Structures and Algorithms Dynamic Programming

Lecture 8. Dynamic Programming

1 i n (p i + r n i ) (Note that by allowing i to be n, we handle the case where the rod is not cut at all.)

Dynamic Programming 1

15.Dynamic Programming

Unit-5 Dynamic Programming 2016

10/24/ Rotations. 2. // s left subtree s right subtree 3. if // link s parent to elseif == else 11. // put x on s left

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Introduction to Algorithms

CS 231: Algorithmic Problem Solving

Greedy Algorithms. Informal Definition A greedy algorithm makes its next step based only on the current state and simple calculations on the input.

Chapter 3 Dynamic programming

Video 9.1 Jianbo Shi. Property of Penn Engineering, Jianbo Shi

Algorithms IV. Dynamic Programming. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Dynamic Programming

Dynamic Programming II

Chapter 6. Dynamic Programming

1 Non greedy algorithms (which we should have covered

Dynamic Programming. Design and Analysis of Algorithms. Entwurf und Analyse von Algorithmen. Irene Parada. Design and Analysis of Algorithms

Algorithm Design and Analysis

Lecture 10. Sequence alignments

Computer Sciences Department 1

Indexing and Searching

Dynamic Programming. An Enumeration Approach. Matrix Chain-Products. Matrix Chain-Products (not in book)

Data Structures and Algorithms Week 8

Elements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples

False Color to NDVI Conversion Precision NDVI Single Sensor

Algorithms. Algorithms ALGORITHM DESIGN

Algorithms. Ch.15 Dynamic Programming

Introduction to Algorithms

Lecture 4: Dynamic programming I

15.4 Longest common subsequence

Chapter 3: Search. c D. Poole, A. Mackworth 2010, W. Menzel 2015 Artificial Intelligence, Chapter 3, Page 1

Last week: Breadth-First Search

Lectures 12 and 13 Dynamic programming: weighted interval scheduling

Dynamic Programming: 1D Optimization. Dynamic Programming: 2D Optimization. Fibonacci Sequence. Crazy 8 s. Edit Distance

Dynamic Programming & Smith-Waterman algorithm

/463 Algorithms - Fall 2013 Solution to Assignment 3

CMSC351 - Fall 2014, Homework #4

Computational Molecular Biology

CS473-Algorithms I. Lecture 10. Dynamic Programming. Cevdet Aykanat - Bilkent University Computer Engineering Department

Dynamic programming 4/9/18

Dynamic Programming Intro

Unit 1, Lesson 1: Moving in the Plane

CSE 421: Introduction to Algorithms

CS 380 ALGORITHM DESIGN AND ANALYSIS

Dynamic Programming. December 15, CMPE 250 Dynamic Programming December 15, / 60

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech

Chain Matrix Multiplication

EECS 4425: Introductory Computational Bioinformatics Fall Suprakash Datta

CSE 417 Dynamic Programming (pt 4) Sub-problems on Trees

Optimization II: Dynamic Programming

Dynamic Programming. Ellen Feldman and Avishek Dutta. February 27, CS155 Machine Learning and Data Mining

Introduction to Optimization

Dynamic Programming part 2

CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms

CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2

Dynamic Programming Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Sequence Alignment. part 2

Algorithm Design Techniques part I

Computational Genomics and Molecular Biology, Fall

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors

The Knapsack Problem an Introduction to Dynamic Programming. Slides based on Kevin Wayne / Pearson-Addison Wesley

Dynamic Programming. Outline and Reading. Computing Fibonacci

Outline. Sequence Alignment. Types of Sequence Alignment. Genomics & Computational Biology. Section 2. How Computers Store Information

Recursive-Fib(n) if n=1 or n=2 then return 1 else return Recursive-Fib(n-1)+Recursive-Fib(n-2)

Memoization/Dynamic Programming. The String reconstruction problem. CS124 Lecture 11 Spring 2018

CSE 3101 Design and Analysis of Algorithms Practice Test for Unit 1 Loop Invariants and Iterative Algorithms

Partha Sarathi Manal

15.4 Longest common subsequence

S Postgraduate Course on Signal Processing in Communications, FALL Topic: Iteration Bound. Harri Mäntylä

.. Fall 2011 CSC 570: Bioinformatics Alexander Dekhtyar..

Computational Biology Lecture 4: Overlap detection, Local Alignment, Space Efficient Needleman-Wunsch Saad Mneimneh

technique: seam carving Image and Video Processing Chapter 9

Lecture 57 Dynamic Programming. (Refer Slide Time: 00:31)

6.001 Notes: Section 4.1

Advanced Algorithms Class Notes for Monday, November 10, 2014

More Dynamic Programming

Richard Feynman, Lectures on Computation

Today: Matrix Subarray (Divide & Conquer) Intro to Dynamic Programming (Rod cutting) COSC 581, Algorithms January 21, 2014

1 Dynamic Programming

The Further Mathematics Support Programme

Dynamic Programming. Nothing to do with dynamic and nothing to do with programming.

Matrix Multiplication Studio April 20, 2007

1. Stereo Correspondence. (100 points)

Shortest Path Problem

Transcription:

Dynamic Programming CIS 110, Fall 2012 University of Pennsylvania

Dynamic Programming Dynamic programming records saves computation for reuse later. Programming: in the optimization sense ( Linear Programming ) Dynamic:... it s impossible to use [it] in a pejorative way. (Richard Bellman) he name was designed to sound cool to RND management and the US Department of Defense more descriptive term is look-up table Richard Bellman 2 CIS 110, Fall 2012

Dynamic Programming Dynamic programming records saves computation for reuse later. Programming: in the optimization sense ( Linear Programming ) Dynamic:... it s impossible to use [it] in a pejorative way. (Richard Bellman) he name was designed to sound cool to RND management and the US Department of Defense more descriptive term is look-up table When is dynamic programming useful? Exponential number of solutions Cost of soluion is recursively computed Different solutions recursively compute same values Richard Bellman 2 CIS 110, Fall 2012

Dynamic Programming Dynamic programming records saves computation for reuse later. Programming: in the optimization sense ( Linear Programming ) Dynamic:... it s impossible to use [it] in a pejorative way. (Richard Bellman) he name was designed to sound cool to RND management and the US Department of Defense more descriptive term is look-up table When is dynamic programming useful? Exponential number of solutions Cost of soluion is recursively computed Different solutions recursively compute same values he trick is exposing the common subproblems Richard Bellman 2 CIS 110, Fall 2012

Rod Cutting Start with a rod of integer length n...... and cut it into several smaller pieces (of integer length). 3 CIS 110, Fall 2012

Rod Cutting Start with a rod of integer length n...... and cut it into several smaller pieces (of integer length). Now suppose each length has a different value: = 1 = 5 = 8 = 9 1 1 5 5 9 = 1 + 1 + 5 = 5 + 9 = 21 5 5 5 5 5 = 5 + 5 + 5 = 5 + 5 = 25 3 CIS 110, Fall 2012

Rod Cutting Start with a rod of integer length n...... and cut it into several smaller pieces (of integer length). Now suppose each length has a different value: = 1 = 5 = 8 = 9 1 1 5 5 9 = 1 + 1 + 5 = 5 + 9 = 21 5 5 5 5 5 = 5 + 5 + 5 = 5 + 5 = 25 How should we cut the rod into pieces? 2 n 1 possibilities for a rod of length n 3 CIS 110, Fall 2012

Rod Cutting First rod-cutting strategy (brute-force): For every possible cut, compute the value of the left part plus the value of optimally cutting the right part. ake the best cut. 4 CIS 110, Fall 2012

Rod Cutting First rod-cutting strategy (brute-force): For every possible cut, compute the value of the left part plus the value of optimally cutting the right part. ake the best cut. } {{ } Cut recursively } {{ } Cut recursively } {{ } Cut recursively } {{ } Cut recursively }{{} Cut recursively. 4 CIS 110, Fall 2012

Rod Cutting First rod-cutting strategy (brute-force): For every possible cut, compute the value of the left part plus the value of optimally cutting the right part. ake the best cut. } {{ } Cut recursively } {{ } Cut recursively Exponential number of recursive calls! 4 CIS 110, Fall 2012 } {{ } Cut recursively } {{ } Cut recursively }{{} Cut recursively.

Rod Cutting Second rod-cutting strategy (top-down): For every cut, compute value of left part and store it in a table Find value of optimal cut for right part in table Compute it recursively if it doesn t exist yet 5 CIS 110, Fall 2012

Rod Cutting Second rod-cutting strategy (top-down): For every cut, compute value of left part and store it in a table Find value of optimal cut for right part in table Compute it recursively if it doesn t exist yet } {{ } Cut recursively } {{ } Look up cost in table 5 CIS 110, Fall 2012

Rod Cutting Second rod-cutting strategy (top-down): For every cut, compute value of left part and store it in a table Find value of optimal cut for right part in table Compute it recursively if it doesn t exist yet } {{ } Cut recursively } {{ } Look up cost in table Reduces computation from O(2 n ) to O(n 2 ) (Why?) Requires an array of length n to store intermediate computations 5 CIS 110, Fall 2012

Rod Cutting hrid rod-cutting strategy (bottom-up): Compute the value of a rod of length 1. Store it. Compute the value of a rod of length 2. You can only cut it into rods of length 1. he value of a rod of length 1 is already computed, so there is no recursion. Compute the value of successively longer rods up to length n. he optimal values of shorter rods are always computed first so there is no recursion. 6 CIS 110, Fall 2012

Sequence Matching Human genes are coded by four bases: denine (), hymine (), uanine (), Cytosine (C) DN undergoes mutations with each copy: Substitutions: replace one base with another Deletions: some bases are dropped Suppose we isolate a gene in a new organism: C C C Predict function by comparing to genes in know, organism: e.g. C How similar are C C C and C? 7 CIS 110, Fall 2012

Sequence Matching How similar are C C C and C? How many mutations to change first sequence into second? How (un)likely is each mutation Edit Distance: minimum cost to convert one string into another. Each change (mutation) has an associated cost: ap 2 Mismatch 1 Match 0 Example matchings: C C C C - - 8 1 0 1 1 0 0 1 0 2 2 C C C - - C 1 0 2 0 0 1 0 2 0 1 7 8 CIS 110, Fall 2012

Edit Distance Brute-force recursive solution: Start at end of sequence and work backwards C C C C 1 C C - 2 C C C C C - C 2 C C C 0 1 C C - 2 1 - C C 2 1 C C - 1 2 C C - - 2 2 - C - 2 2 C - C 0 2 C - - 2 2 - - C 2 2 Recurse until we have all possible matches, then find minimum hree recursive calls per node O(3 n ) matching cost We need to do better! 9 CIS 110, Fall 2012

Edit Distance Dynamic Programming recursive solution Consider a pair of characters in the middle: C C C C What is the cost of matching from this pair of s to the end? Cost of matching s (0) + lowest cost of matching C C to C. Brute force solution computes all possible costs Idea: For each pair of characters, keep track of best match up to end 10 CIS 110, Fall 2012

Dynamic Programming Idea: For each pair of characters, keep track of best match to end C C C - 16 14 12 10 8 6 C 4 2-20 18 16 14 12 10 8 6 4 2 0 Initialization: Cost of zero-length match (lower right) is zero Inserting a gap (move right or down in table) costs two 11 CIS 110, Fall 2012

Dynamic Programming Idea: For each pair of characters, keep track of best match to end C C C - 16 14 12 10 8 6 C 4 1 2-20 18 16 14 12 10 8 6 4 2 0 Iteration: Work back from lower right Cost of cell (i, j) is C(i, j) = min(c(i + i, j) + 2, C(i, j + 1) + 2, C(i + 1, j + 1)) + δ where δ = 1 if the i th character of string and j th character of string B are identical. 11 CIS 110, Fall 2012

Dynamic Programming Idea: For each pair of characters, keep track of best match to end C C C - 16 14 12 10 8 6 C 4 3 1 2-20 18 16 14 12 10 8 6 4 2 0 Iteration: Work back from lower right Cost of cell (i, j) is C(i, j) = min(c(i + i, j) + 2, C(i, j + 1) + 2, C(i + 1, j + 1)) + δ where δ = 1 if the i th character of string and j th character of string B are identical. 11 CIS 110, Fall 2012

Dynamic Programming Idea: For each pair of characters, keep track of best match to end C C C - 16 14 12 10 8 6 C 4 4 3 1 2-20 18 16 14 12 10 8 6 4 2 0 Iteration: Work back from lower right Cost of cell (i, j) is C(i, j) = min(c(i + i, j) + 2, C(i, j + 1) + 2, C(i + 1, j + 1)) + δ where δ = 1 if the i th character of string and j th character of string B are identical. 11 CIS 110, Fall 2012

Dynamic Programming Idea: For each pair of characters, keep track of best match to end C C C - 16 14 12 10 8 6 C 4 6 4 3 1 2-20 18 16 14 12 10 8 6 4 2 0 Iteration: Work back from lower right Cost of cell (i, j) is C(i, j) = min(c(i + i, j) + 2, C(i, j + 1) + 2, C(i + 1, j + 1)) + δ where δ = 1 if the i th character of string and j th character of string B are identical. 11 CIS 110, Fall 2012

Dynamic Programming Idea: For each pair of characters, keep track of best match to end C C C - 16 14 12 10 8 6 C 4 8 6 4 3 1 2-20 18 16 14 12 10 8 6 4 2 0 Iteration: Work back from lower right Cost of cell (i, j) is C(i, j) = min(c(i + i, j) + 2, C(i, j + 1) + 2, C(i + 1, j + 1)) + δ where δ = 1 if the i th character of string and j th character of string B are identical. 11 CIS 110, Fall 2012

Dynamic Programming Idea: For each pair of characters, keep track of best match to end C C C - 16 14 12 10 8 6 C 4 1O 8 6 4 3 1 2-20 18 16 14 12 10 8 6 4 2 0 Iteration: Work back from lower right Cost of cell (i, j) is C(i, j) = min(c(i + i, j) + 2, C(i, j + 1) + 2, C(i + 1, j + 1)) + δ where δ = 1 if the i th character of string and j th character of string B are identical. 11 CIS 110, Fall 2012

Dynamic Programming Idea: For each pair of characters, keep track of best match to end C C C - 16 14 12 10 8 6 C 4 12 1O 8 6 4 3 1 2-20 18 16 14 12 10 8 6 4 2 0 Iteration: Work back from lower right Cost of cell (i, j) is C(i, j) = min(c(i + i, j) + 2, C(i, j + 1) + 2, C(i + 1, j + 1)) + δ where δ = 1 if the i th character of string and j th character of string B are identical. 11 CIS 110, Fall 2012

Dynamic Programming Idea: For each pair of characters, keep track of best match to end C C C - 16 14 12 10 8 6 C 4 14 12 1O 8 6 4 3 1 2-20 18 16 14 12 10 8 6 4 2 0 Iteration: Work back from lower right Cost of cell (i, j) is C(i, j) = min(c(i + i, j) + 2, C(i, j + 1) + 2, C(i + 1, j + 1)) + δ where δ = 1 if the i th character of string and j th character of string B are identical. 11 CIS 110, Fall 2012

Dynamic Programming Idea: For each pair of characters, keep track of best match to end C C C - 16 14 12 10 8 6 C 4 16 14 12 1O 8 6 4 3 1 2-20 18 16 14 12 10 8 6 4 2 0 Iteration: Work back from lower right Cost of cell (i, j) is C(i, j) = min(c(i + i, j) + 2, C(i, j + 1) + 2, C(i + 1, j + 1)) + δ where δ = 1 if the i th character of string and j th character of string B are identical. 11 CIS 110, Fall 2012

Dynamic Programming Idea: For each pair of characters, keep track of best match to end C C C - 16 14 12 10 8 6 C 4 18 16 14 12 1O 8 6 4 3 1 2-20 18 16 14 12 10 8 6 4 2 0 Iteration: Work back from lower right Cost of cell (i, j) is C(i, j) = min(c(i + i, j) + 2, C(i, j + 1) + 2, C(i + 1, j + 1)) + δ where δ = 1 if the i th character of string and j th character of string B are identical. 11 CIS 110, Fall 2012

Dynamic Programming Idea: For each pair of characters, keep track of best match to end C C C - 7 6 6 7 9 8 9 11 13 14 16 8 6 5 5 7 8 8 9 11 12 14 10 8 6 4 5 6 7 7 9 10 12 12 10 8 6 4 4 5 6 7 8 10 13 11 9 7 5 4 3 4 5 6 8 15 13 11 9 7 5 3 2 3 4 6 C 16 14 12 11 9 7 5 3 1 2 4 18 16 14 12 1O 8 6 4 3 1 2-20 18 16 14 12 10 8 6 4 2 0 Iteration: Work back from lower right Cost of cell (i, j) is C(i, j) = min(c(i + i, j) + 2, C(i, j + 1) + 2, C(i + 1, j + 1)) + δ where δ = 1 if the i th character of string and j th character of string B are identical. 11 CIS 110, Fall 2012

Dynamic Programming Idea: For each pair of characters, keep track of best match to end C C C - 7 6 6 7 9 8 9 11 13 14 16 8 6 5 5 7 8 8 9 11 12 14 10 8 6 4 5 6 7 7 9 10 12 12 10 8 6 4 4 5 6 7 8 10 13 11 9 7 5 4 3 4 5 6 8 15 13 11 9 7 5 3 2 3 4 6 C 16 14 12 11 9 7 5 3 1 2 4 18 16 14 12 1O 8 6 4 3 1 2-20 18 16 14 12 10 8 6 4 2 0 Recovering the best alignment: Final cost is in cell (0, 0) Follow arrows to reconstruct string aligns letter in the current column with a gap aligns letter in the current row with a gap matches letters in current row and column with each other otal running time: O(mn)! 11 CIS 110, Fall 2012

Some More Examples Image Compositing iven a set of overlapping images, what is the best way to stitch them? Cut the images along an invisible seam, and splice them together. he optimal seam can be found through dynamic programming. Even better: shortest path Davis98 12 CIS 110, Fall 2012

Some More Examples Matrix parenthesization Need to multiply a sequence of rectangular matrices Which matrices should be multiplied first to minimize the number of operations ( ( )) b1 ( a 1 a 2 a 3 ) b 2 ( c 1 c 2 c 3 ) = ( a 1 b 1 +a2b 2 +a 3 b 3 ) ( c 1 c 2 c 3 ) b 3 ( a 1 a 2 a 3 ) (( ) ) ( ) b1 b1 c 1 b 1 c 2 b 1 c 3 b 2 ( c 1 c 2 c 3 ) = ( a 1 a 2 a 3 ) b 2 c 1 b 2 c 2 b 2 c 3 b 3 b 3 c 1 b 3 c 2 b 3 c 3 13 CIS 110, Fall 2012

Some More Examples Matrix parenthesization Need to multiply a sequence of rectangular matrices Which matrices should be multiplied first to minimize the number of operations ( ( )) b1 ( a 1 a 2 a 3 ) b 2 ( c 1 c 2 c 3 ) = ( a 1 b 1 +a2b 2 +a 3 b 3 ) ( c 1 c 2 c 3 ) b 3 ( a 1 a 2 a 3 ) (( ) ) ( ) b1 b1 c 1 b 1 c 2 b 1 c 3 b 2 ( c 1 c 2 c 3 ) = ( a 1 a 2 a 3 ) b 2 c 1 b 2 c 2 b 2 c 3 b 3 b 3 c 1 b 3 c 2 b 3 c 3 Seam carving (see demo) Shrink an image by finding one row or column of pixels to remove he seam doesn t have to be straight it can wiggle Use dynamic programming to find the best set of pixels to remove 13 CIS 110, Fall 2012