Professional Documents
Culture Documents
tcs1 Slides 50 60
tcs1 Slides 50 60
Version 2024.1
Overview
Reg
DFA NFA
Overview
Reg
DFA NFA
RE
Overview
Reg
DFA NFA
RE
Chapter 5. Regular operations
Operations on regular languages
Example
For A “ tab, cdu and B “ t0, 1, 000u, we have
‚ A Y B “ tab, cd, 0, 1, 000u
Operations on regular languages
Example
For A “ tab, cdu and B “ t0, 1, 000u, we have
‚ A Y B “ tab, cd, 0, 1, 000u
‚ A ˝ B “ tab0, ab1, ab000, cd0, cd1, cd000u
Operations on regular languages
@x,y PS x dy PS
Example
‚ The set of natural numbers N is closed under addition and
multiplication, but not under subtraction and division.
‚ 2´3RN
‚ 1{2 R N
‚ The set of integers Z is closed under addition, multiplication
and subtraction, but not under division.
Closure properties of the set of regular languages
Theorem
The class of regular languages is closed under union, concatenation
and star.
@X ,Y PReg X Y Y , X ˝ Y , X ˚ P Reg
Closure properties of the set of regular languages
Theorem
The class of regular languages is closed under union, concatenation
and star.
@X ,Y PReg X Y Y , X ˝ Y , X ˚ P Reg
Theorem
The class of regular languages is closed under union, concatenation
and star.
@X ,Y PReg X Y Y , X ˝ Y , X ˚ P Reg
Phrased differently:
If M and M 1 are finite automata, which recognize languages LpMq
and LpM 1 q, then there exists a finite automaton N such that
LpNq “ LpMq Y LpM 1 q.
Proof sketch (Union)
Proof sketch (Union)
ε
Example (Union)
L “ tabc, acbu L Y L1
c c
b b
a a
c ε c
b b
ε
a a
b b
L1 “ tpabqn | n ě 0u
Formal proof construction (Union)
ε
ε
ε
Example (Concatenation)
c
b a
a
c b
b
c
b ε a
a
ε
c b
b
L ˝ L1
Formal proof construction (Concatenation)
ε ε
ε
ε
Example (Star)
L “ tabc, acbu L˚
ε
c c
b b
a ε a
c c
b b
ε
Formal proof construction (Star)
Let N1 “ pQ1 , Σ1 , δ1 , q1 , F1 q.
Construct N “ pQ, Σ, δ, q0 , F q such that LpNq “ LpN1 q˚ .
1 Q “ tq0 u Y Q1 (where q0 R Q1 );
2 Σ “ Σ1 ;
3 δ is defined for q P Q and a P Σε by
$
’
’ δ1 pq, aq q P Q1 and q R F1
& δ1 pq, aq q P F1 and a ‰ ε
’
’
δpq, aq “ δ1 pq, aq Y tq1 u q P F1 and a “ ε
tq u q “ q0 and a “ ε
’
% 1
’
’
’
H q “ q0 and a ‰ ε
L1 X L2 “ L1 Y L2
.
Chapter 6. Regular expressions
Examples of regular expressions
Notation:
‚ We often don’t write ˝, so abc means a ˝ b ˝ c.
‚ We often omit parentheses.
‚ Star binds stronger than concatenation, which binds stronger
than union, so a Y b ˝ c ˚ means a Y pb ˝ pc ˚ qq.
‚ If Σ “ ta1 , a2 , . . . , an u, then we write Σ for the regular
expression a1 Y a2 Y . . . Y an .
Language generated by regular expression
Definition
The language generated by a regular expression is inductively
defined as follows:
Lpaq = tau
Lpεq = tεu
LpHq = tu
LpR1 Y R2 q = LpR1 q Y LpR2 q
LpR1 ˝ R2 q = LpR1 q ˝ LpR2 q
LpR1˚ q = LpR1 q˚
Applying the inductive definition
Solution idea: Every 0 (except for the first one) has to be preceded
by at least one 1.
Example
Determine a regular expression r such that
Solution idea: Every 0 (except for the first one) has to be preceded
by at least one 1.
This gives:
loomo 11 . . . 10 looomooon
0 on looomooon 11 . . . 10 . . . 11 . . . 10 loomoon
looomooon 11 . . . 1
optional optional
Solution idea: Every 0 (except for the first one) has to be preceded
by at least one 1.
This gives:
loomo 11 . . . 10 looomooon
0 on looomooon 11 . . . 10 . . . 11 . . . 10 loomoon
looomooon 11 . . . 1
optional optional
This is:
p0 Y εq p11˚ 0q˚ 1˚
Example: Vim editor uses regular expressions
Theorem
A language is regular if and only if some regular expression
describes it.
Regular expressions define regular languages
Theorem
A language is regular if and only if some regular expression
describes it.
R “ R1 Y R2 . ε
R “ R1 ˝ R2 . ε
ε
5 Use the closure construction ε
for Concatenation.
R “ R1˚ .
ε ε
6 Use the closure construction for ε
ε
Star.
Example RE to NFA
Convert pab Y aq˚ into an equivalent NFA.
Example RE to NFA
Convert pab Y aq˚ into an equivalent NFA.
‚ Subexpression a
a
Example RE to NFA
Convert pab Y aq˚ into an equivalent NFA.
‚ Subexpression a
a
‚ Subexpression b
b
Example RE to NFA
Convert pab Y aq˚ into an equivalent NFA.
‚ Subexpression a
a
‚ Subexpression b
b
‚ Subexpression ab
a ε b
Example RE to NFA
Convert pab Y aq˚ into an equivalent NFA.
‚ Subexpression a
a
‚ Subexpression b
b
‚ Subexpression ab
a ε b
‚ Subexpression ab Y a
a ε b
ε
ε
a
Example RE to NFA
Convert pab Y aq˚ into an equivalent NFA.
‚ Subexpression ab Y a
a ε b
ε
ε
a
Example RE to NFA
Convert pab Y aq˚ into an equivalent NFA.
‚ Subexpression ab Y a
a ε b
ε
ε
a
a ε b
ε
ε
ε
a
ε
Example RE to NFA
‚ Expression pab Y aq˚
ε
a ε b
ε
ε
ε
a
ε
‚ Simplification
ε
b
a
ε a
ε
Example RE to NFA
‚ Simplification
ε
b
a
ε a
ε
‚ Further simplification
a
b
a
Regular expressions define regular languages
Theorem
A language is regular if and only if some regular expression
describes it.
Intuition:
a c
b transforms into a˚ bc ˚
Step 2: From DFA to RE
Intuition:
a c
b transforms into a˚ bc ˚
b
a c transforms into ab ˚ c
Generalised NFA
a
s ε
1
b aYb
f ε 2
From DFA to GNFA (Example 1)
a
s ε
1
b aYb
f ε 2
Next, we remove node 2.
a
s ε
1
bpa Y bq˚
f
Next, we remove node 1.
s
a˚ bpa Y bq˚
f
From DFA to GNFA: informal algorithm
1 0, 1
0
0
A B C
1
From DFA to GNFA (Example 2)
Consider a DFA for the following language:
1 0, 1
0
0
A B C
1
Add initial and final state.
1 0, 1
0
ε 0
S A B C
1
ε ε
F
We can remove trap state C .
From DFA to GNFA (Example 2)
1
0
ε
S A B
1
ε ε
F
Remove state A.
11˚ 0
ε1˚ 0
S B
ε1˚ ε ε Y 11˚ ε
F
From DFA to GNFA (Example 2)
11˚ 0
ε1˚ 0
S B
ε1˚ ε ε Y 11˚ ε
F
Remove state B.
F
From DFA to GNFA (Example 2)
11˚ 0
ε1˚ 0
S B
ε1˚ ε ε Y 11˚ ε
F
Remove state B.
F
After simplification, this gives the regular expression:
1˚ Y 1˚ 0p11˚ 0q˚ pε Y 11˚ q
From DFA to GNFA (Example 2)
11˚ 0
ε1˚ 0
S B
ε1˚ ε ε Y 11˚ ε
F
Remove state B.
F
After simplification, this gives the regular expression:
1˚ Y 1˚ 0p11˚ 0q˚ pε Y 11˚ q or even 1˚ Y 1˚ 0p11˚ 0q˚ 1˚ .
From DFA to GNFA (Example 3)
b
a
1 2
a
b a
b
3
Add initial and final state.
b
a
s ε ε
1 2 f
a
b a
ε
b
3
From DFA to GNFA (Example 3)
b
a
s ε ε
1 2 f
a
b a
ε
b
3
Remove state 1.
aa Y b
s a ε
2 f
b ba Y a ab
ε
3
bb
From DFA to GNFA (Example 3)
aa Y b
s a ε
2 f
b ba Y a ab
ε
3
bb
Remove state 2.
apaa Y bq˚
s f
Remove state 3.
` ˘
apaa Y bq˚ ab Y b ˝
` ˘˚
pba Y aqpaa Y bq˚ ab Y bb ˝
` ˘
pba Y aqpaa Y bq˚ Y ε
Y apaa Y bq˚
s f
Regular expressions define regular languages
Theorem
A language is regular if and only if some regular expression
describes it.
Reg
DFA NFA
RE
Properties of regular expressions
We reduced
ε1˚ ε Y ε1˚ 0p11˚ 0q˚ pε Y 11˚ εq
to the expression
1˚ Y 1˚ 0p11˚ 0q˚ 1˚
x Y py Y zq “ px Y y q Y z [associativity of Y ]
xpyzq “ pxy qz [associativity of ˝ ]
x Yy “y Yx [commutativity of Y ]
x Yx “x [idempotence of Y ]
x YH“x [H is a zero]
xH “ Hx “ H [H is a zero]
xε “ εx “ x [ε is a unit]
xpy Y zq “ xy Y xz [left-distributivity of ˝ over Y ]
px Y y qz “ xz Y yz [right-distributivity of ˝ over Y ]
Properties of regular expressions
x Y py Y zq “ px Y y q Y z [associativity of Y ]
xpyzq “ pxy qz [associativity of ˝ ]
x Yy “y Yx [commutativity of Y ]
x Yx “x [idempotence of Y ]
x YH“x [H is a zero]
xH “ Hx “ H [H is a zero]
xε “ εx “ x [ε is a unit]
xpy Y zq “ xy Y xz [left-distributivity of ˝ over Y ]
px Y y qz “ xz Y yz [right-distributivity of ˝ over Y ]
ε Y xx ˚ “ x ˚ I
ε Y x ˚x “ x ˚ II
pxy q˚ x “ xpyxq˚ III
px ˚ y q˚ x ˚ “ px Y y q˚ IV
x ˚ pyx ˚ q˚ “ px Y y q˚ V
pε Y xq˚ “ x ˚ VI
xx ˚ “ x ˚ x VII
px ˚ q˚ “ x ˚ VIII
H˚ “ ε IX
ε˚ “ ε X
px ˚ y ˚ q˚ “ px Y y q˚ XI
px ˚ Y y q˚ “ px Y y q˚ XII
Properties of regular expressions
Star
ε Y xx ˚ “ x ˚ I
ε Y x ˚x “ x ˚ II
pxy q˚ x “ xpyxq˚ III
px ˚ y q˚ x ˚ “ px Y y q˚ IV
x ˚ pyx ˚ q˚ “ px Y y q˚ V
pε Y xq˚ “ x ˚ VI
xx ˚ “ x ˚ x VII
px ˚ q˚ “ x ˚ VIII
H˚ “ ε IX
ε˚ “ ε X
px ˚ y ˚ q˚ “ px Y y q˚ XI
px ˚ Y y q˚ “ px Y y q˚ XII
These are just some properties; pY, ˝,˚ q forms a Kleene algebra.
Example 1
Simplify
ε1˚ ε Y ε1˚ 0p11˚ 0q˚ pε Y 11˚ εq
Example 1
Simplify
ε1˚ ε Y ε1˚ 0p11˚ 0q˚ pε Y 11˚ εq
Simplify p0˚ 1˚ Y 1˚ 0˚ q˚
Example 2
Simplify p0˚ 1˚ Y 1˚ 0˚ q˚
px ˚ q˚ “ x ˚ VIII
px ˚ y ˚ q˚ “ px Y y q˚ XI
px ˚ Y y q˚ “ px Y y q˚ XII
Example 3
The language of binary sequences that do not end in 00:
All sequences with length ă 2 plus all sequences that end in 01,
10, or 11
ε Y 0 Y 1 Y p0 Y 1q˚ p01 Y 10 Y 11q
Example 3
The language of binary sequences that do not end in 00:
All sequences with length ă 2 plus all sequences that end in 01,
10, or 11
ε Y 0 Y 1 Y p0 Y 1q˚ p01 Y 10 Y 11q
Lppx Y y q˚ q ‰ Lpx ˚ Y y ˚ q
Example 5
Distributivity of star over union.
Prove that px Y y q˚ “ x ˚ Y y ˚ is not a generally valid property.
Lppx Y y q˚ q ‰ Lpx ˚ Y y ˚ q
Lppx Y y q˚ q ‰ Lpx ˚ Y y ˚ q
Lppx Y y q˚ q ‰ Lpx ˚ Y y ˚ q