Automata and Formal Languages - CM0081 Finite Automata … · Automata and Formal Languages -...

Preview:

Citation preview

Automata and Formal Languages - CM0081Finite Automata and Regular Expressions

Andrés Sicard-Ramírez

Universidad EAFIT

Semester 2018-2

Introduction

Equivalences

DFA NFA

𝜀-NFA RE

Finite Automata and Regular Expressions 2/40

From Finite Automata to Regular Expressions

Theorem (3.4)If 𝐿 = 𝐿(𝐷) for some DFA 𝐷, then there is a regular expression 𝑅 suchthat 𝐿 = 𝐿(𝑅).

Finite Automata and Regular Expressions 3/40

From Finite Automata to Regular Expressions

Inductive proof on the number of states of the automatonLet the states of 𝐷 be {1, 2, … , 𝑛} with 1 the start state.

𝑅𝑘𝑖𝑗: Regular expression describing the set of labels of all paths in 𝐷

from state 𝑖 to state 𝑗 such that the path has no intermediate nodewhose number is greater than 𝑘.†

†Figure from Hopcroft, Motwani and Ullman (2007, Fig. 3.2).Finite Automata and Regular Expressions 4/40

From Finite Automata to Regular Expressions

Inductive proof on the number of states of the automatonLet the states of 𝐷 be {1, 2, … , 𝑛} with 1 the start state.𝑅𝑘

𝑖𝑗: Regular expression describing the set of labels of all paths in 𝐷from state 𝑖 to state 𝑗 such that the path has no intermediate nodewhose number is greater than 𝑘.†

†Figure from Hopcroft, Motwani and Ullman (2007, Fig. 3.2).Finite Automata and Regular Expressions 5/40

From Finite Automata to Regular Expressions

Basis step: Proof for 𝑘 = 0 (i.e. no intermediate states) and 𝑖 ≠ 𝑗

No transition from state 𝑖 to state 𝑗

𝑖 𝑗 𝑅0𝑖𝑗 = ∅

One transition from state 𝑖 to state 𝑗

𝑖 𝑗a 𝑅0𝑖𝑗 = a

Various transitions from state 𝑖 to state 𝑗

𝑖 𝑗a1…

a𝑛

𝑅0𝑖𝑗 = a1 + ⋯ + an

Finite Automata and Regular Expressions 6/40

From Finite Automata to Regular Expressions

Basis step: Proof for 𝑘 = 0 (i.e. no intermediate states) and 𝑖 ≠ 𝑗No transition from state 𝑖 to state 𝑗

𝑖 𝑗 𝑅0𝑖𝑗 = ∅

One transition from state 𝑖 to state 𝑗

𝑖 𝑗a 𝑅0𝑖𝑗 = a

Various transitions from state 𝑖 to state 𝑗

𝑖 𝑗a1…

a𝑛

𝑅0𝑖𝑗 = a1 + ⋯ + an

Finite Automata and Regular Expressions 7/40

From Finite Automata to Regular Expressions

Basis step: Proof for 𝑘 = 0 (i.e. no intermediate states) and 𝑖 ≠ 𝑗No transition from state 𝑖 to state 𝑗

𝑖 𝑗 𝑅0𝑖𝑗 = ∅

One transition from state 𝑖 to state 𝑗

𝑖 𝑗a 𝑅0𝑖𝑗 = a

Various transitions from state 𝑖 to state 𝑗

𝑖 𝑗a1…

a𝑛

𝑅0𝑖𝑗 = a1 + ⋯ + an

Finite Automata and Regular Expressions 8/40

From Finite Automata to Regular Expressions

Basis step: Proof for 𝑘 = 0 (i.e. no intermediate states) and 𝑖 ≠ 𝑗No transition from state 𝑖 to state 𝑗

𝑖 𝑗 𝑅0𝑖𝑗 = ∅

One transition from state 𝑖 to state 𝑗

𝑖 𝑗a 𝑅0𝑖𝑗 = a

Various transitions from state 𝑖 to state 𝑗

𝑖 𝑗a1…

a𝑛

𝑅0𝑖𝑗 = a1 + ⋯ + an

Finite Automata and Regular Expressions 9/40

