Dynamic Programming David Kauchak cs161 Summer 2009.

Slides:



Advertisements
Similar presentations
Dynamic Programming Nithya Tarek. Dynamic Programming Dynamic programming solves problems by combining the solutions to sub problems. Paradigms: Divide.
Advertisements

Analysis of Algorithms
Lecture 8: Dynamic Programming Shang-Hua Teng. Longest Common Subsequence Biologists need to measure how similar strands of DNA are to determine how closely.
Overview What is Dynamic Programming? A Sequence of 4 Steps
COMP8620 Lecture 8 Dynamic Programming.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Dynamic Programming.
David Luebke 1 5/4/2015 CS 332: Algorithms Dynamic Programming Greedy Algorithms.
Dynamic Programming Reading Material: Chapter 7..
Dynamic Programming CIS 606 Spring 2010.
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)
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
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)
Dynamic Programming 0-1 Knapsack These notes are taken from the notes by Dr. Steve Goddard at
Analysis of Algorithms
1 Dynamic Programming Jose Rolim University of Geneva.
Lecture 7 Topics Dynamic Programming
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
David Luebke 1 8/23/2015 CS 332: Algorithms Greedy Algorithms.
Algorithms and Data Structures Lecture X
Dynamic Programming UNC Chapel Hill Z. Guo.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
ADA: 7. Dynamic Prog.1 Objective o introduce DP, its two hallmarks, and two major programming techniques o look at two examples: the fibonacci.
Dynamic Programming Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by or formulated.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
David Luebke 1 10/24/2015 CS 332: Algorithms Greedy Algorithms Continued.
CSC 413/513: Intro to Algorithms Greedy Algorithms.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
+ Review CS302 Spring 2013 David Kauchak. + Admin Final posted on the course web page on Monday due Sunday at 11:59pm time-boxed (3-4 hours) You may use:
DP (not Daniel Park's dance party). Dynamic programming Can speed up many problems. Basically, it's like magic. :D Overlapping subproblems o Number of.
Greedy Methods and Backtracking Dr. Marina Gavrilova Computer Science University of Calgary Canada.
Greedy algorithms David Kauchak cs161 Summer 2009.
6/4/ ITCS 6114 Dynamic programming Longest Common Subsequence.
Dynamic Programming continued David Kauchak cs302 Spring 2012.
Dynamic Programming David Kauchak cs302 Spring 2013.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Dynamic Programming. Many problem can be solved by D&C – (in fact, D&C is a very powerful approach if you generalize it since MOST problems can be solved.
1 Dynamic Programming Topic 07 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
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)
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
Lecture 151 Programming & Data Structures Dynamic Programming GRIFFITH COLLEGE DUBLIN.
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.
Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1.
Dynamic Programming (DP) By Denon. Outline Introduction Fibonacci Numbers (Review) Longest Common Subsequence (LCS) More formal view on DP Subset Sum.
1 Today’s Material Dynamic Programming – Chapter 15 –Introduction to Dynamic Programming –0-1 Knapsack Problem –Longest Common Subsequence –Chain Matrix.
Greedy algorithms 2 David Kauchak cs302 Spring 2012.
Fundamental Data Structures and Algorithms Ananda Guna March 18, 2003 Dynamic Programming Part 1.
Dynamic Programming Fundamental Data Structures and Algorithms Klaus Sutner March 30, 2004.
Dynamic Programming. What is Dynamic Programming  A method for solving complex problems by breaking them down into simpler sub problems. It is applicable.
9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 16 Dynamic.
TU/e Algorithms (2IL15) – Lecture 4 1 DYNAMIC PROGRAMMING II
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18.
Lecture 12.
CS200: Algorithm Analysis
Dynamic Programming.
Dynamic Programming.
Prepared by Chen & Po-Chuan 2016/03/29
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.
Dynamic Programming.
Dynamic Programming Dynamic Programming 1/18/ :45 AM
Divide and Conquer Algorithms Part I
Longest Common Subsequence
Introduction to Algorithms: Dynamic Programming
Dynamic Programming.
Lecture 4 Dynamic Programming
Dynamic Programming.
Presentation transcript:

Dynamic Programming David Kauchak cs161 Summer 2009

Administrative Final exam next week! (9/14 3:30pm – 6:30pm) Review session 9/10 lecture linear programming network flow problems Map reduce algorithms in popular media

Dynamic programming One of the most important algorithm tools! Very common interview question Method for solving problems where optimal solutions can be defined in terms of optimal solutions to sub-problems AND the sub-problems are overlapping

Fibonacci numbers 1, 1, 2, 3, 5, 8, 13, 21, 34, … What is the recurrence for the n th Fibonacci number? F(n) = F(n-1) + F(n-2) The solution for n is defined with respect to the solution to smaller problems (n-1 and n-2)

Fibonacci: a first attempt

Is it correct? F(n) = F(n-1) + F(n-2)

Running time Each call creates two recursive calls Each call reduces the size of the problem by 1 or 2 Creates a full binary of depth n O(2 n )

Can we do better? Fib(n) Fib(n-1) Fib(n-2) Fib(n-3) Fib(n-4) Fib(n-5) Fib(n-3) Fib(n-4) Fib(n-3) Fib(n-4) Fib(n-5) Fib(n-6)

A lot of repeated work! Fib(n) Fib(n-1) Fib(n-2) Fib(n-3) Fib(n-4) Fib(n-5) Fib(n-3) Fib(n-4) Fib(n-3) Fib(n-4) Fib(n-5) Fib(n-6)

Identifying a dynamic programming problem The solution can be defined with respect to solutions to subproblems The subproblems created are overlapping, that is we see the same subproblems repeated

Two main ideas for dynamic programming Identify a solution to the problem with respect to smaller subproblems F(n) = F(n-1) + F(n-2) Bottom up: start with solutions to the smallest problems and build solutions to the larger problems use an array to store solutions to subproblems

Is it correct? F(n) = F(n-1) + F(n-2)

Running time? Θ(n)

Counting binary search trees How many unique binary search trees can be created using the numbers 1 through n?

Step 1: What is the subproblem? Assume we have some black box solver (call it T) that can give us the answer to smaller subproblems How can we use the answer from this to answer our question? How many options for the root are there? n …

Subproblems i How many trees have i as the root?

Subproblems i 1, 2, …, i-1i+1, i+2, …, i+n ?

Subproblems i 1, 2, …, i-1i+1, i+2, …, i+n T(i-1) subproblem of size i-1 ?

Subproblems i 1, 2, …, i-1i+1, i+2, …, i+n T(i-1) Number of trees for i+1, i+2, …, i+n is the same as the number of trees from 1, 2, …, n-i

Subproblems i 1, 2, …, i-1i+1, i+2, …, i+n T(i-1)T(n-i) Given solutions for T(i-1) and T(n-i) how many trees are there with i as the root?

Subproblems i 1, 2, …, i-1i+1, i+2, …, i+n T(i-1)T(n-i) T(i) = T(i-1) * T(n-i)

Step 1: Defining the answer with respect to subproblems T(i) = T(i-1) * T(n-i)

Is there a problem? As with Fibonacci, we’re repeating a lot of work

Step 2: Generate a solution from the bottom-up

… n

1

… n 1 c[0]*c[1] + c[1]*c[0]

… n c[0]*c[1] + c[1]*c[0]

… n 1 1 2

… n c[0]*c[2] + c[1]*c[1] + c[2]*c[0] 1 23

… n

… n …

Running time? Θ(n 2 )

0-1 Knapsack problem 0-1 Knapsack – A thief robbing a store finds n items worth v 1, v 2,.., v n dollars and weight w 1, w 2, …, w n pounds, where v i and w i are integers. The thief can carry at most W pounds in the knapsack. Which items should the thief take if he wants to maximize value. Repetitions are allowed, that is you can take multiple copies of any item

Longest common subsequence (LCS) For a sequence X = x 1, x 2, …, x n, a subsequence is a subset of the sequence defined by a set of increasing indices (i 1, i 2, …, i k ) where 1 ≤ i 1 < i 2 < … < i k ≤ n X = A B A C D A B A B ABA?

Longest common subsequence (LCS) For a sequence X = x 1, x 2, …, x n, a subsequence is a subset of the sequence defined by a set of increasing indices (i 1, i 2, …, i k ) where 1 ≤ i 1 < i 2 < … < i k ≤ n X = A B A C D A B A B ABA

Longest common subsequence (LCS) For a sequence X = x 1, x 2, …, x n, a subsequence is a subset of the sequence defined by a set of increasing indices (i 1, i 2, …, i k ) where 1 ≤ i 1 < i 2 < … < i k ≤ n X = A B A C D A B A B ACA?

Longest common subsequence (LCS) For a sequence X = x 1, x 2, …, x n, a subsequence is a subset of the sequence defined by a set of increasing indices (i 1, i 2, …, i k ) where 1 ≤ i 1 < i 2 < … < i k ≤ n X = A B A C D A B A B ACA

Longest common subsequence (LCS) For a sequence X = x 1, x 2, …, x n, a subsequence is a subset of the sequence defined by a set of increasing indices (i 1, i 2, …, i k ) where 1 ≤ i 1 < i 2 < … < i k ≤ n X = A B A C D A B A B DCA?

Longest common subsequence (LCS) For a sequence X = x 1, x 2, …, x n, a subsequence is a subset of the sequence defined by a set of increasing indices (i 1, i 2, …, i k ) where 1 ≤ i 1 < i 2 < … < i k ≤ n X = A B A C D A B A B DCA

Longest common subsequence (LCS) For a sequence X = x 1, x 2, …, x n, a subsequence is a subset of the sequence defined by a set of increasing indices (i 1, i 2, …, i k ) where 1 ≤ i 1 < i 2 < … < i k ≤ n X = A B A C D A B A B AADAA?

Longest common subsequence (LCS) For a sequence X = x 1, x 2, …, x n, a subsequence is a subset of the sequence defined by a set of increasing indices (i 1, i 2, …, i k ) where 1 ≤ i 1 < i 2 < … < i k ≤ n X = A B A C D A B A B AADAA

LCS problem Given two sequences X and Y, a common subsequence is a subsequence that occurs in both X and Y Given two sequences X = x 1, x 2, …, x n and Y = y 1, y 2, …, y n, What is the longest common subsequence? X = A B C B D A B Y = B D C A B A

LCS problem Given two sequences X and Y, a common subsequence is a subsequence that occurs in both X and Y Given two sequences X = x 1, x 2, …, x n and Y = y 1, y 2, …, y n, What is the longest common subsequence? X = A B C B D A B Y = B D C A B A

Step 1: Define the problem with respect to subproblems X = A B C B D A B Y = B D C A B A

Step 1: Define the problem with respect to subproblems X = A B C B D A ? Y = B D C A B ? Is the last character part of the LCS?

Step 1: Define the problem with respect to subproblems X = A B C B D A ? Y = B D C A B ? Two cases: either the characters are the same or they’re different

Step 1: Define the problem with respect to subproblems X = A B C B D A A Y = B D C A B A If they’re the same The characters are part of the LCS LCS

Step 1: Define the problem with respect to subproblems X = A B C B D A B Y = B D C A B A If they’re different LCS

Step 1: Define the problem with respect to subproblems X = A B C B D A B Y = B D C A B A If they’re different LCS

Step 1: Define the problem with respect to subproblems X = A B C B D A B Y = B D C A B A If they’re different X = A B C B D A B Y = B D C A B A ?

Step 1: Define the problem with respect to subproblems X = A B C B D A B Y = B D C A B A

Step 2: Build the solution from the bottom up What types of subproblem solutions do we need to store? LCS(X 1…j, Y 1…k ) two different indices

Step 2: Build the solution from the bottom up What types of subproblem solutions do we need to store? LCS(X 1…j, Y 1…k )

0 x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B y j B D C A B A i j

0 x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B y j B D C A B A i j

0 x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B y j B D C A B A i j ? LCS(A, B)

0 x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B y j B D C A B A i j

0 x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B y j B D C A B A i j ? LCS(A, BDCA)

0 x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B y j B D C A B A i j LCS(A, BDCA)

0 x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B y j B D C A B A i j ? LCS(ABCB, BDCAB)

0 x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B y j B D C A B A i j LCS(ABCB, BDCAB)

0 x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B y j B D C A B A i j Where’s the final answer?

The algorithm

Base case initialization

The algorithm Fill in the matrix

The algorithm

Running time? Θ(nm)

Keeping track of the solution Our LCS algorithm only calculated the length of the LCS between X and Y What if we wanted to know the actual sequence? Keep track of this as well…

0 x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B y j B D C A B A i j We can follow the arrows to generate the solution

0 x i 1 A 2 B 3 C 4 B 5 D 6 A 7 B y j B D C A B A i j We can follow the arrows to generate the solution BCBA

Longest increasing subsequence Given a sequence of numbers X = x 1, x 2, …, x n find the longest increasing subsequence (i 1, i 2, …, i k ), that is a subsequence where numbers in the sequence increase

Longest increasing subsequence Given a sequence of numbers X = x 1, x 2, …, x n find the longest increasing subsequence (i 1, i 2, …, i k ), that is a subsequence where numbers in the sequence increase

Step 1: Define the problem with respect to subproblems Two options: Either 5 is in the LIS or it’s not

Step 1: Define the problem with respect to subproblems LIS( ) 5 + LIS(6 9 7) 5 + LIS(9 7) 5 + LIS(7) include LIS( )

Step 1: Define the problem with respect to subproblems don’t include 5 LIS( )

Step 1: Define the problem with respect to subproblems Be sure to be clear how you define your recursive subproblems includedon’t include LIS: longest increasing sequence for elements x i…n LIS: longest increasing sequence starting at element i (i.e. must include i)

Step 1: Define the problem with respect to subproblems Longest increasing sequence starting at i Longest increasing sequence for X is the longest increasing sequence starting at any element

Step 2: build the solution from the bottom up LIS’:

Step 2: build the solution from the bottom up LIS’: 1

Step 2: build the solution from the bottom up LIS’: 1

Step 2: build the solution from the bottom up LIS’: 1 1

Step 2: build the solution from the bottom up LIS’: 1 1

Step 2: build the solution from the bottom up LIS’: 2 1 1

Step 2: build the solution from the bottom up LIS’:

Step 2: build the solution from the bottom up LIS’:

Step 2: build the solution from the bottom up LIS’:

Step 2: build the solution from the bottom up LIS’:

Step 2: build the solution from the bottom up LIS’:

Step 2: build the solution from the bottom up LIS’:

Step 2: build the solution from the bottom up

start from the end (bottom)

Step 2: build the solution from the bottom up

initialization?

Running time? Θ(n 2 )

Another solution Can we use LCS to solve this problem? LCS

Another solution Can we use LCS to solve this problem? LCS

Memoization Sometimes it can be a challenge to write the function in a bottom-up fashion Memoization: Write the recursive function top-down Alter the function to check if we’ve already calculated the value If so, use the pre-calculate value If not, do the recursive call(s)

Memoized fibonacci

Use  to denote uncalculated

Memoized fibonacci Use  to denote uncalculated What else could we use besides an array?

Memoized fibonacci Check if we already calculated the value

Memoized fibonacci calculate the value

Memoized fibonacci store the value

Memoization Pros Can be more intuitive to code/understand Can be memory savings if you don’t need answers to all subproblems Cons Larger overhead because of recursion

Quick summary Step 1: Define the problem with respect to subproblems We did this for divide and conquer too. What’s the difference? You can identify a candidate for dynamic programming if there is overlap or repeated work in the subproblems being created Step 2: build the solution from the bottom up Build the solution such that the subproblems referenced by larger problems are already solved Memoization is also an alternative