Dynamic Programming 4/5/12. Dynamic programming. Fibonacci numbers. Fibonacci: a first attempt. David Kauchak cs302 Spring 2012

Similar documents
Dynamic Programming. Preview. Dynamic Programming. Dynamic Programming. Dynamic Programming (Example: Fibonacci Sequence)

Design and Analysis of Algorithms

Outline and Reading. Dynamic Programming. Dynamic Programming revealed. Computing Fibonacci. The General Dynamic Programming Technique

Problem Set 9 Solutions

Dynamic Programming! CSE 417: Algorithms and Computational Complexity!

Exercises. 18 Algorithms

Math Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Module 9. Lecture 6. Duality in Assignment Problems

Partha Sarathi Mandal

U.C. Berkeley CS294: Beyond Worst-Case Analysis Luca Trevisan September 5, 2017

Structure and Drive Paul A. Jensen Copyright July 20, 2003

On the Repeating Group Finding Problem

CS 770G - Parallel Algorithms in Scientific Computing

Grover s Algorithm + Quantum Zeno Effect + Vaidman

CS 331 DESIGN AND ANALYSIS OF ALGORITHMS DYNAMIC PROGRAMMING. Dr. Daisy Tang

Singular Value Decomposition: Theory and Applications

Quadratic speedup for unstructured search - Grover s Al-

Calculation of time complexity (3%)

Matrix Approximation via Sampling, Subspace Embedding. 1 Solving Linear Systems Using SVD

PROBLEM SET 7 GENERAL EQUILIBRIUM

MMA and GCMMA two methods for nonlinear optimization

ECE 534: Elements of Information Theory. Solutions to Midterm Exam (Spring 2006)

CHAPTER 17 Amortized Analysis

Errors for Linear Systems

Example: (13320, 22140) =? Solution #1: The divisors of are 1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18, 20, 27, 30, 36, 41,

find (x): given element x, return the canonical element of the set containing x;

A 2D Bounded Linear Program (H,c) 2D Linear Programming

Difference Equations

An Experiment/Some Intuition (Fall 2006): Lecture 18 The EM Algorithm heads coin 1 tails coin 2 Overview Maximum Likelihood Estimation

Lecture 5 September 17, 2015

Simultaneous Optimization of Berth Allocation, Quay Crane Assignment and Quay Crane Scheduling Problems in Container Terminals

Classification as a Regression Problem

Additional Codes using Finite Difference Method. 1 HJB Equation for Consumption-Saving Problem Without Uncertainty

Week 5: Neural Networks

Introduction to Algorithms

Introduction to Algorithms

COS 521: Advanced Algorithms Game Theory and Linear Programming

CSC 411 / CSC D11 / CSC C11

Problem Set 6: Trees Spring 2018

The Discretization Process

THE ARIMOTO-BLAHUT ALGORITHM FOR COMPUTATION OF CHANNEL CAPACITY. William A. Pearlman. References: S. Arimoto - IEEE Trans. Inform. Thy., Jan.

Determinants Containing Powers of Generalized Fibonacci Numbers

princeton univ. F 13 cos 521: Advanced Algorithm Design Lecture 3: Large deviations bounds and applications Lecturer: Sanjeev Arora

Min Cut, Fast Cut, Polynomial Identities

Ensemble Methods: Boosting

Some modelling aspects for the Matlab implementation of MMA

Finding the Longest Similar Subsequence of Thumbprints for Intrusion Detection

Finding Dense Subgraphs in G(n, 1/2)

Dynamic Programming. Lecture 13 (5/31/2017)

Expected Value and Variance

Supplement: Proofs and Technical Details for The Solution Path of the Generalized Lasso

Special Relativity and Riemannian Geometry. Department of Mathematical Sciences

Lecture Notes on Linear Regression

Lecture 5 Decoding Binary BCH Codes

EEE 241: Linear Systems

Lecture 3 January 31, 2017

For now, let us focus on a specific model of neurons. These are simplified from reality but can achieve remarkable results.

THE CHINESE REMAINDER THEOREM. We should thank the Chinese for their wonderful remainder theorem. Glenn Stevens

A FAST HEURISTIC FOR TASKS ASSIGNMENT IN MANYCORE SYSTEMS WITH VOLTAGE-FREQUENCY ISLANDS

