Even More on Dynamic Programming

Similar documents
More Dynamic Programming

More Dynamic Programming

Properties of Context-Free Languages

CSCI Compiler Construction

Einführung in die Computerlinguistik

Computational Models - Lecture 5 1

Harvard CS 121 and CSCI E-207 Lecture 12: General Context-Free Recognition

Context Free Languages and Grammars

RNA Secondary Structure Prediction

On the Sizes of Decision Diagrams Representing the Set of All Parse Trees of a Context-free Grammar

CS20a: summary (Oct 24, 2002)

Plan for 2 nd half. Just when you thought it was safe. Just when you thought it was safe. Theory Hall of Fame. Chomsky Normal Form

Chap. 7 Properties of Context-free Languages

Introduction to Computational Linguistics

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

CYK Algorithm for Parsing General Context-Free Grammars

The Pumping Lemma for Context Free Grammars

This lecture covers Chapter 7 of HMU: Properties of CFLs

Grammar formalisms Tree Adjoining Grammar: Formal Properties, Parsing. Part I. Formal Properties of TAG. Outline: Formal Properties of TAG

Context-Free Languages

Grammars and Context Free Languages

Grammars and Context Free Languages

CS311 Computational Structures. NP-completeness. Lecture 18. Andrew P. Black Andrew Tolmach. Thursday, 2 December 2010

Foundations of Informatics: a Bridging Course

Deterministic Finite Automata (DFAs)

Properties of context-free Languages

Tree Adjoining Grammars

CSCI 1010 Models of Computa3on. Lecture 17 Parsing Context-Free Languages

Computational Models - Lecture 4 1

Computability Theory

NPDA, CFG equivalence

Einführung in die Computerlinguistik Kontextfreie Grammatiken - Formale Eigenschaften

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

Parsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26

Non-deterministic Finite Automata (NFAs)

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Theory of Computation 8 Deterministic Membership Testing

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)

CS5371 Theory of Computation. Lecture 9: Automata Theory VII (Pumping Lemma, Non-CFL)

Parsing. Unger s Parser. Laura Kallmeyer. Winter 2016/17. Heinrich-Heine-Universität Düsseldorf 1 / 21

Computability and Complexity

Context Free Grammars: Introduction. Context Free Grammars: Simplifying CFGs

CS375: Logic and Theory of Computing

Section 1 (closed-book) Total points 30

Review. Earley Algorithm Chapter Left Recursion. Left-Recursion. Rule Ordering. Rule Ordering

Computing if a token can follow

CS 301. Lecture 18 Decidable languages. Stephen Checkoway. April 2, 2018

Follow sets. LL(1) Parsing Table

Context-Free Languages (Pre Lecture)

Chapter 4: Context-Free Grammars

Context-Free and Noncontext-Free Languages

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

CSE 202 Homework 4 Matthias Springer, A

Before We Start. The Pumping Lemma. Languages. Context Free Languages. Plan for today. Now our picture looks like. Any questions?

Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:

Recitation 4: Converting Grammars to Chomsky Normal Form, Simulation of Context Free Languages with Push-Down Automata, Semirings

Context- Free Parsing with CKY. October 16, 2014

The View Over The Horizon

Chapter 16: Non-Context-Free Languages

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

Parsing. Unger s Parser. Introduction (1) Unger s parser [Grune and Jacobs, 2008] is a CFG parser that is

Context Free Grammars: Introduction

Maschinelle Sprachverarbeitung

Properties of Context-free Languages. Reading: Chapter 7

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs

Context-Free Grammars and Languages

Statistical Machine Translation

Context-Free Grammars: Normal Forms

Notes for Comp 497 (Comp 454) Week 10 4/5/05

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

Maschinelle Sprachverarbeitung

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

CS5371 Theory of Computation. Lecture 9: Automata Theory VII (Pumping Lemma, Non-CFL, DPDA PDA)

CFLs and Regular Languages. CFLs and Regular Languages. CFLs and Regular Languages. Will show that all Regular Languages are CFLs. Union.

A parsing technique for TRG languages

FORMAL LANGUAGES, AUTOMATA AND COMPUTATION

General Methods for Algorithm Design

CS5371 Theory of Computation. Lecture 18: Complexity III (Two Classes: P and NP)

CPS 220 Theory of Computation

Introduction to Theory of Computing

Computational Models - Lecture 4

CMPSCI 611 Advanced Algorithms Midterm Exam Fall 2015

MA/CSSE 474 Theory of Computation

6.1 The Pumping Lemma for CFLs 6.2 Intersections and Complements of CFLs

Address for Correspondence

Decision problem of substrings in Context Free Languages.