From Finite Automata to Regular Expressions

Basis step: Proof for 𝑘 = 0 (i.e. no intermediate states) and 𝑖 = 𝑗

No loops

𝑖 𝑅0𝑖𝑖 = 𝜀

One loop

𝑖a 𝑅0𝑖𝑖 = 𝜀 + a

QuestionWhy do not to define 𝑅0

𝑖𝑖 = a∗ ?

Various loops

𝑖a1

a𝑛𝑅0

𝑖𝑖 = 𝜀 + a1 + ⋯ + an

Finite Automata and Regular Expressions 10/40

From Finite Automata to Regular Expressions

Basis step: Proof for 𝑘 = 0 (i.e. no intermediate states) and 𝑖 = 𝑗No loops

𝑖 𝑅0𝑖𝑖 = 𝜀

One loop

𝑖a 𝑅0𝑖𝑖 = 𝜀 + a

QuestionWhy do not to define 𝑅0

𝑖𝑖 = a∗ ?

Various loops

𝑖a1

a𝑛𝑅0

𝑖𝑖 = 𝜀 + a1 + ⋯ + an

Finite Automata and Regular Expressions 11/40

From Finite Automata to Regular Expressions

Basis step: Proof for 𝑘 = 0 (i.e. no intermediate states) and 𝑖 = 𝑗No loops

𝑖 𝑅0𝑖𝑖 = 𝜀

One loop

𝑖a 𝑅0𝑖𝑖 = 𝜀 + a

QuestionWhy do not to define 𝑅0

𝑖𝑖 = a∗ ?

Various loops

𝑖a1

a𝑛𝑅0

𝑖𝑖 = 𝜀 + a1 + ⋯ + an

Finite Automata and Regular Expressions 12/40

From Finite Automata to Regular Expressions

Basis step: Proof for 𝑘 = 0 (i.e. no intermediate states) and 𝑖 = 𝑗No loops

𝑖 𝑅0𝑖𝑖 = 𝜀

One loop

𝑖a 𝑅0𝑖𝑖 = 𝜀 + a

QuestionWhy do not to define 𝑅0

𝑖𝑖 = a∗ ?

Various loops

𝑖a1

a𝑛𝑅0

𝑖𝑖 = 𝜀 + a1 + ⋯ + an

Finite Automata and Regular Expressions 13/40

From Finite Automata to Regular Expressions

Basis step: Proof for 𝑘 = 0 (i.e. no intermediate states) and 𝑖 = 𝑗No loops

𝑖 𝑅0𝑖𝑖 = 𝜀

One loop

𝑖a 𝑅0𝑖𝑖 = 𝜀 + a

QuestionWhy do not to define 𝑅0

𝑖𝑖 = a∗ ?

Various loops

𝑖a1

a𝑛𝑅0

𝑖𝑖 = 𝜀 + a1 + ⋯ + an

Finite Automata and Regular Expressions 14/40

From Finite Automata to Regular Expressions

Inductive step: Proof for 𝑘Inductive hypothesis 𝑅𝑘−1

𝑖𝑗 : path from state 𝑖 to state 𝑗 that goes throughno state higher than 𝑘 − 1.

The path does not go through state 𝑘 at all

𝑖 𝑘 𝑗 𝑅𝑘𝑖𝑗 = 𝑅𝑘−1

𝑖𝑗

The path goes through state 𝑘 at least once

𝑖 𝑘 𝑘 𝑘 𝑗𝑅𝑘

𝑖𝑗 = 𝑅𝑘−1𝑖𝑘 (𝑅𝑘−1

𝑘𝑘 )∗𝑅𝑘−1𝑘𝑗

From the previous cases:

𝑅𝑘𝑖𝑗 = 𝑅𝑘−1

𝑖𝑗 + 𝑅𝑘−1𝑖𝑘 (𝑅𝑘−1

𝑘𝑘 )∗𝑅𝑘−1𝑘𝑗

Finite Automata and Regular Expressions 15/40

From Finite Automata to Regular Expressions

Inductive step: Proof for 𝑘Inductive hypothesis 𝑅𝑘−1

𝑖𝑗 : path from state 𝑖 to state 𝑗 that goes throughno state higher than 𝑘 − 1.

