87
CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

CSE 311 Lecture 26: Limitations of DFAs, NFAs,and Regular ExpressionsEmina Torlak and Kevin Zatloukal

1

Page 2: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

TopicsFrom NFAs to DFAs

A quick wrap-up of .DFAs NFAs regular expressions

They are all the same and represent regular languages.Languages and representations

Regular, context-free, and other languages.Proving irregularity

A proof template for showing that a language is not regular.

Lecture 25≡ ≡

2

Page 4: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs and DFAs

Every DFA is an NFA.A DFA is an NFA that satisfies more constraints.

4

Page 5: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs and DFAs

Every DFA is an NFA.A DFA is an NFA that satisfies more constraints.

TheoremFor every NFA there is a DFA that recognizes exactly the same language.

4

Page 6: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs and DFAs

Every DFA is an NFA.A DFA is an NFA that satisfies more constraints.

TheoremFor every NFA there is a DFA that recognizes exactly the same language.

Proof (and algorithm) idea:The DFA constructed for an NFA keeps track of all the states that a prefixof an input string can reach in the NFA.

4

Page 7: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs and DFAs

Every DFA is an NFA.A DFA is an NFA that satisfies more constraints.

TheoremFor every NFA there is a DFA that recognizes exactly the same language.

Proof (and algorithm) idea:The DFA constructed for an NFA keeps track of all the states that a prefixof an input string can reach in the NFA.So there will be one state in the DFA for each subset of the states of theNFA that can be reached by some string.

4

Page 8: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs and DFAs

Every DFA is an NFA.A DFA is an NFA that satisfies more constraints.

TheoremFor every NFA there is a DFA that recognizes exactly the same language.

Proof (and algorithm) idea:The DFA constructed for an NFA keeps track of all the states that a prefixof an input string can reach in the NFA.So there will be one state in the DFA for each subset of the states of theNFA that can be reached by some string.We’ll see how to construct the start state, remaining states andtransitions, and the final states of the DFA.

4

Page 9: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs to DFAs: the start state

The start state of the DFA represents the following set of states in the NFA:All states reachable from the start state of the NFA using only edges.ε

5

Page 10: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs to DFAs: the start state

The start state of the DFA represents the following set of states in the NFA:All states reachable from the start state of the NFA using only edges.

NFA

a bε

c

1

ε

5

Page 11: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs to DFAs: the start state

The start state of the DFA represents the following set of states in the NFA:All states reachable from the start state of the NFA using only edges.

NFA

a bε

c

1

DFA

a, b, e, f

ε

5

Page 12: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs to DFAs: states and transitions

Repeat until fixed point:

6

Page 13: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs to DFAs: states and transitions

Repeat until fixed point:Let be a state of the DFA corresponding to a set of the NFA states.DQ Q

6

Page 14: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs to DFAs: states and transitions

Repeat until fixed point:Let be a state of the DFA corresponding to a set of the NFA states.Let be a symbol for which has no outgoing edge.

DQ Qa ∈ Σ DQ

6

Page 15: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs to DFAs: states and transitions

Repeat until fixed point:Let be a state of the DFA corresponding to a set of the NFA states.Let be a symbol for which has no outgoing edge.Let be the (possibly empty) set of NFA states reachable from some statein by following one edge and zero or more edges.

DQ Qa ∈ Σ DQT

Q a ε

6

Page 16: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs to DFAs: states and transitions

Repeat until fixed point:Let be a state of the DFA corresponding to a set of the NFA states.Let be a symbol for which has no outgoing edge.Let be the (possibly empty) set of NFA states reachable from some statein by following one edge and zero or more edges.Add a state to the DFA, if not included, that represents the set .

DQ Qa ∈ Σ DQT

Q a εDT T

6

Page 17: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs to DFAs: states and transitions

Repeat until fixed point:Let be a state of the DFA corresponding to a set of the NFA states.Let be a symbol for which has no outgoing edge.Let be the (possibly empty) set of NFA states reachable from some statein by following one edge and zero or more edges.Add a state to the DFA, if not included, that represents the set .Add an edge labeled from to .

