Regular Languages, Part III
Theory of Computation
Asst. Prof. Dr. Osman Gökalp
Dr. Ersin Çine
Department of Computer Engineering
İzmir Institute of Technology
2024.10.28 (Week 5)
Recap: Symbols, alphabets, strings, languages
An alphabet is any nonempty finite set.
A symbol is a member of an alphabet.
For example, Σ = {0, 1} is an alphabet, where 0 and 1 are symbols.
A string is a finite sequence of symbols.
For example, w = 0100 is a string over Σ.
Concatenation of strings x and y is obtained by appending y to the end of x.
Concatenation of x = 01 and y = 110 is xy = 01110.
Repetition of string x, written x k is the concatenation of it with itself k times.
If x = 01, then x 3 = 010101.
A language is a set of strings.
For example the set of even numbers {. . . , −4, −2, 0, 2, 4, . . . } is a language
over the alphabet {−, 0, 1, 2, . . . , 9}.
Recap: Deterministic finite automata (DFAs)
Definition
A DFA is a 5-tuple (Q, Σ, δ, q0 , F ) where
• Q is a finite set called the states,
• Σ is a finite set called the alphabet,
• δ : Q × Σ → Q is the transition function.1
• q0 ∈ Q is the start state, and
• F ⊆ Q is the set of accept states (=final states).
1
This function determines the next state (an element of Q)
given the current state (an element of Q) and a symbol to consume (an element of Σ).
Recap: DFA computation
Let M = (Q, Σ, δ, q0 , F ) be a DFA, and
let w = w1 w2 . . . wn be a string where each wi is a member of the alphabet Σ.
M accepts w iff sequence of states r0 , r1 , . . . , rn exists in Q with three conditions:
1. r0 = q0 ,
2. ri+1 = δ(ri , wi+1 ) for i = 0, . . . , n − 1, and
3. rn ∈ F .
Recap: Nondeterministic finite automata (NFAs)
Definition
An NFA is a 5-tuple (Q, Σ, δ, q0 , F ) where
• Q is a finite set called the states,
• Σ is a finite set called the alphabet,
• δ : Q × Σϵ → P(Q) is the transition function.2
• q0 ∈ Q is the start state, and
• F ⊆ Q is the set of accept states (=final states).
2
For a DFA, this was Q × Σ → Q. We define Σϵ as Σ ∪ {ϵ}. Also recall that P(Q) is the power set of Q.
Recap: NFA computation
Let N = (Q, Σ, δ, q0 , F ) be an NFA and w is a string over the alphabet Σ.
N accepts w iff
we can write w as w = y1 y2 . . . yn where each yi is a member of Σϵ , and
a sequence of states r0 , r1 , . . . , rn exists in Q
with three conditions:
1. r0 = q0 ,
2. ri+1 ∈ δ(ri , yi+1 ), for i = 0, . . . , n − 1, and
3. rn ∈ F .
Recap: Equivalence of DFAs and NFAs
DFAs and NFAs are equivalent:
• By definition, every DFA is also an NFA.
• Any NFA can be converted into an equivalent DFA:
Let N = (Q, Σ, δ, q0 , F ) be an NFA.
We can construct an equivalent DFA M = (Q ′ , Σ, δ ′ , q0′ , F ′ ):
• Q ′ = P(Q)
• δ ′ (R, a) = {q ∈ Q | q ∈ E (δ(r , a)) for some r ∈ R}
• q0′ = E ({q0 })
• F ′ = {R ∈ Q ′ | R contains an accept state of N}
where E (R) = {q | q can be reached from R by traveling along 0 or more ϵ arrows}.
Recap: Regular languages and regular operations
Definition
A language is called a regular language iff some DFA (or NFA) accepts it.
In order to prove that a language is regular, we can construct a DFA (or NFA) that accepts it.
Definition
Let A and B be languages. We define the regular operations as follows:
• Union: A ∪ B = {x | x ∈ A or x ∈ B}
• Concatenation: A ◦ B = AB = {xy | x ∈ A and y ∈ B}
• Star: A∗ = {x1 x2 . . . xk | k ≥ 0 and each xi ∈ A}
Regular languages are closed under regular operations:
e.g., (A ∪ B ◦ C )∗ is regular if A, B, C are regular.
Questions we will answer today
• What is a regular expression?
• How to convert regular expressions to equivalent NFAs?
• What is a GNFA?
• How to convert NFAs to equivalent regular expressions?
Regular expressions
Definition
Say that R is a regular expression if R is
1. a for some a in the alphabet Σ,
2. ϵ,
3. ∅,
4. R1 ∪ R2 , where R1 and R2 are regular expressions,
5. R1 ◦ R2 , where R1 and R2 are regular expressions, or
6. R1∗ , where R1 is a regular expression.
This is an inductive definition: e.g., (R1 ∪ R2 )∗ is a regular expression.
The regular expressions a and ϵ represent the languages {a} and {ϵ}.
We usually write R1 R2 instead of R1 ◦ R2 .
Precedence order: star, then concatenation, then union.
For convenience, we let R + be shorthand for RR ∗ . So R + ∪ ϵ = R ∗ .
Examples
We assume that the alphabet Σ is {0, 1}.
1. 0∗ 10∗ = {w ∈ Σ∗ | w contains a single 1}
2. Σ∗ 1Σ∗ = {w ∈ Σ∗ | w has at least one 1}
3. Σ∗ 001Σ∗ = {w ∈ Σ∗ | w contains the string 001 as a substring}
4. 1∗ (01+ )∗ = {w ∈ Σ∗ | every 0 in w is followed by at least one 1}
5. (ΣΣ)∗ = {w ∈ Σ∗ | w is a string of even length}
6. (ΣΣΣ)∗ = {w ∈ Σ∗ | the length of w is a multiple of 3}
7. 01 ∪ 10 = {01, 10}
8. 0Σ∗ 0 ∪ 1Σ∗ 1 ∪ 0 ∪ 1 = {w ∈ Σ∗ | w starts and ends with the same symbol}
9. (0 ∪ ϵ)1∗ = 01∗ ∪ 1∗ = {w ∈ Σ∗ | w contains no 0s except an optional leading 0}
10. (0 ∪ ϵ)(1 ∪ ϵ) = {ϵ, 0, 1, 01}
11. 1∗ ∅ = {}
12. ∅∗ = {ϵ}
Write a regular expression for the desired language
Describe numerical constants using a regular expression.
Example numerical constants include 72, 3.14159, +7., and −.01
Hint:
You can use D = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}.
For example −D + describes the negative integers {−0, −1, . . . , −9, −00, −01, . . . }.
The answer from the book:
(+ ∪ − ∪ ϵ)(D + ∪ D + .D ∗ ∪ D ∗ .D + )
Note:
D is not the alphabet here. It is a set we arbitrarily define.
The alphabet is Σ = {+, −, ., 0, 1, . . . , 9}.
Identities
Let R be a regular expression.
• R ∪∅=R
• R ∪ ϵ may not equal R
• ϵ∪∅=ϵ
• R ◦ϵ=R
• ϵ◦R =R
• R ◦∅=∅
• ∅◦R =∅
• ϵ◦∅=∅
• ∅◦ϵ=∅
R1 ∪ R2 is the same as R2 ∪ R1
but R1 ◦ R2 is not the same as R2 ◦ R1 in general.
Questions we will answer today
• What is a regular expression? ✓
• How to convert regular expressions to equivalent NFAs?
• What is a GNFA?
• How to convert NFAs to equivalent regular expressions?
How to convert a regular expression to an NFA?
All regular expressions can be converted to an equivalent NFA.
Consider six cases in the formal definition of regular expressions:
1. R = a for some a ∈ Σ
4. R = R1 ∪ R2
2. R = ϵ
5. R = R1 ◦ R2
3. R = ∅
6. R = R1∗
We showed the last three cases when we discussed closure under the regular operations.
Example: (Reg. ex. (ab ∪ a)∗ → NFA)
(This NFA can be minimized: Can you find an equivalent NFA with only two states?)
Questions we will answer today
• What is a regular expression? ✓
• How to convert regular expressions to equivalent NFAs? ✓
• What is a GNFA?
• How to convert NFAs to equivalent regular expressions?
Generalized nondeterministic finite automata (GNFAs)
GNFAs are NFAs wherein the transition arrows may have any regular expressions as labels,
instead of only members of the alphabet or ϵ.
Our GNFAs are in a special form:
• Start state: arrows going to all
other states, but none coming in.
• A single accept state, different
from the start; arrows coming in
from all other states but none
going out.
• Each non-start and non-accept
state has one arrow pointing to
every other non-start and
non-accept state, including itself.
Questions we will answer today
• What is a regular expression? ✓
• How to convert regular expressions to equivalent NFAs? ✓
• What is a GNFA? ✓
• How to convert NFAs to equivalent regular expressions?
How to convert an NFA to a regular expression?
1. Convert the NFA to a GNFA in the special form.
2. Convert the GNFA in the special form to a regular expression.
Step 1: Convert the NFA to a GNFA in the special form
1. Add a new start state with an ϵ arrow to the old start state.
2. Add a new accept state state with ϵ arrows from the old accept states.
(Original accept states are not accept states anymore.)
3. If any arrows have multiple labels
(or if there are multiple arrows going between the same two states in the same direction),
replace each with a single arrow whose label is the union of the previous labels.
4. For each missing arrow, add an arrow labeled ∅.
(See the “special form” of GNFAs in the previous slides for the list of all required arrows.)
(A transition labeled with ∅ can never be used.)
Example (NFA → GNFA)
(To avoid cluttering up the figure, we did not draw the arrows labeled ∅.)
Step 2: Convert a GNFA in the special form to a reg. ex.
The GNFA has k states. Convert it to a GNFA with k − 1 states. Repeat it until k = 2.
k = 2 means that we have a start state and a accept state and no other states.
The label of the arrow between these two states is the regular expression.
How to convert a GNFA with k states to a GNFA with k − 1 states?
1. Choose a state qrip excluding the start and accept states and remove it.
2. Repair the machine by altering the regular expressions that label the remaining arrows.
(The new labels compensate for the absence of qrip by adding back the lost computations.
The new label going from a state qi to a state qj is a regular expression that describes all
strings that would take the machine from qi to qj either directly or via qrip .)
How to “repair the machine”?
(Perform this for every (qi , qj ) such that there is an arrow from qi to qrip and an arrow from
qrip to qj . Note that qi and qj are not necessarily distinct, i.e., it is possible that qi = qj .)
Example (4 states → 3 states → 2 states)
(To avoid cluttering up the figure, we did not draw the arrows labeled ∅.)
The regular expression is a∗ b(a ∪ b)∗ .
Example: The big picture (NFA → reg .ex.)
(To avoid cluttering up the figure, we did not draw the arrows labeled ∅.)
The regular expression is a∗ b(a ∪ b)∗ .
Questions we will answer today
• What is a regular expression? ✓
• How to convert regular expressions to equivalent NFAs? ✓
• What is a GNFA? ✓
• How to convert NFAs to equivalent regular expressions? ✓
Conclusion
Any regular expression can be converted to an NFA.
Any NFA can be converted to a regular expression (by first converting it to a GNFA).
Conclusion:
A language is regular iff some regular expression describes it.
(Regular expressions “describe” languages, not “accept”!)
Preparation for next week
Please complete the following tasks by next week:
• Read Section 1.3.
• Complete exercises 1.17-1.28.
• Complete the previous exercise involving regular expressions
(specifically, 1.12 and parts of 1.5 and 1.7).
• Solve problems 1.38-1.45.
Some solutions are provided at the very end of the chapter for reference.
Let us know if you have any questions or need clarification.