The path does not go through state 𝑘 at all

𝑖 𝑘 𝑗 𝑅𝑘𝑖𝑗 = 𝑅𝑘−1

𝑖𝑗

The path goes through state 𝑘 at least once

𝑖 𝑘 𝑘 𝑘 𝑗𝑅𝑘

𝑖𝑗 = 𝑅𝑘−1𝑖𝑘 (𝑅𝑘−1

𝑘𝑘 )∗𝑅𝑘−1𝑘𝑗

From the previous cases:

𝑅𝑘𝑖𝑗 = 𝑅𝑘−1

𝑖𝑗 + 𝑅𝑘−1𝑖𝑘 (𝑅𝑘−1

𝑘𝑘 )∗𝑅𝑘−1𝑘𝑗

Finite Automata and Regular Expressions 16/40

From Finite Automata to Regular Expressions

Inductive step: Proof for 𝑘Inductive hypothesis 𝑅𝑘−1

𝑖𝑗 : path from state 𝑖 to state 𝑗 that goes throughno state higher than 𝑘 − 1.

The path does not go through state 𝑘 at all

𝑖 𝑘 𝑗 𝑅𝑘𝑖𝑗 = 𝑅𝑘−1

𝑖𝑗

The path goes through state 𝑘 at least once

𝑖 𝑘 𝑘 𝑘 𝑗𝑅𝑘

𝑖𝑗 = 𝑅𝑘−1𝑖𝑘 (𝑅𝑘−1

𝑘𝑘 )∗𝑅𝑘−1𝑘𝑗

From the previous cases:

𝑅𝑘𝑖𝑗 = 𝑅𝑘−1

𝑖𝑗 + 𝑅𝑘−1𝑖𝑘 (𝑅𝑘−1

𝑘𝑘 )∗𝑅𝑘−1𝑘𝑗

Finite Automata and Regular Expressions 17/40

From Finite Automata to Regular Expressions

Inductive step: Proof for 𝑘Inductive hypothesis 𝑅𝑘−1

𝑖𝑗 : path from state 𝑖 to state 𝑗 that goes throughno state higher than 𝑘 − 1.

The path does not go through state 𝑘 at all

𝑖 𝑘 𝑗 𝑅𝑘𝑖𝑗 = 𝑅𝑘−1

𝑖𝑗

The path goes through state 𝑘 at least once

𝑖 𝑘 𝑘 𝑘 𝑗𝑅𝑘

𝑖𝑗 = 𝑅𝑘−1𝑖𝑘 (𝑅𝑘−1

𝑘𝑘 )∗𝑅𝑘−1𝑘𝑗

From the previous cases:

𝑅𝑘𝑖𝑗 = 𝑅𝑘−1

𝑖𝑗 + 𝑅𝑘−1𝑖𝑘 (𝑅𝑘−1

𝑘𝑘 )∗𝑅𝑘−1𝑘𝑗

Finite Automata and Regular Expressions 18/40

From Finite Automata to Regular Expressions

Given that the states of 𝐷 are {1, 2, … , 𝑛} with 1 the start state, then

𝐿(𝐷) = 𝐿(𝑅𝑛1𝑓1

+ ⋯ + 𝑅𝑛1𝑓𝑚

) with 𝑓𝑖 ∈ 𝐹.

Finite Automata and Regular Expressions 19/40

From Finite Automata to Regular Expressions

ExampleTo convert the DFA 𝐷 to a regular expression.

1start 2

10 0, 1

𝐿(𝐷) = {𝑤 ∈ {0, 1}∗ ∣ 𝑤 has at least one 0}.

Finite Automata and Regular Expressions 20/40

From Finite Automata to Regular Expressions

Example (cont.)

1start 2

10 0, 1

𝑅0𝑖𝑗

𝑅0𝑖𝑗 Regexp

𝑅011 𝜀 + 1

𝑅012 0

𝑅021 ∅

𝑅022 𝜀 + 0 + 1

Finite Automata and Regular Expressions 21/40

From Finite Automata to Regular Expressions

Example (cont.)𝑅1

𝑖𝑗 = 𝑅0𝑖𝑗 + 𝑅0

