Dynamic Programming. Shuang Zhao. Microsoft Research Asia September 5, Dynamic Programming. Shuang Zhao. Outline. Introduction.

Similar documents
Maximum sum contiguous subsequence Longest common subsequence Matrix chain multiplication All pair shortest path Kna. Dynamic Programming

COSE212: Programming Languages. Lecture 1 Inductive Definitions (1)

{a, b, c} {a, b} {a, c} {b, c} {a}

Non-context-Free Languages. CS215, Lecture 5 c

Automata Theory CS F-08 Context-Free Grammars

COSE212: Programming Languages. Lecture 1 Inductive Definitions (1)

Learning Context Free Grammars with the Syntactic Concept Lattice

BOUNDS ON ZIMIN WORD AVOIDANCE

Online Computation of Abelian Runs

General Methods for Algorithm Design

Shortest paths with negative lengths

A Universal Turing Machine

Relations. We have seen several types of abstract, mathematical objects, including propositions, predicates, sets, and ordered pairs and tuples.

Matrix Arithmetic. j=1

Approximating Shortest Superstring Problem Using de Bruijn Graphs

Lecture 16 Symbolic dynamics.

Harvard CS121 and CSCI E-121 Lecture 2: Mathematical Preliminaries

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Automata Theory and Formal Grammars: Lecture 1

Definition: A binary relation R from a set A to a set B is a subset R A B. Example:

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Dynamic Programming II Date: 10/12/17

Streaming and communication complexity of Hamming distance

With Question/Answer Animations

Single Source Shortest Paths

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

CHAPTER 1. Relations. 1. Relations and Their Properties. Discussion

Regularity-preserving letter selections

1 Review of Vertex Cover

Section Summary. Sequences. Recurrence Relations. Summations. Examples: Geometric Progression, Arithmetic Progression. Example: Fibonacci Sequence

CS375 Midterm Exam Solution Set (Fall 2017)

CSEP 590 Data Compression Autumn Arithmetic Coding

Semigroup presentations via boundaries in Cayley graphs 1

Properties of Context-Free Languages

FABER Formal Languages, Automata. Lecture 2. Mälardalen University

CS 455/555: Mathematical preliminaries

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Greedy Conjecture for Strings of Length 4

Linear Classifiers (Kernels)

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Basic Principles of Lossless Coding. Universal Lossless coding. Lempel-Ziv Coding. 2. Exploit dependences between successive symbols.

Quick Tour of Linear Algebra and Graph Theory

Packet #1: Logic & Proofs. Applied Discrete Mathematics

Partha Sarathi Mandal

Hierarchical Overlap Graph

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

Theoretical Computer Science

Theory of Computer Science

New Bounds and Extended Relations Between Prefix Arrays, Border Arrays, Undirected Graphs, and Indeterminate Strings

Examples of Groups

In English, there are at least three different types of entities: letters, words, sentences.

Solutions to Problem Set 3

Linear-Time Algorithms for Finding Tucker Submatrices and Lekkerkerker-Boland Subgraphs

Theory of Computation

Department of Computer Science University at Albany, State University of New York Solutions to Sample Discrete Mathematics Examination II (Fall 2007)

1. (a) Explain the procedure to convert Context Free Grammar to Push Down Automata.

Section Summary. Definition of a Function.

CS6901: review of Theory of Computation and Algorithms

Section 1 (closed-book) Total points 30

Special Factors and Suffix and Factor Automata

1 Alphabets and Languages

Computing Longest Common Substrings Using Suffix Arrays

On the Number of Distinct Squares

Note that M i,j depends on two entries in row (i 1). If we proceed in a row major order, these two entries will be available when we are ready to comp

Multiplying Products of Prime Powers

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

UNIVERSITY OF MICHIGAN UNDERGRADUATE MATH COMPETITION 27 MARCH 28, 2010

Sets are one of the basic building blocks for the types of objects considered in discrete mathematics.

Today s topics. Introduction to Set Theory ( 1.6) Naïve set theory. Basic notations for sets

Introduction to Kleene Algebras

Theory of Computation

Language-Processing Problems. Roland Backhouse DIMACS, 8th July, 2003

Elementary maths for GMT

Author: Vivek Kulkarni ( )

18.10 Addendum: Arbitrary number of pigeons

CS3719 Theory of Computation and Algorithms

CS60007 Algorithm Design and Analysis 2018 Assignment 1

Proving languages to be nonregular

Chapter Summary. Mathematical Induction Strong Induction Well-Ordering Recursive Definitions Structural Induction Recursive Algorithms

A necessary and sufficient condition for the existence of a spanning tree with specified vertices having large degrees

DFA to Regular Expressions