DQ Qa ∈ Σ DQT

Q a εDT T

a DQ DT

6

Page 18: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs to DFAs: states and transitions

Repeat until fixed point:Let be a state of the DFA corresponding to a set of the NFA states.Let be a symbol for which has no outgoing edge.Let be the (possibly empty) set of NFA states reachable from some statein by following one edge and zero or more edges.Add a state to the DFA, if not included, that represents the set .Add an edge labeled from to .

NFA

b

fεd1

1

c1

1gε

DFA

b, e, f

DQ Qa ∈ Σ DQT

Q a εDT T

a DQ DT

6

Page 19: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs to DFAs: states and transitions

Repeat until fixed point:Let be a state of the DFA corresponding to a set of the NFA states.Let be a symbol for which has no outgoing edge.Let be the (possibly empty) set of NFA states reachable from some statein by following one edge and zero or more edges.Add a state to the DFA, if not included, that represents the set .Add an edge labeled from to .

NFA

b

fεd1

1

c1

1gε

DFA

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

DQ Qa ∈ Σ DQT

Q a εDT T

a DQ DT

6

Page 20: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

NFAs to DFAs: final states

The final states of the DFA:Every DFA state that represents a set of NFA states containing a final state.

NFA

c

a b

e

DFA

a, b, c, e

7

Page 21: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: NFA to DFA conversion

NFA

a b

ε

c1

0

0, 1

0

DFA

8

Page 22: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: NFA to DFA conversion

NFA

a b

ε

c1

0

0, 1

0

DFA

a, b

8

Page 23: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: NFA to DFA conversion

NFA

a b

ε

c1

0

0, 1

0

DFA

a, b

0

8

Page 24: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: NFA to DFA conversion

NFA

a b

ε

c1

0

0, 1

0

DFA

a, b

0

c1

8

Page 25: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: NFA to DFA conversion

NFA

a b

ε

c1

0

0, 1

0

DFA

a, b

0

c1

b, c0

8

Page 26: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: NFA to DFA conversion

NFA

a b

ε

c1

0

0, 1

0

DFA

a, b

0

c1

b, c0

b1

8

Page 27: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: NFA to DFA conversion

NFA

a b

ε

c1

0

0, 1

0

DFA

a, b

0

c1

b, c0

b1

a, b, c0

8

Page 28: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: NFA to DFA conversion

NFA

a b

ε

c1

0

0, 1

0

DFA

a, b

0

c1

b, c0

b1

a, b, c0

1

8

Page 29: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: NFA to DFA conversion

NFA

a b

ε

c1

0

0, 1

0

DFA

a, b

0

c1

b, c0

b1

a, b, c0

1

0

8

Page 30: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: NFA to DFA conversion

NFA

a b

ε

c1

0

0, 1

0

DFA

a, b

0

c1

b, c0

b1

a, b, c0

1

0

1

8

Page 31: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: NFA to DFA conversion

NFA

a b

ε

c1

0

0, 1

0

DFA

a, b

0

c1

b, c0

b1

a, b, c0

1

0

1

0

8

Page 32: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: NFA to DFA conversion

NFA

a b

ε

c1

0

0, 1

0

DFA

a, b

0

c1

b, c0

b1

a, b, c0

1

0

1

0∅

1

8

Page 33: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: NFA to DFA conversion

NFA

a b

ε

c1

0

0, 1

0

DFA

a, b

0

c1

b, c0

b1

a, b, c0

1

0

1

0∅

1

0, 1

8

Page 34: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

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 up to states.

We saw an example of this worst case outcome.n 2n

9

Page 35: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

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 up to states.

We saw an example of this worst case outcome.n 2n

The famous “P=NP?” question asks whether a similar blow-up is alwaysnecessary to get rid of nondeterminism for polynomial-time algorithms.

9

Page 36: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