𝑖1(𝑅011)∗𝑅0

1𝑗

𝑅0𝑖𝑗 Regexp

𝑅011 𝜀 + 1

𝑅012 0

𝑅021 ∅

𝑅022 𝜀 + 0 + 1

𝑅1𝑖𝑗 Regexp

𝑅111 (𝜀 + 1) + (𝜀 + 1)(𝜀 + 1)∗(𝜀 + 1)

𝑅112 0 + (𝜀 + 1)(𝜀 + 1)∗0

𝑅121 ∅ + ∅(𝜀 + 1)∗(𝜀 + 1)

𝑅122 𝜀 + 0 + 1 + ∅(𝜀 + 1)∗0

Finite Automata and Regular Expressions 22/40

From Finite Automata to Regular Expressions

Example (cont.)Some simplifications for regular expressionsLet 𝐿 and 𝑀 be regular expression variables.

(𝜀 + 𝐿)∗ = 𝐿∗

(𝜀 + 𝐿)𝐿∗ = 𝐿∗

𝐿 + 𝑀∗𝐿 = 𝑀∗𝐿𝐿∅ = ∅𝐿 = ∅ (∅ is the annihilator for concatenation)

𝐿 + ∅ = ∅ + 𝐿 = 𝐿 (∅ is the identity for union)

Finite Automata and Regular Expressions 23/40

From Finite Automata to Regular Expressions

Example (cont.)𝑅1

𝑖𝑗 = 𝑅0𝑖𝑗 + 𝑅0

𝑖1(𝑅011)∗𝑅0

1𝑗

𝑅1𝑖𝑗 Regexp Simplified

𝑅111 (𝜀 + 1) + (𝜀 + 1)(𝜀 + 1)∗(𝜀 + 1) 1∗

𝑅112 0 + (𝜀 + 1)(𝜀 + 1)∗0 1∗0

𝑅121 ∅ + ∅(𝜀 + 1)∗(𝜀 + 1) ∅

𝑅122 𝜀 + 0 + 1 + ∅(𝜀 + 1)∗0 𝜀 + 0 + 1

Finite Automata and Regular Expressions 24/40

From Finite Automata to Regular Expressions

Example (cont.)𝑅2

𝑖𝑗 = 𝑅1𝑖𝑗 + 𝑅1

𝑖2(𝑅122)∗𝑅1

2𝑗

𝑅1𝑖𝑗 Regexp

𝑅111 1∗

𝑅112 1∗0

𝑅121 ∅

𝑅122 𝜀 + 0 + 1

𝑅2𝑖𝑗 Regexp

𝑅211 1∗ + 1∗0(𝜀 + 0 + 1)∗∅

𝑅212 1∗0 + 1∗0(𝜀 + 0 + 1)∗(𝜀 + 0 + 1)

𝑅221 ∅ + (𝜀 + 0 + 1)(𝜀 + 0 + 1)∗∅

𝑅222 𝜀 + 0 + 1 + (𝜀 + 0 + 1)(𝜀 + 0 + 1)∗(𝜀 + 0 + 1)

Finite Automata and Regular Expressions 25/40

From Finite Automata to Regular Expressions

Example (cont.)𝑅2

𝑖𝑗 = 𝑅1𝑖𝑗 + 𝑅1

𝑖2(𝑅122)∗𝑅1

2𝑗

𝑅2𝑖𝑗 Regexp Simplified

𝑅211 1∗ + 1∗0(𝜀 + 0 + 1)∗∅ 1∗

𝑅212 1∗0 + 1∗0(𝜀 + 0 + 1)∗(𝜀 + 0 + 1) 1∗0(0 + 1)∗

𝑅221 ∅ + (𝜀 + 0 + 1)(𝜀 + 0 + 1)∗∅ ∅

𝑅222 𝜀 + 0 + 1 + (𝜀 + 0 + 1)(𝜀 + 0 + 1)∗(𝜀 + 0 + 1) (0 + 1)∗

Finite Automata and Regular Expressions 26/40

From Finite Automata to Regular Expressions

Example (cont.)The regular expression equivalent to the automaton 𝐷 is

𝑅212 = 1∗0(0 + 1)∗.