Finite Automata and Formal Languages TMV026/DIT321 LP Useful, Useless, Generating and Reachable Symbols

3.10. TREE DOMAINS AND GORN TREES Tree Domains and Gorn Trees

Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules).

d(ν) = max{n N : ν dmn p n } N. p d(ν) (ν) = ρ.

CS5371 Theory of Computation. Lecture 12: Computability III (Decidable Languages relating to DFA, NFA, and CFG)

Context Free Languages: Decidability of a CFL

Notes for Comp 497 (454) Week 10

Parsing with CFGs L445 / L545 / B659. Dept. of Linguistics, Indiana University Spring Parsing with CFGs. Direction of processing

Parsing with CFGs. Direction of processing. Top-down. Bottom-up. Left-corner parsing. Chart parsing CYK. Earley 1 / 46.

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Context-Free Grammars and Languages. Reading: Chapter 5

Einführung in die Computerlinguistik

Transcription:

Algorithms & Models of Computation CS/ECE 374, Fall 2017 Even More on Dynamic Programming Lecture 15 Thursday, October 19, 2017 Sariel Har-Peled (UIUC) CS374 1 Fall 2017 1 / 26

Part I Longest Common Subsequence Problem Sariel Har-Peled (UIUC) CS374 2 Fall 2017 2 / 26

The LCS Problem Definition LCS between two strings X and Y is the length of longest common subsequence between X and Y. Example LCS between ABAZDC and BACBAD is4 via ABAD Derive a dynamic programming algorithm for the problem. Sariel Har-Peled (UIUC) CS374 3 Fall 2017 3 / 26

The LCS Problem Definition LCS between two strings X and Y is the length of longest common subsequence between X and Y. Example LCS between ABAZDC and BACBAD is4 via ABAD Derive a dynamic programming algorithm for the problem. Sariel Har-Peled (UIUC) CS374 3 Fall 2017 3 / 26

The LCS Problem Definition LCS between two strings X and Y is the length of longest common subsequence between X and Y. Example LCS between ABAZDC and BACBAD is4 via ABAD Derive a dynamic programming algorithm for the problem. Sariel Har-Peled (UIUC) CS374 3 Fall 2017 3 / 26

Part II Maximum Weighted Independent Set in Trees Sariel Har-Peled (UIUC) CS374 4 Fall 2017 4 / 26

Maximum Weight Independent Set Problem Input Graph G = (V, E) and weights w(v) 0 for each v V Goal Find maximum weight independent set in G 2 A 5 B 15 C F 2 10 E D 20 Maximum weight independent set in above graph: {B, D} Sariel Har-Peled (UIUC) CS374 5 Fall 2017 5 / 26

Maximum Weight Independent Set Problem Input Graph G = (V, E) and weights w(v) 0 for each v V Goal Find maximum weight independent set in G 2 A 5 B 15 C F 2 10 E D 20 Maximum weight independent set in above graph: {B, D} Sariel Har-Peled (UIUC) CS374 5 Fall 2017 5 / 26

Maximum Weight Independent Set in a Tree Input Tree T = (V, E) and weights w(v) 0 for each v V Goal Find maximum weight independent set in T r 10 5 a b 8 4 c d e f g 11 4 9 3 2 h i 7 j 8 Maximum weight independent set in above tree:?? Sariel Har-Peled (UIUC) CS374 6 Fall 2017 6 / 26

Towards a Recursive Solution For an arbitrary graph G: 1 Number vertices as v 1, v 2,..., v n 2 Find recursively optimum solutions without v n (recurse on G v n ) and with v n (recurse on G v n N(v n ) & include v n ). 3 Saw that if graph G is arbitrary there was no good ordering that resulted in a small number of subproblems. What about a tree? Natural candidate for v n is root r of T? Sariel Har-Peled (UIUC) CS374 7 Fall 2017 7 / 26

Towards a Recursive Solution For an arbitrary graph G: 1 Number vertices as v 1, v 2,..., v n 2 Find recursively optimum solutions without v n (recurse on G v n ) and with v n (recurse on G v n N(v n ) & include v n ). 3 Saw that if graph G is arbitrary there was no good ordering that resulted in a small number of subproblems. What about a tree? Natural candidate for v n is root r of T? Sariel Har-Peled (UIUC) CS374 7 Fall 2017 7 / 26

Towards a Recursive Solution For an arbitrary graph G: 1 Number vertices as v 1, v 2,..., v n 2 Find recursively optimum solutions without v n (recurse on G v n ) and with v n (recurse on G v n N(v n ) & include v n ). 3 Saw that if graph G is arbitrary there was no good ordering that resulted in a small number of subproblems. What about a tree? Natural candidate for v n is root r of T? Sariel Har-Peled (UIUC) CS374 7 Fall 2017 7 / 26