DFAs NFAs regular expressionsThey are all the same and represent regular languages.

≡ ≡

10

Page 37: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Equivalence of DFAs, NFAs, and regular expressions

We have shown how to build an optimal DFA for every regular expression.Build an NFA.Convert the NFA to a DFA using the subset construction.Minimize the resulting DFA.

11

Page 38: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Equivalence of DFAs, NFAs, and regular expressions

We have shown how to build an optimal DFA for every regular expression.Build an NFA.Convert the NFA to a DFA using the subset construction.Minimize the resulting DFA.

TheoremA language is recognized by a DFA (or NFA) if and only if it has a regularexpression.

You need to know this fact but we won’t ask you anything about the “onlyif” direction from DFAs/NFAs to regular expressions.

11

Page 39: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Equivalence of DFAs, NFAs, and regular expressions

We have shown how to build an optimal DFA for every regular expression.Build an NFA.Convert the NFA to a DFA using the subset construction.Minimize the resulting DFA.

TheoremA language is recognized by a DFA (or NFA) if and only if it has a regularexpression.

Languages represented by NFAs, DFAs, and regular expressions arecalled regular languages.

You need to know this fact but we won’t ask you anything about the “onlyif” direction from DFAs/NFAs to regular expressions.

11

Page 40: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Languages and representationsRegular, context-free, and other languages.

12

Page 41: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

A hierarchy of languages and representations

Finite{010,11, 21}

Regular0*1* DFA

NFARegex

Context-FreeCFG

All

S → 0S1 | ε

13

Page 42: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

A hierarchy of languages and representations

Finite{010,11, 21}

Regular0*1* DFA

NFARegex

Context-FreeCFG

All

S → 0S1 | ε

In HW8, you’ll (almost :) prove that regular languages are context-free. Howdo we prove that all finite languages are regular?

13

Page 43: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

A hierarchy of languages and representations

Finite{010,11, 21}

Regular0*1* DFA

NFARegex

Context-FreeCFG

All

S → 0S1 | ε

In HW8, you’ll (almost :) prove that regular languages are context-free. Howdo we prove that all finite languages are regular? By showing how toconstruct a DFA/NFA/RegEx for every finite language!

13

Page 44: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Converting a finite language to a regular expression

14

Page 45: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Converting a finite language to a regular expression

Convert each string in the language to a regular expression.This is just each string itself.

Then put these regular expressions together using the operator.The resulting regular expression accepts exactly the strings in .

Example

L

∪L

{010, 11, 21} ⟶ 010 ∪ 11 ∪ 21

14

Page 46: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Back to languages and representations …

Finite{010,11, 21}

Regular0*1* DFA

NFARegex

Context-FreeCFG

All

S → 0S1 | ε

15

Page 47: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Back to languages and representations …

Finite{010,11, 21}

Regular0*1* DFA

NFARegex

Context-FreeCFG

All

S → 0S1 | ε

We saw in that the language of all binary palindromes can berepresented by a CFG. We also said that can’t be represented by anyregular expression. How would you prove that?

Lecture 21 BB

15

Page 48: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Why isn’t (binary palindromes) a regular language?

If were regular, we could express it as a DFA/NFA/RegEx.Let’s choose a DFA as our hypothetical representation.

BB

16

Page 49: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Why isn’t (binary palindromes) a regular language?

If were regular, we could express it as a DFA/NFA/RegEx.Let’s choose a DFA as our hypothetical representation.

Now, recall that consists of infinitely many strings where is the reverse of and .

BB

B wvw⎯ ⎯⎯⎯

w⎯ ⎯⎯⎯ w v ∈ {ε, “0”, “1”}

16

Page 50: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Why isn’t (binary palindromes) a regular language?

If were regular, we could express it as a DFA/NFA/RegEx.Let’s choose a DFA as our hypothetical representation.

Now, recall that consists of infinitely many strings where is the reverse of and .

What would a DFA need to keep track of to decide ?

BB

B wvw⎯ ⎯⎯⎯

