cse 311: foundations of computing Fall 2015 Lecture 24: DFAs, NFAs, - - PowerPoint PPT Presentation

cse 311 foundations of computing
SMART_READER_LITE
LIVE PREVIEW

cse 311: foundations of computing Fall 2015 Lecture 24: DFAs, NFAs, - - PowerPoint PPT Presentation

cse 311: foundations of computing Fall 2015 Lecture 24: DFAs, NFAs, and regular expressions highlights FSMs with output at states State minimization 0 2 0 S0 S1 [1] [0] 1 2 2 3 0 3 0 S0 S1 0 1 1 [1] [0] 1 0 2


slide-1
SLIDE 1

cse 311: foundations of computing Fall 2015

Lecture 24: DFAs, NFAs, and regular expressions

slide-2
SLIDE 2

highlights

  • FSMs with output at states
  • State minimization

2 1 3 1 3 2 2 1 3 2 3 3 2 1 2 3 1 S0 [1] S2 [1] S4 [1] S1 [0] S3 [0] S5 [0] 1

2 1 3 1 3 2

2

3 1,2 S0 [1] S2 [1] S1 [0] S3 [0] 1,3

slide-3
SLIDE 3

highlights

s0 s2

s3

s1

1 1 1 0,1

Lemma: The language recognized by a DFA is the set of strings x that label some path from its start state to one

  • f its final states
slide-4
SLIDE 4

nondeterministic finite automaton (NFA)

  • Graph with start state, final states, edges labeled by

symbols (like DFA) but

– Not required to have exactly 1 edge out of each state labeled by each symbol--- can have 0 or >1 – Also can have edges labeled by empty string ɛ

  • Definition: x is in the language recognized by an NFA if and
  • nly if x labels a path from the start state to some final

state

s0 s2

s3

s1

1 1 1 0,1 0,1

slide-5
SLIDE 5

building an NFA

binary strings that have

  • an even # of 1’s
  • r contain the substring 111 or 1000
slide-6
SLIDE 6

Theorem: For any set of strings (language) 𝐵 described by a regular expression, there is an NFA that recognizes 𝐵. Proof idea: Structural induction based on the recursive definition of regular expressions...

NFAs and regular expressions

𝐵 ∪ 𝐶 𝐵𝐶 𝐵∗

slide-7
SLIDE 7

build an NFA for (01 1)*0

slide-8
SLIDE 8

solution (01 1)*0

ɛ ɛ ɛ ɛ

1 1

ɛ ɛ ɛ ɛ ɛ

slide-9
SLIDE 9

NFAs vs. DFAs

Every DFA is an NFA – DFAs have requirements that NFAs don’t have Can NFAs recognize more languages?

slide-10
SLIDE 10

NFAs vs. DFAs

Every DFA is an NFA – DFAs have requirements that NFAs don’t have Can NFAs recognize more languages? No! Theorem: For every NFA there is a DFA that recognizes exactly the same language.

slide-11
SLIDE 11

conversion of NFAs to DFAs

Proof Idea:

– The DFA keeps track of ALL the states that the part of the input string read so far can reach in the NFA – There will be one state in the DFA for each subset of states of the NFA that can be reached by some string

slide-12
SLIDE 12

conversion of NFAs to a DFAs

New start state for DFA

– The set of all states reachable from the start state of the NFA using only edges labeled ɛ

a,b,e,f f e b a

ɛ ɛ ɛ

NFA DFA

slide-13
SLIDE 13

conversion of NFAs to a DFAs

For each state of the DFA corresponding to a set S of states of the NFA and each letter a

– Add an edge labeled a to state corresponding to T, the set of states of the NFA reached by

starting from some state in S, then following one edge labeled by a, and then following some number of edges labeled by ɛ

– T will be  if no edges from S labeled a exist

f e b

ɛ ɛ

c d g

ɛ 1 1 1 1

b,e,f c,d,e,g

1

S T

slide-14
SLIDE 14

conversion of NFAs to a DFAs

Final states for the DFA

– All states whose set contain some final state of the NFA

a,b,c,e c e b a NFA DFA

slide-15
SLIDE 15

example: NFA to DFA c a

b

ɛ 0,1 1

NFA DFA

slide-16
SLIDE 16

example: NFA to DFA c a

b

ɛ 0,1 1

a,b

NFA DFA

slide-17
SLIDE 17

example: NFA to DFA c a

b

ɛ 0,1 1

a,b c

1

NFA DFA

slide-18
SLIDE 18

example: NFA to DFA c a

b

ɛ 0,1 1

a,b c

1

b b,c

1

NFA DFA

slide-19
SLIDE 19

example: NFA to DFA c a

b

ɛ 0,1 1

a,b c

1

b b,c

1

1

NFA DFA

slide-20
SLIDE 20

example: NFA to DFA c a

b

ɛ 0,1 1

a,b c

1

b b,c