Finite Automata and Regular Expressions 27/40

From Regular Expressions to Finite Automata

Theorem (3.7)

Every language defined by a regular expression is also defined by a finiteautomaton.

Inductive proofFor each regular expression 𝑅 we construct an 𝜀-NFA 𝐸 such that 𝐿(𝑅) =𝐿(𝐸) with:

1. exactly one accepting state,2. no arcs into the initial state and3. no arcs out of the accepting state.

Finite Automata and Regular Expressions 28/40

From Regular Expressions to Finite Automata

Theorem (3.7)

Every language defined by a regular expression is also defined by a finiteautomaton.

Inductive proofFor each regular expression 𝑅 we construct an 𝜀-NFA 𝐸 such that 𝐿(𝑅) =𝐿(𝐸) with:

1. exactly one accepting state,2. no arcs into the initial state and3. no arcs out of the accepting state.

Finite Automata and Regular Expressions 29/40

From Regular Expressions to Finite Automata

Basis step: Automata for 𝜀 (a), ∅ (b) and a (c).†

From regex’s to ε-NFA’s

Theorem 3.7: For every regex R we can con-

struct and ε-NFA A, s.t. L(A) = L(R).

Proof: By structural induction:

Basis: Automata for ε, ∅, and a.

ε

a

(a)

(b)

(c)

73

†Figure from Hopcroft, Motwani and Ullman (2007, Fig. 3.16).Finite Automata and Regular Expressions 30/40

From Regular Expressions to Finite Automata

Inductive step: Automaton for 𝑅 + 𝑆.†���� FINITE AUTOMATA AND REGULAR EXPRESSIONS ���

(a)

(b)

(c)

R

S

R S

R

ε ε

εε

ε

ε

ε

ε ε

Figure ����� The inductive step in the regularexpressionto�NFA construction

automaton of Fig� �����c�� That automaton allows us to go either�

�a� Directly from the start state to the accepting state along a pathlabeled �� That path lets us accept �� which is in L�R�� no matterwhat expression R is�

�b� To the start state of the automaton for R� through that automatonone or more times� and then to the accepting state� This set of pathsallows us to accept strings in L�R�� L�R�L�R�� L�R�L�R�L�R�� andso on� thus covering all strings in L�R�� except perhaps �� which wascovered by the direct arc to the accepting state mentioned in ��a��

�� The expression is �R� for some smaller expression R� The automatonfor R also serves as the automaton for �R�� since the parentheses do notchange the language de ned by the expression�

It is a simple observation that the constructed automata satisfy the three conditions given in the inductive hypothesis � one accepting state� with no arcsinto the initial state or out of the accepting state� �

†Figure from Hopcroft, Motwani and Ullman (2007, Fig. 3.17).Finite Automata and Regular Expressions 31/40

From Regular Expressions to Finite Automata

Inductive step: Automaton for 𝑅𝑆.†

���� FINITE AUTOMATA AND REGULAR EXPRESSIONS ���

(a)

(b)

(c)

R

S

R S

R

ε ε

εε

ε

ε

ε

ε ε

Figure ����� The inductive step in the regularexpressionto�NFA construction

automaton of Fig� �����c�� That automaton allows us to go either�

�a� Directly from the start state to the accepting state along a pathlabeled �� That path lets us accept �� which is in L�R�� no matterwhat expression R is�

�b� To the start state of the automaton for R� through that automatonone or more times� and then to the accepting state� This set of pathsallows us to accept strings in L�R�� L�R�L�R�� L�R�L�R�L�R�� andso on� thus covering all strings in L�R�� except perhaps �� which wascovered by the direct arc to the accepting state mentioned in ��a��

�� The expression is �R� for some smaller expression R� The automatonfor R also serves as the automaton for �R�� since the parentheses do notchange the language de ned by the expression�

It is a simple observation that the constructed automata satisfy the three conditions given in the inductive hypothesis � one accepting state� with no arcsinto the initial state or out of the accepting state� �

†Figure from Hopcroft, Motwani and Ullman (2007, Fig. 3.17).Finite Automata and Regular Expressions 32/40

From Regular Expressions to Finite Automata

