Dynamic Programming David Kauchak cs302 Spring 2013.

Slides:



Advertisements
Similar presentations
Back to Sorting – More efficient sorting algorithms.
Advertisements

Dynamic Programming Nithya Tarek. Dynamic Programming Dynamic programming solves problems by combining the solutions to sub problems. Paradigms: Divide.
Lecture 3: Parallel Algorithm Design
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
Algorithms Dynamic programming Longest Common Subsequence.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
David Luebke 1 5/4/2015 CS 332: Algorithms Dynamic Programming Greedy Algorithms.
Introduction to Algorithms
CSC 252 Algorithms Haniya Aslam
CS 2210 (22C:19) Discrete Structures Advanced Counting
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Comp 122, Fall 2004 Dynamic Programming. dynprog - 2 Lin / Devi Comp 122, Spring 2004 Longest Common Subsequence  Problem: Given 2 sequences, X =  x.
1 Longest Common Subsequence (LCS) Problem: Given sequences x[1..m] and y[1..n], find a longest common subsequence of both. Example: x=ABCBDAB and y=BDCABA,
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
CPSC 411 Design and Analysis of Algorithms Set 5: Dynamic Programming Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 5 1.
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)
© 2004 Goodrich, Tamassia Dynamic Programming1. © 2004 Goodrich, Tamassia Dynamic Programming2 Matrix Chain-Products (not in book) Dynamic Programming.
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
Lecture 7 Topics Dynamic Programming
Longest Common Subsequence
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Algorithms and Data Structures Lecture X
Dynamic Programming UNC Chapel Hill Z. Guo.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
October 21, Algorithms and Data Structures Lecture X Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
1 Recurrences Algorithms Jay Urbain, PhD Credits: Discrete Mathematics and Its Applications, by Kenneth Rosen The Design and Analysis of.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
CS 8833 Algorithms Algorithms Dynamic Programming.
+ 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:
INTRODUCTION. What is an algorithm? What is a Problem?
6/4/ ITCS 6114 Dynamic programming Longest Common Subsequence.
Dynamic Programming continued David Kauchak cs302 Spring 2012.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Introduction to Algorithms Jiafen Liu Sept
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)
Computer Sciences Department1.  Property 1: each node can have up to two successor nodes (children)  The predecessor node of a node is called its.
Dynamic Programming David Kauchak cs161 Summer 2009.
Dynamic Programming (DP) By Denon. Outline Introduction Fibonacci Numbers (Review) Longest Common Subsequence (LCS) More formal view on DP Subset Sum.
1Computer Sciences Department. 2 Advanced Design and Analysis Techniques TUTORIAL 7.
David Luebke 1 2/26/2016 CS 332: Algorithms Dynamic Programming.
Dynamic Programming Tutorial &Practice on Longest Common Sub-sequence.
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
1 Chapter 15-2: Dynamic Programming II. 2 Matrix Multiplication Let A be a matrix of dimension p x q and B be a matrix of dimension q x r Then, if we.
Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
CS200: Algorithm Analysis
Chapter 8 Dynamic Programming.
CSCE 411 Design and Analysis of Algorithms
Dynamic Programming.
Dynamic Programming.
Data Structures and Algorithms
Merge Sort 1/12/2019 5:31 PM Dynamic Programming Dynamic Programming.
Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.
Divide and Conquer Algorithms Part I
Longest Common Subsequence
Introduction to Algorithms: Dynamic Programming
DYNAMIC PROGRAMMING.
Longest Common Subsequence
Longest Common Subsequence
Dynamic Programming.
Presentation transcript:

Dynamic Programming David Kauchak cs302 Spring 2013

Administative

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-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-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

Overlapping sub-problems … divide and conquer dynamic programming

Creating a dynamic programming solution Step 1: Identify a solution to the problem with respect to smaller subproblems (pretend like you have a solver, but it only works on smaller problems): F(n) = F(n-1) + F(n-2) Step 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, …, n ?

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

Subproblems i 1, 2, …, i-1i+1, i+2, …, 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, …, 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, …, n T(i-1)T(n-i) T(i) = T(i-1) * T(n-i)

Step 1: define 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 )

Longest common subsequence (LCS) X = A B A C D A B A B ABA? 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

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) X = A B A C D A B A B ACA? 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

Longest common subsequence (LCS) X = A B A C D A B A B ACA 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

Longest common subsequence (LCS) X = A B A C D A B A B DCA? 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

Longest common subsequence (LCS) X = A B A C D A B A B DCA 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

Longest common subsequence (LCS) X = A B A C D A B A B AADAA? 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

Longest common subsequence (LCS) X = A B A C D A B A B AADAA 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

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 Assume you have a solver for smaller problems

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 What is the recursive relationship?

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 (for now, let’s just worry about counting the length of the LCS)

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 For Fibonacci and tree counting, we had to initialize some entries in the array. Any here?

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 Need to initialize values within 1 smaller in either dimension.

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