COMP-330 Theory of Computation - McGill...

Preview:

Citation preview

COMP-330 Theory of Computation

Fall 2017 -- Prof. Claude Crépeau

LECTURE 3 :(NON) Deterministic F A

COMP 330 Fall 2017: Lectures Schedule

14. Context-free languages15. Pushdown automata16. Parsing17. The pumping lemma for CFLs18. Introduction to computability19. Models of computation Basic computability theory20. Reducibility, undecidability and Rice’s theorem21. Undecidable problems about CFGs22. Post Correspondence Problem23. Validity of FOL is RE / Gödel’s and Tarski’s thms24. Universality / The recursion theorem 25. Degrees of undecidability26. Introduction to complexity

1-2. Introduction 1.5. Some basic mathematics2-3. Deterministic finite automata +Closure properties,3-4. Nondeterministic finite automata5. Minimization+ Myhill-Nerode theorem 6. Determinization+Kleene’s theorem7. Regular Expressions+GNFA8. Regular Expressions and Languages9-10. The pumping lemma11. Duality12. Labelled transition systems13. MIDTERM

Regular Languages

Let M=(Q,∑, δ,q0,F) be a finite state automaton and let w=w1w2...wn (n≥0) be a string where each symbol wi is from the alphabet ∑.

M accepts w if states s0,s1,...,sn exist s.t. 1. s0 = q0 2. si+1 = δ(si,wi+1) for i = 0 ... n-1, and 3. sn ∈ F

ExampleEx

ampl

e10010101

q1

M1

Regular Languages

M1 accepts 10010101 since states s0,s1,...,s8 exist s.t.

1. s0 = q1

2. s1 = q2 = δ(q1,1), s2 = q3 = δ(q2,0), s3 = q2 = δ(q3,0), s4 = q2 = δ(q2,1), s5 = q3 = δ(q2,0), s6 = q2 = δ(q3,1), s7 = q3 = δ(q2,0), s8 = q2 = δ(q3,1)

3. s8 ∈ F

Regular LanguagesLet M be a finite state automaton and let w=w1w2...wn (n≥0) be a string where each symbol wi is from the alphabet ∑.

M recognizes language A if A = { w | M accepts w }

Proving the language M1 accepts...

Theorem 1.A: L(M1) = { All binary strings that contain at least one “1” and end with an even number of “0”s }

Theorem 1.B : Let w∈{0,1}* be of length n≥0.1) M1 stops in state q1 ⟺ w contains no “1”s.2) M1 stops in state q2 ⟺ w contains at least one “1” and ends with an even number of “0”s.3) M1 stops in state q3 ⟺ w contains at least one “1” and ends with an odd number of “0”s.

Theorem 1.B : Let w∈{0,1}* be of length n≥0.1) M1 stops in state q1 ⟺ w contains no “1”s.2) M1 stops in state q2 ⟺ w contains at least one “1” and ends with an even number of “0”s.3) M1 stops in state q3 ⟺ w contains at least one “1” and ends with an odd number of “0”s.

q1

Theorem 1.B : Let w∈{0,1}* be of length n≥0.1) M1 stops in state q1 ⟺ w contains no “1”s.2) M1 stops in state q2 ⟺ w contains at least one “1” and ends with an even number of “0”s.3) M1 stops in state q3 ⟺ w contains at least one “1” and ends with an odd number of “0”s.

q2

Theorem 1.B : Let w∈{0,1}* be of length n≥0.1) M1 stops in state q1 ⟺ w contains no “1”s.2) M1 stops in state q2 ⟺ w contains at least one “1” and ends with an even number of “0”s.3) M1 stops in state q3 ⟺ w contains at least one “1” and ends with an odd number of “0”s.

q3

Theorem 1.B ⟹ Theorem 1.A

Proof of Theorem 1.B by induction.

q1

Induction basis

Let w∈{0,1}* be a string of length n=0, w=ℇ.

The evaluation of w by M1 stops in state q1 right after starting and rejects w.

Therefore, 1) is valid because w=ℇ does not contain a “1”. Since there are no strings containing a “1” and no strings leading to q2 or q3, 2) and 3) are also valid.

1) M1 stops in state q1 ⟺ w contains no “1”s.2) M1 stops in state q2 ⟺ w contains at least one “1” and ends with an even number of “0”s.3) M1 stops in state q3 ⟺ w contains at least one “1” and ends with an odd number of “0”s.

Induction Step : Let w∈{0,1}* be a string of length n>0.

We assume for Induction Hypothesis that 1), 2), and 3) are valid for n-1 and all strings v of size n-1.

We now prove that 1), 2), and 3) are also valid for n and all strings w of size n.

1) M1 stops in state q1 ⟺ w contains no “1”s.2) M1 stops in state q2 ⟺ w contains at least one “1” and ends with an even number of “0”s.3) M1 stops in state q3 ⟺ w contains at least one “1” and ends with an odd number of “0”s.

Let w∈{0,1}* be a string of length n>0.

If w ends with a “0” then it means that w = v0 with v a string of length n-1. Let q be the state in which M1 ends when evaluating v.

If q=q1 then by induction we have that v=0n-1 and therefore w=0n contains no “1”, proving 1).

q1

0

1) M1 stops in state q1 ⟺ w contains no “1”s.

If q=q2 then by induction we have that v contains at least one “1” and ends with an even number of “0”s.

