Expressions of DFAs, NFAs, and Regular CSE 311 Lecture 26 ...Key Idea 1 If tw o string prefixes...

Post on 12-Oct-2020

1 views 0 download

Transcript of Expressions of DFAs, NFAs, and Regular CSE 311 Lecture 26 ...Key Idea 1 If tw o string prefixes...

CSE 311 Lecture 26: LimitationsCSE 311 Lecture 26: Limitationsof DFAs, NFAs, and Regularof DFAs, NFAs, and RegularExpressionsExpressions

and Emina Torlak Sami Davies

1

TopicsTopicsDFAs NFAs regular expressions

A quick review of .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

DFAs DFAs NFAs NFAs regular expressions regular expressionsA quick review of .

≡≡ ≡≡

Lecture 25

3

Equivalence of DFAs, NFAs, and regular expressionsEquivalence 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.

4

Equivalence of DFAs, NFAs, and regular expressionsEquivalence 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 “only if”direction from DFAs/NFAs to regular expressions.

4

Equivalence of DFAs, NFAs, and regular expressionsEquivalence 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 are calledregular languages.

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

4

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

5

A hierarchy of languages and representationsA hierarchy of languages and representations

Finite{010,11, 21}

Regular0*1* DFA

NFA Regex

Context-FreeCFG

All

S → 0S1 | ε

6

A hierarchy of languages and representationsA hierarchy of languages and representations

Finite{010,11, 21}

Regular0*1* DFA

NFA Regex

Context-FreeCFG

All

S → 0S1 | ε

How do we prove that all finite languages are regular?

6

A hierarchy of languages and representationsA hierarchy of languages and representations

Finite{010,11, 21}

Regular0*1* DFA

NFA Regex

Context-FreeCFG

All

S → 0S1 | ε

How do we prove that all finite languages are regular? By showing how toconstruct a DFA/NFA/RegEx for every finite language!

6

Converting a finite language to a regular expressionConverting a finite language to a regular expression

7

Converting a finite language to a regular expressionConverting 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

7

Back to languages and representations …Back to languages and representations …

Finite{010,11, 21}

Regular0*1* DFA

NFA Regex

Context-FreeCFG

All

S → 0S1 | ε

8

Back to languages and representations …Back to languages and representations …

Finite{010,11, 21}

Regular0*1* DFA

NFA Regex

Context-FreeCFG

All

S → 0S1 | ε

How do we prove that all regular languages are context-free?

8

Back to languages and representations …Back to languages and representations …

Finite{010,11, 21}

Regular0*1* DFA

NFA Regex

Context-FreeCFG

All

S → 0S1 | ε

How do we prove that all regular languages are context-free? By showing how toconstruct a CFG for every regular language!

8

Converting a regular expression to a CFGConverting a regular expression to a CFG

Use the following function on regular expressions: the CFG with no productions. the CFG with just the production . the CFG with just the production for every .

the CFG with the productions , , and . the CFG with the productions , , and .

the CFG with the productions and .

Example:

𝖼𝖿𝗀(∅) =

𝖼𝖿𝗀(ε) = → εSε

𝖼𝖿𝗀(a) = → aSa a ∈ Σ

𝖼𝖿𝗀(AB) = 𝖼𝖿𝗀(A) 𝖼𝖿𝗀(B) →SAB SASB

𝖼𝖿𝗀(A ∪ B) = 𝖼𝖿𝗀(A) 𝖼𝖿𝗀(B) → |SA∪B SA SB

𝖼𝖿𝗀( ) =A∗ 𝖼𝖿𝗀(A) → ε |SA∗ SASA∗

𝖼𝖿𝗀((1 ∪ 0 0))∗

S(1∪0 0)∗

S(1∪0)∗

S(1∪0)

S1

S0

→ S(1∪0)∗ S0

→ ε | S1∪0S(1∪0)∗

→ |S1 S0

→ 1

→ 0

9

Back again to languages and representations …Back again to languages and representations …

Finite{010,11, 21}

Regular0*1* DFA

NFA Regex

Context-FreeCFG

All

S → 0S1 | ε

10

Back again to languages and representations …Back again to languages and representations …

Finite{010,11, 21}

Regular0*1* DFA

NFA Regex

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 any regularexpression. How would you prove that?

Lecture 21 B

B

10