Lecture 10 Support Vector Machines II

Low-Connectivity Network Design on Series-Parallel Graphs

Computational issues surrounding the management of an ecological food web

U.C. Berkeley CS294: Spectral Methods and Expanders Handout 8 Luca Trevisan February 17, 2016

College of Computer & Information Science Fall 2009 Northeastern University 20 October 2009

Hidden Markov Models

Annexes. EC.1. Cycle-base move illustration. EC.2. Problem Instances

CSCE 790S Background Results

General theory of fuzzy connectedness segmentations: reconciliation of two tracks of FC theory

Spectral Clustering. Shannon Quinn

arxiv: v1 [math.ho] 18 May 2008

1 Matrix representations of canonical matrices

VQ widely used in coding speech, image, and video

FTCS Solution to the Heat Equation

NP-Completeness : Proofs

n α j x j = 0 j=1 has a nontrivial solution. Here A is the n k matrix whose jth column is the vector for all t j=0

U.C. Berkeley CS278: Computational Complexity Professor Luca Trevisan 2/21/2008. Notes for Lecture 8

The Minimum Universal Cost Flow in an Infeasible Flow Network

Lecture 4. Instructor: Haipeng Luo

APPROXIMATE PRICES OF BASKET AND ASIAN OPTIONS DUPONT OLIVIER. Premia 14

An Interactive Optimisation Tool for Allocation Problems

CME 302: NUMERICAL LINEAR ALGEBRA FALL 2005/06 LECTURE 13

10-701/ Machine Learning, Fall 2005 Homework 3

Suggested solutions for the exam in SF2863 Systems Engineering. June 12,

MACHINE APPLIED MACHINE LEARNING LEARNING. Gaussian Mixture Regression

Edge Isoperimetric Inequalities

Which Separator? Spring 1

Lecture 21: Numerical methods for pricing American type derivatives

Probability Theory (revisited)

CIS526: Machine Learning Lecture 3 (Sept 16, 2003) Linear Regression. Preparation help: Xiaoying Huang. x 1 θ 1 output... θ M x M

Common loop optimizations. Example to improve locality. Why Dependence Analysis. Data Dependence in Loops. Goal is to find best schedule:

Homework Notes Week 7

Fitting a Graph to One-Dimensional Data. September 11, 2018

Consistency & Convergence

1 The Mistake Bound Model

2. PROBLEM STATEMENT AND SOLUTION STRATEGIES. L q. Suppose that we have a structure with known geometry (b, h, and L) and material properties (EA).

Nice plotting of proteins II

Module 3 LOSSY IMAGE COMPRESSION SYSTEMS. Version 2 ECE IIT, Kharagpur

Feature Selection: Part 1

Lectures - Week 4 Matrix norms, Conditioning, Vector Spaces, Linear Independence, Spanning sets and Basis, Null space and Range of a Matrix

Chapter 2 Transformations and Expectations. , and define f

Transcription:

Dynamc Programmng Davd Kauchak cs32 Sprng 212 Dynamc programmng l One of the most mportant algorthm tools! l Very common ntervew queston l Method for solvng problems where optmal solutons can be defned n terms of optmal solutons to sub-problems AND l the sub-problems are overlappng Fbonacc numbers Fbonacc: a frst attempt 1, 1, 2, 3, 5, 8, 13, 21, 34, What s the recurrence for the n th Fbonacc number? F(n) = F(n-1) + F(n-2) The soluton for n s defned wth respect to the soluton to smaller problems (n-1 and n-2) 1

Is t correct? Runnng tme F(n) = F(n-1) + F(n-2) l Each call creates two recursve calls l Each call reduces the sze of the problem by 1 or 2 l Creates a full bnary of depth n l O(2 n ) Can we do better? Fb(n) A lot of repeated work! Fb(n) Fb(n-1) Fb(n-2) Fb(n-1) Fb(n-2) Fb(n-2) Fb(n-3) Fb(n-3) Fb(n-4) Fb(n-2) Fb(n-3) Fb(n-3) Fb(n-4) Fb(n-3) Fb(n-4) Fb(n-4) Fb(n-5) Fb(n-4) Fb(n-5) Fb(n-5) Fb(n-6) Fb(n-3) Fb(n-4) Fb(n-4) Fb(n-5) Fb(n-4) Fb(n-5) Fb(n-5) Fb(n-6) 2