Therefore w contains at least one “1” and ends with an odd number of “0”s., proving 3).

q3

0

3) M1 stops in state q3 ⟺ w contains at least one “1” and ends with an odd number of “0”s.

If q=q3 then by induction we have that v contains at least one “1” and ends with an odd number of “0”s.

Therefore w contains at least one “1” and ends with an even number of “0”s greater than zero, proving part of 2).

0

q2

2) M1 stops in state q2 ⟺ w contains at least one “1” and ends with an even number of “0”s.

If w ends with a “1” then it means that w = v1 with v a string of length n-1. Let q be the state in which M1 ends when evaluating v.

By examination of δ we conclude that for all q, δ(q,1)=q2. Thus M1 accepts w and 2) is valid whenever w ends with zero “0”s. This completes the proof of 2) and of the Thm. QED

q2

1

1

1

2) M1 stops in state q2 ⟺ w contains at least one “1” and ends with an even number of “0”s.

Another example: multiples of 3...

Remember what you learned in elementary school: N is a multiple of 3 if N=0,3,6,9 or if the sum of its digits is a multiple of 3...

Example: 54708 is a multiple of 3 because the sum of its digits 5+4+7+0+8=24 is a multiple of 3. We know that because the sum of its digits 2+4=6 is a multiple of 3.

0 MOD 3 (base 10)

q0

0,3,6,9

1,4,7

2,5,8

0,3,6,9

0,3,6,9

1,4,7

2,5,8 q1

q2

1,4,7 2,5,8

gcd(B,N) ≠ 0

M3,10

0 MOD 3 (base 10)

Theorem 1.C : Let w∈{0,1,...,9}* be of length n≥0.1) M1 stops in state q0 ⟺ w = 0 mod 3.2) M1 stops in state q1 ⟺ w = 1 mod 3.3) M1 stops in state q2 ⟺ w = 2 mod 3.

54708

q0

0,3,6,9

1,4,7

2,5,8

0,3,6,9

0,3,6,9

1,4,7

2,5,8 q1

q2

1,4,7 2,5,8

M3,10

M3,10 stops in state qr ⟺ w = r mod 3

54708

q0

0,3,6,9

1,4,7

2,5,8

0,3,6,9

0,3,6,9

1,4,7

2,5,8 q1

q2

1,4,7 2,5,8

M3,10

M3,10 stops in state qr ⟺ w = r mod 3

54708

q0

0,3,6,9

1,4,7

2,5,8

0,3,6,9

0,3,6,9

1,4,7

2,5,8 q1

q2

1,4,7 2,5,8

M3,10

M3,10 stops in state qr ⟺ w = r mod 3

54708

q0

0,3,6,9

1,4,7

2,5,8

0,3,6,9

0,3,6,9

1,4,7

2,5,8 q1

q2

1,4,7 2,5,8

M3,10

M3,10 stops in state qr ⟺ w = r mod 3

54708

q0

0,3,6,9

1,4,7

2,5,8

0,3,6,9

0,3,6,9

1,4,7

2,5,8 q1

q2

1,4,7 2,5,8

M3,10

M3,10 stops in state qr ⟺ w = r mod 3

54708

q0

0,3,6,9

1,4,7

2,5,8

0,3,6,9

0,3,6,9

1,4,7

2,5,8 q1

q2

1,4,7 2,5,8

M3,10

M3,10 stops in state qr ⟺ w = r mod 3

54708

q0 ∈F

is a multiple of 3

M3,10 stops in state qr ⟺ w = r mod 3

54709

q0

0,3,6,9

1,4,7

2,5,8

0,3,6,9

0,3,6,9

1,4,7

2,5,8 q1

q2

1,4,7 2,5,8

M3,10

M3,10 stops in state qr ⟺ w = r mod 3

54709

q0

0,3,6,9

1,4,7

2,5,8

0,3,6,9

0,3,6,9

1,4,7

2,5,8 q1

q2

1,4,7 2,5,8

M3,10

M3,10 stops in state qr ⟺ w = r mod 3

54709

q1 ∉F

is NOT a multiple of 3

M3,10 stops in state qr ⟺ w = r mod 3

Examples: automata for multiples of N base B

automata for multiples of N = 0 mod N

examples mod 2, mod 3, mod 7

0 MOD 2 (base 10)

q1q0

0,2,4,6,8 1,3,5,7,90,2,4,6,8

1,3,5,7,9

M2,10

Remember what you learned in elementary school: N is a multiple of 2 iff it ends by 0,2,4,6 or 8.

M2,10 stops in state qr ⟺ w = r mod 2

0 MOD 2 (base 2)

q1q0

0 10

1

M2,2

Remember what you learned in school of CS: N (in binary) is a multiple of 2 iff it ends by 0.

M2,2 stops in state qr ⟺ w = r mod 2

0 MOD 3 (base 2)gcd(B,N) ≠ 0

M3,2

1

q0

0

10

1q1

q2

0

M3,2 stops in state qr ⟺ w = r mod 3

2

0 MOD 3 (base 3)

q0

0

1

2

1

0

0 q1

q2

12

gcd(B,N) = 0

M3,3

M3,3 stops in state qr ⟺ w = r mod 3

COMP-330 Theory of Computation

Fall 2017 -- Prof. Claude Crépeau

LECTURE 3 :(NON) Deterministic F A

Recommended