lecture 16 - iitk - indian institute of technology...

54
MTH401A Theory of Computation Lecture 16

Upload: letuong

Post on 25-Mar-2018

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

MTH401ATheory of Computation

Lecture 16

Page 2: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Outside Context Free Languages

Akash Anand

Context sensitive grammar for 0n1n0n !• S → LDABCR

• LDA → LAAD• ADA → AAD

• ADB → ABBD

• BDB → BBD

• BDC → BCCD• CDC → CCD

• DR → ER

• CE → EC

• BE → EB

• AE → EA• LE → LD• A → 0• B → 1• C → 0

• R → ε• LD → ε

Page 3: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Union

Akash Anand

Page 4: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Union

Akash Anand

Grammar 1

• S1 → …

• - - - -• - - - -

• - - - -

Grammar 2

• S2 → …

• - - - -• - - - -

• - - - -

Page 5: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Union

Akash Anand

Grammar 1

• S1 → …

• - - - -• - - - -

• - - - -

Grammar 2

• S2 → …

• - - - -• - - - -

• - - - -

Page 6: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Union

Akash Anand

Grammar 1

• S1 → …

• - - - -• - - - -

• - - - -

Grammar 2

• S2 → …

• - - - -• - - - -

• - - - -

Union

• S → S1 | S2

• S1 → …• S2 → …

• - - - -

Page 7: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Union

Akash Anand

Grammar 1

• S1 → …

• - - - -• - - - -

• - - - -

Grammar 2

• S2 → …

• - - - -• - - - -

• - - - -

Union

• S → S1 | S2

• S1 → …• S2 → …

• - - - -CFL : Closed under union.

Page 8: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Concatenation

Akash Anand

Grammar 1

• S1 → …

• - - - -• - - - -

• - - - -

Grammar 2

• S2 → …

• - - - -• - - - -

• - - - -

Page 9: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Concatenation

Akash Anand

Grammar 1

• S1 → …

• - - - -• - - - -

• - - - -

Grammar 2

• S2 → …

• - - - -• - - - -

• - - - -

Concatenation

• S → S1S2

• S1 → …

• S2 → …

• - - - -

Page 10: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Concatenation

Akash Anand

Grammar 1

• S1 → …

• - - - -• - - - -

• - - - -

Grammar 2

• S2 → …

• - - - -• - - - -

• - - - -

Concatenation

• S → S1S2

• S1 → …

• S2 → …

• - - - -CFL : Closed under concatenation.

Page 11: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Intersection

Akash Anand

Page 12: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Intersection

Akash Anand

L1 = {0p1m0m : m ≥ 0, p ≥ 0}

Page 13: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Intersection

Akash Anand

L1 = {0p1m0m : m ≥ 0, p ≥ 0}

• Concatenation of {0}* and {1m0m : m ≥ 0}!

Page 14: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Intersection

Akash Anand

L1 = {0p1m0m : m ≥ 0, p ≥ 0}

• Concatenation of {0}* and {1m0m : m ≥ 0}!

• S1 → 0S1 | ε

Page 15: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Intersection

Akash Anand

L1 = {0p1m0m : m ≥ 0, p ≥ 0}

• Concatenation of {0}* and {1m0m : m ≥ 0}!

• S1 → 0S1 | ε• S2 → 1S20 | ε

Page 16: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Intersection

Akash Anand

L1 = {0p1m0m : m ≥ 0, p ≥ 0}

• Concatenation of {0}* and {1m0m : m ≥ 0}!

• S → S1S2

• S1 → 0S1 | ε• S2 → 1S20 | ε

Page 17: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Intersection

Akash Anand

L2 = {0n1n0q : n ≥ 0, q ≥ 0}

Page 18: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Intersection

Akash Anand

L2 = {0n1n0q : n ≥ 0, q ≥ 0}

• Again a concatenation! Of {0n1n : n ≥ 0}and {0}* !

Page 19: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Intersection

Akash Anand

L2 = {0n1n0q : n ≥ 0, q ≥ 0}

• Again a concatenation! Of {0n1n : n ≥ 0}and {0}* !

• S1 → 0S11 | ε

Page 20: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Intersection

Akash Anand

L2 = {0n1n0q : n ≥ 0, q ≥ 0}

• Again a concatenation! Of {0n1n : n ≥ 0}and {0}* !

• S1 → 0S11 | ε• S2 → 0S2 | ε

Page 21: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Intersection

Akash Anand

L2 = {0n1n0q : n ≥ 0, q ≥ 0}

• Again a concatenation! Of {0n1n : n ≥ 0}and {0}* !

• S → S1S2

• S1 → 0S11 | ε• S2 → 0S2 | ε

Page 22: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Intersection

Akash Anand

L1 ∩ L2

Page 23: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Intersection

Akash Anand

L1 ∩ L2

• L1 ∩ L2 = {0k1k0k: k ≥ 0} !

• Not context free !

Page 24: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Intersection

Akash Anand

L1 ∩ L2

• L1 ∩ L2 = {0k1k0k: k ≥ 0} !

• Not context free !

CFL : Not closed under intersection.

Page 25: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Complement

Akash Anand

Page 26: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Complement

Akash Anand

• Not closed under intersection ⇒ Complement may or may not be context free !

Page 27: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Complement

Akash Anand

• Not closed under intersection ⇒ Complement may or may not be context free !

CFL : Not closed under complement.