Identfyng a dynamc programmng problem The soluton can be defned wth respect to solutons to subproblems The subproblems created are overlappng, that s we see the same subproblems repeated Creatng a dynamc programmng soluton Step 1: Identfy a soluton to the problem wth respect to smaller subproblems l F(n) = F(n-1) + F(n-2) Step 2: bottom up - start wth solutons to the smallest problems and buld solutons to the larger problems use an array to store solutons to subproblems Is t correct? Runnng tme? F(n) = F(n-1) + F(n-2) Θ(n) 3

Countng bnary search trees l How many unque bnary search trees can be created usng the numbers 1 through n? 2 1 3 6 4 5 Step 1: What s the subproblem? l Assume we have some black box solver (call t T) that can gve us the answer to smaller subproblems l How can we use the answer from ths to answer our queston? l How many optons for the root are there? 1 2 3 n Subproblems Subproblems 1, 2,, -1 +1, +2,, +n How many trees have as the root?? 4

Subproblems Subproblems 1, 2,, -1 +1, +2,, +n T(-1) subproblem of sze -1? 1, 2,, -1 +1, +2,, +n T(-1) Number of trees for +1, +2,, +n s the same as the number of trees from 1, 2,, n- Subproblems Subproblems 1, 2,, -1 +1, +2,, +n T(-1) T(n-) 1, 2,, -1 +1, +2,, +n T(-1) T(n-) Gven solutons for T(-1) and T(n-) how many trees are there wth as the root? T() = T(-1) * T(n-) 5

Step 1: defne the answer wth respect to subproblems T() = T(-1) * T(n-) Is there a problem? T( n) = n = 1 T( 1)* T( n ) As wth Fbonacc, we re repeatng a lot of work Step 2: Generate a soluton from the bottom-up 1 2 3 4 5 n 6

c[]*c[1] + c[1]*c[] 1 1 1 2 3 4 5 n 1 1 1 2 3 4 5 n 1 2 2 1 c[]*c[1] + c[1]*c[] 1 1 1 2 3 4 5 n 1 1 2 1 2 3 4 5 n 7

1 2 3 c[]*c[2] + c[1]*c[1] + c[2]*c[] 1 1 2 1 2 3 4 5 n 1 1 2 5 1 2 3 4 5 n Runnng tme? 1 1 2 5 1 2 3 4 5 n Θ(n 2 ) 8

Longest common subsequence (LCS) For a sequence X = x 1, x 2,, x n, a subsequence s a subset of the sequence defned by a set of ncreasng ndces ( 1, 2,, k ) where 1 1 < 2 < < k n X = A B A C D A B A B ABA? Longest common subsequence (LCS) l For a sequence X = x 1, x 2,, x n, a subsequence s a subset of the sequence defned by a set of ncreasng ndces ( 1, 2,, k ) where 1 1 < 2 < < k n X = A B A C D A B A B ABA Longest common subsequence (LCS) l For a sequence X = x 1, x 2,, x n, a subsequence s a subset of the sequence defned by a set of ncreasng ndces ( 1, 2,, k ) where 1 1 < 2 < < k n X = A B A C D A B A B ACA? Longest common subsequence (LCS) l For a sequence X = x 1, x 2,, x n, a subsequence s a subset of the sequence defned by a set of ncreasng ndces ( 1, 2,, k ) where 1 1 < 2 < < k n X = A B A C D A B A B ACA 9

Longest common subsequence (LCS) l For a sequence X = x 1, x 2,, x n, a subsequence s a subset of the sequence defned by a set of ncreasng ndces ( 1, 2,, k ) where 1 1 < 2 < < k n X = A B A C D A B A B DCA? Longest common subsequence (LCS) l For a sequence X = x 1, x 2,, x n, a subsequence s a subset of the sequence defned by a set of ncreasng ndces ( 1, 2,, k ) where 1 1 < 2 < < k n X = A B A C D A B A B DCA Longest common subsequence (LCS) l For a sequence X = x 1, x 2,, x n, a subsequence s a subset of the sequence defned by a set of ncreasng ndces ( 1, 2,, k ) where 1 1 < 2 < < k n X = A B A C D A B A B AADAA? Longest common subsequence (LCS) l For a sequence X = x 1, x 2,, x n, a subsequence s a subset of the sequence defned by a set of ncreasng ndces ( 1, 2,, k ) where 1 1 < 2 < < k n X = A B A C D A B A B AADAA 1

