3 FiniteAutomata Anim
3 FiniteAutomata Anim
Lecture: 3
1
Finite Automaton (FA)
A finite-state machine (FSM) or finite automaton is a
mathematical model of computation.
It is an abstract machine that can be in exactly one of a finite
number of states at any given time.
The FSM can change from one state to another in response to
some inputs; the change from one state to another is called
a transition.
2
Finite Automaton (FA)
Finite-state machines are of two types:
1. Deterministic finite-state machines
2. Non-deterministic finite-state machines.
3
Deterministic Finite Automata (DFA)
In a DFA, for a particular input character, the machine goes to one
state only.
A transition function is defined on every state for every input symbol.
Also in DFA null (or ε) move is not allowed, i.e., DFA cannot change
state without any input character.
For example, below DFA with Σ = {0, 1} accepts all strings ending
with 0.
One important thing to note is, that there can be many possible DFAs
for a pattern. A DFA with a minimum number of states is generally
preferred.
4
Deterministic Finite Automata - Definition
A DFA is defined by the 5-tuple:
{Q, ∑ , q0,F, δ }
5
What does a DFA do on reading an input string?
Input: a word w in ∑*
Question: Is w acceptable by the DFA?
Steps:
Start at the “start state” q
0
For every input symbol in the sequence w do
Compute the next state from the current state, given the
6
Regular Languages
Let L(A) be a language recognized by a DFA A.
Then L(A) is called a “Regular Language”.
7
Example #1
Build a DFA for the following language:
L = {w | w is a binary string that contains 01 as a substring}
Steps for building a DFA to recognize L:
∑ = {0,1}
Decide on the states: Q
Designate start state and final state(s)
δ: Decide on the transitions:
“Final” states = “accepting states”
Other states = “non-accepting states”
8
Regular expression: (0+1)*01(0+1)*
1 0 0,1 • ∑ = {0,1}
• start state = q0
start 0 1
q0 q1 q2 • F = {q2}
Accepting • Transition table
state symbols
0 1
q0 q1 q0
states
q1 q1 q2
*q2 q2 q2
9
Example #2
Build a DFA for the following language:
L = { w | w is a bit string which contains the
substring 11}
State Design:
q0 : start state (initially 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
10
Example #3
Build a DFA over {a,b} for the following
language:
o L = { w | w is a string that starting with a}
o L = { w | w is a string that ending with a}
o L = { w | w is a string that containing a}
?
11
Example #4
Build a DFA for the following language:
L = { w | w is a binary string that has even
number of 1s and even number of 0s}
?
12
Non-deterministic Finite
Automata (NFA)
A Non-deterministic Finite Automaton
(NFA)
is of course “non-deterministic”
Implying that the machine can exist in more
than one state at the same time
Transitions could be non-deterministic
1 qj
qi … • Each transition function therefore
1 maps to a set of states
qk
13
Nondeterministic Finite Automata(NFA):
14
Relation between DFA and NFA
Since all the tuples in DFA and NFA are the same except for one of the tuples, which is
Transition Function (δ)
In the case of DFA δ: Q X Σ --> Q
In the case of NFA δ: Q X Σ --> 2Q
Yet there is a way to convert an NFA to DFA, so there exists an equivalent DFA for every
NFA.
Both NFA and DFA have the same power and each NFA can be translated into a DFA.
There can be multiple final states in both DFA and NFA.
NFA is more of a theoretical concept.
DFA is used in Lexical Analysis in Compiler.
If the number of states in the NFA is N then, its DFA can have a maximum 2 N number of
states.
15
Non-deterministic Finite
Automata (NFA)
A Non-deterministic Finite Automaton (NFA)
consists of:
Q ==> a finite set of states
∑ ==> a finite set of input symbols (alphabet)
q0 ==> a start state
F ==> set of accepting states
δ ==> a transition function, which is a mapping
between Q x ∑ ==> subset of Q
An NFA is also defined by the 5-tuple:
{Q, ∑ , q0,F, δ }
16
How to use an NFA?
Input: a word w in ∑*
Question: Is w acceptable by the NFA?
Steps:
Start at the “start state” q0
For every input symbol in the sequence w do
Determine all possible next states from all current states, given
the current input symbol in w and the transition function
If after all symbols in w are consumed and if at least one of
the current states is a final state then accept w;
Otherwise, reject w.
17
Regular expression: (0+1)*01(0+1)*
18
Note: Omitting to explicitly show error states is just a matter of design convenience
(one that is generally followed for NFAs), and
i.e., this feature should not be confused with the notion of non-determinism.
20
But, DFAs and NFAs are equivalent in their power to capture langauges !!
21
Equivalence of DFA & NFA
Theorem:
Should be A language L is accepted by a DFA if and only if
true for
any L
it is accepted by an NFA.
Proof:
1. If part:
Prove by showing every NFA can be converted to an
equivalent DFA (in the next few slides…)
Subset construction
23
NFA to DFA by subset construction
Let N = {QN,∑,δN,q0,FN}
Goal: Build D={QD,∑,δD,{q0},FD} s.t.
L(D)=L(N)
Construction:
1. QD= all subsets of QN (i.e., power set)
2. FD=set of subsets S of QN s.t. S∩FN≠Φ
3. δD: for each subset S of QN and for each input symbol
a in ∑:
δD(S,a) = U
p inδs (p,a)
N
24
Idea: To avoid enumerating all of
power set, do
“lazy creation of states”
δD 0 1 δD 0 1
δN 0 1
Ø Ø Ø [q0] [q0,q1] [q0]
q0 {q0,q1} {q0}
[q0] {q0,q1} {q0} [q0,q1] [q0,q1] [q0,q2]
q1 Ø {q2}
[q1] Ø {q2} *[q0,q2] [q0,q1] [q0]
*q2 Ø Ø *[q2] Ø Ø
[q0,q1] {q0,q1} {q0,q2}
*[q0,q2] {q0,q1} {q0} 0. Enumerate all possible subsets
*[q1,q2] Ø {q2} 1. Determine transitions
*[q0,q1,q2] {q0,q1} {q0,q2}
2. Retain only those states
reachable from {q0}
25
NFA to DFA: Repeating the example
using LAZY CREATION
L = {w | w ends in 01} 1 0
NFA: DFA: 0 1
0,1 [q0] [q0,q1] [q0,q2]
0
0 1 1
q0 q1 q2
δN 0 1 δD 0 1
q0 {q0,q1} {q0} [q0] [q0,q1] [q0]
q1 Ø {q2} [q0,q1] [q0,q1] [q0,q2]
*q2 Ø Ø *[q0,q2] [q0,q1] [q0]
Main Idea:
Introduce states as you go
(on a need basis)
26
Correctness of subset construction
Theorem: If D is the DFA constructed from
NFA N by subset construction, then
L(D)=L(N)
Proof:
27
A bad case where
#states(DFA)>>#states(NFA)
L = {w | w is a binary string s.t., the kth symbol
from its end is a 1}
29
A few subtle properties of
DFAs and NFAs
The machine never really terminates.
It is always waiting for the next input symbol or making
transitions.
The machine decides when to consume the next symbol from
the input and when to ignore it.
(but the machine can never skip a symbol)
30
FA with -Transitions
We can allow explicit -transitions in finite
automata
i.e., a transition from one state to another state
without consuming any additional input symbol
Explicit -transitions between different states
introduce non-determinism.
Makes it easier sometimes to construct NFAs
Definition: -NFAs are those NFAs with at
least one explicit -transition defined.
-NFAs have one more column in their
transition table 31
Example of an -NFA
L = {w | w is empty, or if non-empty will end in 01}
0,1
0 1
-closure of a state q,
q0 q1 q2 ECLOSE(q), is the set
of all states (including
start q’0 itself) that can be
reached from q by
δE 0 1
repeatedly making an
*q’0 Ø Ø {q’0,q0}
ECLOSE(q’0) arbitrary number of -
q0 {q0,q1} {q0} {q0} ECLOSE(q0) transitions.
ECLOSE(q1)
q1 Ø {q2} {q1}
*q2 Ø Ø {q2} ECLOSE(q2) 32
To simulate any transition:
Step 1) Go to all immediate destination states.
Step 2) From there go to all their -closure states as well.
Example of an -NFA
L = {w | w is empty, or if non-empty will end in 01}
0,1
0 1
Simulate for w=101:
q0 q1 q2
q0’
start q’0
q0’ q0
1 1
δE 0 1 Ø q0
x 0
ECLOSE(q’0)
*q’0 Ø Ø {q’0,q0} q1
q0 {q0,q1} {q0} {q0} ECLOSE(q0) 1
q2
q1 Ø {q2} {q1}
*q2 Ø Ø {q2}
33
To simulate any transition:
Step 1) Go to all immediate destination states.
Step 2) From there go to all their -closure states as well.
start q’0 q3
δE 0 1
*q’0 Ø Ø {q’0,q0,q3}
q0 {q0,q1} {q0} {q0,q3}
q1 Ø {q2} {q1}
*q2 Ø Ø {q2}
q3 Ø {q2} {q3}
34
Equivalency of DFA, NFA, -NFA
Theorem: A language L is accepted by
some -NFA if and only if L is accepted by
some DFA
Implication:
DFA ≡ NFA ≡ -NFA
(all accept Regular Languages)
35
Eliminating -transitions
Let E = {QE,∑,δE,q0,FE} be an -NFA
Goal: To build DFA D={QD,∑,δD,{qD},FD} s.t. L(D)=L(E)
Construction:
1. QD= all reachable subsets of QE factoring in -closures
2. qD = ECLOSE(q0)
3. FD=subsets S in QD s.t. S∩FE≠Φ
4. δD: for each subset S of QE and for each input symbol a∑:
Let R= U δE(p,a) // go to destination states
p in s
δD(S,a) = U ECLOSE(r) // from there, take a union
r in R of all their -closures
Reading: Section 2.5.5 in book 36
Example: -NFA DFA
L = {w | w is empty, or if non-empty will end in 01}
0,1
0 1
q0 q1 q2
start q’0
δE 0 1 δD 0 1
*q’0 Ø Ø {q’0,q0} *{q’0,q0}
q0 {q0,q1} {q0} {q0} …
q1 Ø {q2} {q1}
*q2 Ø Ø {q2}
37
Example: -NFA DFA
L = {w | w is empty, or if non-empty will end in 01}
0,1 0
0
0 1 {q0,q1}
q0 q1 q2 0 1 {q0,q2}
0
1
start start {q’0, q0} 1
q’0 q0 1
ECLOSE union
δE 0 1 δD 0 1
*q’0 Ø Ø {q’0,q0} *{q’0,q0} {q0,q1} {q0}
q0 {q0,q1} {q0} {q0} {q0,q1} {q0,q1} {q0,q2}
q1 Ø {q2} {q1} {q0} {q0,q1} {q0}
*q2 Ø Ø {q2} *{q0,q2} {q0,q1} {q0}
38
Summary
DFA
Definition
Regular language
NFA
Definition