Towards a Recursive Solution Natural candidate for v n is root r of T? Let O be an optimum solution to the whole problem. Case r O : Then O contains an optimum solution for each subtree of T hanging at a child of r. Case r O : None of the children of r can be in O. O {r} contains an optimum solution for each subtree of T hanging at a grandchild of r. Subproblems? Subtrees of T rooted at nodes in T. How many of them? O(n) Sariel Har-Peled (UIUC) CS374 8 Fall 2017 8 / 26

Towards a Recursive Solution Natural candidate for v n is root r of T? Let O be an optimum solution to the whole problem. Case r O : Then O contains an optimum solution for each subtree of T hanging at a child of r. Case r O : None of the children of r can be in O. O {r} contains an optimum solution for each subtree of T hanging at a grandchild of r. Subproblems? Subtrees of T rooted at nodes in T. How many of them? O(n) Sariel Har-Peled (UIUC) CS374 8 Fall 2017 8 / 26

Towards a Recursive Solution Natural candidate for v n is root r of T? Let O be an optimum solution to the whole problem. Case r O : Then O contains an optimum solution for each subtree of T hanging at a child of r. Case r O : None of the children of r can be in O. O {r} contains an optimum solution for each subtree of T hanging at a grandchild of r. Subproblems? Subtrees of T rooted at nodes in T. How many of them? O(n) Sariel Har-Peled (UIUC) CS374 8 Fall 2017 8 / 26

Towards a Recursive Solution Natural candidate for v n is root r of T? Let O be an optimum solution to the whole problem. Case r O : Then O contains an optimum solution for each subtree of T hanging at a child of r. Case r O : None of the children of r can be in O. O {r} contains an optimum solution for each subtree of T hanging at a grandchild of r. Subproblems? Subtrees of T rooted at nodes in T. How many of them? O(n) Sariel Har-Peled (UIUC) CS374 8 Fall 2017 8 / 26

Towards a Recursive Solution Natural candidate for v n is root r of T? Let O be an optimum solution to the whole problem. Case r O : Then O contains an optimum solution for each subtree of T hanging at a child of r. Case r O : None of the children of r can be in O. O {r} contains an optimum solution for each subtree of T hanging at a grandchild of r. Subproblems? Subtrees of T rooted at nodes in T. How many of them? O(n) Sariel Har-Peled (UIUC) CS374 8 Fall 2017 8 / 26

Example r 10 5 a b 8 4 c d e f g 11 4 9 3 2 h i 7 j 8 Sariel Har-Peled (UIUC) CS374 9 Fall 2017 9 / 26