w⎯ ⎯⎯⎯ w v ∈ {ε, “0”, “1”}B

16

Page 51: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Why isn’t (binary palindromes) a regular language?

If were regular, we could express it as a DFA/NFA/RegEx.Let’s choose a DFA as our hypothetical representation.

Now, recall that consists of infinitely many strings where is the reverse of and .

What would a DFA need to keep track of to decide ?It would need to keep track of in order to check against it.

BB

B wvw⎯ ⎯⎯⎯

w⎯ ⎯⎯⎯ w v ∈ {ε, “0”, “1”}B

w w⎯ ⎯⎯⎯

16

Page 52: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Why isn’t (binary palindromes) a regular language?

If were regular, we could express it as a DFA/NFA/RegEx.Let’s choose a DFA as our hypothetical representation.

Now, recall that consists of infinitely many strings where is the reverse of and .

What would a DFA need to keep track of to decide ?It would need to keep track of in order to check against it.But there are infinitely many possible ’s and finitely many DFA states!

BB

B wvw⎯ ⎯⎯⎯

w⎯ ⎯⎯⎯ w v ∈ {ε, “0”, “1”}B

w w⎯ ⎯⎯⎯

w

16

Page 53: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Why isn’t (binary palindromes) a regular language?

If were regular, we could express it as a DFA/NFA/RegEx.Let’s choose a DFA as our hypothetical representation.

Now, recall that consists of infinitely many strings where is the reverse of and .

What would a DFA need to keep track of to decide ?It would need to keep track of in order to check against it.But there are infinitely many possible ’s and finitely many DFA states!

BB

B wvw⎯ ⎯⎯⎯

w⎯ ⎯⎯⎯ w v ∈ {ε, “0”, “1”}B

w w⎯ ⎯⎯⎯

w

This is the intuition for why is not regular. Let’s see how to turn thisintuition into a formal proof.

B

16

Page 54: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

A strategy for proving that is not regular

Proof by contradiction:Assume that is regular.Therefore, there is a DFA that recognizes .Show that accepts or rejects a string it shouldn’t.

B

BM B

M

17

Page 55: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

A strategy for proving that is not regular

Proof by contradiction:Assume that is regular.Therefore, there is a DFA that recognizes .Show that accepts or rejects a string it shouldn’t.

Key Idea 1If two string prefixes collide by reaching the samestate, a DFA can no longer distinguish their suffixes.

0a10b1

?

B

BM B

M

17

Page 56: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

A strategy for proving that is not regular

Proof by contradiction:Assume that is regular.Therefore, there is a DFA that recognizes .Show that accepts or rejects a string it shouldn’t.

Key Idea 1If two string prefixes collide by reaching the samestate, a DFA can no longer distinguish their suffixes.

0a10b1

?

Key Idea 2The machine has finitely many states, and sincethe strings in have infinitely many distinct prefixes,two of them must collide!

B

BM B

M

MB

17

Page 57: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

A strategy for proving that is not regular

Proof by contradiction:Assume that is regular.Therefore, there is a DFA that recognizes .Show that accepts or rejects a string it shouldn’t.

Key Idea 1If two string prefixes collide by reaching the samestate, a DFA can no longer distinguish their suffixes.

0a10b1

?

Key Idea 2The machine has finitely many states, and sincethe strings in have infinitely many distinct prefixes,two of them must collide!

B

BM B

M

MB

We choose an infinite set of prefixes. This choice mustensure that for every pair ofprefixes in ,there is a suffix such thatone of of is in butnot the other.

S

≠ ∈ Ssa sbt

t, tsa sb B

S= {1, 01, 001, 0001, …}= { 1 : n ≥ 0}0n

17

Page 58: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Proving that is not regularSuppose that some DFA recognizes . We show accepts or rejectsa string it shouldn’t. Consider the set .

BM B M

S = { 1 : n ≥ 0}0n

18

Page 59: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Proving that is not regularSuppose that some DFA recognizes . We show accepts or rejectsa string it shouldn’t. Consider the set .