Page 28: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Page 29: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

For every context free language, L, the language L – {ε} has a grammar in which every production looks like, either

1. S → AB , or2. B → a

Chomsky Normal Form for CFG’s

Akash Anand

Page 30: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

For every context free language, L, the language L – {ε} has a grammar in which every production looks like, either

1. S → AB , or2. B → a

Chomsky Normal Form for CFG’s

Akash Anand

Body has exactly two non- terminal

symbols.

Page 31: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Body has exactly two non- terminal

symbols.Body has a single terminal symbol.

For every context free language, L, the language L – {ε} has a grammar in which every production looks like, either

1. S → AB , or2. B → a

Page 32: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Page 33: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Algorithm to eliminate variables that derive nothing :

1. Discover all variables that derive terminal strings.

2. For all other variables, remove all productions in which they appear either on the left or the right.

Page 34: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Example : Eliminate VariablesS → AB | C A → 0A | 0 B → 1B C → 1

• Basis : A and C are identified because of A → 0 and C → 1.

• Induction : S is identified because of S →C.

• Nothing else can be identified.

• S → C A → 0A | 0 C → 1

Page 35: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Algorithm to eliminate unreachable symbols :

1. Basis : We can reach S (the start symbol).

2. Induction : If we can reach A and A → αthen we can reach all symbols in α.

3. Algorithm : Remove from the grammar all symbols not reachable from S and all productions that involve these symbols.

Page 36: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Eliminating useless symbols :• A symbol is useful if it appears in some

derivation of some terminal string from the start symbol.

• It is useless, otherwise. Eliminate all useless symbols by :1. Eliminating symbols that derive no

terminal string.2. Eliminate unreachable symbols.

Page 37: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Example : Eliminate Useless SymbolsS → AB A → C | 0 B → 0B C → 1

1. If we eliminate unreachable symbols first we would find that everything is reachable.

2. A, C, 0, 1 would never get eliminated.

Page 38: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

ε- Productions :

1. We can almost avoid productions of the form A → ε.• The problem is that ε can not be in the

language of any grammar that has no ε-productions.

2. Theorem : If L is a CFL, then L – {ε} has a CFG with no ε- productions.

Page 39: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Discovering Nullable Symbols :1. To eliminate all ε- productions, we first

need to discover all nullable variables (variables A such that A⇒*ε).

2. Basis : If there is a production A → ε then A is nullable.

3. Induction : If there is a production A → αand all symbols in α are nullable then Ais nullable.

Page 40: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Example : Eliminate ε- Productions S → ABC A → 0A |ε B → 1B |ε C →ε

1. A, B, C, S are nullable.2. New grammar

S → ABC | AB | AC | BC | A | B | CA → 0A | 0B → 1B | 1

Page 41: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Example : Eliminate ε- Productions S → ABC A → 0A |ε B → 1B |ε C →ε

1. A, B, C, S are nullable.2. New grammar

S → ABC | AB | AC | BC | A | B | CA → 0A | 0B → 1B | 1

C is now useless. Eliminate its productions

Page 42: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Example : Eliminate ε- Productions S → ABC A → 0A |ε B → 1B |ε C →ε

1. A, B, C, S are nullable.2. New grammar

S → ABC | AB | AC | BC | A | B | CA → 0A | 0B → 1B | 1

C is now useless. Eliminate its productions

Page 43: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Unit Productions :• A unit production is one whose right side

consists of exactly one variable.• Algorithm : Find all pairs (A,B) such that

A ⇒* B by a sequence of unit productions only.

• Basis : Pairs (A,A) are there.• Induction : If we have found (A,B) and B

→ C is a unit production then add (A,C).

Page 44: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Cleaning Up a Grammar :• Theorem : If L is a CFL, then there is a

CFG for L – {ε} that has

• No useless symbols.• No ε- productions.

• No unit productions.• In other words, every right side is either

a single terminal or has length ≥ 2.

Page 45: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Chomsky Normal Form Steps:Step 1 : Clean up the grammar.Step 2 : For each right side that is not a single

terminal, make it all variables.• For each terminal symbol a, create a new variable

Aa and a production Aa → a.

• Replace a by Aa in right sides of length ≥ 2.

Step 3 : Break right sides of length longer than 2 into a chain of productions with right sides of two variables.

Page 46: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Example : TerminalsS → 0A1B | DEFA → CC

Page 47: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Example : TerminalsS → 0A1B | DEFA → CCZ → 0O → 1

Page 48: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Example : TerminalsS → ZAOB | DEFA → CCZ → 0O → 1

Page 49: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Example : Long productionsS → ZAOB | DEFA → CCZ → 0O → 1

Page 50: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Example : Long productionsS → ZAOB | DEFA → CCZ → 0O → 1

Too long.

Page 51: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Example : Long productionsS → ZAOB | DEFA → CCZ → 0O → 1

Too long.

New group of symbols.

Page 52: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Example : Long productionsS → ZM | DEFA → CCZ → 0O → 1M → AOB

Page 53: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Example : Long productionsS → ZM | DEFA → CCZ → 0O → 1M → AOB

New group of symbols.

Page 54: Lecture 16 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture16.pdf ·  · 2016-11-11Theory of Computation Lecture 16. ... terminal symbol

Chomsky Normal Form for CFG’s

Akash Anand

Example : Long productionsS → ZM | DHA → CCZ → 0O → 1M → AN

N → OBH → EF