Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite...

37
CSE 311: Foundations of Computing Lecture 24: NFAs, Regular expressions, and NFADFA

Transcript of Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite...

Page 1: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

CSE 311: Foundations of Computing

Lecture 24: NFAs, Regular expressions, and NFA→DFA

Page 2: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Nondeterministic Finite Automata (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 ε

• DefnDefnDefnDefn: : : : x is in the language recognized by an NFA if

and only if x labels a path from the start state to

some final state

s0 s2 s3s1

111

0,10,1

Page 3: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Three ways of thinking about NFAs

• Outside observer: Is there a path labeled by x from

the start state to some final state?

• Perfect guesser: The NFA has input x and whenever

there is a choice of what to do it magically guesses a

good one (if one exists)

• Parallel exploration: The NFA computation runs all

possible computations on x step-by-step at the same

time in parallel

Page 4: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

NFA for set of binary strings with a 1 in the 3rd position from the end

Page 5: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

NFA for set of binary strings with a 1 in the 3rd position from the end

0,1

ssss3333 ssss2222ssss1111 ssss0000

0,1 0,11

Page 6: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

001 011

111

110

101010000

100

1

11 0

1

1

1

1

000

1

0

0

00

Compare with the smallest DFA

0,1

ssss3333 ssss2222 ssss1111 ssss0000

0,1 0,11

Page 7: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

0,1

ssss3333 ssss2222 ssss1111 ssss0000

0,1 0,11

Parallel Exploration view of an NFA

Input string 0101100

ssss3333

0 1 0 1 1 0 0

ssss3333

ssss1111

ssss3333

ssss2222

ssss3333

ssss0000

ssss1111

ssss3333

ssss0000

ssss2222

ssss3333 ssss3333

ssss0000

X

ssss3333

ssss1111

ssss2222

X

Page 8: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Theorem:Theorem:Theorem: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

Page 9: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Regular Expressions over Σ

• Basis:

– ∅∅∅∅, ɛɛɛɛ are regular expressions

– aaaa is a regular expression for any a ∈ Σ

• Recursive step:

– If AAAA and BBBB are regular expressions then so are:

(A ∪ B)

(AB)

A*

Page 10: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Base Case

• Case ∅∅∅∅:

• Case ɛɛɛɛ:

• Case aaaa:

Page 11: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Base Case

• Case ∅∅∅∅:

• Case ɛɛɛɛ:

• Case aaaa:a

Page 12: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Inductive Hypothesis

• Suppose that for some regular expressions

A and B there exist NFAs NA and NB such

that NA recognizes the language given by A

and NB recognizes the language given by B

NA NB

Page 13: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Inductive Step

Case (A ∪ B):

NA

NB

Page 14: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Inductive Step

Case (A ∪ B):

ɛɛɛɛ

ɛɛɛɛ

NA

NB

Page 15: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Inductive Step

Case (AB):

NA NB

Page 16: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Inductive Step

Case (AB):

ɛɛɛɛ

ɛɛɛɛ

NA NB

Page 17: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Inductive Step

Case A*

NA

Page 18: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Inductive Step

Case A*

ɛɛɛɛ

ɛɛɛɛ

ɛɛɛɛ

NA

Page 19: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Build an NFA for (01 ∪1)*0

Page 20: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Solution

(01 ∪∪∪∪1)*0

0ɛɛɛɛ

ɛɛɛɛ

ɛɛɛɛ

ɛɛɛɛ

0

1

1

ɛɛɛɛ

ɛɛɛɛ

ɛɛɛɛ

ɛɛɛɛ

ɛɛɛɛ

Page 21: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

NFAs and DFAs

Every DFA isisisis an NFA

– DFAs have requirements that NFAs don’t have

Can NFAs recognize more languages?

Page 22: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

NFAs and DFAs

Every DFA isisisis an NFA

– DFAs have requirements that NFAs don’t have

Can NFAs recognize more languages? No!

Theorem: Theorem: Theorem: Theorem: For every NFA there is a DFA that

recognizes exactly the same language

Page 23: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Three ways of thinking about NFAs

• Outside observer: Is there a path labeled by x from

the start state to some final state?

• Perfect guesser: The NFA has input x and whenever

there is a choice of what to do it magically guesses a

good one (if one exists)

• Parallel exploration: The NFA computation runs all

possible computations on x step-by-step at the same

time in parallel

Page 24: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Conversion of NFAs to a 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

Page 25: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

0,1

ssss3333 ssss2222 ssss1111 ssss0000

0,1 0,11

Parallel Exploration view of an NFA

Input string 0101100

ssss3333 ssss3333 ssss3333 ssss3333 ssss3333 ssss3333 ssss3333

0 1 0 1 1 0 0

ssss2222 ssss1111 ssss0000

ssss2222 ssss1111 ssss0000

ssss2222 ssss1111 ssss0000

ssss3333

X

X

Page 26: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

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

baɛɛɛɛ

ɛɛɛɛ

ɛɛɛɛ

NFA DFA

Page 27: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Conversion of NFAs to a DFAs

For each state of the DFA corresponding to a set S of

states of the NFA and each symbol s– Add an edge labeled s 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 s, and

then following some number of edges labeled by ɛɛɛɛ

– T will be ∅∅∅∅ if no edges from S labeled s exist

f

e

b

ɛɛɛɛ

ɛɛɛɛc

d

g

ɛɛɛɛ

1

1

1

1

b,e,f c,d,e,g1

Page 28: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

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

ce

ba

NFADFA

Page 29: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Example: NFA to DFA

c

a

b

0

ɛɛɛɛ

0,1

1

0

NFA

DFA

Page 30: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Example: NFA to DFA

c

a

b

0

ɛɛɛɛ

0,1

1

0

NFA

a,b

DFA

Page 31: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Example: NFA to DFA

c

a

b

0

ɛɛɛɛ

0,1

1

0

NFA

a,b

DFA

0

c

1

Page 32: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Example: NFA to DFA

c

a

b

0

ɛɛɛɛ

0,1

1

0

NFA

a,b

DFA

0

c

1

b

b,c

1

0

Page 33: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Example: NFA to DFA

c

a

b

0

ɛɛɛɛ

0,1

1

0

NFA

a,b

DFA

0

c

1

b

b,c

1

0

∅∅∅∅

10

Page 34: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Example: NFA to DFA

c

a

b

0

ɛɛɛɛ

0,1

1

0

NFA

a,b

DFA

0

c

1

b

b,c

1

0

∅∅∅∅

1

0,1

0

Page 35: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Example: NFA to DFA

c

a

b

0

ɛɛɛɛ

0,1

1

0

NFA

a,b

DFA

0

c

1

b

b,c

1

0

a,b,c

∅∅∅∅

1

0,1

0

0

1

Page 36: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Example: NFA to DFA

c

a

b

0

ɛɛɛɛ

0,1

1

0

NFA

a,b

DFA

0

c

1

b

b,c

1

0

a,b,c

∅∅∅∅

1

0,1

0

0

1

10

Page 37: Lecture 24: NFAs, Regular expressions, and NFA DFA · 2016-11-28 · Nondeterministic Finite Automata (NFA) • Graph with start state, final states, edges labeled by symbols (like

Exponential Blow-up in Simulating Nondeterminism

• 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

– �-state NFA yields DFA with at most �� states

– We saw an example where roughly �� is necessary

“Is the �th 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