Vous êtes sur la page 1sur 34

CSC 3130: Automata theory and formal languages

Andrej Bogdanov
http://www.cse.cuhk.edu.hk/~andrejb/csc3130
The Chinese University of Hong Kong
NFA to DFA conversion
and regular expressions
Fall 2009
NFAs are as powerful as DFAs
Obviously, an NFA can do everything a DFA can
do
But can it do more?



Theorem
If a language L is accepted by some NFA, then
it is also accepted by some DFA.
NO!
Proof of theorem
We will show a general way to convert every NFA
into an equivalent DFA

Step 1: Simplify NFA by eliminating e-transitions

Step 2: Convert simplified NFA (without es)
We do this first
NFA to DFA conversion intuition
1 0
0, 1
q
0
q
1
q
2
NFA:
DFA:
1
q
0
q
0
or q
1
1
q
0
or q
2
1
0
0
0
NFA to DFA conversion intuition
1 0
0, 1
q
0
q
1
q
2
NFA:
DFA:
1
q
0
{q
0
, q
1
}
1
{q
0
, q
2
}
1
0
0
0
General method
NFA DFA
states
q
0
, q
1
, , q
n

q
0
}, {q
1
}, {q
0
,q
1
}, , {q
0
,,q
n
}
one for each subset of states in the
NFA
initial state
q
0
q
0
}
transitions d
d({q
i1
,,q
ik
}, a) =
d(q
i1
, a) d(q
ik
, a)
accepting
states
F Q F = {S: S contains some state in F}
Why the method works



At the end, the DFA accepts when it is in a state
that contains some accepting state of NFA

So the DFA accepts only when the NFA accepts
too
After reading n symbols, the DFA is in state
{q
i1
,,q
ik
} if and only if the NFA is in one of
the states q
i1
,,q
ik
Converting via the general method
1 0
0, 1
q
0
q
1
q
2
NFA:
DFA:
{q
0
, q
1
}
{q
0
, q
2
} {q
0
, q
1
, q
2
}
{q
1
, q
2
}
{q
0
}
{q
1
}
{q
2
}

0, 1
0
1
0
1
0
1
0 1
0
1
0
1
Converting via the general method
{q
0
, q
1
}
{q
0
, q
2
} {q
0
, q
1
, q
2
}
{q
1
, q
2
}
{q
0
}
{q
1
}
{q
2
}

0, 1
0
1
0
1
0
1
0 1
0
1
0
1
After eliminating the dead states and
transitions, we end up with the same picture
Proof of theorem
We will show a general way to convert every NFA
into an equivalent DFA

Step 1: Simplify NFA by eliminating e-transitions

Step 2: Convert simplified NFA (without es)
Eliminating e-transitions
q
0
q
1
q
2
e, 1
0
0
e
eNFA:
Transition table of corresponding NFA:
s
t
a
t
e
s

inputs
0 1
q
0
q
1
q
2
{q
1
, q
2
} {q
0
, q
1
, q
2
}
{q
0
, q
1
, q
2
}



Accepting states of NFA:
q
0
, q
1
, q
2

Eliminating e-transitions
q
0
q
1
q
2
e, 1
0
0
e
NFA:
NFA without es:
q
0
q
1
q
2
0, 1
0
0
0
0, 1
0
Eliminating e-transitions: General
method
For every state q
i
and every symbol a ,
replace every path out of q
i
like






For every accept state q
f
, make accepting all
states connected to it via es:
q
4
q
i
q
5
q
2
q
0
q
5
e e e a
q
i
q
5
a
e
e
q
i
q
5
a
q
i
a
e e
e
q
9
q
7
q
3
q
f
Regular expressions
Operations on strings
Given two strings s = a
1
a
n
and t = b
1
b
m
, we
define their concatenation st = a
1
a
n
b
1
b
m



We define s
n
as the concatenation sss n times
s = abb, t = cba st = abbcba
s = 011 s
3
= 011011011
Operations on languages
The concatenation of languages L
1
and L
2
is


Similarly, we write L
n
for LLL (n times)
The union of languages L
1
L
2
is the set of all
strings that are in L
1
or in L
2

