CM0081 Finite Automata and Regular Expressions

Automata and Formal Languages - CM0081
Finite Automata and Regular Expressions
Andrés Sicard-Ramírez
Universidad EAFIT
Semester 2017-2
Introduction
DFA
NFA
πœ€-NFA
RE
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
2/39
From Finite Automata to Regular Expressions
Theorem (Hopcroft, Motwani and Ullman [2007], Theorem 3.4)
If 𝐿 = 𝐿(𝐴) for some DFA 𝐴, then there is a regular expression 𝑅
such that 𝐿 = 𝐿(𝑅).
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
3/39
From Finite Automata to Regular Expressions
Inductive proof on the number of states of the automaton
Let the states of 𝐴 be {1, 2, … , 𝑛} with 1 the start state.
βˆ—
Hopcroft, Motwani and Ullman [2007, Fig. 3.2].
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
4/39
From Finite Automata to Regular Expressions
Inductive proof on the number of states of the automaton
Let 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 node whose number is greater than π‘˜.βˆ—
βˆ—
Hopcroft, Motwani and Ullman [2007, Fig. 3.2].
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
5/39
From Finite Automata to Regular Expressions
Basis step: Proof for π‘˜ = 0 (i.e. no intermediate states) and 𝑖 β‰  𝑗
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
6/39
From Finite Automata to Regular Expressions
Basis step: Proof for π‘˜ = 0 (i.e. no intermediate states) and 𝑖 β‰  𝑗
No transition from state 𝑖 to state 𝑗
𝑖
𝑗
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
0
𝑅𝑖𝑗
=βˆ…
7/39
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
𝑗
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
0
𝑅𝑖𝑗
=a
8/39
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
0
…
𝑅𝑖𝑗
= a1 + β‹― + an
𝑗
𝑖
a𝑛
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
9/39
From Finite Automata to Regular Expressions
Basis step: Proof for π‘˜ = 0 (i.e. no intermediate states) and 𝑖 = 𝑗
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
10/39
From Finite Automata to Regular Expressions
Basis step: Proof for π‘˜ = 0 (i.e. no intermediate states) and 𝑖 = 𝑗
No loops
𝑖
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
0
𝑅𝑖𝑖
=πœ€
11/39
From Finite Automata to Regular Expressions
Basis step: Proof for π‘˜ = 0 (i.e. no intermediate states) and 𝑖 = 𝑗
No loops
0
𝑅𝑖𝑖
=πœ€
𝑖
One loop
a
𝑖
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
0
𝑅𝑖𝑖
=πœ€+a
12/39
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
0
0
Question: Why not define 𝑅𝑖𝑖
by 𝑅𝑖𝑖
= πœ€ + aβˆ— ?
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
13/39
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
𝑖
0
0
Question: Why not define 𝑅𝑖𝑖
by 𝑅𝑖𝑖
= πœ€ + aβˆ— ?
Various loops
a1
𝑖
…
a𝑛
0
𝑅𝑖𝑖
= πœ€ + a1 + β‹― + an
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
14/39
From Finite Automata to Regular Expressions
Inductive step: Proof for π‘˜
π‘˜βˆ’1
Inductive hypothesis 𝑅𝑖𝑗
: path from state 𝑖 to state 𝑗 that goes
through no state higher than π‘˜ βˆ’ 1.
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
15/39
From Finite Automata to Regular Expressions
Inductive step: Proof for π‘˜
π‘˜βˆ’1
Inductive hypothesis 𝑅𝑖𝑗
: path from state 𝑖 to state 𝑗 that goes
through no state higher than π‘˜ βˆ’ 1.
The path does not through state π‘˜ at all
𝑖
π‘˜
𝑗
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
π‘˜
π‘˜βˆ’1
𝑅𝑖𝑗
= 𝑅𝑖𝑗
16/39
From Finite Automata to Regular Expressions
Inductive step: Proof for π‘˜
π‘˜βˆ’1
Inductive hypothesis 𝑅𝑖𝑗
: path from state 𝑖 to state 𝑗 that goes
through no state higher than π‘˜ βˆ’ 1.
The path does not through state π‘˜ at all
𝑖
π‘˜
π‘˜βˆ’1
𝑅𝑖𝑗
= 𝑅𝑖𝑗
𝑗
π‘˜
The path goes through state π‘˜ at least once
𝑖
π‘˜
π‘˜
π‘˜
𝑗
π‘˜
π‘˜βˆ’1
π‘˜βˆ’1 βˆ— π‘˜βˆ’1
𝑅𝑖𝑗
= π‘…π‘–π‘˜
(π‘…π‘˜π‘˜
) π‘…π‘˜π‘—
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
17/39
From Finite Automata to Regular Expressions
Inductive step: Proof for π‘˜
π‘˜βˆ’1
Inductive hypothesis 𝑅𝑖𝑗
: path from state 𝑖 to state 𝑗 that goes
through no state higher than π‘˜ βˆ’ 1.
The path does not through state π‘˜ at all
𝑖
π‘˜
π‘˜βˆ’1
𝑅𝑖𝑗
= 𝑅𝑖𝑗
𝑗
π‘˜
The path goes through state π‘˜ at least once
𝑖
π‘˜
π‘˜
π‘˜
𝑗
π‘˜
π‘˜βˆ’1
π‘˜βˆ’1 βˆ— π‘˜βˆ’1
𝑅𝑖𝑗
= π‘…π‘–π‘˜
(π‘…π‘˜π‘˜
) π‘…π‘˜π‘—
From the previous cases:
π‘˜
π‘˜βˆ’1
π‘˜βˆ’1
π‘˜βˆ’1 βˆ— π‘˜βˆ’1
𝑅𝑖𝑗
= 𝑅𝑖𝑗
+ π‘…π‘–π‘˜
(π‘…π‘˜π‘˜
) π‘…π‘˜π‘—
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
18/39
From Finite Automata to Regular Expressions
Given that the states of 𝐴 are {1, 2, … , 𝑛} with 1 the start state,
then
𝑛
𝑛
𝐿(𝐴) = 𝐿(𝑅1𝑓
+ β‹― + 𝑅1𝑓
) with 𝑓𝑖 ∈ 𝐹 .
1
π‘š
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
19/39
From Finite Automata to Regular Expressions
Example
To convert the DFA 𝐴 to a regular expression.
1
start
1
0
2
0, 1
𝐿(𝐴) = {𝑀 ∈ {0, 1}βˆ— ∣ 𝑀 has at least one 0}.
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
20/39
From Finite Automata to Regular Expressions
Example (cont.)
1
start
1
0
2
0, 1
0
𝑅𝑖𝑗
0
𝑅𝑖𝑗
Regexp
0
𝑅11
πœ€+1
0
𝑅12
0
0
𝑅21
βˆ…
0
𝑅22
πœ€+0+1
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
21/39
From Finite Automata to Regular Expressions
Example (cont.)
1
0
0
0 βˆ— 0
𝑅𝑖𝑗
= 𝑅𝑖𝑗
+ 𝑅𝑖1
(𝑅11
) 𝑅1𝑗
0
𝑅𝑖𝑗
Regexp
1
𝑅𝑖𝑗
Regexp
0
𝑅11
πœ€+1
1
𝑅11
(πœ€ + 1) + (πœ€ + 1)(πœ€ + 1)βˆ— (πœ€ + 1)
0
𝑅12
0
1
𝑅12
0 + (πœ€ + 1)(πœ€ + 1)βˆ— 0
0
𝑅21
βˆ…
1
𝑅21
βˆ… + βˆ…(πœ€ + 1)βˆ— (πœ€ + 1)
0
𝑅22
πœ€+0+1
1
𝑅22
πœ€ + 0 + 1 + βˆ…(πœ€ + 1)βˆ— 0
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
22/39
From Finite Automata to Regular Expressions
Example (cont.)
Some simplifications for regular expressions
Let 𝐿 and 𝑀 be regular expression variables.
(πœ€ + 𝐿)βˆ— = πΏβˆ—
(πœ€ + 𝐿)πΏβˆ— = πΏβˆ—
𝐿 + 𝑀 βˆ—πΏ = 𝑀 βˆ—πΏ
πΏβˆ… = βˆ…πΏ = βˆ…
(βˆ… is the annihilator for concatenation)
𝐿 + βˆ… = βˆ… + 𝐿 = 𝐿 (βˆ… is the identity for union)
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
23/39
From Finite Automata to Regular Expressions
Example (cont.)
1
0
0
0 βˆ— 0
𝑅𝑖𝑗
= 𝑅𝑖𝑗
+ 𝑅𝑖1
(𝑅11
) 𝑅1𝑗
1
𝑅𝑖𝑗
Regexp
Simplified
1
𝑅11
(πœ€ + 1) + (πœ€ + 1)(πœ€ + 1)βˆ— (πœ€ + 1)
1βˆ—
1
𝑅12
0 + (πœ€ + 1)(πœ€ + 1)βˆ— 0
1βˆ— 0
1
𝑅21
βˆ… + βˆ…(πœ€ + 1)βˆ— (πœ€ + 1)
βˆ…
1
𝑅22
πœ€ + 0 + 1 + βˆ…(πœ€ + 1)βˆ— 0
πœ€+0+1
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
24/39
From Finite Automata to Regular Expressions
Example (cont.)
2
1
1
1 βˆ— 1
𝑅𝑖𝑗
= 𝑅𝑖𝑗
+ 𝑅𝑖2
(𝑅22
) 𝑅2𝑗
1
𝑅𝑖𝑗
Regexp
2
𝑅𝑖𝑗
1
𝑅11
1
𝑅12
1
𝑅21
1
𝑅22
1βˆ—
2
𝑅11
2
𝑅12
2
𝑅21
2
𝑅22
1βˆ— 0
βˆ…
πœ€+0+1
Regexp
1βˆ—
+
1βˆ— 0(πœ€
+ 0 + 1)βˆ— βˆ…
1βˆ— 0 + 1βˆ— 0(πœ€ + 0 + 1)βˆ— (πœ€ + 0 + 1)
βˆ… + (πœ€ + 0 + 1)(πœ€ + 0 + 1)βˆ— βˆ…
πœ€ + 0 + 1 + (πœ€ + 0 + 1)(πœ€ + 0 + 1)βˆ— (πœ€ + 0 + 1)
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
25/39
From Finite Automata to Regular Expressions
Example (cont.)
2
1
1
1 βˆ— 1
𝑅𝑖𝑗
= 𝑅𝑖𝑗
+ 𝑅𝑖2
(𝑅22
) 𝑅2𝑗
2
𝑅𝑖𝑗
2
𝑅11
2
𝑅12
2
𝑅21
2
𝑅22
Regexp
1βˆ—
+
1βˆ— 0(πœ€
+0+
Simplified
1)βˆ— βˆ…
1βˆ—
1βˆ— 0 + 1βˆ— 0(πœ€ + 0 + 1)βˆ— (πœ€ + 0 + 1)
1βˆ— 0(0 + 1)βˆ—
βˆ… + (πœ€ + 0 + 1)(πœ€ + 0 + 1)βˆ— βˆ…
βˆ…
πœ€ + 0 + 1 + (πœ€ + 0 + 1)(πœ€ + 0 + 1)βˆ— (πœ€ + 0 + 1)
(0 + 1)βˆ—
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
26/39
From Finite Automata to Regular Expressions
Example (cont.)
The regular expression equivalent to the automaton 𝐴 is
2
𝑅12
= 1βˆ— 0(0 + 1)βˆ— .
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
27/39
From Regular Expressions to Finite Automata
Theorem (Hopcroft, Motwani and Ullman [2007], Theorem 3.7)
Every language defined by a regular expression is also defined by a
finite automaton.
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
28/39
From Regular Expressions to Finite Automata
Theorem (Hopcroft, Motwani and Ullman [2007], Theorem 3.7)
Every language defined by a regular expression is also defined by a
finite automaton.
Inductive proof
For each regular expression 𝑅 we construct an πœ€-NFA 𝐸 such that
𝐿(𝑅) = 𝐿(𝐸) with:
1. exactly one accepting state,
2. no arcs into the initial state and
3. no arcs out of the accepting state.
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
29/39
struct and -NFA A, s.t. L(A) = L(R).
From Regular
Expressions to Finite Automata
Proof: By structural induction:
Basis step: Automata for πœ€ (a), βˆ… (b) and a (c).βˆ—
Basis: Automata for , βˆ…, and a.
Ξ΅
(a)
(b)
a
(c)
73
βˆ—
Hopcroft, Motwani and Ullman [2007, Fig. 3.16].
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
30/39
From Regular Expressions to Finite Automata
3.2.
FINITE
AUTOMATA
AND
Inductive
step:
Automaton
for REGULAR
𝑅 + 𝑆.βˆ— EXPRESSIONS
Ξ΅
Ξ΅
R
S
105
Ξ΅
Ξ΅
(a)
R
Ξ΅
S
(b)
βˆ—
Hopcroft, Motwani and Ullman [2007, Fig.Ξ΅ 3.17].
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
31/39
Ξ΅
Ξ΅
From Regular Expressions to Finite Automata
S
Inductive step: Automaton for 𝑅𝑆.(βˆ—a)
Ξ΅
R
S
(b)
Ξ΅
Ξ΅
R
Ξ΅
Ξ΅
(c)
Figure
3.17: The inductive step in the regular-expression-to--NFA construction
βˆ—
Hopcroft, Motwani and Ullman [2007, Fig. 3.17].
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
32/39
Ξ΅
R
S
From Regular Expressions to Finite Automata
(b)
Inductive step: Automaton for π‘…βˆ— .βˆ—
Ξ΅
Ξ΅
R
Ξ΅
Ξ΅
(c)
Figure 3.17: The inductive step in the regular-expression-to--NFA construction
automaton of Fig. 3.17(c). That automaton allows us to go either:
(a) Directly from the start state to the accepting state along a path
labeled . That path lets us accept , which is in L(R ) no matter
what
expression
is. [2007, Fig. 3.17].
βˆ—
Hopcroft, Motwani
and R
Ullman
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
33/39
From Regular Expressions to Finite Automata
Example
Convert the regular expression 𝑅 = (0 + 1)βˆ— 1(0 + 1) to an πœ€-NFA 𝐸
such 𝐿(𝑅) = 𝐿(𝐸).
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
34/39
From Regular Expressions to Finite Automata
Example
Convert the regular expression 𝑅 = (0 + 1)βˆ— 1(0 + 1) to an πœ€-NFA 𝐸
such 𝐿(𝑅) = 𝐿(𝐸).
1. πœ€-NFA for 0 + 1:
πœ€
0
πœ€
start
πœ€
1
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
πœ€
35/39
From Regular Expressions to Finite Automata
Example (cont.)
2. πœ€-NFA for (0 + 1)βˆ— :
start
πœ€
πœ€
πœ€
πœ€
0
1
πœ€
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
πœ€
πœ€
πœ€
36/39
From Regular Expressions to Finite Automata
Example (cont.)
3. πœ€-NFA for (0 + 1)βˆ— 1:
start
πœ€
πœ€
πœ€
πœ€
0
1
πœ€
πœ€
πœ€
πœ€
1
πœ€
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
37/39
From Regular Expressions to Finite Automata
Example (cont.)
4. πœ€-NFA for (0 + 1)βˆ— 1(0 + 1):
start
πœ€
πœ€
πœ€
πœ€
0
1
πœ€
πœ€
πœ€
πœ€
πœ€
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
1
πœ€
πœ€
πœ€
0
1
πœ€
πœ€
38/39
References
Hopcroft, J. E., Motwani, R. and Ullman, J. D. (2007). Introduction to
Automata theory, Languages, and Computation. 3rd ed. Pearson
Education.
Automata and Formal Languages - CM0081. Finite Automata and Regular Expressions
39/39