Why isn’t Why isn’t (binary palindromes) a regular language? (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

B

11

Why isn’t Why isn’t (binary palindromes) a regular language? (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

B wvw⎯ ⎯⎯⎯

w⎯ ⎯⎯⎯

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

11

Why isn’t Why isn’t (binary palindromes) a regular language? (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

B wvw⎯ ⎯⎯⎯

w⎯ ⎯⎯⎯

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

B

11

Why isn’t Why isn’t (binary palindromes) a regular language? (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

B wvw⎯ ⎯⎯⎯

w⎯ ⎯⎯⎯

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

B

w w⎯ ⎯⎯⎯

11

Why isn’t Why isn’t (binary palindromes) a regular language? (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

B wvw⎯ ⎯⎯⎯

w⎯ ⎯⎯⎯

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

B

w w⎯ ⎯⎯⎯

w

11

Why isn’t Why isn’t (binary palindromes) a regular language? (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

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 this intuitioninto a formal proof.

B

11

A strategy for proving that A strategy for proving that is not regular 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.

BB

B

M B

M

12

A strategy for proving that A strategy for proving that is not regular 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 same state, aDFA can no longer distinguish their suffixes.

0a10b1

?

BB

B

M B

M

12

A strategy for proving that A strategy for proving that is not regular 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 same state, aDFA can no longer distinguish their suffixes.

0a10b1

?

Key Idea 2The machine has finitely many states, and since thestrings in have infinitely many distinct prefixes, two ofthem must collide!

BB

B

M B

M

M

B

12

A strategy for proving that A strategy for proving that is not regular 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 same state, aDFA can no longer distinguish their suffixes.

0a10b1

?

Key Idea 2The machine has finitely many states, and since thestrings in have infinitely many distinct prefixes, two ofthem must collide!

BB

B

M B

M

M

B

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 sb

t

t, tsa sb B

S = {1, 01, 001, 0001, …}

= { 1 : n ≥ 0}0n

12

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

BB

M B M

S = { 1 : n ≥ 0}0n

13

Proving that Proving that is not regular is not regularSuppose that some DFA recognizes . We show accepts or rejects astring 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 samestate of .

BB

M B M

S = { 1 : n ≥ 0}0n

M S

1 ∈ S0a 1 ∈ S0b a ≠ b

M

13

Proving that Proving that is not regular is not regularSuppose that some DFA recognizes . We show accepts or rejects astring 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 samestate of .

BB

M B M

S = { 1 : n ≥ 0}0n

M S

1 ∈ S0a 1 ∈ S0b a ≠ b

M

Important: We don’t get to choose and ! We just know they exist.a b

13

Proving that Proving that is not regular is not regularSuppose that some DFA recognizes . We show accepts or rejects astring 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 samestate of .

Now, consider appending to both strings. 0a10b1

0a

BB

M B M

S = { 1 : n ≥ 0}0n

M S

1 ∈ S0a 1 ∈ S0b a ≠ b

M

0a

13

Proving that Proving that is not regular is not regularSuppose that some DFA recognizes . We show accepts or rejects astring 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 samestate 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.

BB

M B M

S = { 1 : n ≥ 0}0n

M S

1 ∈ S0a 1 ∈ S0b a ≠ b

M

0a

10a 10b 0a10a 0b10a q

M q

∈ B0a10a M ∉ B0b10a◻

13

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

14

A template for proving that a language A template for proving that a language is not regular is not regular

① Suppose for contradiction that some DFA recognizes .

LL

M

L

15

A template for proving that a language A template for proving that a language is not regular is not regular

① Suppose for contradiction that some DFA recognizes .

② Consider the set { }.

LL

M

L

S = …

must be infinite, andfor every pair of prefixes

, there is asuffix such that one ofof is in but notthe other.

S

≠ ∈ Ssa sb

t

t, tsa sb L

15

A template for proving that a language A template for proving that a language is not regular is not regular

① Suppose for contradiction that some DFA recognizes .

② Consider the set { }.

③ Since is infinite and has finitely many states, theremust be two strings such that and bothend in the same state of .

LL

M

L

S = …

S M

,sa sb ∈ S ≠sa sb

M

must be infinite, andfor every pair of prefixes

, there is asuffix such that one ofof is in but notthe other.

We don’t get to choose and !

S

≠ ∈ Ssa sb

t

t, tsa sb L

sa sb

15

A template for proving that a language A template for proving that a language is not regular is not regular

① Suppose for contradiction that some DFA recognizes .

② Consider the set { }.

③ Since is infinite and has finitely many states, theremust be two strings such that and bothend in the same state of .

④ Consider appending to both and .

LL

M

L

S = …

S M

,sa sb ∈ S ≠sa sb

M

t sa sb

must be infinite, andfor every pair of prefixes

, there is asuffix such that one ofof is in but notthe other.

We don’t get to choose and !

should be an acceptsuffix for but not .

S

≠ ∈ Ssa sb

t

t, tsa sb L

sa sb

t

sa sb

15

A template for proving that a language A template for proving that a language is not regular is not regular

① Suppose for contradiction that some DFA recognizes .

② Consider the set { }.

③ Since is infinite and has finitely many states, theremust be two strings such that and bothend 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 .

LL

M

L

S = …

S M

,sa sb ∈ S ≠sa sb

M

t sa sb

sa sb M tsa

tsb q M t ∈ Lsa

t ∉ Lsb M L

must be infinite, andfor every pair of prefixes

, there is asuffix such that one ofof is in but notthe other.

We don’t get to choose and !

should be an acceptsuffix for but not .

S

≠ ∈ Ssa sb

t

t, tsa sb L

sa sb

t

sa sb

15

A template for proving that a language A template for proving that a language is not regular is not regular

① Suppose for contradiction that some DFA recognizes .

② Consider the set { }.

③ Since is infinite and has finitely many states, theremust be two strings such that and bothend 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 .

LL

M

L

S = …

S M

,sa sb ∈ S ≠sa sb

M

t sa sb

sa sb M tsa

tsb q M t ∈ Lsa

t ∉ Lsb M L

M L

must be infinite, andfor every pair of prefixes

, there is asuffix such that one ofof is in but notthe other.

We don’t get to choose and !

should be an acceptsuffix for but not .

S

≠ ∈ Ssa sb

t

t, tsa sb L

sa sb

t

sa sb

15

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

Consider the set

LL == {{ :: nn ≥≥ 00}}00nn11nn

M L

S =

16

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

Consider the set .

LL == {{ :: nn ≥≥ 00}}00nn11nn

M L

S = { : n ≥ 0}0n

16

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

Consider the set .

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

LL == {{ :: nn ≥≥ 00}}00nn11nn

M L

S = { : n ≥ 0}0n

S M

,0a 0b∈ S a ≠ b M

16

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

Consider the set .

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

Consider appending to both and .

LL == {{ :: nn ≥≥ 00}}00nn11nn

M L

S = { : n ≥ 0}0n

S M

,0a 0b∈ S a ≠ b M

0a 0b

16

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

Consider the set .

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

Consider appending to both and .

LL == {{ :: nn ≥≥ 00}}00nn11nn

M L

S = { : n ≥ 0}0n

S M

,0a 0b∈ S a ≠ b M

1a 0a 0b

16

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

Consider the set .

Since is infinite and has finitely many states, there must be two strings 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 end inthe same state of . Since and , does not recognize

.

LL == {{ :: nn ≥≥ 00}}00nn11nn

M L

S = { : n ≥ 0}0n

S M

,0a 0b∈ S a ≠ b M

1a 0a 0b

0a 0b M 0a1a 0b1a

q M ∈ L0a1a∉ L0b1a M

L

16

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

Consider the set .

Since is infinite and has finitely many states, there must be two strings 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 end inthe same state of . Since and , does not recognize

.

Since was arbitrary, no DFA recognizes .

LL == {{ :: nn ≥≥ 00}}00nn11nn

M L

S = { : n ≥ 0}0n

S M

,0a 0b∈ S a ≠ b M

1a 0a 0b

0a 0b M 0a1a 0b1a

q M ∈ L0a1a∉ L0b1a M

L

M L

16

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

Consider the set

LL == {{ :: nn ≥≥ 00}}((nn))nn

M L

S =

17

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

Consider the set .

LL == {{ :: nn ≥≥ 00}}((nn))nn

M L

S = { : n ≥ 0}(n

17

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

Consider the set .

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

LL == {{ :: nn ≥≥ 00}}((nn))nn

M L

S = { : n ≥ 0}(n

S M

,(a (b ∈ S a ≠ b M

17

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

Consider the set .

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

Consider appending to both and .

LL == {{ :: nn ≥≥ 00}}((nn))nn

M L

S = { : n ≥ 0}(n

S M

,(a (b ∈ S a ≠ b M

(a (b

17

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

Consider the set .

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

Consider appending to both and .

LL == {{ :: nn ≥≥ 00}}((nn))nn

M L

S = { : n ≥ 0}(n

S M

,(a (b ∈ S a ≠ b M

)a (a (b

17

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

Consider the set .

Since is infinite and has finitely many states, there must be two strings 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 end in thesame state of . Since and , does not recognize .

LL == {{ :: nn ≥≥ 00}}((nn))nn

M L

S = { : 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 M L

17

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

Consider the set .

Since is infinite and has finitely many states, there must be two strings 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 end in thesame state of . Since and , does not recognize .

Since was arbitrary, no DFA recognizes .

LL == {{ :: nn ≥≥ 00}}((nn))nn

M L

S = { : 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 M L

M L

17

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

L S

≠ ∈ Ssa sb t

t, tsa sb L

18

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

If is infinite, then is not regular.

L S

≠ ∈ Ssa sb t

t, tsa sb L

S L

18

A fun fact about this proof methodA fun fact about this proof methodSuppose that for a language , the set is a largest set of prefix strings withthe property that for every pair , there is some string such thatone 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, one reachedby each member of .

L S

≠ ∈ Ssa sb t

t, tsa sb L

S L

S L |S|

S

18

SummarySummaryDFAs 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.

≡ ≡

⊂ ⊂

19