Example: L
1
= {01, 0}, L
2
= {e, 1, 11, 111, }.
What is L
1
L
2
and L
1
L
2
?
L
1
L
2
= {st: s L
1
, t L
2
}
Operations on languages
The star (Kleene closure) of L are all strings
made up of zero or more chunks from L:


This is always infinite, and always contains e

Example: L
1
= {01, 0}, L
2
= {e, 1, 11, 111, }.
What is L
1
*
and L
2
*
?
L
*
= L
0
L
1
L
2

Constructing languages with operations
Lets fix an alphabet, say = {0, 1}
We can construct languages by starting with
simple ones, like {0}, {1} and combining them
{0}({0}{1})*
all strings that start with
0
({0}{1}*)({1}{0}*)
0(0+1)*
01*+10*
Regular expressions
A regular expression over is an expression
formed using the following rules:
The symbol is a regular expression
The symbol e is a regular expression
For every a , the symbol a is a regular expression
If R and S are regular expressions, so are R+S, RS and
R*.
A language is regular if it is represented
by a regular expression
Examples
01*
(01*)(01)
0 followed by any number of 1s
= 0(1*) = {0, 01, 011, 0111, }
= {001, 0101, 01101, 011101, }
0 followed by any number of 1s and then 01
= {0, 1}
Examples
(0+1)* = {e, 0, 1, 00, 01, 10, 11, }
any string
0+1 = {0, 1}
(0+1)*01(0+1)*
strings of length 1
any string that contatins the pattern 01
(0+1)*010 any string that ends in 010
Examples
(0+1)(0+1)
(0+1)(0+1)(0+1)
strings of length 2
strings of length 3
((0+1)(0+1))*+((0+1)(0+1)(0+1))*
((0+1)(0+1))* strings of even length
((0+1)(0+1)(0+1))* strings of length divisible by 3
all strings whose length is even or divisible by 3
= strings of length 0, 2, 3, 4, 6, 8, 9, 10, 12, ...
Examples
((0+1)(0+1)+(0+1)(0+1)(0+1))*
(0+1)(0+1)
(0+1)(0+1)(0+1)
strings of length 2
strings of length 3
(0+1)(0+1)+(0+1)(0+1)(0+1) strings of length 2 or 3
strings that can be broken in blocks,
where each block has length 2 or 3
Examples
((0+1)(0+1)+(0+1)(0+1)(0+1))*
strings that can be broken in blocks,
where each block has length 2 or 3
00110 10 011
e
1 011010110

this includes all strings except those of length 1
((0+1)(0+1)+(0+1)(0+1)(0+1))* = all strings except 0 and 1
Examples
(1+01+001)*(e+0+00)
ends in at most two 0s
there can be at most two 0s between
consecutive 1s
Guess: (1+01+001)*(e+0+00) = {x: x does not contain 000}
there are never three consecutive 0s
0110010110 0010010 00
e
Examples
Write a regular expression for
all strings with two consecutive 0s.
= {0, 1}
(0+1)*00(0+1)*
(anything) 00 (anything else)
Examples
Write a regular expression for
all strings that do not contain two consecutive 0s.
= {0, 1}
... at most one 0 in every block ending in 1
... and at most one 0 in the last block
(1 + 01)
(e + 0)
(1 + 01)*(e + 0)
0110101101010
blocks ending in 1 last block
Examples
Write a regular expression for
all strings with an even number of 0s.
= {0, 1}
even number of zeros = (two zeros)*
two zeros = 1*01*01*
(1*01*01*)*
Main theorem for regular languages
Theorem
A language is regular if and only if it is the
language of some DFA
DFA
NFA
regular
expression
regular languages
Road map
NFA
regular
expression
NFA
without e
DFA


M
2
Examples: regular expression NFA
R
1
= 0

R
2
= 0 + 1



R
3
= (0 + 1)*
q
0
q
1
0
q
0
q
1
e
e e
e
q
2
q
3
0
q
4
q
5
1
q
0
q
1

e
M
2

e
e
e
General method
regular expr NFA

q
0
e
q
0
symbol a
q
0
q
1
a
RS
q
0
q
1
e
M
R
M
S

e e
General method continued
regular expr NFA
R + S
q
0
q
1
e
M
R

M
S

e e
e
R* q
0
q
1
e
M
R

e
e
e
Road map
NFA
regular
expression
NFA
without e
DFA

Vous aimerez peut-être aussi