3 Fa
3 Fa
AUTOMATA
1. FINITE AUTOMATA
2. PUSHDOWN AUTOMATA
3. LINEAR BOUND AUTOMATA
4. TURING MACHINE
Finite Automata
• FA is a simple and special case of generalized automata.
This type of automata is characterized by having no
memory storage.
Finite
Control
5
TYPES OF FINITE AUTOMATA
There are two type of Finite Automata:
TYPES OF FINITE AUTOMATA
There are two type of Finite Automata:
8
FINITE AUTOMATA
Some Applications
Software for designing and checking the
behavior of digital circuits
Lexical analyzer of a typical compiler
Software for scanning large bodies of text
(e.g., web pages) for pattern finding
Software for verifying systems of all types
that have a finite number of states (e.g.,
stock market transaction,
communication/network protocol)
9
FINITE AUTOMATA : EXAMPLES
On/Off switch action
state
A DFA is a five-tuple:
M = (Q, Σ, δ, q0, F)
Q A finite set of states
Σ A finite input alphabet
q0 The initial/starting state, q0 is in Q
F A set of final/accepting states, which is a subset of Q
δ A transition function, which is a total function from Q x Σ to Q
11
WHAT DOES A DFA DO ON READING
AN INPUT STRING?
Input: a word w in ∑*
Question: Is w acceptable by the DFA?
Steps:
Startat the “start state” q0
For every input symbol in the sequence w
do
Compute the next state from the current state,
given the current input symbol in w and the
transition function
Ifafter all symbols in w are consumed, the
current state is one of the accepting
states (F) then accept w;
Otherwise, reject w.
12
REGULAR LANGUAGES
Let L(A) be a language recognized by a DFA
A.
Then L(A) is called a “Regular Language”.
13
THE CHOMSKY HIERACHY
Regular Context-
(DFA) Context-
free Recursively-
sensitive
(PDA) enumerable
(LBA)
(TM)
14
TRANSITION DIAGRAM (TRANSITION
GRAPH)
following things:
Columns correspond to input symbols.
Rows correspond to states.
Entries correspond to the next state.
The start state is denoted by an arrow with no
source.
The accept state is denoted by a star
TRANSITION TABLE
example #1:
1
Q = {q0, q1}
0
Σ = {0, 1} q1 1
q0
Start state is q0
0
F = {q0}
δ:
0 1
q0 q1 q0
q1 q0 q1
19
example #2:
a a a/b/c
Q = {q0, q1, q2}
Σ = {a, b, c} c c
q0 q1 q2
Start state is q0
F = {q2} b b
δ: a b c
q0 q0 q0 q1
q1 q1 q1 q2
q2 q2 q2 q2
21
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
• What if the language allows
states
q1 q1 q2
empty strings? *q2 q2 q2
22
EXTENSION OF Δ TO STRINGS
δ* : (Q x Σ*) –> Q
Formally:
1) δ*(q, ε) = q, and
2) For all w in Σ* and a in Σ
δ* (q,wa) = δ (δ*(q,w), a)
23
Recall Example #1: 1
0
q0 q1 1
0
What is δ* (q0, 011)? Informally, it is the state entered by M
after processing 011 having started in state q0.
Formally:
Therefore:
25
Example #3:
1 1 1
0
0
q0 q1 q2
0
What is δ(q0, 011)? Informally, it is the state entered by M
after processing 011 having started in state q0.
Formally:
δ(q0, 011) = δ (δ(q0,01), 1) by rule #2
= δ (δ (δ(q0,0), 1), 1) by rule #2
= δ (δ (q1, 1), 1) by definition of δ
= δ (q1, 1) by definition of δ
= q1 by definition of δ
27
LANGUAGE OF A DFA
A DFA A accepts string w if there is a path
from q0 to an accepting (or final) state that is
labeled by w
28
DEFINITIONS RELATED TO DFAS
Let M = (Q, Σ, δ,q0,F) be a DFA and let w be in Σ*. Then w is
accepted by M iff δ(q0,w) = p for some state p in F.
30
Give a DFA M such that:
0/1
0/1 0/1
q0 q1 q2
31
Give a DFA M such that:
b/c a/b/c
a
a
q0 q1 q2
b/c
Logic:
In Start state (q0): b’s and c’s: ignore – stay in same state
q0 is also “accept” state
First ‘a’ appears: get ready (q1) to reject
But followed by a ‘b’ or ‘c’: go back to start state q0
When second ‘a’ appears after the “ready” state: go to reject
state q2
Ignore everything after getting to the “reject” state q2
32
Give a DFA M such that:
b/c a a/b/c
a a
b
q0 q1 q2 q3
c
b/c
q0 a b q7
b
b a
b a
q4 q5 q6
b
0/1
q0 q0 q1
35
Problem: Third symbol from last is 1
0/1
1 0/1 0/1
q0 q1 q2 q3
Is this a DFA?
36
EXAMPLES
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}
Build a DFA for the following
language:
L = { w | w is a bit string which
contains the substring 11}
37
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
38
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)
q ==> a start state
0
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, δ }
39
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.
40
Regular expression: (0+1)*01(0+1)*
41
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.
WHAT IS AN “ERROR STATE”?
A DFA for recognizing the key word “while”
w h i l e
q0 q1 q2 q3 q4 q5
w h i l e
q0 q1 q2 q3 q4 q5
Other examples
Keyword recognizer (e.g., if, then, else, while, for,
include, etc.)
Strings where the first symbol is present
somewhere later on at least once
43
EXTENSION OF Δ TO NFA PATHS
Basis: δ (q,) = {q}
Induction:
Let δ (q0,w) = {p1,p2…,pk}
δ (p ,a) = S for i=1,2...,k
i i
Then, δ (q0,wa) = S1 U S2 U … U Sk
44
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 | δ(q ,w) ∩ F ≠ Φ }
0
45
But, DFAs and NFAs are equivalent in their power to capture langauges !!
DIFFERENCES: DFA VS. NFA
DFA NFA
1. All transitions are 1. Some transitions could be
deterministic non-deterministic
A transition could lead to a
Each transition leads to
exactly one state subset of states
2. For each state, transition 2. Not all symbol transitions
on all possible symbols need to be defined
explicitly (if undefined will
(alphabet) should be go to an error state – this is
defined just a design convenience,
3. Accepts input if the last not to be confused with
state visited is in F “non-determinism”)
4. Sometimes harder to 3. Accepts input if one of the
construct because of the last states is in F
number of states 4. Generally easier than a
5. Practical implementation DFA to construct
is feasible 5. Practical implementations
limited but emerging (e.g.,
Micron automata
processor)
48
EQUIVALENCE OF DFA & NFA
Theorem:
A language L is accepted by a DFA if
Should be and only if it is accepted by an NFA.
true
L
Proof:
for any
1. If part:
Prove by showing every NFA can be converted
to an equivalent DFA (in the next few slides…)
49
PROOF FOR THE IF-PART
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 convert an NFA into a DFA?
Observation:In an NFA, each transition
maps to a subset of states
Idea:Represent:
each “subset of NFA_states” a single
“DFA_state”
Subset construction
50
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 δN(p,a)
p in s
52
NFA TO DFA CONSTRUCTION:
EXAMPLE Idea: To avoid enumerating all of
power set, do
“lazy creation of states”
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
δ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}
53
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)
54
CORRECTNESS OF SUBSET
CONSTRUCTION
Theorem: If D is the DFA constructed from NFA
N by subset construction, then L(D)=L(N)
Proof:
Show that δD({q0},w) ≡ δN(q0,w} , for all w
Using induction on w’s length:
Let w = xa
δ ({q },xa) ≡ δ ( δ (q ,x}, a ) ≡ δ (q ,w}
D 0 D N 0 N 0
55
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}
56
APPLICATIONS
Text indexing
inverted indexing
For each unique word in the database,
store all locations that contain it using an
NFA or a DFA
Find pattern P in text T
Example: Google querying
Extensions of this idea:
PATRICIA tree, suffix tree
57
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)
=> A transition can happen even without really
consuming an input symbol (think of consuming as a
free token) – if this happens, then it becomes an -NFA
(see next few slides).
A single transition cannot consume more than one
(non-) symbol.
58
FA WITH -TRANSITIONS
Wecan 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
transition table
59
EXAMPLE OF AN -NFA
EXAMPLE OF AN -NFA
δE 0 1
*q’0 Ø Ø {q’0,q0,q3}
q0 {q0,q1} {q0} {q0,q3}
q1 Ø {q2} {q1}
*q2 Ø Ø {q2}
q3 Ø {q2} {q3}
62
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)
63
ELIMINATING -TRANSITIONS
Let N = {Q,∑,δ,q0,F} be an -NFA
Goal: To build NFA without move. i.e.
D={QD,∑,δD,{qD},FD}.
Construction:
1. Find the -closures for each state.
2. Make p as accepting state of D iff, -closures(p)
containing an accepting state of N.
3. Determine δD: for each state S of Q and for each
input symbol a∑:
δD(S,a) = - closure(δ*,a).
64
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
68