CS 21 Decidability and Tractability Lecture 9 January
CS 21 Decidability and Tractability Lecture 9 January 25, 2021 CS 21 Lecture 9 1
Outline • Proof of the CFL Pumping Lemma (continued) Chomsky Normal Form and deciding CFLs Turing Machines and variants • • – – multitape TMs nondeterministic TMs January 25, 2021 CS 21 Lecture 9 2
Pumping Lemma for CFLs • January 25, 2021 CS 21 Lecture 9 3
CFL Pumping Lemma • long path S A A . . . S C S D a A C b A A CS 21 Lecture 9 C . . . B A D a b b S S b b some non-terminal must repeat a a on long path January 25, 2021 . . . B D C B A b b a 4
CFL Pumping Lemma S • Schematic proof: S A A u v x S u v A y z A u January 25, 2021 CS 21 Lecture 9 v A y z v x y 5
CFL Pumping Lemma S • Schematic proof: S A A u v x u A z y z x u January 25, 2021 CS 21 Lecture 9 z 6
CFL Pumping Lemma – how large should pumping length p be? – need to ensure other conditions: |vy| > 0 |vxy| ≤ p – b = max # symbols on rhs of any production (assume b ≥ 2) – if parse tree has height ≤ h, then string generated has length ≤ bh (so length > bh implies height > h) January 25, 2021 CS 21 Lecture 9 7
CFL Pumping Lemma – let m be the # of nonterminals in the grammar – to ensure path of length at least m+2, require |w| ≥ p = bm+2 – since |w| > bm+1, any parse tree for w has height > m+1 – let T be the smallest parse tree for w – longest root-leaf path must consist of ≥ m+1 non-terminals and 1 terminal. January 25, 2021 CS 21 Lecture 9 8
CFL Pumping Lemma S • A A u v x y z – is |vy| > 0 ? • smallest parse tree T ensures – is |vxy| ≤ p? • red path has length ≤ m+2, so ≤ bm+2 = p leaves January 25, 2021 CS 21 Lecture 9 9
Chomsky Normal Form • Useful to deal only with CFGs in a simple normal form • Most common: Chomsky Normal Form (CNF) • Definition: every production has form A → BC or S→ε A→a or where A, B, C are any non-terminals (and B, C are not S) and a is any terminal. January 25, 2021 CS 21 Lecture 9 10
Chomsky Normal Form Theorem: Every CFL is generated by a CFG in Chomsky Normal Form. Proof: exercise or in book… January 25, 2021 CS 21 Lecture 9 11
Deciding CFLs • Useful to have an efficient algorithm to decide whether string x is in given CFL – e. g. programming language often described by CFG. Determine if string is valid program. • If CFL recognized by deterministic PDA, just simulate the PDA. – but not all CFLs are (homework)… • Can simulate NPDA, but this takes exponential time in the worst case. January 25, 2021 CS 21 Lecture 9 12
Deciding CFLs • Convert CFG into Chomsky Normal Form • parse tree for string x generated by nonterminal A: A B C x January 25, 2021 CS 21 Lecture 9 13
Deciding CFLs • An algorithm: Is. Generated(x, A) if |x| = 1, then return YES if A → x is a production, else return NO for all n-1 ways of splitting x = yz for all ≤ m productions of form A → BC if Is. Generated(y, B) and Is. Generated(z, C), return YES return NO • worst case running time? January 25, 2021 CS 21 Lecture 9 14
Deciding CFLs • January 25, 2021 CS 21 Lecture 9 15
Deciding CFLs • January 25, 2021 CS 21 Lecture 9 16
Deciding CFLs • O(nm) steps O(n 3 m 3) steps January 25, 2021 CS 21 Lecture 9 17
Deterministic PDA • January 25, 2021 CS 21 Lecture 9 18
Deterministic PDA • A technical detail: we will give our deterministic machine the ability to detect end of input string – add special symbol ■ to alphabet – require input tape to contain x■ • language recognized by a deterministic PDA is called a deterministic CFL (DCFL) January 25, 2021 CS 21 Lecture 9 19
Example deterministic PDA 0, ε → 0 ε, ε → $ 1, 0 → ε ■, $ → ε 1, 0 → ε • January 25, 2021 CS 21 Lecture 9 20
Deterministic PDA Theorem: DCFLs are closed under complement (complement of L in Σ* is (Σ* - L) ) Proof attempt: – swap accept/non-accept states – problem: might enter infinite loop before reading entire string – machine for complement must accept in these cases, and read to end of string January 25, 2021 CS 21 Lecture 9 21
Example of problem 1, ε → ε 0, ε → ε ■, ε → ε 1, ε → ε ε, ε → $ January 25, 2021 CS 21 Lecture 9 22
Example of problem 1, ε → ε 0, ε → ε ■, ε → ε 1, ε → ε ε, ε → $ January 25, 2021 CS 21 Lecture 9 23
Deterministic PDA Proof: – convert machine into “normal form” • always reads to end of input • always enters either an accept state or single distinguished “reject” state – step 1: keep track of when we have read to end of input – step 2: eliminate infinite loops January 25, 2021 CS 21 Lecture 9 24
Deterministic PDA step 1: keep track of when we have read to end of input q 0 ’ q 1 ■, ? → ? q 3 January 25, 2021 q 1 ’ ■, ? → ? q 3 ’ q 2 CS 21 Lecture 9 q 2 ’ 25
Deterministic PDA step 1: keep track of when we have read to end of input q 0 q 1 ■, ? → ? q 0 ’ q 1 ’ ■, ? → ? q 3 ’ q 2 ’ for accept state q’: replace outgoing “ε, ? → ? ” transition with self-loop with same label January 25, 2021 CS 21 Lecture 9 26
Deterministic PDA step 2: eliminate infinite loops – add new “reject” states a, t →t (for all a, t) r ε, t → t (for all t) r’ ■, t → t (for all t) January 25, 2021 CS 21 Lecture 9 27
Deterministic PDA step 2: eliminate infinite loops – on input x, if infinite loop, then: stack height time i 0 January 25, 2021 i 2 i 3 infinite sequence i 0< i 1< i 2< … such that for all k, stack height never decreases below ht(ik) after time ik CS 21 Lecture 9 28
Deterministic PDA step 2: eliminate infinite loops – infinite seq. i 0< i 1< … such that for all k, stack height never decreases below ht(ik) after time ik – infinite subsequence j 0< j 1< j 2< … such that same transition is applied at each time jk • never see any stack symbol below t from jk on p ε, t → s January 25, 2021 • we are in a periodic, deterministic sequence of stack operations independent of the input CS 21 Lecture 9 29
Deterministic PDA step 2: eliminate infinite loops – infinite subsequence j 0< j 1< j 2< … such that same transition is applied at each time jk – safe to replace: a, t →t (for all a, t) ε, t → t (for all t) ε, t → s p r’ r ε, t → s or ε, t → s ■, t → t (for all t) p’ January 25, 2021 ε, t → s CS 21 Lecture 9 30
Deterministic PDA – finishing up… – have a machine M with no infinite loops – therefore it always reads to end of input – either enters an accept state q’, or enters “reject” state r’ – now, can swap: make r’ unique accept state to get a machine recognizing complement of L January 25, 2021 CS 21 Lecture 9 31
Summary • Nondeterministic Pushdown Automata (NPDA) • Context-Free Grammars (CFGs) describe Context-Free Languages (CFLs) – terminals, non-terminals – productions – yields, derivations – parse trees January 25, 2021 CS 21 Lecture 9 32
Summary – grouping determined by grammar – Chomsky Normal Form (CNF) • NDPAs and CFGs are equivalent • CFL Pumping Lemma is used to show certain languages are not CFLs January 25, 2021 CS 21 Lecture 9 33
Summary • deterministic PDAs recognize DCFLs • DCFLs are closed under complement • there is an efficient algorithm (based on dynamic programming) to determine if a string x is generated by a given grammar G January 25, 2021 CS 21 Lecture 9 34
So far… • several models of computation – finite automata – pushdown automata • fail to capture our intuitive notion of what is computable all languages regular languages context free languages January 25, 2021 CS 21 Lecture 9 35
So far… • We proved (using constructions of FA and NPDAs and the two pumping lemmas): {anbn : n ≥ 0 } regular languages all languages context free languages {anbncn : n ≥ 0 } January 25, 2021 CS 21 Lecture 9 36
A more powerful machine • limitation of NPDA related to fact that their memory is stack-based (last in, first out) • What is the simplest alteration that adds general-purpose “memory” to our machine? • Should be able to recognize, e. g. , {anbncn : n ≥ 0 } January 25, 2021 CS 21 Lecture 9 37
Turing Machines input tape finite control q 0 0 1 1 1 0 0 … read/write head • New capabilities: – infinite tape – can read OR write to tape – read/write head can move left and right January 25, 2021 CS 21 Lecture 9 38
Turing Machine • Informal description: – input written on left-most squares of tape – rest of squares are blank – at each point, take a step determined by • current symbol being read • current state of finite control – a step consists of • writing new symbol • moving read/write head left or right • changing state January 25, 2021 CS 21 Lecture 9 39
Example Turing Machine • input tape finite control q 0 January 25, 2021 0 1 # 0 1 … read/write head CS 21 Lecture 9 40
Turing Machine diagrams a → b, L a→R start state qreject states (1 accept + 1 reject) b→L b→R qaccept b → a, R transition label: (tape symbol read → tape symbol written, direction moved) “_” means blank tape square – a → R means “read a, move right” – a → L means “read a, move left” – a → b, R means “read a, write b, move right January 25, 2021 CS 21 Lecture 9 41
Example TM diagram 0, 1 → R q 1 #→R 0, 1 → R 0, 1, # → L _→R q 3 0 → _, R q 5 0 → x, R 0, 1, x, # → L q 0 q 11 #→R 0, 1 → R January 25, 2021 q 12 q 7 q 4 _→L 0, 1 → R q 6 _→R 0, 1, # → L CS 21 Lecture 9 #→R x→R q 9 0 → x, L x→R #→R _→R q 13 1 → x, R 1 → _, R q 2 _→R 0, 1 → R qaccept 1 → x, L q 8 #→R 0, 1 → R q 10 x→R 42
TM formal definition • January 25, 2021 CS 21 Lecture 9 43
- Slides: 43