Indian Institute of Information Technology Design and Manufacturing, Kancheepuram Chennai , India. Instructor N.Sadagopan Scribe: P.

Data Structures in Java

CMSC 330: Organization of Programming Languages

Complexity Theory VU , SS The Polynomial Hierarchy. Reinhard Pichler

Outline. Complexity Theory EXACT TSP. The Class DP. Definition. Problem EXACT TSP. Complexity of EXACT TSP. Proposition VU 181.

CSCI 340: Computational Models. Regular Expressions. Department of Computer Science

Probabilistic Aspects of Computer Science: Probabilistic Automata

CS6902 Theory of Computation and Algorithms

UNIT-I. Strings, Alphabets, Language and Operations

Homework 4 Solutions. 2. Find context-free grammars for the language L = {a n b m c k : k n + m}. (with n 0,

Mathematical Preliminaries. Sipser pages 1-28

Chapter 3 Source Coding. 3.1 An Introduction to Source Coding 3.2 Optimal Source Codes 3.3 Shannon-Fano Code 3.4 Huffman Code

19. Logic constraints, integer variables

Introduction. I Dynamic programming is a technique for solving optimization problems. I Key element: Decompose a problem into subproblems, solve them

q-counting hypercubes in Lucas cubes

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Introduction to Automata

Chapter 1. Introduction

Transcription:

Microsoft Research Asia September 5, 2005

1 2 3 4

Section I

What is? Definition is a technique for efficiently recurrence computing by storing partial results. In this slides, I will NOT use too many formal words, but only look on some interesting problems.

The Longest Ascending Subsequence P : a 1, a 2,..., a n. Q : a b1, a b2,..., a bk, satisfying and 1 b 1 < b 2 <... < b k n a b1 < a b2 <... < a bk We say Q is an ascending subsequence of P. Your task is: given a sequence P, find the length its longest ascending subsequence (LAS).

The Longest Ascending Subsequence We use f i to denote the length of P s LAS ending with element a i. Let a 0 :=, f 0 := 0. Then we have f k = max 0 i<k {f i + 1 : a i < a k }, Hence the length of P s LAS is max{f i }. 1 k n This naive algorithm runs in O(n 2 ) time, and later we will look on this problem again.

The Shortest Hamilton Path Given a connected graph G(V, E) and a vertex s V. Given a weight function w over E, denoting the lengths of edges. Your task is to find the length of shortest Hamilton path starting from s.

The Shortest Hamilton Path This problem is N P-hard. When V is small, one FAST algorithm to solve this problem is. Use f i,s where i V, S S to denote the length of shortest Hamilton path over S, which ending at vertex i. Then min i V { fi,s } is what we want, and f i,s = min j S { fj,s {i} + w(j, i) } This algorithm runs in O( V 2 2 V ) time.

Section II

Problem I

The Problem: There is a board with N rows and 6 columns. How many ways can we cover the board with L pieces?............ One way to solve this problem, is. But where are the partial results?

This is a pattern, say P n,(101011)2 :

And by completely covering the last row of P n,(101011)2, we can obtain P n+1,(110110)2 and P n+1,(111100)2.

Observation For any pattern P i,j, after covering its (i + 1)-th row, we can only get patterns with the form of P i+1,j. We call P i+1,j can be generated from P i,j, denoting as P i+1,j P i,j.

Observation For any given covering of the N 6 board and an integer i (0 i < N), there will be one and only one pattern P i,j, which is contained in the covering. i := 4

The partial results are: Let f i,j to be the number of ways to cover pattern P i,j. And f i,j = f i,j = { 1 j = 0 0 otherwise P i,j P i 1,j f i 1,j i = 0 0 < i < n Finally f n 1,(111111)2 is the answer to this problem. By a coarse calculation, we know this algorithm runs in O(2 6 N 4 6 ) = O(N) time.

Problem II

The Problem: Given an alphabet Σ, a set of strings S f Σ, and an integer n. Your task is to count the number of n-length-strings which contain at least one string in S as its substring.

Let Σ := {a, b, c}, S := {ab} and n := 3. Then we have: aab aba abb abc bab cab One method to solve this problem is using the inclusion and exclusion theorem. But the time complexity of this method is quite high (at least O(2 S )).

Definition Let and be two binary relations over Σ : x y x is a prefix of y x y x is a suffix of y Definition We define the prefix set of S (S ) by pre(s) := { s : ( s S)(s s) } Obviously, S pre(s).

For instance, let Σ := {a, b, c} and S := {aa, ba, cba}, then pre(s) = {ε, a, aa, b, ba, c, cb, cba}

Let pre(s) = {s 0, s 1,..., s t } where s 0 = ε. Let F i (i = 0, 1,..., t) be the subset of Σ, satisfying for all s F i : s i s ( s s )[( r S)(r s )] s j (s i s j s j s )

Observation Given p Σ, for all s F i, there will be exact one j (0 j t) satisfying s p F j. We say F i F j.

Define f i,j by the number of i-length strings in F j. The we have f i,j = F j F k f i 1,k and the number of strings which contain at least one string in S as their substring is n f i,j Σ n i i=1 s j S

A brute-force approach to determine the binary relation takes O( pre(s) Σ L) O(L 2 ) where L is the total length of strings in pre(s). The time complexity of doing the is O(n pre(s) Σ ) O(n pre(s) ) O(n L).

Section III

Speed up partial results calculation

Speed up partial results calculation Longest Ascending Subsequence (LAS) problem revisited: The naive algorithm runs in O(n 2 ) time. Can we solve this problem more efficiently?

Speed up partial results calculation Let n := 6, {a n } := {5, 1, 6, 2, 4, 1.5}: Vertices, Contours Red 1, Green 2, Blue 3, etc.

Speed up partial results calculation Proposition The contours will NOT intersect. Proof. Assume two contours with levels i and j (i < j) intersects. Then it holds that at least one vertex of level j is below contours i. This gives the contradiction that one vertex of level i is below contours i.

Speed up partial results calculation {a n } = {5}

Speed up partial results calculation {a n } = {5, 1}

Speed up partial results calculation {a n } = {5, 1, 6}

Speed up partial results calculation {a n } = {5, 1, 6, 2}

Speed up partial results calculation {a n } = {5, 1, 6, 2, 4}

Speed up partial results calculation {a n } = {5, 1, 6, 2, 4, 1.5}

Speed up partial results calculation How to update the contours?

Speed up partial results calculation How to update the contours? 1 When the new vertex is above all contours Then a new contour is created.

Speed up partial results calculation How to update the contours? 2 When the new vertex is below some contour Then the contour immediately above the new vertex, is lowered.

Speed up partial results calculation How to update the contours? 3 When the new vertex is just on some contour Then the contours remain the same.

Speed up partial results calculation Find the contour immediately above the new vertex A brute-force approach runs in O(n) time. A Binary Search algorithm takes only O(log n) time. So by using Binary Search, the time complexity of entry algorithm is reduced to O(n log n).

Speed up partial results calculation Further thinking How to implement this algorithm? How to find the longest non-descending subsequence of a given sequence? If we given a weight to every number in the sequence, how to find the ascending subsequence which maximizes the sum of weights?

Speed up by monotonicity

Speed up by monotonicity The Optimal Binary Search Tree (OBST) Problem n numbers a 1 < a 2 <... < a n n weights w 1, w 2,..., w n 0 Construct a binary search tree using a 1,..., a n. Cost = n w i d i i=1 Your task is to find a binary search tree which minimizes the cost.

Speed up by monotonicity Let n := 5 and {w n } := {1, 2, 2, 3, 1}. The cost of OBST is 18.

Speed up by monotonicity The naive approach f i,j : The cost of OBST constructed by w i, w i+1,..., w j Then f i,j = 0 (i > j) f i,j = min {f i,k 1 + f k+1,j } + w t (i j) i k j The answer is f 1,n. The time complexity is O(n 3 ). How to speed up this algorithm? i t j

Speed up by monotonicity Definition For a given m n matrix A, if for all i 1 i 2 j 1 j 2, it holds A[i 1, j 1 ] + A[i 2, j 2 ] A[i 1, j 2 ] + A[i 2, j 1 ] then we say A is totally monotonic.

Speed up by monotonicity The inequality A[i 1, j 1 ] + A[i 2, j 2 ] A[i 1, j 2 ] + A[i 2, j 1 ] is called Quadrangle Inequality. j j 2 A 12 A 22 j 1 A 11 A 21 i 1 i 2 i

Speed up by monotonicity Recurrent formula of OBST problem f i,j = 0 (i > j) f i,j = min {f i,k 1 + f k+1,j } + w t (i j) i k j Define F, W R n n + by Then when i j F [i, j] := f i,j i t j W [i, j] := i k j w k F [i, j] = min {F [i, k 1] + F [k + 1, j]} + W [i, j] i k j

Speed up by monotonicity Proposition Matrix W is totally monotonic. Proof. For all i 1 i 2 < j 1 j 2, W [i 1, j 1 ] + W [i 2, j 2 ] = W [i 1, j 2 ] + W [i 2, j 1 ] i 1 i 2 j 1 j 2

Speed up by monotonicity Proposition Matrix F is also totally monotonic. This can be proved by induction on j 2 i 1. To see the details, read in the section.

Speed up by monotonicity Definition For all 1 i j n, define s(i, j) by s(i, j) := max i k j { k : F [i, j] = F [i, k 1] + F [k + 1, j] + W [i, j] }

Speed up by monotonicity Proposition s(i, j) is monotonic, namely for all 1 i j < n, s(i, j) s(i, j + 1) s(i + 1, j + 1) To see the proof, read in the section.

Speed up by monotonicity The new recurrent formula f i,j = min {f i,k 1 + f k+1,j } + s(i,j 1) k s(i+1,j) The time complexity to solve it is O(n 2 ). i t j w t (i j)

(In the OBST problem) Proposition Matrix F is also totally monotonic. Proof (Part 1). When i 1 = i 2 or j 1 = j 2 the Quadrangle Inequality holds.

Proof (Part 2). When i 1 < i 2 = j 1 < j 2, we prove by induction on j 2 i 1. Let F [i 1, j 2 ] = F [i 1, k 1] + F [k + 1, j 2 ] + W [i 1, j 2 ] Without loss of generality, k j 1.

Case 1. k < j 1 (= i 2 ) F [i 1, j 1 ] + F [i 2, j 2 ] F [i 1, k 1] + F [k + 1, j 1 ] + W [i 1, j 1 ] + F [i 2, j 2 ] F [i 1, k 1] + W [i 1, j 1 ] + F [k + 1, j 2 ] + F [i 2, j 1 ] F [i 1, k 1] + W [i 1, j 2 ] + F [k + 1, j 2 ] + F [i 2, j 1 ] = F [i 1, j 2 ] + F [i 2, j 1 ]

Case 2. k = j 1 (= i 2 ) F [i 1, j 1 ] + F [i 2, j 2 ] F [i 1, k 1] + W [i 1, j 1 ] + F [i 2, j 2 ] F [i 1, k 1] + W [i 1, j 1 ] + F [j 1 + 1, j 2 ] + W [i 2, j 2 ] = F [i 1, k 1] + W [i 1, j 2 ] + F [k + 1, j 2 ] + W [i 2, j 1 ] = F [i 1, j 2 ] + F [i 2, j 1 ]

Proof (Part 3). When i 1 < i 2 < j 1 < j 2, we prove by induction on j 2 i 1. Let F [i 2, j 1 ] = F [i 2, k 1] + F [k + 1, j 1 ] + W [i 2, j 1 ] F [i 1, j 2 ] = F [i 1, t 1] + F [t + 1, j 2 ] + W [i 1, j 2 ] Without loss of generality, t k. Hence i 1 t k j 1.

Then we have F [i 1, j 1 ] + F [i 2, j 2 ] F [i 1, t 1] + F [t + 1, j 1 ] + W [i 1, j 1 ] + F [i 2, k 1] + F [k + 1, j 2 ] + W [i 2, j 2 ] F [i 1, t 1] + F [t + 1, j 2 ] + W [i 1, j 2 ] + F [i 2, k 1] + F [k + 1, j 1 ] + W [i 2, j 1 ] = F [i 1, j 2 ] + F [i 2, j 1 ]

(In the OBST problem) Proposition s(i, j) is monotonic, namely for all 1 i j < n, s(i, j) s(i, j + 1) s(i + 1, j + 1) Proof. By symmetry, we need only to prove that s(i, j) s(i, j + 1). When i = j, s(i, j) = i s(i, j + 1).

Next we assume i < j. For convenience, we use symbol F k [i, j] to be the short from of F [i, k 1] + F [k + 1, j] + W [i, j]. So F s(i,j) [i, j] = F [i, j]. Since matrix F is totally monotonic, for all k k j, F [k + 1, j] + F [k + 1, j + 1] F [k + 1, j] + F [k + 1, j + 1]

Thus F [i, k 1] + F [k + 1, j] + W [i, j] + F [i, k 1] + F [k + 1, j + 1] + W [i, j + 1] F [i, k 1] + F [k + 1, j + 1] + W [i, j + 1] + F [i, k 1] + F [k + 1, j] + W [i, j] namely F k [i, j] + F k [i, j + 1] F k [i, j + 1] + F k [i, j] that is F k [i, j] F k [i, j] F k [i, j + 1] F k [i, j + 1]

Therefore F k [i, j] F k [i, j] F k [i, j + 1] F k [i, j + 1] For all k < s(i, j), F s(i,j) [i, j] = F [i, j] F k [i, j]. So F s(i,j) (i, j + 1) F k (i, j + 1). Hence F s(i,j) [i, j + 1] F k [i, j + 1]. This gives s(i, j) s(i, j + 1).