cs332 elements of theory of computation · cs332 elements of theory of computation author: sofya...

26
9/10/2019 Sofya Raskhodnikova Intro to Theory of Computation LECTURE 3 Last time: DFAs and NFAs Operations on languages Today: Nondeterminism Equivalence of NFAs and DFAs Closure properties of regular languages L3.1 Sofya Raskhodnikova; based on slides by Nick Hopper

Upload: others

Post on 13-Jul-2020

18 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

9/10/2019

Sofya Raskhodnikova

Intro to Theory of Computation

LECTURE 3Last time:

• DFAs and NFAs

• Operations on languages

Today:

• Nondeterminism

• Equivalence of NFAs and DFAs

• Closure properties of regular

languages

L3.1Sofya Raskhodnikova; based on slides by Nick Hopper

Page 2: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Nondeterminism

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

Nondeterministic Finite Automaton (NFA)

accepts a string w if there is a way to

make it reach an accept state on input w.

L3.2

0

0

ε

Page 3: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Example

L2.39/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

1

0

0

L(M)={1,00}

ε

ε

Page 4: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Example

L2.49/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

0,1

0,ε 1

0,1

1

L(M)={w | w contains 101 or 11}

Page 5: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Exercise

What is the language of this NFA?

(0𝑘 means 00…0 )

A. {0𝑘 ∣ 𝑘 is a multiple of 2}.

B. {0𝑘 ∣ 𝑘 is a multiple of 3}.

C. {0𝑘 ∣ 𝑘 is a multiple of 6}.

D. {0𝑘 ∣ 𝑘 is a multiple of 2 or 3}.

E. None of the above.

0

00

ε

ε

0

0

𝑘

Page 6: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Formal Definition

9/10/2019

Q is the set of states

Σ is the alphabet

q0 Q is the start state

F Q is the set of accept states

• An NFA is a 5-tuple M = (Q, Σ, , q0, F)

L3.6Sofya Raskhodnikova; based on slides by Nick Hopper

: Q Σε → P(Q) is the transition function

• Σε = Σ {ε} and P(Q) is the set of subsets of Q

• M accepts a string w if there is a path from q0 to

an accept state that w follows.

Page 7: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Example

L3.89/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

0,1

0,ε 1

0,1

1

(q1,ε) =

N = (Q, Σ, , q0, F)

Q = {q0, q1, q2, q3}

Σ = {0,1}

F = {q3}

(q0,1) = {q0, q1, q2}

{q1,q2}

(q2,0) =

q1 q2 q3q0

(q0,0) = {q0}

Page 8: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Nondeterminism

Ways to think about

nondeterminism

• parallel

computation

• tree of possible

computations

• guessing and

verifying the

“right” choice

L3.9

Deterministic

ComputationNondeterministic

Computation

accept or reject accept

reject

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

Page 9: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

NFAs ARE SIMPLER THAN DFAs

An NFA that recognizes the language {1}:

1

1 0,1

0,10

A DFA that recognizes

the language {1}:

L3.10

Page 10: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

A DFA recognizing {1}

Theorem. Every DFA for language {1} must have

at least 3 states.

Proof:

L3.119/10/2019

Page 11: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Equivalence of NFAs & DFAs

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

Theorem. Every NFA has an equivalent DFA.

Corollary: A language is regular iff

it is recognized by an NFA.

L3.12

Page 12: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Input: N = (Q, Σ, , q0, F)

Intuition: Do the computation

in parallel, maintaining the set

of states where all threads are.

Q = P(Q)

Idea:

accept

reject

NFA to DFA Conversion

Output: M = (Q, Σ, , q0, F)

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper L3.13

Page 13: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

(R,) = for all R⊆Q and ∈Σ.

Q = P(Q)

: Q Σ → Q

q0 =

F = { R Q | R contains some accept state of N}

NFA to DFA Conversion

Input: N = (Q, Σ, , q0, F)

Output: M = (Q, Σ, , q0, F)

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper L3.14

Page 14: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Example: NFA to DFA

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

1a b

1)

L3.15

