9/10/2019
Sofya Raskhodnikova
Intro to Theory of Computation
LECTURE 4Last time:
• Equivalence of NFAs and DFAs
• Closure properties of regular
languages
Today:
• Finish closure properties
Equivalence of NFAs, DFAs
and regular expressions
Sofya Raskhodnikova; based on slides by Nick Hopper L4.1
Operations on languages
9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper
Union: A B = { w | w A or w B }
Intersection: A B = { w | w A and w B }
Complement: A = { w | w A }
Reverse: AR = { w1 …wk | wk …w1 A }
Concatenation: A B = { vw | v A and w B }
Star: A* = { w1 …wk | k ≥ 0 and each wi A }
L3.2
Closure properties of the class of
regular languages
9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper
THEOREM. The class of regular languages
is closed under all 6 operations.
If A and B are regular, applying any of these
operation yields a regular language.
L3.3
Star operation
9/10/2019
Sofya Raskhodnikova; based on slides by Nick Hopper
Star: A* = { w1 …wk | k ≥ 0 and each wi A }
Theorem. If A is regular, A* is also regular.
L(M)=Aε
ε
ε
L3.4
The class of regular languages is
closed under
9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper
Union: A B = { w | w A or w B }
Intersection: A B = { w | w A and w B }
Complement: A = { w | w A }
Reverse: AR = { w1 …wk | wk …w1 A }
Regular operations
Union: A B = { w | w A or w B }
Concatenation: A B = { vw | v A and w B }
Star: A* = { w1 …wk | k ≥ 0 and each wi A }
Other operations
L3.5
Regular expressions
9/12/2019 Sofya Raskhodnikova; based on slides by Nick Hopper L4.6
• In a regular expression, we can use
– Constants: ε , ∅ , a set Σ, members of Σ.
– Regular operations: *, , ∪
• Examples:
– 0*1*
– (0∪1)*
– 0*1*(ε ∪ 0)
• L(R) = the language regular expression R describes
= the set of all strings over the alphabet Σ={0,1}
= { w | w has a run of 0s followed by a run of 1s}
* ∪
Precedence
(𝑹𝟏(𝑹𝟐∗ )) ∪ (𝑹𝟑𝑹𝟒)
EXAMPLE
𝑹𝟏𝑹𝟐∗ ∪ 𝑹𝟑𝑹𝟒 =
9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper L4.7
{ w | w has exactly one character 1, any # of 0s}
0*10*
1)
2)
3)
Regular expressions: examples
{ w | w has length ≥ 3 and its 3rd symbol is 0 }
(0 ∪ 1)(0 ∪ 1) 0 (0 ∪ 1)*
{ w | every odd position of w is a 1 }
(1(0 ∪ 1))* (ε ∪ 1)
9/12/2019 Sofya Raskhodnikova; based on slides by Nick Hopper L4.8
Regular expressions to NFAs
9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper
Theorem. Every regular expression has an equivalent NFA.
Proof: Induction on the length of regular expression R.
Base case: length 1
Inductive step: follows from closure of the class of
regular languages under the regular operations.
L4.9
R =
R = ε
R =
Exercise
What should the induction hypothesis be?
A. Suppose some regular expression of length 𝑘 can be
converted an NFA, for some 𝑘 ∈ ℕ.
B. Suppose each regular expression of length 𝑘 can be
converted an NFA, for some 𝑘 ∈ ℕ.
C. Suppose each regular expression of length at most k
can be converted an NFA, for some 𝑘 ∈ ℕ.
D. None of the above.
Regular expression to NFA
L4.119/12/2019 Sofya Raskhodnikova; based on slides by Nick Hopper
Transform (1(0 ∪ 1))* to an NFA
1ε 1,0
ε
NFAs to regular expressions
9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper
Theorem. Every NFA has an equivalent regular expression.
Proof idea:
Transform NFA to a regular expression by removing states
and relabeling the arrows with regular expressions.
L4.12
0
1
001*0
Exercise
What is the regular expression that generates all
strings that take this machine from 𝒒𝒔 to 𝒒𝒂?
A. 𝑎𝑏∗𝑐 ∪ 𝑑
B. 𝑎𝑏∗𝑐 ∩ 𝑑
C. 𝑎𝑏𝑐 ∪ 𝑑
D. 𝑎𝑏∗𝑐𝑑 ∗
E. None of the above.
q
𝒃
𝒄𝒂qs qa
𝒅
Generalized NFAs
• Each transition is labeled with a regular
expression
• Unique and distinct start and accept states
• No transitions to the start state
• No transitions from the accept state
L4.149/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper
R(qs,q) = a*b
R(qa,q) = Ø
R(q,qs) = Ø
A GNFA accepts w if ∃q0,q1,…,qk, w1,…,wk:
wi is generated by R(qi-1,qi)
w = w1w2…wk
q0=qs, qk=qa
q
a ∪ b
aa*bqs qa
Generalized NFAs
NFA to GNFA
L4.169/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper
NFAε
ε
ε
ε
Add a new start state with no incoming arrows.
Make a unique accept state with no outgoing arrows.
GNF to regular expression
L4.179/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper
NFAε
ε
ε
While machine has more than 2 states:
Pick an internal state, rip it out and relabel
the arrows with regular expressions to
account for the missing state.
q1
b
a
εq2
a,b
εa*b(a*b)(ab)*q0 q3
R(q0,q3) = (a*b)(ab)*
q3
q2
b
a
b
q1
b
a
a
ε
ε
ε
bb
9/10/2019 Sofya Raskhodnikova; based on
slides by Nick Hopper
q2
b
a
b
q1
a
a
ε
ε
ε
bb
a ba
b
9/10/2019 Sofya Raskhodnikova; based on
slides by Nick Hopper
bb (a ba)b*a
a ba
q2
b
q1
a
ε
ε
bb
bb (a ba)b*
= R(q1,q1)
9/10/2019 Sofya Raskhodnikova; based on
slides by Nick Hopper
bb (a ba)b*a
q1
ε
b (a ba)b*
= R(q1,q1)
9/10/2019 Sofya Raskhodnikova; based on
slides by Nick Hopper
= R(q1,qa)qs
qa
DFA NFA
Regular
Language
Regular
Expression
definition
Conversion procedures
9/10/2019 Sofya Raskhodnikova; based on slides by Nick Hopper 4.27