Finite Automata
Finite Automata
Reading: Chapter 2
Finite Automata
Informally, a state machine that comprehensively captures all possible states and transitions that a machine can take while responding to a stream (or sequence) of input symbols Recognizer for Regular Languages Deterministic Finite Automata (DFA)
The machine can exist in only y one state at any y given g time The machine can exist in multiple states at the same time
Q ==> a finite set of states ==> a finite set of input symbols (alphabet) q0 ==> > a start state F ==> set of final states ==> a transition function, which is a mapping b t between Q x ==> Q {Q , q0,F, {Q, F }
3
Start at the start state q0 F every input For i t symbol b li in th the sequence w d do
Compute the next state from the current state, given the current input symbol in w and the transition function
If after all symbols in w are consumed, the current state is one of the final states (F) then accept w; Otherwise, , reject j w.
4
Regular Languages
Example #1
L = {w | w is a binary string that contains 01 as a substring} = {0,1} Decide on the states: Q D i Designate t start t t state t t and d final fi l state(s) t t ( ) : Decide on the transitions:
Final states == same as accepting states Other states == same as non-accepting states
1 start q0 0
0 q1 1
q0 q1 *q2
q1 q1 q2
Example #2
Clamping Logic:
A clamping circuit waits for a 1 input, and turns on forever. However to avoid clamping on spurious noise However, noise, well we ll design a DFA that waits for two consecutive 1s in a row before clamping on. Build a DFA for the following g language: g g L = { w | w is a bit string which contains the substring 11} q0 : start state (initially off) off), also means the most recent input was not a 1 q1: has never seen 11 but the most recent input was a 1 q2: has seen 11 at least once
8
State Design:
Example #3
Build a DFA for the following language: L = { w | w has an even number of 0s and an even number of 1s} N t Alphabet Note: Al h b t i implied li d i is {0 {0,1} 1}
(q0,w) = ending state of the path taken from q0 on input string w (q0,wa) = ((q0,w), a) Exercise:
Language of a DFA
A DFA A accepts w if there is exactly a path from q0 to an accepting (or final) state that is labeled by w i.e., L(A) = { w | (q0,w) F } I.e., L(A) = all strings that lead to a final state from o q0
11
is of course non-deterministic
Implying that the machine can exist in more than one state at the same time Outgoing transitions could be non-deterministic qi 1 1 qj qk
Each transition function therefore aps to a set o of states maps
12
Q ==> a finite set of states ==> a finite set of input symbols (alphabet) q0 ==> > a start state F ==> set of final states ==> a transition function, which is a mapping b t between Q x ==> subset b t of fQ {Q , q0,F, {Q, F }
13
Start at the start state q0 For every input symbol in the sequence w do
Determine all the possible next states from the current state, given the current input symbol in w and the transition function
If after all symbols in w are consumed, at least one of the current states is a final state then accept w; Otherwise, reject w.
14
0,1 start q0 0 q1 1
q2 Final state
sta ates
q0 q1 *q2
{q0,q1} {q2}
Example #2
Build an NFA for the following language: L = { w | w ends in 01} ? How about a DFA now?
16
But imaginary, g y , in the sense that it has to be implemented deterministically in practice Could a non-deterministic state machine be implemented in practice?
17
1.
2.
3. 4.
5.
For each state, transition on all possible symbols (alphabet) ( p ) should be defined Accepts input if the last state is in F Sometimes harder to construct because of the number of states Practical implementation is feasible
3. 4. 5.
For each state, not all symbols necessarily have to be defined in the transition function Accepts input if one of the last states is in F Generally y easier than a DFA to construct Practical implementation has to be deterministic (so needs conversion to DFA)
19
Language of an NFA
An NFA accepts w if there exists at least one path from the start state to an accepting (or final) state that is labeled by w L(N) = { w | (q0,w) F }
20
Theorem:
A language L is accepted by a DFA if and only if it i is accepted t db by an NFA NFA.
1 1.
Proof:
If part:
2.
Only-if part:
Every DFA is a special case of an NFA where each state has exactly one transition for every input symbol. Th f Therefore, if L i is accepted t db by a DFA DFA, it i is accepted t db by a corresponding NFA.
21
If-part: A language L is accepted by a DFA if it is accepted by an NFA rephrasing Given any NFA N, we can construct a DFA D such that L(N)=L(D) How to construct a DFA from an NFA?
Observation: the transition function of an NFA maps to subsets of states Idea: Make one DFA state for every possible subset of the NFA states Subset construction
22
1. 2. 3.
Let N = {QN,,N,q0,FN} Goal: Build D={Q {QD,,D,{q0}, },FD} s.t. L(D)=L(N) Construction:
QD= all subsets of QN (i.e., power set) FD=set of subsets S of QN s.t. SFN D: for each subset S of QN and for each input symbol a in :
D(S,a) (S a) = U N(p,a) (p a)
p in s
23
1 0
0 1 0
NFA:
01 0,1 q0 0 q1 1 q2
{q0}
{q0,q q 1} 1
{q0,q q 2}
N q0 q1 *q2
0 {q0,q1}
1 {q0} {q2}
24
1 0
0 1 0
NFA:
01 0,1 q0 0 q1 1 q2
{q0}
{q0,q q 1} 1
{q0,q q 2}
N q0 q1 *q2
0 {q0,q1}
1 {q0} {q2}
Theorem: If D is the DFA constructed from NFA N by subset construction, then L(D)=L(N) Proof:
Dead states
Example:
q0
q2
q3
q4
q5
28
Applications
Text indexing
inverted indexing For each unique word in the database, store all locations that contain it using an NFA or a DFA Example: Google querying PATRICIA tree, suffix tree
29
FA with Epsilon-Transitions
i.e., a state can jump to another state without consuming any input symbol Makes it easier sometimes for NFA construction
Use:
30
Example of an -NFA
L = {w | possibily empty w s.t. if non-empty will end in 01}
0,1 q0 0 q1 1
-closure of a state q,
ECLOSE(q), is the set of all states that can be reached from q by repeatedly making transitions (including itself).
start q0
E *q0 q0 q1 *q2
q2
0 {q0,q q 1}
1 {q0} {q2}
ECLOSE(q0) ECLOSE(q0)
31
Proof:
32
Eliminating -transitions
1. 2. 3. 4.
33
Summary
DFA Definition Transition diagrams & tables Regular language NFA Definition Transition diagrams & tables DFA vs. NFA NFA to DFA conversion using subset construction Equivalency of DFA & NFA Removal of redundant states and including dead states
-transitions in NFA
Pigeon hole principles Text searching applications
34