Since there are finitely many states in and infinitely many strings in , there exist strings and with that end in the

same state of .

BM B M

S = { 1 : n ≥ 0}0n

MS 1 ∈ S0a 1 ∈ S0b a ≠ b

M

18

Page 60: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Proving that is not regularSuppose that some DFA recognizes . We show accepts or rejectsa string it shouldn’t. Consider the set .

Since there are finitely many states in and infinitely many strings in , there exist strings and with that end in the

same state of .

BM B M

S = { 1 : n ≥ 0}0n

MS 1 ∈ S0a 1 ∈ S0b a ≠ b

MImportant: We don’t get to choose

anda! We just know they exist.b

18

Page 61: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Proving that is not regularSuppose that some DFA recognizes . We show accepts or rejectsa string it shouldn’t. Consider the set .

Since there are finitely many states in and infinitely many strings in , there exist strings and with that end in the

same state of .

Now, consider appending to both strings. 0a10b1

0a

BM B M

S = { 1 : n ≥ 0}0n

MS 1 ∈ S0a 1 ∈ S0b a ≠ b

M0a

18

Page 62: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Proving that is not regularSuppose that some DFA recognizes . We show accepts or rejectsa string it shouldn’t. Consider the set .

Since there are finitely many states in and infinitely many strings in , there exist strings and with that end in the

same state of .

Now, consider appending to both strings. 0a10b1

0a

Since and end in the same state, so do and , call it. But then must make a mistake: needs to be an accept state since

, but then would accept , which is an error.

BM B M

S = { 1 : n ≥ 0}0n

MS 1 ∈ S0a 1 ∈ S0b a ≠ b

M0a

10a 10b 0a10a 0b10a

q M q∈ B0a10a M ∉ B0b10a ◻

18

Page 63: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Proving irregularityA proof template for showing that a language is not regular.

19

Page 64: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

A template for proving that a language is not regular

ɠ Suppose for contradiction that some DFA recognizes .

L

ML

20

Page 65: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

A template for proving that a language is not regular

ɠ Suppose for contradiction that some DFA recognizes .

ɡ Consider the set { }.

L

ML

S = …

must be infinite, andfor every pair ofprefixes ,there is a suffix suchthat one of of is in but not theother.

S

≠ ∈ Ssa sbtt, tsa sb

L

20

Page 66: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

A template for proving that a language is not regular

ɠ Suppose for contradiction that some DFA recognizes .

ɡ Consider the set { }.

ɢ Since is infinite and has finitely many states,there must be two strings such that

and both end in the same state of .

L

ML

S = …S M

,sa sb ∈ S≠sa sb M

must be infinite, andfor every pair ofprefixes ,there is a suffix suchthat one of of is in but not theother.

We don’t get to choose and !

S

≠ ∈ Ssa sbtt, tsa sb

L

sa sb

20

Page 67: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

A template for proving that a language is not regular

ɠ Suppose for contradiction that some DFA recognizes .

ɡ Consider the set { }.

ɢ Since is infinite and has finitely many states,there must be two strings such that

and both end in the same state of .

ɣ Consider appending to both and .

L

ML

S = …S M

,sa sb ∈ S≠sa sb M

t sa sb

must be infinite, andfor every pair ofprefixes ,there is a suffix suchthat one of of is in but not theother.

We don’t get to choose and !

should be an acceptsuffix for but not .

S

≠ ∈ Ssa sbtt, tsa sb

L

sa sb

tsa sb

20

Page 68: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

A template for proving that a language is not regular

ɠ Suppose for contradiction that some DFA recognizes .

ɡ Consider the set { }.

ɢ Since is infinite and has finitely many states,there must be two strings such that

and both end in the same state of .

ɣ Consider appending to both and .

ɤ Since and end in the same state of , then and also end in the same state of . Since

and , does not recognize .

L

ML

S = …S M

,sa sb ∈ S≠sa sb M