Page 15: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Examples NFA to DFA

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

0,1

ε 02 31

1

2)

L3.16

Page 16: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

(R,) = ∪ E( (r,) ) for all R⊆Q and ∈Σ.

Q = P(Q)

: Q Σ → Q

rR

q0 = E({q0})

F = { R Q | R contains some accept state of N}

NFA to DFA Conversion

Input: N = (Q, Σ, , q0, F)

Output: M = (Q, Σ, , q0, F)

For R⊆Q, let E(R) be the set of

states reachable by ε-transitions

from the states in R.

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper L3.17

Page 17: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Operations on languages

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

Union: A B = { w | w A or w B }

Intersection: A B = { w | w A and w B }

Complement: A = { w | w A }

Reverse: AR = { w1 …wk | wk …w1 A }

Concatenation: A B = { vw | v A and w B }

Star: A* = { w1 …wk | k ≥ 0 and each wi A }

Regular

Union: A B = { w | w A or w B }

Concatenation: A B = { vw | v A and w B }

Star: A* = { w1 …wk | k ≥ 0 and each wi A }

L3.18

Page 18: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Closure properties of the class of

regular languages

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

THEOREM. The class of regular languages

is closed under all 6 operations.

If A and B are regular, applying any of these

operation yields a regular language.

L3.19

Page 19: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Palindromes

A palindrome is a word or a phrase that reads the

same forward and backward.

Examples

• mom

• madam

• Never odd or even.

• Stressed? No tips? Spit on desserts!

L3.209/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

Page 20: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Exercise

Let L be the set of words in English.

Then 𝑳 ∩ 𝑳𝑹 is

A. The set of English words in alphabetical order, followed

by the same words in reverse alphabetical order.

B. {𝑤 ∣ 𝑤 is an English word or an English word written

backwords}.

C. {𝑤 ∣ 𝑤 is an English word that is a palindrome}.

D. None of the above.

Page 21: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Closure under reverse

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

Theorem. The reverse of a regular language is also regular

Proof: Let L be a regular language and M be a DFA that

recognizes it. Construct an NFA 𝑴′ recognizing LR:

• Define 𝑴′ as M with the arrows

reversed.

• Make the start state of M be the

accept state in 𝑴′.

• Make a new start state that goes to all

accept states of M by ε-transitions.

ε ε ε

L3.22

Page 22: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

New construction for A ∪ B

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

ε

ε

Construct an NFA M:

L(MA)=A

L(MB)=B

L3.23

Page 23: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Concatenation operation

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

Concatenation: A B = { vw | v A and w B }

Theorem. If A and B are regular, A B is also regular.

Proof: Given DFAs M1 and M2, construct NFA by

connecting all accept states in M1 to the start state in M2.

ε

ε

L(M1)=A L(M2)=B

L3.24

Page 24: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Concatenation operation

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

Concatenation: A B = { vw | v A and w B }

Theorem. If A and B are regular, A B is also regular.

Proof: Given DFAs M1 and M2, construct NFA by

connecting all accept states in M1 to the start state in M2.

• Make all states in M1 non-accepting.

ε

ε

L(M1)=A L(M2)=B

L3.25

Page 25: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

Star operation

9/10/2019

Sofya Raskhodnikova; based on slides by Nick Hopper

Star: A* = { w1 …wk | k ≥ 0 and each wi A }

Theorem. If A is regular, A* is also regular.

L(M)=Aε

ε

ε

L3.26

Page 26: CS332 Elements of Theory of Computation · CS332 Elements of Theory of Computation Author: Sofya Raskhodnikova Created Date: 9/10/2019 3:54:25 PM

The class of regular languages is

closed under

9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper

Union: A B = { w | w A or w B }

Intersection: A B = { w | w A and w B }

Complement: A = { w | w A }

Reverse: AR = { w1 …wk | wk …w1 A }

Regular operations

Union: A B = { w | w A or w B }

Concatenation: A B = { vw | v A and w B }

Star: A* = { w1 …wk | k ≥ 0 and each wi A }

Other operations

L3.27