Inductive step: Automaton for 𝑅∗.†

���� FINITE AUTOMATA AND REGULAR EXPRESSIONS ���

(a)

(b)

(c)

R

S

R S

R

ε ε

εε

ε

ε

ε

ε ε

Figure ����� The inductive step in the regularexpressionto�NFA construction

automaton of Fig� �����c�� That automaton allows us to go either�

�a� Directly from the start state to the accepting state along a pathlabeled �� That path lets us accept �� which is in L�R�� no matterwhat expression R is�

�b� To the start state of the automaton for R� through that automatonone or more times� and then to the accepting state� This set of pathsallows us to accept strings in L�R�� L�R�L�R�� L�R�L�R�L�R�� andso on� thus covering all strings in L�R�� except perhaps �� which wascovered by the direct arc to the accepting state mentioned in ��a��

�� The expression is �R� for some smaller expression R� The automatonfor R also serves as the automaton for �R�� since the parentheses do notchange the language de ned by the expression�

It is a simple observation that the constructed automata satisfy the three conditions given in the inductive hypothesis � one accepting state� with no arcsinto the initial state or out of the accepting state� �

†Figure from Hopcroft, Motwani and Ullman (2007, Fig. 3.17).Finite Automata and Regular Expressions 33/40

From Regular Expressions to Finite Automata

ExampleConvert the regular expression 𝑅 = (0 + 1)∗1(0 + 1) to an 𝜀-NFA 𝐸 such𝐿(𝑅) = 𝐿(𝐸).

1. 𝜀-NFA for 0 + 1:

start𝜀

𝜀

0 𝜀

1𝜀

Finite Automata and Regular Expressions 34/40

From Regular Expressions to Finite Automata

ExampleConvert the regular expression 𝑅 = (0 + 1)∗1(0 + 1) to an 𝜀-NFA 𝐸 such𝐿(𝑅) = 𝐿(𝐸).

1. 𝜀-NFA for 0 + 1:

start𝜀

𝜀

0 𝜀

1𝜀

Finite Automata and Regular Expressions 35/40

From Regular Expressions to Finite Automata

Example (cont.)2. 𝜀-NFA for (0 + 1)∗:

start 𝜀 𝜀

𝜀

0 𝜀

1𝜀

𝜀

𝜀

𝜀

Finite Automata and Regular Expressions 36/40

From Regular Expressions to Finite Automata

Example (cont.)3. 𝜀-NFA for (0 + 1)∗1:

start 𝜀 𝜀

𝜀

0 𝜀

1𝜀

𝜀

𝜀

𝜀

𝜀 1

Finite Automata and Regular Expressions 37/40

From Regular Expressions to Finite Automata

Example (cont.)4. 𝜀-NFA for (0 + 1)∗1(0 + 1):

start 𝜀 𝜀

𝜀

0 𝜀

1𝜀

𝜀

𝜀

𝜀

𝜀 1 𝜀 𝜀

𝜀

0 𝜀

1𝜀

Finite Automata and Regular Expressions 38/40

From Regular Expressions to Finite Automata

Exercise (3.2.7)There are some simplifications to the constructions of Theorem 3.7, where weconverted a regular expression to an 𝜀-NFA. Here are three:

1. For the union operator, instead of creating new start and accepting states,merge the two start states into one state with all the transitions of bothstart states. Likewise, merge the two accepting states, having all transitionsto either go to the merged state instead.

2. For the concatenation operator, merge the accepting state of the first auto-maton with the start state of the second.

3. For the closure operator, simply add 𝜀-transitions from the accepting state tothe start state and vice-versa.

Each of these simplifications, by themselves, still yield a correct construction; thatis, the resulting 𝜀-NFA for any regular expression accepts the language of the expres-sion. Which subsets of changes (1), (2) and (3) may be made to the constructiontogether, while still yielding a correct automaton for every regular expression?

Finite Automata and Regular Expressions 39/40

References

Hopcroft, J. E., Motwani, R. and Ullman, J. D. (2007). Introduction toAutomata theory, Languages, and Computation. 3rd ed. Pearson Education(cit. on pp. 4, 5, 30–33).

Finite Automata and Regular Expressions 40/40

Recommended