1

1 0,1

NFA DFA

slide-21
SLIDE 21

example: NFA to DFA c a

b

ɛ 0,1 1

a,b c

1

b b,c

1

a,b,c 

1 0,1 1

NFA DFA

slide-22
SLIDE 22

example: NFA to DFA c a

b

ɛ 0,1 1

a,b c

1

b b,c

1

a,b,c 

1 0,1 1 1

NFA DFA

slide-23
SLIDE 23

exponential blow-up in simulating mondeterminism

  • In general the DFA might need a state for every subset of

states of the NFA

– Power set of the set of states of the NFA – n-state NFA yields DFA with at most 2n states – We saw an example where roughly 2n is necessary

Is the nth char from the end a 1?

  • The famous “P=NP?” question asks whether a similar blow-

up is always necessary to get rid of nondeterminism for polynomial-time algorithms

slide-24
SLIDE 24

1 in third position from end

A C

D

B

0,1 0,1 1 0,1

slide-25
SLIDE 25

1 in third position from end

A C

D

B

0,1 0,1 1 0,1

{A} {A, B} {A, B, C} {A, C} {A, B, C, D} {A, C, D} {A, B, D} {A, D}

1 1 1 1 1 1 1 1

slide-26
SLIDE 26

1 in third position from end

A C

D

B

0,1 0,1 1 0,1

{A,B} {A,B,C} {A,B,C,D} {A,C,D} {A,B,D} {A,C} {A} {A,D}

1 1 1 1 1 1 1 1

slide-27
SLIDE 27

DFAs ≡ regular expressions

We have shown how to build an optimal DFA for every regular expression

– Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA

Theorem: A language is recognized by a DFA if and

  • nly if it has a regular expression.

We show the other direction of the proof at the end of these lecture slides.

slide-28
SLIDE 28

languages and machines!

All

Context-Free Regular Finite

0* DFA NFA Regex {001, 10, 12}

slide-29
SLIDE 29

languages and machines!

All

Context-Free Regular Finite

{001, 10, 12} 0* Warmup: All finite languages are regular. DFA NFA Regex

slide-30
SLIDE 30

DFAs recognize any finite language

Exercise: Hard code it into the NFA.

slide-31
SLIDE 31

All

Context-Free Regular Finite

0* Warmup 2: Surprising example here

languages and machines!

{001, 10, 12} DFA NFA Regex

slide-32
SLIDE 32

All

Context-Free Regular Finite

0* ??? Main Event: Prove there is a context-free language that isn’t regular.

languages and machines!

{001, 10, 12} DFA NFA Regex

slide-33
SLIDE 33

DFAs ≡ regular expressions Theorem: A language is recognized by a DFA if and

  • nly if it has a regular expression

Proof: We already saw: RegExp → NFA → DFA Now: NFA → RegExp

(Enough to show this since every DFA is also an NFA.)

slide-34
SLIDE 34

generalized NFAs

  • Like NFAs but allow

– Parallel edges – Regular Expressions as edge labels NFAs already have edges labeled ɛ or a

  • An edge labeled by A can be followed by reading a

string of input chars that is in the language represented by A

  • A string x is accepted iff there is a path from start to

final state labeled by a regular expression whose language contains x

slide-35
SLIDE 35

starting from an NFA Add new start state and final state ɛ ɛ ɛ A Then eliminate original states one by one, keeping the same language, until it looks like: Final regular expression will be A

slide-36
SLIDE 36
  • nly two simplification rules
  • Rule 1: For any two states q1 and q2 with parallel edges

(possibly q1=q2), replace

  • Rule 2: Eliminate non-start/final state q3 by replacing

all for every pair of states q1, q2 (even if q1=q2)

q1 q2 A B

by

A⋃B q1 q2 A B C AB*C q1 q3 q2 q1 q2

by

slide-37
SLIDE 37

converting an NFA to a regular expression Consider the DFA for the mod 3 sum

– Accept strings from {0,1,2}* where the digits mod 3 sum of the digits is 0

t0 t2 t1

1 1 1 2 2 2

slide-38
SLIDE 38

splicing out a node Label edges with regular expressions

t0 t2 t1

1 1 1 2 2 2

t0→t1→t0 : 10*2 t0→t1→t2 : 10*1 t2→t1→t0 : 20*2 t2→t1→t2 : 20*1

s ɛ f ɛ

slide-39
SLIDE 39

finite automaton without t1

t0 t2

R1

R1: 0 ∪ 10*2 R2: 2 ∪ 10*1 R3: 1 ∪ 20*2 R4: 0 ∪ 20*1 R5: R1 ∪ R2R4*R3

R4 R2 R3

t0

R5

Final regular expression: (0 ∪ 10*2 ∪ (2 ∪ 10*1)(0 ∪ 20*1)*(1 ∪ 20*2))*

f ɛ s ɛ f ɛ s ɛ