Title: Chapter 2 Finite Automata
1Chapter 2Finite Automata
2A Finite-State Machine (1)
- A finite state machine is a mathematical model of
a system, with discrete inputs and outputs
3A Finite-State Machine (2)
- Finite Automata FA
- a finite set of states
- a set of transitions (edges)
- a start state
- a set of final states
- Defining a FA is a kind of programming.
- Problem definition
- Includes defining possible actions accepting
condition. - States ? structure of program
- Includes designating which are initial final.
- Transitions ? program
42.1 Deterministic Finite Automata (1)
- DFA M (Q, S, d, q0, F)
- Q a finite set of states
- S a finite set called the alphabet
- d transition function
- total function Q ? S ? Q
- q0 start state q0 ? Q
- F final or accepting states F ? Q
5Deterministic Finite Automata (2)
- DFA M
- Q q0, q1
- S a, b
- F q1
- The transition function d is given in a tabular
form called the transition table - d(q0, a) q1 d(q0, b) q0
- d(q1, a) q1 d(q1, b) q0
6Deterministic Finite Automata (3)
- A DFA M can be considered to be a language
acceptor. - The language of M, L(M), is the set of strings S
accepted by M. - A DFA M reads an input string from left to right.
- The next state depends on the current state and
the unread (unprocessed) symbol.
7Deterministic Finite Automata (5)
- The DFA M accepts the set of strings over a, b
that contain the substring bb - M Q q0, q1, q2, S a, b, F q2
- The transition function d is given in a tabular
form called the transition table - d(q0, a) q0 d(q0, b) q1
- d(q1, a) q0 d(q1, b) q2
- d(q2, a) q2 d(q2, b) q2
- Is abba ? L(M)? Yes, since the computation halts
in state q2, which is a final state - Is abab ? L(M)? No, since the computation halts
in state q1, which is NOT a final state
8Deterministic Finite Automata (6)
- Extended transition function d of a DFA with
transition function d is a function from Q ? S ?
Q defined recursively on the length of the input
string w - Basis w 0. Then w e and d(qi, e) qi
- Recursive step Let w ? 1. Then d(qi,
av) d(d(qi, a), v) - ? qi ? Q, ? a ? S, ? v ? S
9Deterministic Finite Automata (7)
- A string w is accepted if d(q0, w) ? F
- The language of a DFA M is
- L(M) w ? S d(q0, w) ? F
- DFA M (Q, S, d, q0, F) accepts w ? S ?
- d(q0, w) ? F
10Deterministic Finite Automata (8)
- Two possibilities for DFA M running on w
- M accepts w
- M accepts w iff the computation of M on w ends up
(halts) in an accepting configuration - d(q0, w) ? F
- M rejects w
- M rejects w iff the computation of M on w ends up
(halts) in a rejecting configuration - d(q0, w) ? F
11State Diagrams and Examples (1)
- The state diagram of a DFA M (Q, S, d,q0, F) is
a labeled graph G defined by the following
conditions - The nodes of G are the elements of Q
- The labels on the arcs of G are elements of S
- q0 is the start node, denoted by
- F is the set of accepting nodes, denoted by
- There is an arc from node qi to qj labeled a if
d(qi, a) qj - For every node qi and symbol a ? S, there is
exactly one arc labeled a leaving qi
a
q0
12State Diagrams and Examples (2)
- Deterministic Finite Automata DFA
- all outgoing edges are labelled with an input
character - no state has e- transition, transition on input e
- no two edges leaving a given state have the same
label - for each state s and input symbol a, there is at
most one edge label a leaving s - Therefore the next state can be determined
uniquely, given the current state and the current
input character
13State Diagrams and Examples (1)
- strings over a,b with at least 3 as
14State Diagrams and Examples (2)
- strings over a,b with length mod 3 0
15State Diagrams and Examples (3)
- strings over a,b without 3 consecutive as
- A simple example of strings not of the form .
16State Diagrams and Examples (4)
- Draw a state diagram for DFA M that accepts the
set of strings over a, b that contain the
substring bb - The string ababb is accepted since the halting
state is the accepting state q2
17State Diagrams and Examples (5)
- The DFA
- accepts (bab)(ae)
- the set of strings over a, b that do not
contain the substring aa
b
S
1 a
2 a
0 a
a
a
b
18State Diagrams and Examples (6)
- The language anbn, n?0 is not regular, so we
can not build a DFA that accept this language - It needs an infinite number of states
- But anbn, 1 ? n ? 3 is regular and its DFA is
This DFA is NOT Complete
a
a
a
q0
q1
q2
q3
b
b
b
q4
q5
q6
b
b
19State Diagrams and Examples (7)
- strings over a, b that contain the substring bb
OR do not contain the substring aa - This language is the union of the languages of
the previous examples
a
1 a
2 a
0 a
a
a
2 a
b
a
b
a
b
b
1 b
2 b
b
S
20State Diagrams and Examples (8)
- strings over a, b that contain an even number
of as AND an odd number of bs
b
ea, eb
ea, ob
b
a
a
a
a
oa, eb
oa, ob
b
b
21State Diagrams and Examples (9)
- Let M be the DFA previous slide
- A DFA M that accepts all strings over a, b
that do not contain an even number of as AND an
odd number of bs is shown below - L(M) a, b - L(M) S - L(M)
- Any string accepted by M is rejected by M and
vice versa
b
ea, eb
ea, ob
b
a
a
a
a
oa, eb
oa, ob
b
b
22State Diagrams and Examples (10)
- Let S 0, 1, 2, 3. A string in S is a
sequence of integers from S - The DFA M determines whether the sum of elements
of a string is divisible by 4 - The string 12302 and 0130 should be accepted and
0111 rejected by M
0
0
w mod 4 0
w mod 4 1
1
3
1
3
3
1
2
2
2
2
w mod 4 2
1
w mod 4 3
0
3
0
23State Diagrams and Examples (11)
- DFA M1 accepts (ab)c
- M1 is incomplete determinism
- The string abcc is rejected since M1 is unable to
process the final c from state q2
a
q0
q1
b
c
q2
M1
24State Diagrams and Examples (12)
- M2 accepts the same language as M1 in previous
example (ab)c - The state qe is the error state (dead end)
a
q0
q1
b
c
a, c
b
q2
qe
a, b, c
M2
S
25State Diagrams and Examples (13)
- strings over a,b with next-to-last symbol a
26State Diagrams and Examples (14)
What Language is Accepted?
272.2 Nondeterministic Finite Automata (1)
28Nondeterministic Finite Automata (2)
- NFA Formal Definition
- NFA M (Q, S, d, q0, F)
- Q a finite set of states
- S a finite set alphabet
- d transition function
- total function Q ? S ? ?(Q) 2Q - power set of
Q - q0 initial/starting state q0 ? Q
- F final or accepting states F ? Q
29Nondeterministic Finite Automata (3)
Q 0, 1, 2, 3 q0 0 F 3 ? a, b
What Language is defined ?
What is the Transition Table ?
i n p u t
a
b
state
0
0, 1
0
1
?
2
2
?
3
30Nondeterministic Finite Automata (4)
- Change in d
- For an DFA M, d(q, a) results in one and only one
state for all states q and alphabet a - For an NFA M, d(q, a) can result in a set of
states, zero, one, or more states
qi
a
a
qn
qi
qn
qn
qj
a
qk
a
d(qn, a) qi
d(qn, a) qi, qj , qk
d(qn, a) ?
31Nondeterministic Finite Automata (5)
a,b
a,b
1
2
3
4
5
a
a
a
b
- Why is this only an NFA and not an DFA?
32Nondeterministic Finite Automata (6)
- Computing with NFAs
- Computations are different
- We always start from start state. Call it the
root of the computation. - Then we might go to different states on one
symbol. - Then from those states to new sets of states,
creating a tree-like computation. - If one path ends up in a final state, then
ACCEPT, else REJECT
33Nondeterministic Finite Automata (7)
- Given an input string, we trace moves
- If no more input in final state, ACCEPT
EXAMPLE Input ababb
Path 1 0 -gt 0 -gt 0 -gt 0 -gt 0 -gt 0 (REJECT) Path
2 0 -gt 0 -gt 0 -gt 1 -gt 2 -gt 3 (ACCEPT)
34Nondeterministic Finite Automata (8)
- Extended transition function d of a NFA with
transition function d is a function from Q ? S ?
2Q (power set) defined recursively on the length
of the input string w - Basis w 0. Then w e and d(qi, e) qi
- Recursive step Let w ? 1. Then d(qi,
av) U d(qj, v), qj ? d(qi, a) - ? qi ? Q, ? qj ? Q ,? a ? S, ? v? S
- The language of a NFA M is
- L(M) w ? S d(q0, w) ? F ? ?
35NFA (9)
- The state diagram DFA M1 and NFA M2 accepts
(ab)bb(ab)
a, b
a
36NFA (10)
- An NFA that accepts string over a, b with
substring aa or bb - There are 2 distinct acceptance paths for the
string abaaabb
a, b
a, b
start
a
a
2
b
a, b
b
4
37NFA (11)
- The state diagram DFA M1 and NFA M2 accepts
(ab)abba(ab)
a, b
a
b
start
b
a
M1
b
a
4
a
b
a, b
a, b
start
b
a
M2
b
a
4
38NFA with e-Transitions (1)
- NFA with e-transitions, denoted by NFA-e
- Formal Definition
- NFA-e M (Q, S, d, q0, F)
- Q a finite set of states
- S a finite set alphabet
- d transition function
- total function Q ? (S ? e) ? ?(Q)
- q0 initial/starting state q0 ? Q
- F final or accepting states F ? Q
39 e-Transitions (2)
- The input string w is accepted using NFA-e if
there is a computation that processes the entire
string and halts in an accepting state - A computation may continue using e-transition
after the input string has been completely
processed
40 e-Transitions (3)
- L(M1) (ab)bb(ab), L(M2) (bab)(ae)
- L(M) L(M1) ? L(M2)
b
a, b
a, b
a
q2,0
q2,1
q1,1
q1,2
b
b
q1,0
M2
M1
b
a, b
a, b
q1,1
q1,2
b
b
q1,0
?
M
b
?
a
q2,0
q2,1
b
41 e-Transitions (4)
- L(M1) (ab)bb(ab), L(M2) (bab)(ae)
- L(M) L(M1)L(M2)
b
a, b
a, b
a
q2,0
q2,1
q1,1
q1,2
b
b
q1,0
M2
M1
b
b
a, b
a, b
a
q1,1
q1,2
b
b
q1,0
q2,0
?
q2,1
M
b
42 e-Transitions (5)
- M accepts all strings over a, b of even length
e
q1
q2
a, b
q0
a, b
e
43 e-Transitions (6)
- Let M1 and M2 be 2 NFA-es as follows
q1,0
q1,f
M1
q2,0
q2,f
M2
M1 (Q1, S, d1, q1,0, F1) M2 (Q2, S, d2,
q2,0, F2)
44 e-Transitions (7)
q1,0
q1,f
M1
?
q0
?
q2,0
q2,f
M2
M (Q1 ? Q2 ? q0, S, d, q0, F1 ? F2) d is the
same as in d1 and d2, but add d(q0, ?) q1,0,
q2,0
45 e-Transitions (8)
?
q1,0
q2,0
q1,f
M1
q2,f
M2
M (Q1 ? Q2, S, d, q1,0, F2) d is the same as in
d1 and d2, but add d(q, ?) q2,0, where q ?
F1
46 e-Transitions (9)
M (Q1 ? q0, qf, S, d, q0, qf) d is the same
as in d1, but add d(q0, ?) q1,0 ,qf d(q1,f,
?) q1,0 ,qf, where q1,f? F1
47 e-Transitions (10)
Given the regular expression a(bc) a( b
c e) Find a transition diagram NFA-? that
recognizes it.
48 e-Transitions (11)
a (bc)
a( b c e)
Now that you have the individual diagrams, or
them as follows
49 e-Transitions (12)
50 e-Transitions (12)
- See examples 2.7, 2.8, 2.9, and 2.10
51 2.3 Equivalence of DFA NFA (1)
- Is NFA more powerful than DFA? NO!
- NFA inefficient to implement directly, so convert
to a DFA that recognizes the same strings - Is there a language accepted by an NFA that is
not accepted by any DFA? No - NFA is equivalent to DFA.
- Each state in DFA corresponds to a SET of states
of the NFA
52 Removing Nondeterminism (2)
- ?-closure
- In an NFA-?, the ?-closure(q) of a state q is the
set of all states that can be reached from q by
following a path whose edges are all labeled by ?.
a
Start
?
?
q0
q1
q3
?-closure(q0) q0, q1, q2, q3 ?-closure(q1)
q1, q3 ?-closure(q2) q1, q2, q3
?
?
b
b
q2
53 Removing Nondeterminism (3)
NFA-e N (Q, ?, d, q0, F)
?-closure(q) q ? Q
set of NFA-e states that are reachable from q
on ?-transitions only ?-closure(T) T ? Q
NFA-e states reachable from
some state t ? T on
?-transitions only move(T, a) T ? Q, a ?
? set of states to
which there is a transition on
input a from some state t ? T move(q1, q2, ,
qi, a) ?(q1, a) ? ?(q2, a) ? ? ?(qi, a)
No input is consumed
These 3 operations are utilized by algorithms /
techniques to facilitate the conversion process
54 Removing Nondeterminism (4)
- ?-closure(0) 0, 1, 2, 4, 7 (all states
reachable from 0 on ?-moves) - ?-closure(0, 6) ?-closure(0) ? ?-closure(6)
- 0, 1, 2, 4, 7 ? 6, 1, 2, 4,7 0, 1,
2, 4, 6, 7 - move(0, 1, 2, 4, 7, a) 3, 8 (since move(2,
a)3 and move(7, a)8)
55NFA-e ?DFA with Subset Construction
NFA-e N ? DFA M construction Given N (Q, S, d,
q0, F), define M (Q, S, d, q0, F) Q ?
P(Q), all the possible NFA-e states M could be
in. q0 e-closure(q0) F q ? Q q ? F
? ? d(q, a) e-closure(move(q, a))
- DFA M has a state ERR ? Q P(Q)
- serves as the error state, when needed
- ?(ERR, a) ERR, ?a ? ? By definition of ?
56Subset Construction Algorithm Concepts
Start with NFA-e
(a b)abb
First we calculate ?-closure(0) (i.e., state
0) ?-closure(0) 0, 1, 2, 4, 7 (all states
reachable from 0 on ?-moves) Let A 0, 1, 2,
4, 7 be a state of new DFA M
57Conversion Example (1)
2nd , we calculate a ?-closure(move(A, a))
and b
?-closure(move(A, b)) a ?-closure(move(A, a))
?-closure(move(0,1,2,4,7, a)) adds 3, 8
(since move(2, a) 3 and move(7, a) 8) From
this we have ?-closure(3, 8)
1,2,3,4,6,7,8 (since 3?6?1?4, 6?7, and 1?2
all by ?-moves) Let B 1,2,3,4,6,7,8 be a new
state in D. Define ?M(A, a) B
b ?-closure(move(A,b)) ?-closure(move(0,1,2,4
,7, b)) adds 5 (since move(4, b) 5)
From this we have ?-closure(5)
1,2,4,5,6,7 (since 5?6?1?4, 6?7, and 1?2 all
by ?-moves) Let C 1,2,4,5,6,7 be a new state
in D. Define ?M(A, b) C
58Conversion Example (2)
3rd, we calculate for state B on a, b a
?-closure(move(B, a)) ?-closure(move(1,2,3,4,6,
7,8, a)) 1,2,3,4,6,7,8 B Define ?M(B, a)
B b ?-closure(move(B, b))
?-closure(move(1,2,3,4,6,7,8, b))
1,2,4,5,6,7,9 D Define ?M(B, b) D
4th, we calculate for state C on a, b a
?-closure(move(C, a)) ?-closure(move(1,2,4,5,6,
7, a)) 1,2,3,4,6,7,8 B Define ?M(C, a)
B b ?-closure(move(C, b)) ?-closure(move(1,2
,4,5,6,7, b)) 1,2,4,5,6,7 C Define ?M(C,
b) C
59Conversion Example (3)
5th , we calculate for state D on a, b a
?-closure(move(D, a)) ?-closure(move(1,2,4,5,6,
7,9, a)) 1,2,3,4,6,7,8 B Define ?M(D, a)
B b ?-closure(move(D, b))
?-closure(move(1,2,4,5,6,7,9, b))
1,2,4,5,6,7,10 E Define ?M(D, b) E
Finally, we calculate for state E on a, b a
?-closure(move(E, a)) ?-closure(move(1,2,4,5,6,
7,10, a)) 1,2,3,4,6,7,8 B Define ?M(E,
a) B b ?-closure(move(E, b))
?-closure(move(1,2,4,5,6,7,10, b))
1,2,4,5,6,7 C Define ?M(E, b) C
60Conversion Example (4)
This gives the transition table for the DFA M of
b
b
b
a
b
b
a
start
a
a
a
61Converting NFA-e to DFA 2nd Example
RE c(ab)c
1st we calculate ?-closure(0) ?-closure(0)
0, 1, 2, 6, 8 (all states reachable from 0 on
?-moves) Let A0, 1, 2, 6, 8 be a state of new
DFA M
62Conversion Example (1)
2nd , we calculate a ?-closure(move(A, a))
and b
?-closure(move(A, b)) c
?-closure(move(A, c)) a ?-closure(move(A, a))
?-closure(move(0,1,2,6,8, a)) adds 3
(since move(2, a) 3) From this we have
?-closure(3) 3 (since 3?3 by ?-moves) Let
B 3 be a new state. Define ?M(A, a) B
b ?-closure(move(A,b)) ?-closure(move(0,1,2,6
,8, b)) There is NO transition on b for all
states 0,1,2,6 and 8 Define ?M(A, b) Reject
c ?-closure(move(A, c)) ?-closure(move(0,1,2,
6,8, c)) adds 7 (since move(6, c) 7) From
this we have ?-closure(7)
1,2,5,6,7,8 (since 7?5?8, 7?5?1?2, and 7?5?1?6
by ?-moves) Let C 1,2,5,6,7,8 be a new state.
Define ?M(A, c) C
63Conversion Example (2)
3rd , we calculate a ?-closure(move(B, a))
and b
?-closure(move(B, b)) c
?-closure(move(B, c)) a ?-closure(move(B, a))
?-cclosure(move(3, a)) There is NO
transition on a for state 3 Define ?M(B, a)
Reject
b ?-closure(move(B, b)) ?-closure(move(3,
b)) adds 4 (since move(3, b) 4) From this we
have ?-closure(4) 1,2,4,5,6,8 (since
4?5?8, 4?5?1?2, and 4?5?1?6 by ?-moves) Let D
1,2,4,5,6,8 be a new state. Define ?M(B, b)
D
c ?-closure(move(B, c)) ?-closure(move(3,
c)) There is NO transition on c for state 3
Define ?M(B, c) Reject
64Conversion Example (3)
4th , we calculate a ?-closure(move(C, a))
and b
?-closure(move(C, b)) c
?-closure(move(C, c)) a ?-cclosure(move(C,
a)) ?-closure(move(1,2,5,6,7,8,a)) adds 3
(since move(2, a) 3) ?-closure(3)
3 Define ?M(C, a) B
b ?-closure(move(C, b)) ?-closure(move(1,2,5,
6,7,8,b)) There is NO transition on b for all
states 1,2,5,6, and 7 Define ?M(C, b) Reject
c ?-closure(move(C, c)) ?-closure(move(1,2,5,
6,7,8,c)) adds 7 (since move(6, c) 7) From
this we have ?-closure(7)
1,2,5,6,7,8 (since 7?5?8, 7?5?1?2, and 7?5?1?6
by ?-moves) Define ?M(C, c) C
65Conversion Example (4)
5th , we calculate a ?-closure(move(D, a))
and b
?-closure(move(D, b)) c
?-closure(move(D, c)) a ?-closure(move(D, a))
?-closure(move(1,2,4,5,6,8,a)) adds 3
(since move(2, a) 3) ?-closure(3)
3 Define ?M(D, a) B
b ?-closure(move(D, b)) ?-closure(move(1,2,4,
5,6,8, b)) There is NO transition on b for all
states 1,2,4,5,6, and 8 Define ?M(D, b) Reject
c ?-closure(move(D, c)) ?-closure(move(1,2,4,
5,6,8,c)) adds 7 (since move(6, c) 7) From
this we have ?-closure(7)
1,2,5,6,7,8 (since 7?5?8, 7?5?1?2, and 7?5?1?6
by ?-moves) Define ?M(D, c) C
66The Resulting DFA M
Which States are FINAL States ?
67NFA-e ? DFA Example
- strings over a,b with next-to-last symbol a
a/b
d a b e
q0 q0, q1 q0 ?
q1 q2 q2 ?
q2 ? ? ?
a
a/b
q2
q1
q0
M (q0, q1, q2, a,b, d, q0, q2)
b
q0 q1
q0
b
b
q0 q2
q0 q1 q2
b
Equivalent DFA
68An Example of NFA-e ? DFA
- Consider a simple NFA
- Construct a corresponding DFA
69Examples
- See examples 2.11, 2.12, and 2.13
70Class Discussion
- Construct a DFA equivalent to this NFA
0
71Class Discussion
Notice that the state with label 0, 1, 2 is
from the set of states given by the
nondeterministic transition ?(0, a) 0, 1, 2.
Also notice that any state whose label contains
an accepting state is defined as an accepting
state in the deterministic machine.
722.4 Reduction of the Number of States in FA
- Self study
- but not included in the exams material