Design and Analysis of Algorithms

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

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

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

Dynamic Programming! CSE 417: Algorithms and Computational Complexity!

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

On the Repeating Group Finding Problem

Problem Set 9 Solutions

Calculation of time complexity (3%)

Exercises. 18 Algorithms

Finding the Longest Similar Subsequence of Thumbprints for Intrusion Detection

Chapter Newton s Method

EEL 6266 Power System Operation and Control. Chapter 3 Economic Dispatch Using Dynamic Programming

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

Week 5: Neural Networks

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

Lecture Notes on Linear Regression

Module 9. Lecture 6. Duality in Assignment Problems

CHAPTER 17 Amortized Analysis

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

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

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

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

Computing Correlated Equilibria in Multi-Player Games

Lecture 17: Lee-Sidford Barrier

Errors for Linear Systems

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

Computational Biology Lecture 8: Substitution matrices Saad Mneimneh

Case A. P k = Ni ( 2L i k 1 ) + (# big cells) 10d 2 P k.

On the Multicriteria Integer Network Flow Problem

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

Introduction to Algorithms

Chapter 5. Solution of System of Linear Equations. Module No. 6. Solution of Inconsistent and Ill Conditioned Systems

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

Section 8.3 Polar Form of Complex Numbers

Single-Facility Scheduling over Long Time Horizons by Logic-based Benders Decomposition

Section 3.6 Complex Zeros

p 1 c 2 + p 2 c 2 + p 3 c p m c 2

8/25/17. Data Modeling. Data Modeling. Data Modeling. Patrice Koehl Department of Biological Sciences National University of Singapore

Lecture 10 Support Vector Machines II

Hidden Markov Models

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

Difference Equations

Lecture 4: Constant Time SVD Approximation

CS 770G - Parallel Algorithms in Scientific Computing

Min Cut, Fast Cut, Polynomial Identities

princeton univ. F 17 cos 521: Advanced Algorithm Design Lecture 7: LP Duality Lecturer: Matt Weinberg

Lecture 5 Decoding Binary BCH Codes

Lecture 10 Support Vector Machines. Oct

Compiling for Parallelism & Locality. Example. Announcement Need to make up November 14th lecture. Last time Data dependences and loops

Hashing. Alexandra Stefan

Edit Distance with Duplications and Contractions Revisited - Supplementary Materials

Newton s Method for One - Dimensional Optimization - Theory

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

Feature Selection: Part 1

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,

Outline. Communication. Bellman Ford Algorithm. Bellman Ford Example. Bellman Ford Shortest Path [1]

Approximate Smallest Enclosing Balls

Curve Fitting with the Least Square Method

10-701/ Machine Learning, Fall 2005 Homework 3

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

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

The General Nonlinear Constrained Optimization Problem

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

Math 217 Fall 2013 Homework 2 Solutions

Tracking with Kalman Filter

Notes on Frequency Estimation in Data Streams

CS 3710: Visual Recognition Classification and Detection. Adriana Kovashka Department of Computer Science January 13, 2015

The Minimum Universal Cost Flow in an Infeasible Flow Network

ECE559VV Project Report

LECTURE 9 CANONICAL CORRELATION ANALYSIS

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

Course organization. Part II: Algorithms for Network Biology (Week 12-16)

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

Communication Complexity 16:198: February Lecture 4. x ij y ij

CS4495/6495 Introduction to Computer Vision. 3C-L3 Calibrating cameras

The Second Anti-Mathima on Game Theory

The Study of Teaching-learning-based Optimization Algorithm

HMMT February 2016 February 20, 2016

CS 350 Algorithms and Complexity

CS473 - Algorithms I

VARIATION OF CONSTANT SUM CONSTRAINT FOR INTEGER MODEL WITH NON UNIFORM VARIABLES

Planning and Scheduling to Minimize Makespan & Tardiness. John Hooker Carnegie Mellon University September 2006

CS : Algorithms and Uncertainty Lecture 17 Date: October 26, 2016

Math 261 Exercise sheet 2

The Geometry of Logit and Probit

Download the files protein1.txt and protein2.txt from the course website.

Split alignment. Martin C. Frith April 13, 2012

Capacity Constraints Across Nests in Assortment Optimization Under the Nested Logit Model

Kernel Methods and SVMs Extension

Algorithms. Algorithms. Algorithms 2.2 M ERGESORT. mergesort bottom-up mergesort. sorting complexity divide-and-conquer

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

The Problem: Mapping programs to architectures

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

Generalized Linear Methods

Yong Joon Ryang. 1. Introduction Consider the multicommodity transportation problem with convex quadratic cost function. 1 2 (x x0 ) T Q(x x 0 )

Problem Set 6: Trees Spring 2018

1 Matrix representations of canonical matrices

Introduction ( Week 1-2) Course introduction A brief introduction to molecular biology A brief introduction to sequence comparison Part I: Algorithms

An Interactive Optimisation Tool for Allocation Problems

COS 521: Advanced Algorithms Game Theory and Linear Programming

Transcription:

Desgn and Analyss of Algorthms CSE 53 Lecture 4 Dynamc Programmng Junzhou Huang, Ph.D. Department of Computer Scence and Engneerng CSE53 Desgn and Analyss of Algorthms

The General Dynamc Programmng Technque Apples to a problem that at frst seems to requre a lot of tme (possbly exponental), provded we have: Subproblem optmalty: the global optmum value can be defned n terms of optmal subproblems Subproblem overlap: the subproblems are not ndependent, but nstead they overlap (hence, should be constructed bottom-up). CSE53 Desgn and Analyss of Algorthms

Longest Common Subsequence Problem: Gven sequences, X = x,...,x m and Y = y,...,y n, fnd a common subsequence whose length s maxmum. sprngtme ncaa tournament basketball prntng north carolna krzyzewsk Subsequence need not be consecutve, but must be n order. CSE53 Desgn and Analyss of Algorthms 3

Other Sequence Questons Edt dstance: Gven sequences, X = x,...,x m and Y = y,...,y n, what s the mnmum number of deletons, nsertons, and changes that you must do to change one to another? Proten sequence algnment: Gven a score matrx on amno acd pars, s(a,b) for a,b {Λ} A, and amno acd sequences, X = x,...,x m A m and Y = y,...,y n A n, fnd the algnment wth lowest score CSE53 Desgn and Analyss of Algorthms 4

More Problems Optmal ST: Gven sequence K = k < k < < k n of n sorted keys, wth a search probablty p for each key k, buld a bnary search tree (ST) wth mnmum expected search cost. Mnmum convex decomposton of a polygon, Hydrogen placement n proten structures, CSE53 Desgn and Analyss of Algorthms 5

Dynamc Programmng Dynamc Programmng s an algorthm desgn technque for optmzaton problems: often mnmzng or maxmzng. Lke dvde and conquer, DP solves problems by combnng solutons to subproblems. Unlke dvde and conquer, subproblems are not ndependent. Subproblems may share subsubproblems, However, soluton to one subproblem may not affect the solutons to other subproblems of the same problem. (More on ths later.) DP reduces computaton by Solvng subproblems n a bottom-up fashon. Storng soluton to a subproblem the frst tme t s solved. Lookng up the soluton when subproblem s encountered agan. Key: determne structure of optmal solutons CSE53 Desgn and Analyss of Algorthms 6

Recallng: Steps n Dynamc Programmng. Characterze structure of an optmal soluton.. Defne value of optmal soluton recursvely. 3. Compute optmal soluton values ether top-down wth cachng or bottom-up n a table. 4. Construct an optmal soluton from computed values. CSE53 Desgn and Analyss of Algorthms 7

Naïve Algorthm sprngtme ncaa tournament basketball prntng north carolna krzyzewsk For every subsequence of X = x,...,x m, check whether t s a subsequence of Y = y,...,y n. Tme: Θ(n m ). m subsequences of X to check. Each subsequence takes Θ(n) tme to check: scan Y for frst letter, for second, and so on. CSE53 Desgn and Analyss of Algorthms 8

Optmal Substructure Theorem Let Z = z,..., z k be any LCS of X and Y.. If x m = y n, then z k = x m = y n and Z k- s an LCS of X m- and Y n-.. If x m y n, then ether z k x m and Z s an LCS of X m- and Y. 3. or z k y n and Z s an LCS of X and Y n-. k n n- Notaton: prefx X = x,...,x s the frst letters of X. Ths says what any longest common subsequence must look lke; do you beleve t? CSE53 Desgn and Analyss of Algorthms 9

Optmal Substructure Theorem Let Z = z,..., z k be any LCS of X and Y.. If x m = y n, then z k = x m = y n and Z k- s an LCS of X m- and Y n-.. If x m y n, then ether z k x m and Z s an LCS of X m- and Y. 3. or z k y n and Z s an LCS of X and Y n-. k n n- Proof: (case : x m = y n ) Any sequence Z that does not end n x m = y n can be made longer by addng x m = y n to the end. Therefore, () longest common subsequence (LCS) Z must end n x m = y n. () Z k- s a common subsequence of X m- and Y n-, and (3) there s no longer CS of X m- and Y n-, or Z would not be an LCS. CSE53 Desgn and Analyss of Algorthms

Optmal Substructure Theorem Let Z = z,..., z k be any LCS of X and Y.. If x m = y n, then z k = x m = y n and Z k- s an LCS of X m- and Y n-.. If x m y n, then ether z k x m and Z s an LCS of X m- and Y. 3. or z k y n and Z s an LCS of X and Y n-. k n n- Proof: (case : x m y n, and z k x m ) Snce Z does not end n x m, () Z s a common subsequence of X m- and Y, and () there s no longer CS of X m- and Y, or Z would not be an LCS. CSE53 Desgn and Analyss of Algorthms

Recursve Soluton Defne c[, j] = length of LCS of X and Y j. We want c[m,n]. c [, j ] = c [, j max( c [ ] +, j ], c [, j ]) f f f =, j, j or j = > and > and, x x = y y j j,. Ths gves a recursve algorthm and solves the problem. ut does t solve t well? CSE53 Desgn and Analyss of Algorthms

Recursve Soluton c [ α, β ] = c [ prefx α, prefx β ] + max( c [ prefx α, β ], c [ α, prefx β ]) f f f α empty or β empty, end( α ) = end( β ), end( α ) end( β ). c[sprngtme, prntng] c[sprngtm, prntng] c[sprngtme, prntn] [sprngt, prntng] [sprngtm, prntn] [sprngtm, prntn] [sprngtme, prnt] [sprngt, prntng] [sprngt, prntn] [sprngtm, prnt] [sprngtme, prnt] CSE53 Desgn and Analyss of Algorthms 3

Recursve Soluton c [ α, β ] = c [ prefx α, prefx β ] + max( c [ prefx α, β ], c [ α, prefx β ]) f f f α empty or β empty end( α ) = end( end( α ) end( β ), β )., p r n t n g Keep track of c[a,b] n a table of nm entres: top/down bottom/up S P r n g t m e CSE53 Desgn and Analyss of Algorthms 4

Computng the length of an LCS LCS-LENGTH (X, Y). m length[x]. n length[y] 3. for to m 4. do c[, ] 5. for j to n 6. do c[, j ] 7. for to m 8. do for j to n 9. do f x = y j. then c[, j ] c[, j ] +. b[, j ]. else f c[, j ] c[, j ] 3. then c[, j ] c[, j ] 4. b[, j ] 5. else c[, j ] c[, j ] 6. b[, j ] 7.return c and b b[, j ] ponts to table entry whose subproblem we used n solvng LCS of X and Y j. c[m,n] contans the length of an LCS of X and Y. Tme: O(mn) CSE53 Desgn and Analyss of Algorthms 5

Constructng an LCS PRINT-LCS (b, X,, j). f = or j =. then return 3. f b[, j ] = 4. then PRINT-LCS(b, X,, j ) 5. prnt x 6. elsef b[, j ] = 7. then PRINT-LCS(b, X,, j) 8. else PRINT-LCS(b, X,, j ) Intal call s PRINT-LCS (b, X,m, n). When b[, j ] =, we have extended LCS by one character. So LCS = entres wth n them. Tme: O(m+n) CSE53 Desgn and Analyss of Algorthms 6

LCS Example We ll see how LCS algorthm works on the followng example: X = AC Y = DCA What s the Longest Common Subsequence of X and Y? LCS(X, Y) = C X = A C Y = D C A CSE53 Desgn and Analyss of Algorthms 7

LCS Example () j 3 4 5 Yj D C A X A AC DCA 3 4 C X = AC; m = X = 4 Y = DCA; n = Y = 5 Allocate array c[5,4] CSE53 Desgn and Analyss of Algorthms 8

LCS Example () j 3 4 5 Yj D C A X 3 4 A C AC DCA for = to m c[,] = for j = to n c[,j] = CSE53 Desgn and Analyss of Algorthms 9

LCS Example () j 3 4 5 Yj D C A X 3 4 A C AC DCA f ( X == Y j ) c[,j] = c[-,j-] + else c[,j] = max( c[-,j], c[,j-] ) CSE53 Desgn and Analyss of Algorthms

LCS Example (3) j 3 4 5 Yj D C A X 3 4 A C AC DCA f ( X == Y j ) c[,j] = c[-,j-] + else c[,j] = max( c[-,j], c[,j-] ) CSE53 Desgn and Analyss of Algorthms

LCS Example (4) j 3 4 5 Yj D C A X 3 4 A C AC DCA f ( X == Y j ) c[,j] = c[-,j-] + else c[,j] = max( c[-,j], c[,j-] ) CSE53 Desgn and Analyss of Algorthms

LCS Example (5) j 3 4 5 Yj D C A X 3 4 A C AC DCA f ( X == Y j ) c[,j] = c[-,j-] + else c[,j] = max( c[-,j], c[,j-] ) CSE53 Desgn and Analyss of Algorthms 3

LCS Example (6) 3 4 j 3 4 5 Yj D C A X A C AC DCA f ( X == Y j ) c[,j] = c[-,j-] + else c[,j] = max( c[-,j], c[,j-] ) CSE53 Desgn and Analyss of Algorthms 4

LCS Example (7) 3 4 j 3 4 5 Yj D C A X A C AC DCA f ( X == Y j ) c[,j] = c[-,j-] + else c[,j] = max( c[-,j], c[,j-] ) CSE53 Desgn and Analyss of Algorthms 5

LCS Example (8) j 3 4 5 Yj D C A X 3 4 A C AC DCA f ( X == Y j ) c[,j] = c[-,j-] + else c[,j] = max( c[-,j], c[,j-] ) CSE53 Desgn and Analyss of Algorthms 6

LCS Example () 3 4 j 3 4 5 Yj D C A X A C AC DCA f ( X == Y j ) c[,j] = c[-,j-] + else c[,j] = max( c[-,j], c[,j-] ) CSE53 Desgn and Analyss of Algorthms 7

LCS Example () 3 4 j 3 4 5 Yj D C A X A C AC DCA f ( X == Y j ) c[,j] = c[-,j-] + else c[,j] = max( c[-,j], c[,j-] ) CSE53 Desgn and Analyss of Algorthms 8

LCS Example () j 3 4 5 Yj D C A X 3 4 A C AC DCA f ( X == Y j ) c[,j] = c[-,j-] + else c[,j] = max( c[-,j], c[,j-] ) CSE53 Desgn and Analyss of Algorthms 9

LCS Example (3) j 3 4 5 Yj D C A X 3 4 A C AC DCA f ( X == Y j ) c[,j] = c[-,j-] + else c[,j] = max( c[-,j], c[,j-] ) CSE53 Desgn and Analyss of Algorthms 3

LCS Example (4) j 3 4 5 Yj D C A X 3 4 A C AC DCA f ( X == Y j ) c[,j] = c[-,j-] + else c[,j] = max( c[-,j], c[,j-] ) CSE53 Desgn and Analyss of Algorthms 3

LCS Example (5) 3 4 j 3 4 5 Yj D C A X A C f ( X == Y j ) c[,j] = c[-,j-] + else c[,j] = max( c[-,j], c[,j-] ) 3 AC DCA CSE53 Desgn and Analyss of Algorthms 3

LCS Algorthm Runnng Tme LCS algorthm calculates the values of each entry of the array c[m,n] So what s the runnng tme? O(m*n) snce each c[,j] s calculated n constant tme, and there are m*n elements n the array CSE53 Desgn and Analyss of Algorthms 33

How to fnd actual LCS So far, we have just found the length of LCS, but not LCS tself. We want to modfy ths algorthm to make t output Longest Common Subsequence of X and Y Each c[,j] depends on c[-,j] and c[,j-] or c[-, j-] For each c[,j] we can say how t was acqured: 3 For example, here c[,j] = c[-,j-] + = +=3 CSE53 Desgn and Analyss of Algorthms 34

How to fnd actual LCS - contnued Remember that c[, j] = c[, j ] + max( c[, j ], c[, j]) f x[ ] = y[ otherwse j], So we can start from c[m,n] and go backwards Whenever c[,j] = c[-, j-]+, remember x[] (because x[] s a part of LCS) When = or j= (.e. we reached the begnnng), output remembered letters n reverse order CSE53 Desgn and Analyss of Algorthms 35

Fndng LCS 3 4 j 3 4 5 Yj D C A X A C 3 CSE53 Desgn and Analyss of Algorthms 36

Fndng LCS () 3 4 j 3 4 5 Yj D C A X A C 3 LCS (reversed order): C LCS (straght order): C (ths strng turned out to be a palndrome) CSE53 Desgn and Analyss of Algorthms 37