t sa sb

sa sb Mtsa tsb q Mt ∈ Lsa t ∉ Lsb M L

must be infinite, andfor every pair ofprefixes ,there is a suffix suchthat one of of is in but not theother.

We don’t get to choose and !

should be an acceptsuffix for but not .

S

≠ ∈ Ssa sbtt, tsa sb

L

sa sb

tsa sb

20

Page 69: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

A template for proving that a language is not regular

ɠ Suppose for contradiction that some DFA recognizes .

ɡ Consider the set { }.

ɢ Since is infinite and has finitely many states,there must be two strings such that

and both end in the same state of .

ɣ Consider appending to both and .

ɤ Since and end in the same state of , then and also end in the same state of . Since

and , does not recognize .

ɥ Since was arbitrary, no DFA recognizes .

L

ML

S = …S M

,sa sb ∈ S≠sa sb M

t sa sb

sa sb Mtsa tsb q Mt ∈ Lsa t ∉ Lsb M L

M L

must be infinite, andfor every pair ofprefixes ,there is a suffix suchthat one of of is in but not theother.

We don’t get to choose and !

should be an acceptsuffix for but not .

S

≠ ∈ Ssa sbtt, tsa sb

L

sa sb

tsa sb

20

Page 70: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: prove that is not regularSuppose for contradiction that some DFA recognizes .

Consider the set

L = { : n ≥ 0}0n1n

M LS =

21

Page 71: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: prove that is not regularSuppose for contradiction that some DFA recognizes .

Consider the set .

L = { : n ≥ 0}0n1n

M LS = { : n ≥ 0}0n

21

Page 72: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: prove that is not regularSuppose for contradiction that some DFA recognizes .

Consider the set .

Since is infinite and has finitely many states, there must be twostrings with that both end in the same state of .

L = { : n ≥ 0}0n1n

M LS = { : n ≥ 0}0n

S M,0a 0b ∈ S a ≠ b M

21

Page 73: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: prove that is not regularSuppose for contradiction that some DFA recognizes .

Consider the set .

Since is infinite and has finitely many states, there must be twostrings with that both end in the same state of .

Consider appending to both and .

L = { : n ≥ 0}0n1n

M LS = { : n ≥ 0}0n

S M,0a 0b ∈ S a ≠ b M

0a 0b

21

Page 74: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: prove that is not regularSuppose for contradiction that some DFA recognizes .

Consider the set .

Since is infinite and has finitely many states, there must be twostrings with that both end in the same state of .

Consider appending to both and .

L = { : n ≥ 0}0n1n

M LS = { : n ≥ 0}0n

S M,0a 0b ∈ S a ≠ b M

1a 0a 0b

21

Page 75: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: prove that is not regularSuppose for contradiction that some DFA recognizes .

Consider the set .

Since is infinite and has finitely many states, there must be twostrings with that both end in the same state of .

Consider appending to both and .

Since and end in the same state of , then and alsoend in the same state of . Since and , doesnot recognize .

L = { : n ≥ 0}0n1n

M LS = { : n ≥ 0}0n

S M,0a 0b ∈ S a ≠ b M

1a 0a 0b

0a 0b M 0a1a 0b1a

q M ∈ L0a1a ∉ L0b1a ML

21

Page 76: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: prove that is not regularSuppose for contradiction that some DFA recognizes .

Consider the set .

Since is infinite and has finitely many states, there must be twostrings with that both end in the same state of .

Consider appending to both and .

Since and end in the same state of , then and alsoend in the same state of . Since and , doesnot recognize .

Since was arbitrary, no DFA recognizes .

L = { : n ≥ 0}0n1n

M LS = { : n ≥ 0}0n

S M,0a 0b ∈ S a ≠ b M

1a 0a 0b

0a 0b M 0a1a 0b1a

q M ∈ L0a1a ∉ L0b1a ML

M L

21

Page 77: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: prove that is not regularSuppose for contradiction that some DFA recognizes .

Consider the set