LCS problem Gven two sequences X and Y, a common subsequence s a subsequence that occurs n both X and Y Gven two sequences X = x 1, x 2,, x n and Y = y 1, y 2,, y n, What s the longest common subsequence? X = A B C B D A B LCS problem l Gven two sequences X and Y, a common subsequence s a subsequence that occurs n both X and Y l Gven two sequences X = x 1, x 2,, x n and Y = y 1, y 2,, y n, What s the longest common subsequence? X = A B C B D A B Step 1: Defne the problem wth respect to subproblems X = A B C B D A B Step 1: Defne the problem wth respect to subproblems X = A B C B D A? Y = B D C A B? Is the last character part of the LCS? 11

Step 1: Defne the problem wth respect to subproblems Step 1: Defne the problem wth respect to subproblems X = A B C B D A? Y = B D C A B? X = A B C B D A A LCS The characters are part of the LCS What s the recursve relatonshp? Two cases: ether the characters are the same or they re dfferent If they re the same LCS + x ( X, Y) = LCS ( X1... n 1, Y1... m 1) n Step 1: Defne the problem wth respect to subproblems X = A B C B D A B LCS Step 1: Defne the problem wth respect to subproblems X = A B C B D A B LCS If they re dfferent LCS X, Y) = LCS ( X 1, Y) (... n 1 If they re dfferent LCS ( X, Y) = LCS ( X, Y1... m 1) 12

Step 1: Defne the problem wth respect to subproblems Step 1: Defne the problem wth respect to subproblems X = A B C B D A B X = A B C B D A B If they re dfferent? X = A B C B D A B X1... n 1, Y1... m 1) f xn = ym X, Y) X1... n 1, Y), X, Y1... m 1) otherwse (for now, let s ust worry about countng the length of the LCS) Step 2: Buld the soluton from the bottom up X1... n 1, Y1... m 1) f xn = ym X, Y) X1... n 1, Y), X, Y1... m 1) otherwse Step 2: Buld the soluton from the bottom up X1... n 1, Y1... m 1) f xn = ym X, Y) X1... n 1, Y), X, Y1... m 1) otherwse What types of subproblem solutons do we need to store? What types of subproblem solutons do we need to store? X 1, Y 1 k ) X 1, Y 1 k ) two dfferent ndces 1,, ] 1, ],, f otherwse 13

1,, ] 1, ],, f otherwse 1,, ] 1, ],, f otherwse 1 2 3 4 5 6 y B D C A B A 1 2 3 4 5 6 y B D C A B A x For Fbonacc and tree countng, we had to ntalze some entres n the array. Any here? x Need to ntalze values wthn 1 smaller n ether dmenson. 1,, ] 1, ],, f otherwse 1,, ] 1, ],, f otherwse 1 2 3 4 5 6 y B D C A B A 1 2 3 4 5 6 y B D C A B A x? A, B) x 14

1,, ] 1, ],, f otherwse 1,, ] 1, ],, f otherwse 1 2 3 4 5 6 y B D C A B A 1 2 3 4 5 6 y B D C A B A x? A, BDCA) x 1 A, BDCA) 1,, ] 1, ],, f otherwse 1,, ] 1, ],, f otherwse 1 2 3 4 5 6 y B D C A B A 1 2 3 4 5 6 y B D C A B A x 1 1 1 1 1 1 1 2 2 1 1 2 2 2 2 1 1 2 2? ABCB, BDCAB) x 1 1 1 1 1 1 1 2 2 1 1 2 2 2 2 1 1 2 2 3 ABCB, BDCAB) 15

1, f x = y, ] 1, ],, otherwse x 1 2 3 4 5 6 y B D C A B A 1 1 1 1 1 1 1 2 2 1 1 2 2 2 2 1 1 2 2 3 3 1 2 2 2 3 3 1 2 2 3 3 4 1 2 2 3 4 4 Where s the fnal answer? The algorthm The algorthm The algorthm Base case ntalzaton Fll n the matrx 16

The algorthm The algorthm The algorthm Runnng tme? Θ(nm) 17