Title: Lecture 5: Finite Automata
1Lecture 5 Finite Automata
??????? ?????????
2Content
- Alphabets and Languages
- Deterministic Finite Automata
- Nondeterministic Finite Automata
- Equivalence of DFA NFA
- Properties of Languages Accepted by FAs
- Finite Automata Regular Expressions
- Languages Not Accepted by FAs
3Lecture 5 Finite Automata
??????? ?????????
4Alphabets
- Alphabet ? a finite set of symbols
- Examples
? binary alphabet
? decimal alphabet
? Roman alphabet
? Greek alphabet
? my alphabet
5Strings
- String over an alphabet (?) ? a finite sequence
of symbols from ? - ? ? the set of all strings over ?
- Examples
6More on Strings
- String length ? symbols in a string, e.g.,
- A string, w, can be considered as a function
, e.g., - Some string operations
- Concatenation
- Reversal
- Duplication
7More on Strings
? a prefix of w and a substring of w.
? a suffix of w and a substring of w.
? a substring of w.
8Countability of ?
? is countably infinite for any finite alphabet
?.
Example
? can be enumerated by
9Languages
- Language L over ? ? any subset of ? i.e., L ? ?
- How many possible languages over ??
- How to specify a language?
? uncountably infinite
10Operations of Languages
- Complement
- Union
- Intersection
11Operations of Languages
- Complement, Union and Intersection
- Concatenation
- Closure or Kleene Star
- Positive Closure
12Example Kleene Star
1909 1994
13Example Kleene Star
1909 1994
?
?
14Example Kleene Star
1909 1994
?
15Language Recognition Device
A machine or algorithm to answer the question
16Lecture 5 Finite Automata
- Deterministic Finite Automata
??????? ?????????
17Finite State Machine
Input tape
Reading Head
18Definition ? Deterministic Finite Automaton
- A deterministic finite automaton is a 5-tuple
- (K, ?, ?, s, F)
K a finite set of states
? a finite set of symbols
? the transition function K??? K
s ?K the initial state
F ?K the set of final states
19Example
(K, ?, ?, s, F)
Kq0, q1, q2, q3
?0, 1
sq0
F q0
20Operations of a DFA
21Operations of a DFA
Strings are fed into the device by means of an
input tape.
- Main part of the machine
- Initially,
- putting reading head at the leftmost
- in state s q0
- Sense input symbol
- Change state based on ?
- Move head right one cell
- Give answer when head reaches end
22DFA As a Language Acceptor
- The input string is accepted by the machine if
the reading head ends at a final state. - Otherwise, it is rejected.
Accepted
JFLAP
Rejected
23DFA As a Language Acceptor
- The input string is accepted by the machine if
the reading head ends at a final state. - Otherwise, it is rejected.
What language the machine accepts?
24DFA As a Language Acceptor
- The input string is accepted by the machine if
the reading head ends at a final state. - Otherwise, it is rejected.
What language the machine accepts?
q3
25Memory Configuration of a DFA
M (K, ?, ?, s, F)
26Memory Configuration of a DFA
M (K, ?, ?, s, F)
Does not effect the result.
27Memory Configuration of a DFA
M (K, ?, ?, s, F)
28Memory Configuration of a DFA
M (K, ?, ?, s, F)
29Memory Configuration of a DFA
30Yields in One Step
if
31Yields
in some (including zero) steps
if
32String Acceptance by a DFA
M (K, ?, ?, s, F)
w?? is said to be accepted by M iff
such that
33Language Defined by a DFA
M (K, ?, ?, s, F)
Language L(M) denotes the set of all strings
accepted by M, i.e.,
34Example
w0100100110
w?L(M)?
(q0, 0100100110)
M
35Example
w0100100110
?
w?L(M)?
(q0, 0100100110)
M
36Example
M
37Example
38Exercises
- Describe informally the languages accepted by the
deterministic finite automata shown below
(a)
(b)
39Exercises
- Construct deterministic finite automata accepting
each of the following languages - w?a, b each a in w is immediately preceded
and immediately followed by a b. - w?a, b w has abab as a substring.
- w?a, b w has neither aa nor bb as a
substring. - w?a, b w has both ab and ba as a substring.
40Lecture 5 Finite Automata
- Nondeterministic Finite Automata
??????? ?????????
41Review ? Deterministic Finite Automaton
- A deterministic finite automaton is a 5-tuple
- (K, ?, ?, s, F)
K a finite set of states
? a finite set of symbols
? the transition function K??? K
s ?K the initial state
F ?K the set of final states
42Definition ?Nondeterministic Finite Automaton
- A deterministic finite automaton is a 5-tuple
- (K, ?, ?, s, F)
A nondeterministic finite automaton is a
5-tuple (K, ?, ?, s, F)
K a finite set of states
? a finite set of symbols
? the transition function K??? K
? the transition function K??? 2K
2K
s ?K the initial state
F ?K the set of final states
43Transition Functions of FAs
? K??? 2K
? K??? K
? K
q
?K
Q
44Transition Functions of FAs
? K??? 2K
? K??? K
Change to one of state in Q nondeterministically.
Change to state q deterministically.
? K
q
?K
Q
45Yields in One Step
M (K, ?, ?, s, F)
if
46Yields
M (K, ?, ?, s, F)
in some (including zero) steps
47String Acceptance by a NFA
M (K, ?, ?, s, F)
w?? is said to be accepted by M iff
such that
48Language Defined by an NFA
M (K, ?, ?, s, F)
Language L(M) denotes the set of all strings
accepted by M, i.e.,
49Example L(ababa)
NFA
DFA
q1
q2
50Extension ?-Move Multi-Move
L(ababa)
q1
NFA
q2
q1
q2
51Extension ?-Move Multi-Move
L(ababa)
q1
NFA
q2
q1
q1
q2
52Definition ?Nondeterministic Finite Automaton
A nondeterministic finite automaton is a
5-tuple (K, ?, ?, s, F)
K a finite set of states
? a finite set of symbols
? the transition function K??? 2K
s ?K the initial state
F ?K the set of final states
53Yields in One Step
M (K, ?, ?, s, F)
54Yields
M (K, ?, ?, s, F)
in some (including zero) steps
55String Acceptance by a NFA
M (K, ?, ?, s, F)
w?? is said to be accepted by M iff
such that
56Language Defined by an NFA
M (K, ?, ?, s, F)
Language L(M) denotes the set of all strings
accepted by M, i.e.,
57Lecture 5 Finite Automata
??????? ?????????
58Equivalence of Finite Automata
Finite automata M1 and M2 are said to be
equivalent iff
59Machine Simulation
Of Course
?
DFA
NFA
?
To be discussed
60Lemma
For each NFA, there is an equivalent NFA finite
automaton without multi-move on the transition
function.
61Example
In the following, we will assume the NFA being
discussed is one without multi-move unless
otherwise stated.
62?-Closure
In the following, we will assume the NFA being
discussed is one without multi-move unless
otherwise stated.
M (K, ?, ?, s, F)
?-Closure of state q
63Example
M
To be used in the next example.
64Theorem
For each nondeterministic finite automaton, there
is an equivalent deterministic finite automaton.
65Theorem
For each nondeterministic finite automaton, there
is an equivalent deterministic finite automaton.
Pf)
Let M (K, ?, ?, s, F) be an NFA.
To prove the theorem, we must be able to build a
DFA M (K, ?, ?, s, F) such that L(M)L(M).
66Theorem
For each nondeterministic finite automaton, there
is an equivalent deterministic finite automaton.
Pf)
Let M (K, ?, ?, s, F) be an NFA.
To prove the theorem, we must be able to build a
DFA M (K, ?, ?, s, F) such that L(M)L(M).
67Theorem
For each nondeterministic finite automaton, there
is an equivalent deterministic finite automaton.
Pf)
Next, we have to show that
68Theorem
For each nondeterministic finite automaton, there
is an equivalent deterministic finite automaton.
- Exercises
- Referring to the textbook to complete the proof
in detail. - To present the proof using PowerPoint.
Pf)
Next, we have to show that
69Example
M
70Example
M
71Example
M
There are 32 possible subsets. However, most of
them are useless.
72Example
M
73Example
M
M
gt
74Example
M
M
gt
75Example
M
M
gt
?
76Example
M
JFLAP
M
gt
?
77Exercises
- Construct deterministic finite automata
equivalent to the nondeterministic automata shown
below
(a)
(b)
78Lecture 5 Finite Automata
- Properties of Languages Accepted by Finite
Automata
??????? ?????????
79The Chomsky Hierarchy
80The Chomsky Hierarchy
81Properties of Languages Accepted by FAs
- The class of languages accepted by finite
automata is - closed under
- union
- concatenation
- Keene star
- complementation
- intersection.
82Properties of Languages Accepted by FAs
- The class of languages accepted by finite
automata is - closed under
- union
- concatenation
- Keene star
- complementation
- intersection.
accepts
83Properties of Languages Accepted by FAs
- The class of languages accepted by finite
automata is - closed under
- union
- concatenation
- Keene star
- complementation
- intersection.
accepts
84Properties of Languages Accepted by FAs
- The class of languages accepted by finite
automata is - closed under
- union
- concatenation
- Keene star
- complementation
- intersection.
accepts
85Properties of Languages Accepted by FAs
- The class of languages accepted by finite
automata is - closed under
- union
- concatenation
- Keene star
- complementation
- intersection.
86Properties of Languages Accepted by FAs
- The class of languages accepted by finite
automata is - closed under
- union
- concatenation
- Keene star
- complementation
- intersection.
87Theorem
- There are algorithms to answer the following
- questions about finite automata
- w ? L(M)?
- L(M) ??
- L(M) ??
- L(M1) ? L(M2) ?
- L(M1) L(M2) ?
88Theorem
- There are algorithms to answer the following
- questions about finite automata
- w ? L(M)?
- L(M) ??
- L(M) ??
- L(M1) ? L(M2) ?
- L(M1) L(M2) ?
? Feed w into M.
? Check any final state is reachable
- Check L(M1) ? L(M2) ?
- L(M2) ? L(M1) ?
89Exercises
- Draw state diagrams for nondeterministic finite
automata accepting these languages - (ab)(ba)?aa
- ((ab?aab)a)
- ((aba)b)
- (ba?b)?(bb?a)
90Lecture 5 Finite Automata
- Finite Automata
-
- Regular Expressions
??????? ?????????
91The Languages Accepted by FAs
- The languages accepted by FAs are called regular
sets. - How to describe a regular set?
- Regular Expression
92Definition ? Regular Expressions
A regular expression is defined inductively over
the alphabet ? ? (, ), ?, ?, , as
follows
- ?, ?, and each ??? is a regular expression
- If ? and ? are regular expressions, then
- (? ?)
- (? ?)
- ?
- are regular expressions
? basic
? inductive
93Definition ? Regular Expressions
A regular expression is defined inductively over
the alphabet ? ? (, ), ?, ?, , as
follows
- ?, ?, and each ??? is a regular expression
- If ? and ? are regular expressions, then
- (? ?)
- (? ?)
- ?
- are regular expressions
? basic
? union, or
? concatenation
? Kleene closure
? inductive
94Writing a Regular Expression
For omitting parentheses, the precedence of
operators are assigned as follows
gt gt
?
((0(1))0)
010
What is this language?
95Examples
What do the following regular expressions
represent?
- 00
- (01)
- (01) 00 (01)
- (0?) (110)
- (01)011
- 012
- 001122
96More Examples
Is the following language L an r.e. over ?a-z?
Lwhat, which, when, where.
Write L as an r.e.
L what which when where
L (wh)(at ich en ere)
L (wh)(at ich) (whe) (n re)
97Theorem
A language is regular if and only if it is
accepted by a finite automaton.
Pf)
?
Given a regular expression, how to construct an
FA to accept the language described by it?
?
Given an FA, what is its corresponding regular
expression?
98Theorem
A language is regular if and only if it is
accepted by a finite automaton.
?
Pf)
Suppose that we are given a regular expression r,
we can construct an NFA to accept the language
described it inductively based on the number of
operators' it possesses as follows.
Basis (zero operators)
Case 1 r ?
Case 2 r ?
Case 3 r a
99Theorem
A language is regular if and only if it is
accepted by a finite automaton.
?
Pf)
Suppose that we are given a regular expression r,
we can construct an NFA to accept the language
described it inductively based on the number of
operators' it possesses as follows.
? true
Basis (zero operators)
Hypothesis
Assumed true for r.e.s few than m operators
Induction
To be shown true for r.e.s with m1 operators
Case 1 r r1r2
r m1 operators r1 ? m operators r2 ? m
operators
Case 2 r r1r2
Case 3 r r1
100Theorem
A language is regular if and only if it is
accepted by a finite automaton.
?
Pf)
Suppose that we are given a regular expression r,
we can construct an NFA to accept the language
described it inductively based on the number of
operators' it possesses as follows.
? true
Basis (zero operators)
Hypothesis
Assumed true for r.e.s few than m operators
Induction
To be shown true for r.e.s with m1 operators
Case 1 r r1r2
r m1 operators r1 ? m operators r2 ? m
operators
Case 2 r r1r2
Case 3 r r1
101Theorem
A language is regular if and only if it is
accepted by a finite automaton.
?
Pf)
Suppose that we are given a DFA, we will
determine its corresponding regular expression
also inductively.
Consider DFA M (q1, q2,,qn, ?, ?, q1, F).
Define
l ? k
ql
y
qi
qj
102Theorem
A language is regular if and only if it is
accepted by a finite automaton.
?
Pf)
Suppose that we are given a DFA, we will
determine its corresponding regular expression
also inductively.
Consider DFA M (q1, q2,,qn, ?, ?, q1, F).
Define
is regular for any i, j, and k.
103Theorem
A language is regular if and only if it is
accepted by a finite automaton.
?
Pf)
Suppose that we are given a DFA, we will
determine its corresponding regular expression
also inductively.
Consider DFA M (q1, q2,,qn, ?, ?, q1, F).
Define
is regular for any i, j, and k.
104Equivalence FA RE
FA
RE
105The Chomsky Hierarchy
106The Chomsky Hierarchy
107Example
Find an r.e. to represent the following FA.
M
108Example
k 0
k 1
?
0
1
0
?
1
?
01
?
109Example
k 0
k 1
k 2
?
(00)
0
0(00)
01
1
0
0(00)
?
(00)
1
01
(01)(00)0
?
01
(01)(00)
?
?(01)01
110Example
k 0
k 1
k 2
?
(00)
0
0(00)
01
1
0
0(00)
?
(00)
1
01
(01)(00)0
?
01
(01)(00)
?
?(01)01
111Example
k 0
k 1
k 2
?
(00)
0
0(00)
01
1
0
0(00)
?
(00)
1
01
(01)(00)0
?
01
(01)(00)
?
?(01)01
112Lecture 5 Finite Automata
- Languages Not Accepted by Finite Automata
??????? ?????????
113What FAs can? What FAs cannot?
Which of the following languages are regular?
114The Pumping Theory
Let L be an infinite regular set. Then there are
strings u,v, and w st. vgt0 and uvnw?L for all n
? 0.
Pf)
Let L is accepted by a DFA M (K, ?, ?, q0 , F)
with m states.
Consider input w ?1 ? 2 ?l, l ? m.
Then,
. . .
By pigeonhole, there exists j, k with 0? j lt k ?
l st. qj qk.
115The Pumping Theory
Let L be an infinite regular set. Then there are
strings u,v, and w st. vgt0 and uvnw?L for all n
? 0.
Pf)
Let L is accepted by a DFA M (K, ?, ?, q0 , F)
with m states.
Consider input w ?1 ? 2 ?l, l ? m.
Then,
. . .
By pigeonhole, there exists j, k with 0? j lt k ?
l st. qj qk.
It is then seen that if qm?F, then
uw ?L,
uvw ?L,
uv2w ?L,
. . .,
uvnw?L for all n ? 0.
116Example
- Show that the language Lanbn n?0 is not
regular.
Pf)
By pumping theorem,
Suppose that L is regular.
? z anbn uvw?L st. uvmw?L for all m ? 0.
w
Case 1 z a a a b b b
v
u
uvmw?L for all m ? 0
Case 2 z a a a b b b
v
w
u
u
Case 3 z a a a b b b
w
v
117The Chomsky Hierarchy
Lanbn n?0
118Lecture 5 Finite Automata
??????? ?????????
119Example
- Input a 0/1 sting.
- If the numbers of 0 and 1 in the string are both
even, then it is legal otherwise, it is illegal. - 0011001001(legal)
- 11001001001(illegal)
- Writing a C program to do so.
120Finite State Machine
Examples
00100011ltCRgt
01101011ltCRgt
001010101ltCRgt
0010010101ltCRgt
121Finite State Machine
q1
q2
ok
q0
q3
err
q3
q0
err
err
q2
q1
The Parser
122Implementation (I)
define q0 0 define q1 1 define q2 2 define q3
3 define fini 4
123Implementation (I)
int parser43 q1, q2, fini, q0, q3, fini,
q3, q0, fini, q2, q1, fini int stateq0 int
event char str
124Implementation (I)
void ToEvent(char c) if(c 0) event
0 else if(c 1) event 1 else event
2
Event (or Message) Encoding
125Implementation (I)
void main() // Ask user to input a 0/1
string // Store the string into str state q0
//initialization while(state!fini) ToEvent(s
tr) EventHandler(event)
Event (or Message) Loop
126Implementation (I)
void EventHandler(int event) int next_state
parserstateevent switch(next_state)
case fini printf(s\n, stateq0 ? ok
err) default state next_state
//change state
Event Handler
127Implementation (II)
pq1
pq2
ok
pq0
pq3
err
pq3
pq0
err
err
pq2
pq1
The Parser
128Implementation (II)
define q0 0 define q1 1 define q2 2 define q3
3 define fini 4
void pq0(), pq1(), pq2(), pq3() void ok(), err()
129Implementation (II)
void pq0() state q0 void pq1() state
q1
void pq2() state q2 void pq3() state
q3
130Implementation (II)
void ok() printf(ok\n) state
fini void err() printf(error\n) state
fini
131Implementation (II)
typedef void (FUNCTION)() FUNCTION
parser43 pq1, pq2, ok, pq0, pq3,
err, pq3, pq0, err, pq2, pq1, err
132Implementation (II)
void main() // Ask user to input a 0/1
string // Store the string into str state q0
//initialization while(state!fini) ToEvent(s
tr) (parserstateevent)()
Event (or Message) Loop