L = { : n ≥ 0}(n)n

M LS =

22

Page 78: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: prove that is not regularSuppose for contradiction that some DFA recognizes .

Consider the set .

L = { : n ≥ 0}(n)n

M LS = { : n ≥ 0}(n

22

Page 79: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: prove that is not regularSuppose for contradiction that some DFA recognizes .

Consider the set .

Since is infinite and has finitely many states, there must be twostrings with that both end in the same state of .

L = { : n ≥ 0}(n)n

M LS = { : n ≥ 0}(n

S M,(a (b ∈ S a ≠ b M

22

Page 80: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: prove that is not regularSuppose for contradiction that some DFA recognizes .

Consider the set .

Since is infinite and has finitely many states, there must be twostrings with that both end in the same state of .

Consider appending to both and .

L = { : n ≥ 0}(n)n

M LS = { : n ≥ 0}(n

S M,(a (b ∈ S a ≠ b M

(a (b

22

Page 81: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: prove that is not regularSuppose for contradiction that some DFA recognizes .

Consider the set .

Since is infinite and has finitely many states, there must be twostrings with that both end in the same state of .

Consider appending to both and .

L = { : n ≥ 0}(n)n

M LS = { : n ≥ 0}(n

S M,(a (b ∈ S a ≠ b M

)a (a (b

22

Page 82: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: prove that is not regularSuppose for contradiction that some DFA recognizes .

Consider the set .

Since is infinite and has finitely many states, there must be twostrings with that both end in the same state of .

Consider appending to both and .

Since and end in the same state of , then and also endin the same state of . Since and , does notrecognize .

L = { : n ≥ 0}(n)n

M LS = { : n ≥ 0}(n

S M,(a (b ∈ S a ≠ b M

)a (a (b

(a (b M (a)a (b)a

q M ∈ L(a)a ∉ L(b)a ML

22

Page 83: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

Example: prove that is not regularSuppose for contradiction that some DFA recognizes .

Consider the set .

Since is infinite and has finitely many states, there must be twostrings with that both end in the same state of .

Consider appending to both and .

Since and end in the same state of , then and also endin the same state of . Since and , does notrecognize .

Since was arbitrary, no DFA recognizes .

L = { : n ≥ 0}(n)n

M LS = { : n ≥ 0}(n

S M,(a (b ∈ S a ≠ b M

)a (a (b

(a (b M (a)a (b)a

q M ∈ L(a)a ∉ L(b)a ML

M L

22

Page 84: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

A fun fact about this proof methodSuppose that for a language , the set is a largest set of prefix stringswith the property that for every pair , there is some string such that one of is in but the other isn’t.

L S≠ ∈ Ssa sb t

t, tsa sb L

23

Page 85: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

A fun fact about this proof methodSuppose that for a language , the set is a largest set of prefix stringswith the property that for every pair , there is some string such that one of is in but the other isn’t.

If is infinite, then is not regular.

L S≠ ∈ Ssa sb t

t, tsa sb LS L

23

Page 86: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

A fun fact about this proof methodSuppose that for a language , the set is a largest set of prefix stringswith the property that for every pair , there is some string such that one of is in but the other isn’t.

If is infinite, then is not regular.

If is finite, then the minimal DFA for has precisely states, onereached by each member of .

L S≠ ∈ Ssa sb t

t, tsa sb LS LS L |S|

S

23

Page 87: CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular ...CSE 311 Lecture 26: Limitations of DFAs, NFAs, and Regular Expressions Emina Torlak and Kevin Zatloukal 1

SummaryDFAs NFAs regular expressions.

We’ve shown how to go from a regular expression to an NFA to a DFA.(And going from an NFA to a DFA can lead to exponential blow-up.)But we won’t show how to go from an NFA/DFA to a regular expression.

Finite regular context-free languages.To show that a language is regular, construct a DFA/NFA/RegEx for it.To show that it is not regular, use the proof method from this lecture.

≡ ≡

⊂ ⊂

24