A Recursive Solution T(u): subtree of T hanging at node u OPT(u): max weighted independent set value in T(u) { v child of u OPT(u) = max OPT(v), w(u) + v grandchild of u OPT(v) Sariel Har-Peled (UIUC) CS374 10 Fall 2017 10 / 26

A Recursive Solution T(u): subtree of T hanging at node u OPT(u): max weighted independent set value in T(u) { v child of u OPT(u) = max OPT(v), w(u) + v grandchild of u OPT(v) Sariel Har-Peled (UIUC) CS374 10 Fall 2017 10 / 26

Iterative Algorithm 1 Compute OPT(u) bottom up. To evaluate OPT(u) need to have computed values of all children and grandchildren of u 2 What is an ordering of nodes of a tree T to achieve above? Post-order traversal of a tree. Sariel Har-Peled (UIUC) CS374 11 Fall 2017 11 / 26

Iterative Algorithm 1 Compute OPT(u) bottom up. To evaluate OPT(u) need to have computed values of all children and grandchildren of u 2 What is an ordering of nodes of a tree T to achieve above? Post-order traversal of a tree. Sariel Har-Peled (UIUC) CS374 11 Fall 2017 11 / 26

Iterative Algorithm MIS-Tree(T): Let v 1, v 2,..., v n be a post-order traversal of nodes of T for i = 1 to n do ( ) v M[v i ] = max j child of v i M[v j ], w(v i ) + v j grandchild of v i M[v j ] return M[v n ] (* Note: v n is the root of T *) Space: O(n) to store the value at each node of T Running time: 1 Naive bound: O(n 2 ) since each M[v i ] evaluation may take O(n) time and there are n evaluations. 2 Better bound: O(n). A value M[v j ] is accessed only by its parent and grand parent. Sariel Har-Peled (UIUC) CS374 12 Fall 2017 12 / 26

Iterative Algorithm MIS-Tree(T): Let v 1, v 2,..., v n be a post-order traversal of nodes of T for i = 1 to n do ( ) v M[v i ] = max j child of v i M[v j ], w(v i ) + v j grandchild of v i M[v j ] return M[v n ] (* Note: v n is the root of T *) Space: O(n) to store the value at each node of T Running time: 1 Naive bound: O(n 2 ) since each M[v i ] evaluation may take O(n) time and there are n evaluations. 2 Better bound: O(n). A value M[v j ] is accessed only by its parent and grand parent. Sariel Har-Peled (UIUC) CS374 12 Fall 2017 12 / 26

Iterative Algorithm MIS-Tree(T): Let v 1, v 2,..., v n be a post-order traversal of nodes of T for i = 1 to n do ( ) v M[v i ] = max j child of v i M[v j ], w(v i ) + v j grandchild of v i M[v j ] return M[v n ] (* Note: v n is the root of T *) Space: O(n) to store the value at each node of T Running time: 1 Naive bound: O(n 2 ) since each M[v i ] evaluation may take O(n) time and there are n evaluations. 2 Better bound: O(n). A value M[v j ] is accessed only by its parent and grand parent. Sariel Har-Peled (UIUC) CS374 12 Fall 2017 12 / 26

Iterative Algorithm MIS-Tree(T): Let v 1, v 2,..., v n be a post-order traversal of nodes of T for i = 1 to n do ( ) v M[v i ] = max j child of v i M[v j ], w(v i ) + v j grandchild of v i M[v j ] return M[v n ] (* Note: v n is the root of T *) Space: O(n) to store the value at each node of T Running time: 1 Naive bound: O(n 2 ) since each M[v i ] evaluation may take O(n) time and there are n evaluations. 2 Better bound: O(n). A value M[v j ] is accessed only by its parent and grand parent. Sariel Har-Peled (UIUC) CS374 12 Fall 2017 12 / 26

Iterative Algorithm MIS-Tree(T): Let v 1, v 2,..., v n be a post-order traversal of nodes of T for i = 1 to n do ( ) v M[v i ] = max j child of v i M[v j ], w(v i ) + v j grandchild of v i M[v j ] return M[v n ] (* Note: v n is the root of T *) Space: O(n) to store the value at each node of T Running time: 1 Naive bound: O(n 2 ) since each M[v i ] evaluation may take O(n) time and there are n evaluations. 2 Better bound: O(n). A value M[v j ] is accessed only by its parent and grand parent. Sariel Har-Peled (UIUC) CS374 12 Fall 2017 12 / 26

Example r 10 5 a b 8 4 c d e f g 11 4 9 3 2 h i 7 j 8 Sariel Har-Peled (UIUC) CS374 13 Fall 2017 13 / 26

Part III Context free grammars: The CYK Algorithm Sariel Har-Peled (UIUC) CS374 14 Fall 2017 14 / 26

Parsing We saw regular languages and context free languages. Most programming languages are specified via context-free grammars. Why? CFLs are sufficiently expressive to support what is needed. At the same time one can efficiently solve the parsing problem: given a string/program w, is it a valid program according to the CFG specification of the programming language? Sariel Har-Peled (UIUC) CS374 15 Fall 2017 15 / 26

CFG specification for C Sariel Har-Peled (UIUC) CS374 16 Fall 2017 16 / 26

Algorithmic Problem Given a CFG G = (V, T, P, S) and a string w T, is w L(G)? That is, does S derive w? Equivalently, is there a parse tree for w? Simplifying assumption: G is in Chomsky Normal Form (CNF) Productions are all of the form A BC or A a. If ɛ L then S ɛ is also allowed. (This is the only place in the grammar that has an ε.) Every CFG G can be converted into CNF form via an efficient algorithm Advantage: parse tree of constant degree. Sariel Har-Peled (UIUC) CS374 17 Fall 2017 17 / 26

Algorithmic Problem Given a CFG G = (V, T, P, S) and a string w T, is w L(G)? That is, does S derive w? Equivalently, is there a parse tree for w? Simplifying assumption: G is in Chomsky Normal Form (CNF) Productions are all of the form A BC or A a. If ɛ L then S ɛ is also allowed. (This is the only place in the grammar that has an ε.) Every CFG G can be converted into CNF form via an efficient algorithm Advantage: parse tree of constant degree. Sariel Har-Peled (UIUC) CS374 17 Fall 2017 17 / 26

CYK Algorithm CYK Algorithm = Cocke-Younger-Kasami algorithm Sariel Har-Peled (UIUC) CS374 18 Fall 2017 18 / 26

Example S ɛ AB XB Y AB XB X AY A 0 B 1 Question: Is 000111 in L(G)? Is 00011 in L(G)? Sariel Har-Peled (UIUC) CS374 19 Fall 2017 19 / 26

Towards Recursive Algorithm Assume G is a CNF grammar. S derives w iff one of the following holds: w = 1 and S w is a rule in P w > 1 and there is a rule S AB and a split w = uv with u, v 1 such that A derives u and B derives v Observation: Subproblems generated require us to know if some non-terminal A will derive a substring of w. Sariel Har-Peled (UIUC) CS374 20 Fall 2017 20 / 26

Towards Recursive Algorithm Assume G is a CNF grammar. S derives w iff one of the following holds: w = 1 and S w is a rule in P w > 1 and there is a rule S AB and a split w = uv with u, v 1 such that A derives u and B derives v Observation: Subproblems generated require us to know if some non-terminal A will derive a substring of w. Sariel Har-Peled (UIUC) CS374 20 Fall 2017 20 / 26

Recursive solution 1 Input: w = w 1 w 2... w n 2 Assume r non-terminals in G: R 1,..., R r. 3 R 1 : Start symbol. 4 f (l, s, b): TRUE w s w s+1..., w s+l 1 L(R b ). = Substring w starting at pos l of length s is deriveable by R b. ) 5 Recursive formula: f (1, s, a) is 1 iff (R a w s G. 6 For l > 1: f (l, s, a) = l 1 p=1 (R a R b R c) G ( ) f (p, s, b) f (l p, s + p, c) 7 Output: w L(G) f (n, 1, 1) = 1. Sariel Har-Peled (UIUC) CS374 21 Fall 2017 21 / 26

Recursive solution 1 Input: w = w 1 w 2... w n 2 Assume r non-terminals in G: R 1,..., R r. 3 R 1 : Start symbol. 4 f (l, s, b): TRUE w s w s+1..., w s+l 1 L(R b ). = Substring w starting at pos l of length s is deriveable by R b. ) 5 Recursive formula: f (1, s, a) is 1 iff (R a w s G. 6 For l > 1: f (l, s, a) = l 1 p=1 (R a R b R c) G ( ) f (p, s, b) f (l p, s + p, c) 7 Output: w L(G) f (n, 1, 1) = 1. Sariel Har-Peled (UIUC) CS374 21 Fall 2017 21 / 26

Analysis Assume G = {R 1, R 2,..., R r } with start symbol R 1 Number of subproblems: O(rn 2 ) Space: O(rn 2 ) Time to evaluate a subproblem from previous ones: O( P n) where P is set of rules Total time: O( P rn 3 ) which is polynomial in both w and G. For fixed G the run time is cubic in input string length. Running time can be improved to O(n 3 P ). Not practical for most programming languages. Most languages assume restricted forms of CFGs that enable more efficient parsing algorithms. Sariel Har-Peled (UIUC) CS374 22 Fall 2017 22 / 26

CYK Algorithm Input string: X = x 1... x n. Input grammar G: r nonterminal symbols R 1...R r, R 1 P[n][n][r]: Array of booleans. Initialize all to FALSE for s = 1 to n do for each unit production R v x s do P[1][s][v] TRUE for l = 2 to n do // Length of span for s = 1 to n l + 1 do // Start of span for p = 1 to l 1 do // Partition of span for all (R a R b R c ) G do if P[p][s][b] and P[l p][s + p][c] then P[l][s][a] TRUE if P[n][1][1] is TRUE then return ``X is member of language'' else return ``X is not member of language'' start symbol. Sariel Har-Peled (UIUC) CS374 23 Fall 2017 23 / 26

Example S ɛ AB XB Y AB XB X AY A 0 B 1 Question: Is 000111 in L(G)? Is 00011 in L(G)? Order of evaluation for iterative algorithm: increasing order of substring length. Sariel Har-Peled (UIUC) CS374 24 Fall 2017 24 / 26

Example S ɛ AB XB Y AB XB X AY A 0 B 1 Sariel Har-Peled (UIUC) CS374 25 Fall 2017 25 / 26

Takeaway Points 1 Dynamic programming is based on finding a recursive way to solve the problem. Need a recursion that generates a small number of subproblems. 2 Given a recursive algorithm there is a natural DAG associated with the subproblems that are generated for given instance; this is the dependency graph. An iterative algorithm simply evaluates the subproblems in some topological sort of this DAG. 3 The space required to evaluate the answer can be reduced in some cases by a careful examination of that dependency DAG of the subproblems and keeping only a subset of the DAG at any time. Sariel Har-Peled (UIUC) CS374 26 Fall 2017 26 / 26