SlideShare a Scribd company logo
Finite-State Machines
Michael T. Goodrich
University of California, Irvine
Some of the slides below are adapted from slides for CS154 at Stanford University
2
Introduction
 To understand how to match regular
expressions we first need to understand
finite-state machines, which are are finite
collections of states with transition rules that
take you from one state to another.
 Today, several kinds of software can be
modeled by finite automata.
3
Representing Finite-State Machines
 The simplest representation is often a graph.
 Nodes = states.
 Directed arcs indicate state transitions.
 Labels on arcs tell what causes the transition.
 We define the size of a finite-state machine to be
the total number of nodes and edges in its graph
representation.
A Finite-State Machine Example
 Congestion control in TCP Reno:
5
Finite Automata
 A finite automaton is a finite-state machine where
transitions are defined by reading characters in a
text string left-to-right.
Image from https://er.yuvayana.org/how-dfa-operates-theory-with-block-diagram-of-fa/
^
6
Example: Recognizing Strings Ending
in “ing”
Saw i
i
Not i
Saw ing
g
i
Not i or g
i
Saw in
n
Not i or n
nothing
Start
i
Not i
A final state accepts the input string and
is indicated with a boundary double circle.
7
Automata to Code (by hand)
• In C/C++/Java:
1. Initialize state q to start state.
2. Loop through the string one character at a
time.
3. Make a switch statement with a case for each
state for q, where each case sets q according
to the transitions for that state.
4. Accept if you end in a final state.
8
Example in Java
Scanner scan = new Scanner(System.in);
String s = scan.next();
int q = 0;
for (char c : s.toCharArray()) {
switch (q) {
case 0: q = (c=='i')? 1 : 0; break;
case 1: q = (c=='n')? 2 : ((c=='i')? 1 : 0); break;
case 2: q = (c=='g')? 3 : ((c=='i')? 1 : 0); break;
case 3: q = (c=='i')? 1 : 0;
}
}
if (q==3)
System.out.println("accept.");
else
System.out.println("reject.");
0 1 2 3
Start state
Final state
Loop through string s
Transitions
9
Automata to Code – General
 It would be nice to have an automatic way to
generate such code or test for a match of a
regular expression to an input string…
 Example: How grep works. This is where finite
automata come in…
Image from https://linuxconfig.org/how-to-correctly-grep-for-text-in-bash-scripts
10
Another Finite Automaton Example:
Matching An Even Number of 1’s
even odd
1
0
Start
0
1
• How would it look to accept a number
of 1’s that is a multiple of 3?
Password/Keyword Example
11
This is sometimes
called a dead state.
BTW, there is a potential security risk on the password application if
this finite automaton reports failure too quickly.
any character
any character
12
Exactly Two a’s
13
At Least Two b’s
14
Exactly two a’s and at least two b’s
Containing a Substrings or Not
• Contains baba:
• Does not contain baba:
15
General Comments
 Some things are easy with finite automata:
 Substrings (…abcabc…)
 Subsequences (…a…b…c…b…a…)
 Modular counting (odd number of 1’s)
 Some things are impossible with finite automata:
 An equal number of a’s and b’s
 More 0’s than 1’s
 But when they can be used, they are generally
fast.
16
17
Deterministic Finite Automata (DFA)
 A formalism for defining languages,
consisting of:
1. A finite set of states (Q, typically).
2. An input alphabet (Σ, typically).
3. A transition function (δ, typically).
4. A start state (q0, in Q, typically).
5. A set of final states (F ⊆ Q, typically).
 “Final” and “accepting” are synonyms.
18
The Transition Function
 Takes two arguments: a state and an input
symbol.
 δ(q, a) = the state that the DFA goes to when it is
in state q and input a is received.
0 1
A A B
B A C
C C C
Rows = states
Columns =
input symbols
Final states
starred *
*
*
Arrow for
start state
19
Graph Representation of DFA’s
 Nodes = states.
 Arcs represent transition function.
 Arc from state p to state q labeled by all those input
symbols that have transitions from p to q.
 Arrow labeled “Start” to the start state.
 Final states indicated by double circles.
 Recall that the size of a DFA is the total number
of nodes and edges in its graph representation.
Note that this is also proportional to the size of
the transition table representation.
20
Example: Graph of a DFA
Start
1
0
A C
B
1
0 0,1
Previous
string OK,
does not
end in 1.
Previous
String OK,
ends in a
single 1.
Consecutive
1’s have
been seen.
Accepts all strings without two consecutive 1’s.
21
Language of a DFA
 Automata of all kinds define languages.
 If A is an automaton, L(A) is its language.
 For a DFA, A, L(A) is the set of strings
labeling paths from the start state to a final
state.
22
Example: String in a Language
Start
1
0
A C
B
1
0 0,1
String 101 is in the language of the DFA below.
Start at A.
23
Example: String in a Language
Start
1
0
A C
B
1
0 0,1
Follow arc labeled 1.
String 101 is in the language of the DFA below.
24
Example: String in a Language
Start
1
0
A C
B
1
0 0,1
Then arc labeled 0 from current state B.
String 101 is in the language of the DFA below.
25
Example: String in a Language
Start
1
0
A C
B
1
0 0,1
Finally arc labeled 1 from current state A. Result
is an accepting state, so 101 is in the language.
String 101 is in the language of the DFA below.
26
Example – Concluded
 The language of our example DFA is:
{w | w is in {0,1}* and w does not have
two consecutive 1’s}
Read a set former as
“The set of strings w…
Such that… These conditions
about w are true.
DFA Matching is Fast
 Given a DFA, A, of size, m, and an input string, T, of
length n, we can test if T is in L(A) in O(m + n) time:
1. Read in the graph representation for A.
 Step 1 takes O(m) time.
2. Let s = the initial state for A.
3. For each character, c, in T (in order): Let s = d(s,c).
 Step 3 takes O(n) time in total.
4. If s is a final state, accept T.
28
Nondeterminism
 A nondeterministic finite automaton has the
ability to be in several states at once.
 Transitions from a state on an input symbol can
be to any set of states.
 Start in one start state.
 Accept if any sequence of choices leads to a
final state.
 Intuitively: the NFA always “guesses right.”
29
Example: Moves on a Chessboard
• States = squares.
• Inputs = r (move to an adjacent red square) and
b (move to an adjacent black square).
• Start state, final state are in opposite corners.
1 2
5
7 9
3
4
8
6
30
Example: Chessboard
1 2
5
7 9
3
4
8
6
1
r b b
4
2 1
5
3
7
5
1
3
9
7
r b
1 2,4 5
2 4,6 1,3,5
3 2,6 5
4 2,8 1,5,7
5 2,4,6,8 1,3,7,9
6 2,8 3,5,9
7 4,8 5
8 4,6 5,7,9
9 6,8 5
*
Accept, since final state reached
31
Formal Nondeterministic Finite
Automaton (NFA) Definition
 A finite set of states, typically Q.
 An input alphabet, typically Σ.
 A transition function, typically δ.
 δ(q, a) is a set of states.
 Extend to strings as follows:
 Basis: δ(q, ε) = {q}
 Induction: δ(q, wa) = the union over all states p in
δ(q, w) of δ(p, a)
 A start state in Q, typically q0.
 A set of final states F ⊆ Q.
32
NFA Properties
 A string w is accepted by an NFA if δ(q0, w)
contains at least one final state.
 That is, there exists a sequence of valid
transitions from q0 to a final state given the input w.
 The language of the NFA is the set of strings it
accepts.
 As with a DFA, we say that the size of an NFA is
the total number of nodes and edges in its graph
representation. Note that this is also proportional to
the size of the NFA’s transition table representation
(where we store each δ(q, w) entry as a list).
Example NFA
 Set of all strings with two consecutive a’s or
two consecutive b’s:
 Note that some states have an empty
transition on an a or b, and some have
multiple transitions on a or b. 33
NFA Simulation
 Given an NFA, A, of size, m, and an input string, T, of
length n, we can test if T is in L(A) in O(mn) time:
1. Read in the the graph representation for A.
 Step 1 takes O(m) time.
2. Let S = {q}, where q is the initial state for A.
3. For each character, c, in T (in order):
 Let S be the union of d(q,c), for each q in S.
 Note: we can compute S in O(m) time, since the total size
of all the d(q,c) sets is at most O(m) even counting
duplicates.
4. If S contains a final state, accept T.
35
Every DFA can be Converted to an
Equivalent NFA
 A DFA can easily be turned into an NFA that
accepts the same language.
 If δD(q, a) = p, let the NFA have δN(q, a) = {p}.
 Then the NFA is always in a state set
containing exactly one DFA state – the state
the DFA is in after reading the same input.
36
Every NFA can be Converted to an
Equivalent DFA
 Surprisingly, for any NFA there is a DFA that
accepts the same language.
 The proof is called the subset construction.
 It was defined by Michael Rabin and Dana Scott.
 The drawback is that the number of states of the
DFA can be exponential in the number of states
of the NFA.
Michael Rabin Dana Scott
 1959: They jointly published the subset construction
and introduced the nondeterminism concept.
 1976: They jointly received the Turing Award
 The “Nobel Prize” of Computer Science
Images from https://en.wikipedia.org/wiki/Michael_O._Rabin, https://en.wikipedia.org/wiki/Dana_Scott
38
Subset Construction
 Given an NFA with states Q, inputs Σ,
transition function δN, state state q0, and
final states F, construct equivalent DFA
with:
 States 2Q (Set of subsets of Q).
 Inputs Σ.
 Start state {q0}.
 Final states = all those with a member of F.
39
Critical Point
• The DFA states have names that are sets of
NFA states.
• So as a DFA state, an expression like {p,q}
must be read as a single symbol, not as a set.
• Analogy: a class of objects whose values are
sets of objects of another class.
40
Subset Construction – (2)
 The transition function δD is defined by:
δD({q1,…,qk}, a) is the union over all i = 1,…,k of
δN(qi, a).
 Example: We’ll construct the DFA equivalent of
our “chessboard” NFA.
41
Example: Subset Construction
r b
1 2,4 5
2 4,6 1,3,5
3 2,6 5
4 2,8 1,5,7
5 2,4,6,8 1,3,7,9
6 2,8 3,5,9
7 4,8 5
8 4,6 5,7,9
9 6,8 5
*
r b
{1} {2,4} {5}
{2,4}
{5}
Alert: What we’re doing here is
the lazy form of DFA construction,
where we only construct a state
if we are forced to.
42
Example: Subset Construction
r b
1 2,4 5
2 4,6 1,3,5
3 2,6 5
4 2,8 1,5,7
5 2,4,6,8 1,3,7,9
6 2,8 3,5,9
7 4,8 5
8 4,6 5,7,9
9 6,8 5
*
r b
{1}
{2,4,6,8}
{5}
{2,4} {2,4,6,8} {1,3,5,7}
{1,3,5,7}
{2,4} {5}
43
Example: Subset Construction
r b
1 2,4 5
2 4,6 1,3,5
3 2,6 5
4 2,8 1,5,7
5 2,4,6,8 1,3,7,9
6 2,8 3,5,9
7 4,8 5
8 4,6 5,7,9
9 6,8 5
*
r b
{1}
* {1,3,7,9}
{2,4,6,8}
{2,4,6,8} {1,3,7,9}
{5}
{2,4} {2,4,6,8} {1,3,5,7}
{1,3,5,7}
{2,4} {5}
44
Example: Subset Construction
r b
1 2,4 5
2 4,6 1,3,5
3 2,6 5
4 2,8 1,5,7
5 2,4,6,8 1,3,7,9
6 2,8 3,5,9
7 4,8 5
8 4,6 5,7,9
9 6,8 5
*
r b
{1}
* {1,3,5,7,9}
* {1,3,7,9}
{2,4,6,8} {1,3,5,7,9}
{2,4,6,8}
{2,4,6,8} {1,3,7,9}
{5}
{2,4} {2,4,6,8} {1,3,5,7}
{1,3,5,7}
{2,4} {5}
45
Example: Subset Construction
r b
1 2,4 5
2 4,6 1,3,5
3 2,6 5
4 2,8 1,5,7
5 2,4,6,8 1,3,7,9
6 2,8 3,5,9
7 4,8 5
8 4,6 5,7,9
9 6,8 5
*
r b
{1}
* {1,3,5,7,9}
* {1,3,7,9}
{2,4,6,8} {1,3,5,7,9}
{2,4,6,8}
{2,4,6,8} {1,3,7,9}
{5}
{2,4} {2,4,6,8} {1,3,5,7}
{1,3,5,7}
{2,4} {5}
{2,4,6,8} {1,3,5,7,9}
46
Example: Subset Construction
r b
1 2,4 5
2 4,6 1,3,5
3 2,6 5
4 2,8 1,5,7
5 2,4,6,8 1,3,7,9
6 2,8 3,5,9
7 4,8 5
8 4,6 5,7,9
9 6,8 5
*
r b
{1}
* {1,3,5,7,9}
* {1,3,7,9} {2,4,6,8} {5}
{2,4,6,8} {1,3,5,7,9}
{2,4,6,8}
{2,4,6,8} {1,3,7,9}
{5}
{2,4} {2,4,6,8} {1,3,5,7}
{1,3,5,7}
{2,4} {5}
{2,4,6,8} {1,3,5,7,9}
47
Example: Subset Construction
r b
1 2,4 5
2 4,6 1,3,5
3 2,6 5
4 2,8 1,5,7
5 2,4,6,8 1,3,7,9
6 2,8 3,5,9
7 4,8 5
8 4,6 5,7,9
9 6,8 5
*
r b
{1}
* {1,3,5,7,9} {2,4,6,8} {1,3,5,7,9}
* {1,3,7,9} {2,4,6,8} {5}
{2,4,6,8} {1,3,5,7,9}
{2,4,6,8}
{2,4,6,8} {1,3,7,9}
{5}
{2,4} {2,4,6,8} {1,3,5,7}
{1,3,5,7}
{2,4} {5}
{2,4,6,8} {1,3,5,7,9}
48
NFA’s With ε-Transitions
 We can allow state-to-state transitions on ε (null)
input.
 These transitions are done spontaneously,
without looking at the input string.
 A convenience at times, but still only regular
languages are accepted.
49
Example: ε-NFA
C
E F
A
B D
1
1 1
0
0
0
ε
ε ε
0 1 ε
A {E} {B} ∅
B ∅ {C} {D}
C ∅ {D} ∅
D ∅ ∅ ∅
E {F} ∅ {B, C}
F {D} ∅ ∅
*
50
Closure of States
 CL(q) = set of states you can reach from state
q following only arcs labeled ε.
 Example: CL(A) = {A};
CL(E) = {B, C, D, E}.
 Closure of a set of states = union of the
closure of each state.
 Can be computed by doing a depth-first
search that just follows ε-transitions.
C
E F
A
B D
1
1 1
0
0
0
ε
ε ε
51
Equivalence of NFA and ε-NFA
 Every NFA is an ε-NFA.
 It just has no transitions on ε.
 The converse requires us to take an ε-NFA with
m states and construct an NFA that accepts the
same language (also with m states).
 We do so by combining ε–transitions with the
next transition on a real input.
52
Picture of ε-Transition Removal
Transitions
on ε
a
a
a
Transitions
on ε
53
Equivalence – (2)
 Start with an ε-NFA with states Q, inputs Σ, start
state q0, final states F, and transition function δE.
 Construct an “ordinary” NFA with states Q, inputs
Σ, start state q0, final states F’, and transition
function δN.
 If the ε-NFA has size m, then the resulting
equivalent NFA will have size O(m2) in the worst
case.
54
Compute δN as follows:
1. For each q in Q, let Sq = CL(q).
 Step 1 takes O(m2) time (by doing a depth-first search
from each q that just follows ε-transitions).
2. Let δN(q, a) be the union over all p in Sq of δE(p, a)
for which δE(p, a) is defined.
 Step 2 takes O(m) time for each state q, since the
total size of all the δE(p, a) sets is O(m).
 So, the total running time for ε-NFA-to-NFA
conversion is O(m2). The resulting NFA could
have size O(m2), since it will have the same
number of nodes, but could have more edges.
 F’ = set of states q such that CL(q) contains a
state of F.
55
Example: ε-NFA-to-NFA
0 1 ε
A {E} {B} ∅
B ∅ {C} {D}
C ∅ {D} ∅
D ∅ ∅ ∅
E {F} ∅ {B, C}
F {D} ∅ ∅
*
ε-NFA
0 1
A {E} {B}
B ∅ {C}
C ∅ {D}
D ∅ ∅
E {F} {C, D}
F {D} ∅
*
*
*
Since closure of
E includes B and
C; which have
transitions on 1
to C and D.
Since closures of
B and E include
final state D.
Interesting
closures:
CL(B) = {B,D};
CL(E) = {B,C,D,E}
NFA
δN
δE
ε-NFA Matching via DFA Conversion
can be Slow for Big Finite Automata
 Given an ε-NFA, A, of size m, and an input string, T,
of length n, we can test if T is in L(A) in O(2m + n)
time:
1. Convert A into an equivalent DFA, B, using the above
ε-NFA to NFA to DFA constructions.
 Step 1 takes O(2m) time in the worst case and creates a
DFA of size O(2m) in the worst case.
2. Test if T is in L(B) using the DFA simulation algorithm.
 Step 2 takes O(2m + n) time.
There is an Alternative Approach
 Instead of converting the ε-NFA to a DFA, and
then matching using the DFA, simulate the ε-NFA
directly.
Image from http://algs4.cs.princeton.edu
ε-NFA Simulation
 Given an ε-NFA, A, of size, m, and an input string, T,
of length n, we can test if T is in L(A) in O(mn) time:
1. Read in the graph representation for A.
 Step 1 takes O(m) time.
2. Let S = {q}, where q is the initial state for A.
3. For each character, c, in T (in order):
 Let S = CL(S). This can be done in O(m) time by a DFS
that only follows ε-transitions.
 Let S be the union of d(q,c), for each q in S. This takes
O(m) time, since the size of all the d(q,c) sets is O(m).
4. If S contains a final state, accept T.
59
Summary
 DFA’s, NFA’s, and ε–NFA’s all accept exactly the
same set of languages: the regular languages.
 The NFA types are easier to design and may
have exponentially fewer states than a DFA.
 But only a DFA can be implemented in linear
time, that is, O(m + n) time.
 Implementing an ε-NFA by the conversion to DFA
method takes O(2m + n) time.
 Implementing an ε-NFA by the ε-NFA simulation
method takes O(mn) time.

More Related Content

Similar to 03-FiniteAutomata.pptx

AUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTESAUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTES
suthi
 
flat unit1
flat unit1flat unit1
flat unit1
Janhavi Vishwanath
 
Nondeterministic Finite Automata AFN.pdf
Nondeterministic Finite Automata AFN.pdfNondeterministic Finite Automata AFN.pdf
Nondeterministic Finite Automata AFN.pdf
SergioUlisesRojasAla
 
2_4 Finite Automata.ppt
2_4 Finite Automata.ppt2_4 Finite Automata.ppt
2_4 Finite Automata.ppt
Ratnakar Mikkili
 
Automata theory introduction
Automata theory introductionAutomata theory introduction
Automata theory introduction
NAMRATA BORKAR
 
5. NFA & DFA.pdf
5. NFA & DFA.pdf5. NFA & DFA.pdf
5. NFA & DFA.pdf
TANZINTANZINA
 
Mba ebooks ! Edhole
Mba ebooks ! EdholeMba ebooks ! Edhole
Mba ebooks ! Edhole
Edhole.com
 
Free Ebooks Download ! Edhole
Free Ebooks Download ! EdholeFree Ebooks Download ! Edhole
Free Ebooks Download ! Edhole
Edhole.com
 
Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1
Srimatre K
 
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Mohammad Ilyas Malik
 
Winter 9 Decidability.pptx
Winter 9 Decidability.pptxWinter 9 Decidability.pptx
Winter 9 Decidability.pptx
HarisPrince
 
Nondeterministic Finite Automat
Nondeterministic Finite AutomatNondeterministic Finite Automat
Nondeterministic Finite AutomatAdel Al-Ofairi
 
CS 5th.pptx
CS 5th.pptxCS 5th.pptx
CS 5th.pptx
MadniFareed1
 
deterministicfiniteautomatondfa-181008145215 (1).pdf
deterministicfiniteautomatondfa-181008145215 (1).pdfdeterministicfiniteautomatondfa-181008145215 (1).pdf
deterministicfiniteautomatondfa-181008145215 (1).pdf
AmayJaiswal4
 
TOC Introduction
TOC Introduction TOC Introduction
TOC Introduction
Thapar Institute
 
Automata based programming
Automata based programmingAutomata based programming
Automata based programming
VisnuDharsini
 
FiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxFiniteAutomata_anim.pptx
FiniteAutomata_anim.pptx
Meghnadh
 
Automata
AutomataAutomata
Automata
Gaditek
 
Automata
AutomataAutomata
Automata
Gaditek
 
Theory of automata
Theory of automataTheory of automata
Theory of automata
Arslan905905
 

Similar to 03-FiniteAutomata.pptx (20)

AUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTESAUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTES
 
flat unit1
flat unit1flat unit1
flat unit1
 
Nondeterministic Finite Automata AFN.pdf
Nondeterministic Finite Automata AFN.pdfNondeterministic Finite Automata AFN.pdf
Nondeterministic Finite Automata AFN.pdf
 
2_4 Finite Automata.ppt
2_4 Finite Automata.ppt2_4 Finite Automata.ppt
2_4 Finite Automata.ppt
 
Automata theory introduction
Automata theory introductionAutomata theory introduction
Automata theory introduction
 
5. NFA & DFA.pdf
5. NFA & DFA.pdf5. NFA & DFA.pdf
5. NFA & DFA.pdf
 
Mba ebooks ! Edhole
Mba ebooks ! EdholeMba ebooks ! Edhole
Mba ebooks ! Edhole
 
Free Ebooks Download ! Edhole
Free Ebooks Download ! EdholeFree Ebooks Download ! Edhole
Free Ebooks Download ! Edhole
 
Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1
 
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
 
Winter 9 Decidability.pptx
Winter 9 Decidability.pptxWinter 9 Decidability.pptx
Winter 9 Decidability.pptx
 
Nondeterministic Finite Automat
Nondeterministic Finite AutomatNondeterministic Finite Automat
Nondeterministic Finite Automat
 
CS 5th.pptx
CS 5th.pptxCS 5th.pptx
CS 5th.pptx
 
deterministicfiniteautomatondfa-181008145215 (1).pdf
deterministicfiniteautomatondfa-181008145215 (1).pdfdeterministicfiniteautomatondfa-181008145215 (1).pdf
deterministicfiniteautomatondfa-181008145215 (1).pdf
 
TOC Introduction
TOC Introduction TOC Introduction
TOC Introduction
 
Automata based programming
Automata based programmingAutomata based programming
Automata based programming
 
FiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxFiniteAutomata_anim.pptx
FiniteAutomata_anim.pptx
 
Automata
AutomataAutomata
Automata
 
Automata
AutomataAutomata
Automata
 
Theory of automata
Theory of automataTheory of automata
Theory of automata
 

More from ssuser47f7f2

rs1.ppt
rs1.pptrs1.ppt
rs1.ppt
ssuser47f7f2
 
Lecture7x.ppt
Lecture7x.pptLecture7x.ppt
Lecture7x.ppt
ssuser47f7f2
 
NORMAL-FORMS.ppt
NORMAL-FORMS.pptNORMAL-FORMS.ppt
NORMAL-FORMS.ppt
ssuser47f7f2
 
functions (1).pptx
functions (1).pptxfunctions (1).pptx
functions (1).pptx
ssuser47f7f2
 
09.LearningMaterial_Sample.pdf
09.LearningMaterial_Sample.pdf09.LearningMaterial_Sample.pdf
09.LearningMaterial_Sample.pdf
ssuser47f7f2
 
Module 2_Conditional Statements.pptx
Module 2_Conditional Statements.pptxModule 2_Conditional Statements.pptx
Module 2_Conditional Statements.pptx
ssuser47f7f2
 
15159222.ppt
15159222.ppt15159222.ppt
15159222.ppt
ssuser47f7f2
 
FiniteAutomata (1).ppt
FiniteAutomata (1).pptFiniteAutomata (1).ppt
FiniteAutomata (1).ppt
ssuser47f7f2
 
Module 1-System Software PROGRAMS.pptx
Module 1-System Software PROGRAMS.pptxModule 1-System Software PROGRAMS.pptx
Module 1-System Software PROGRAMS.pptx
ssuser47f7f2
 
module1 network security.pdf
module1 network security.pdfmodule1 network security.pdf
module1 network security.pdf
ssuser47f7f2
 

More from ssuser47f7f2 (10)

rs1.ppt
rs1.pptrs1.ppt
rs1.ppt
 
Lecture7x.ppt
Lecture7x.pptLecture7x.ppt
Lecture7x.ppt
 
NORMAL-FORMS.ppt
NORMAL-FORMS.pptNORMAL-FORMS.ppt
NORMAL-FORMS.ppt
 
functions (1).pptx
functions (1).pptxfunctions (1).pptx
functions (1).pptx
 
09.LearningMaterial_Sample.pdf
09.LearningMaterial_Sample.pdf09.LearningMaterial_Sample.pdf
09.LearningMaterial_Sample.pdf
 
Module 2_Conditional Statements.pptx
Module 2_Conditional Statements.pptxModule 2_Conditional Statements.pptx
Module 2_Conditional Statements.pptx
 
15159222.ppt
15159222.ppt15159222.ppt
15159222.ppt
 
FiniteAutomata (1).ppt
FiniteAutomata (1).pptFiniteAutomata (1).ppt
FiniteAutomata (1).ppt
 
Module 1-System Software PROGRAMS.pptx
Module 1-System Software PROGRAMS.pptxModule 1-System Software PROGRAMS.pptx
Module 1-System Software PROGRAMS.pptx
 
module1 network security.pdf
module1 network security.pdfmodule1 network security.pdf
module1 network security.pdf
 

Recently uploaded

Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 

Recently uploaded (20)

Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 

03-FiniteAutomata.pptx

  • 1. Finite-State Machines Michael T. Goodrich University of California, Irvine Some of the slides below are adapted from slides for CS154 at Stanford University
  • 2. 2 Introduction  To understand how to match regular expressions we first need to understand finite-state machines, which are are finite collections of states with transition rules that take you from one state to another.  Today, several kinds of software can be modeled by finite automata.
  • 3. 3 Representing Finite-State Machines  The simplest representation is often a graph.  Nodes = states.  Directed arcs indicate state transitions.  Labels on arcs tell what causes the transition.  We define the size of a finite-state machine to be the total number of nodes and edges in its graph representation.
  • 4. A Finite-State Machine Example  Congestion control in TCP Reno:
  • 5. 5 Finite Automata  A finite automaton is a finite-state machine where transitions are defined by reading characters in a text string left-to-right. Image from https://er.yuvayana.org/how-dfa-operates-theory-with-block-diagram-of-fa/ ^
  • 6. 6 Example: Recognizing Strings Ending in “ing” Saw i i Not i Saw ing g i Not i or g i Saw in n Not i or n nothing Start i Not i A final state accepts the input string and is indicated with a boundary double circle.
  • 7. 7 Automata to Code (by hand) • In C/C++/Java: 1. Initialize state q to start state. 2. Loop through the string one character at a time. 3. Make a switch statement with a case for each state for q, where each case sets q according to the transitions for that state. 4. Accept if you end in a final state.
  • 8. 8 Example in Java Scanner scan = new Scanner(System.in); String s = scan.next(); int q = 0; for (char c : s.toCharArray()) { switch (q) { case 0: q = (c=='i')? 1 : 0; break; case 1: q = (c=='n')? 2 : ((c=='i')? 1 : 0); break; case 2: q = (c=='g')? 3 : ((c=='i')? 1 : 0); break; case 3: q = (c=='i')? 1 : 0; } } if (q==3) System.out.println("accept."); else System.out.println("reject."); 0 1 2 3 Start state Final state Loop through string s Transitions
  • 9. 9 Automata to Code – General  It would be nice to have an automatic way to generate such code or test for a match of a regular expression to an input string…  Example: How grep works. This is where finite automata come in… Image from https://linuxconfig.org/how-to-correctly-grep-for-text-in-bash-scripts
  • 10. 10 Another Finite Automaton Example: Matching An Even Number of 1’s even odd 1 0 Start 0 1 • How would it look to accept a number of 1’s that is a multiple of 3?
  • 11. Password/Keyword Example 11 This is sometimes called a dead state. BTW, there is a potential security risk on the password application if this finite automaton reports failure too quickly. any character any character
  • 14. 14 Exactly two a’s and at least two b’s
  • 15. Containing a Substrings or Not • Contains baba: • Does not contain baba: 15
  • 16. General Comments  Some things are easy with finite automata:  Substrings (…abcabc…)  Subsequences (…a…b…c…b…a…)  Modular counting (odd number of 1’s)  Some things are impossible with finite automata:  An equal number of a’s and b’s  More 0’s than 1’s  But when they can be used, they are generally fast. 16
  • 17. 17 Deterministic Finite Automata (DFA)  A formalism for defining languages, consisting of: 1. A finite set of states (Q, typically). 2. An input alphabet (Σ, typically). 3. A transition function (δ, typically). 4. A start state (q0, in Q, typically). 5. A set of final states (F ⊆ Q, typically).  “Final” and “accepting” are synonyms.
  • 18. 18 The Transition Function  Takes two arguments: a state and an input symbol.  δ(q, a) = the state that the DFA goes to when it is in state q and input a is received. 0 1 A A B B A C C C C Rows = states Columns = input symbols Final states starred * * * Arrow for start state
  • 19. 19 Graph Representation of DFA’s  Nodes = states.  Arcs represent transition function.  Arc from state p to state q labeled by all those input symbols that have transitions from p to q.  Arrow labeled “Start” to the start state.  Final states indicated by double circles.  Recall that the size of a DFA is the total number of nodes and edges in its graph representation. Note that this is also proportional to the size of the transition table representation.
  • 20. 20 Example: Graph of a DFA Start 1 0 A C B 1 0 0,1 Previous string OK, does not end in 1. Previous String OK, ends in a single 1. Consecutive 1’s have been seen. Accepts all strings without two consecutive 1’s.
  • 21. 21 Language of a DFA  Automata of all kinds define languages.  If A is an automaton, L(A) is its language.  For a DFA, A, L(A) is the set of strings labeling paths from the start state to a final state.
  • 22. 22 Example: String in a Language Start 1 0 A C B 1 0 0,1 String 101 is in the language of the DFA below. Start at A.
  • 23. 23 Example: String in a Language Start 1 0 A C B 1 0 0,1 Follow arc labeled 1. String 101 is in the language of the DFA below.
  • 24. 24 Example: String in a Language Start 1 0 A C B 1 0 0,1 Then arc labeled 0 from current state B. String 101 is in the language of the DFA below.
  • 25. 25 Example: String in a Language Start 1 0 A C B 1 0 0,1 Finally arc labeled 1 from current state A. Result is an accepting state, so 101 is in the language. String 101 is in the language of the DFA below.
  • 26. 26 Example – Concluded  The language of our example DFA is: {w | w is in {0,1}* and w does not have two consecutive 1’s} Read a set former as “The set of strings w… Such that… These conditions about w are true.
  • 27. DFA Matching is Fast  Given a DFA, A, of size, m, and an input string, T, of length n, we can test if T is in L(A) in O(m + n) time: 1. Read in the graph representation for A.  Step 1 takes O(m) time. 2. Let s = the initial state for A. 3. For each character, c, in T (in order): Let s = d(s,c).  Step 3 takes O(n) time in total. 4. If s is a final state, accept T.
  • 28. 28 Nondeterminism  A nondeterministic finite automaton has the ability to be in several states at once.  Transitions from a state on an input symbol can be to any set of states.  Start in one start state.  Accept if any sequence of choices leads to a final state.  Intuitively: the NFA always “guesses right.”
  • 29. 29 Example: Moves on a Chessboard • States = squares. • Inputs = r (move to an adjacent red square) and b (move to an adjacent black square). • Start state, final state are in opposite corners. 1 2 5 7 9 3 4 8 6
  • 30. 30 Example: Chessboard 1 2 5 7 9 3 4 8 6 1 r b b 4 2 1 5 3 7 5 1 3 9 7 r b 1 2,4 5 2 4,6 1,3,5 3 2,6 5 4 2,8 1,5,7 5 2,4,6,8 1,3,7,9 6 2,8 3,5,9 7 4,8 5 8 4,6 5,7,9 9 6,8 5 * Accept, since final state reached
  • 31. 31 Formal Nondeterministic Finite Automaton (NFA) Definition  A finite set of states, typically Q.  An input alphabet, typically Σ.  A transition function, typically δ.  δ(q, a) is a set of states.  Extend to strings as follows:  Basis: δ(q, ε) = {q}  Induction: δ(q, wa) = the union over all states p in δ(q, w) of δ(p, a)  A start state in Q, typically q0.  A set of final states F ⊆ Q.
  • 32. 32 NFA Properties  A string w is accepted by an NFA if δ(q0, w) contains at least one final state.  That is, there exists a sequence of valid transitions from q0 to a final state given the input w.  The language of the NFA is the set of strings it accepts.  As with a DFA, we say that the size of an NFA is the total number of nodes and edges in its graph representation. Note that this is also proportional to the size of the NFA’s transition table representation (where we store each δ(q, w) entry as a list).
  • 33. Example NFA  Set of all strings with two consecutive a’s or two consecutive b’s:  Note that some states have an empty transition on an a or b, and some have multiple transitions on a or b. 33
  • 34. NFA Simulation  Given an NFA, A, of size, m, and an input string, T, of length n, we can test if T is in L(A) in O(mn) time: 1. Read in the the graph representation for A.  Step 1 takes O(m) time. 2. Let S = {q}, where q is the initial state for A. 3. For each character, c, in T (in order):  Let S be the union of d(q,c), for each q in S.  Note: we can compute S in O(m) time, since the total size of all the d(q,c) sets is at most O(m) even counting duplicates. 4. If S contains a final state, accept T.
  • 35. 35 Every DFA can be Converted to an Equivalent NFA  A DFA can easily be turned into an NFA that accepts the same language.  If δD(q, a) = p, let the NFA have δN(q, a) = {p}.  Then the NFA is always in a state set containing exactly one DFA state – the state the DFA is in after reading the same input.
  • 36. 36 Every NFA can be Converted to an Equivalent DFA  Surprisingly, for any NFA there is a DFA that accepts the same language.  The proof is called the subset construction.  It was defined by Michael Rabin and Dana Scott.  The drawback is that the number of states of the DFA can be exponential in the number of states of the NFA.
  • 37. Michael Rabin Dana Scott  1959: They jointly published the subset construction and introduced the nondeterminism concept.  1976: They jointly received the Turing Award  The “Nobel Prize” of Computer Science Images from https://en.wikipedia.org/wiki/Michael_O._Rabin, https://en.wikipedia.org/wiki/Dana_Scott
  • 38. 38 Subset Construction  Given an NFA with states Q, inputs Σ, transition function δN, state state q0, and final states F, construct equivalent DFA with:  States 2Q (Set of subsets of Q).  Inputs Σ.  Start state {q0}.  Final states = all those with a member of F.
  • 39. 39 Critical Point • The DFA states have names that are sets of NFA states. • So as a DFA state, an expression like {p,q} must be read as a single symbol, not as a set. • Analogy: a class of objects whose values are sets of objects of another class.
  • 40. 40 Subset Construction – (2)  The transition function δD is defined by: δD({q1,…,qk}, a) is the union over all i = 1,…,k of δN(qi, a).  Example: We’ll construct the DFA equivalent of our “chessboard” NFA.
  • 41. 41 Example: Subset Construction r b 1 2,4 5 2 4,6 1,3,5 3 2,6 5 4 2,8 1,5,7 5 2,4,6,8 1,3,7,9 6 2,8 3,5,9 7 4,8 5 8 4,6 5,7,9 9 6,8 5 * r b {1} {2,4} {5} {2,4} {5} Alert: What we’re doing here is the lazy form of DFA construction, where we only construct a state if we are forced to.
  • 42. 42 Example: Subset Construction r b 1 2,4 5 2 4,6 1,3,5 3 2,6 5 4 2,8 1,5,7 5 2,4,6,8 1,3,7,9 6 2,8 3,5,9 7 4,8 5 8 4,6 5,7,9 9 6,8 5 * r b {1} {2,4,6,8} {5} {2,4} {2,4,6,8} {1,3,5,7} {1,3,5,7} {2,4} {5}
  • 43. 43 Example: Subset Construction r b 1 2,4 5 2 4,6 1,3,5 3 2,6 5 4 2,8 1,5,7 5 2,4,6,8 1,3,7,9 6 2,8 3,5,9 7 4,8 5 8 4,6 5,7,9 9 6,8 5 * r b {1} * {1,3,7,9} {2,4,6,8} {2,4,6,8} {1,3,7,9} {5} {2,4} {2,4,6,8} {1,3,5,7} {1,3,5,7} {2,4} {5}
  • 44. 44 Example: Subset Construction r b 1 2,4 5 2 4,6 1,3,5 3 2,6 5 4 2,8 1,5,7 5 2,4,6,8 1,3,7,9 6 2,8 3,5,9 7 4,8 5 8 4,6 5,7,9 9 6,8 5 * r b {1} * {1,3,5,7,9} * {1,3,7,9} {2,4,6,8} {1,3,5,7,9} {2,4,6,8} {2,4,6,8} {1,3,7,9} {5} {2,4} {2,4,6,8} {1,3,5,7} {1,3,5,7} {2,4} {5}
  • 45. 45 Example: Subset Construction r b 1 2,4 5 2 4,6 1,3,5 3 2,6 5 4 2,8 1,5,7 5 2,4,6,8 1,3,7,9 6 2,8 3,5,9 7 4,8 5 8 4,6 5,7,9 9 6,8 5 * r b {1} * {1,3,5,7,9} * {1,3,7,9} {2,4,6,8} {1,3,5,7,9} {2,4,6,8} {2,4,6,8} {1,3,7,9} {5} {2,4} {2,4,6,8} {1,3,5,7} {1,3,5,7} {2,4} {5} {2,4,6,8} {1,3,5,7,9}
  • 46. 46 Example: Subset Construction r b 1 2,4 5 2 4,6 1,3,5 3 2,6 5 4 2,8 1,5,7 5 2,4,6,8 1,3,7,9 6 2,8 3,5,9 7 4,8 5 8 4,6 5,7,9 9 6,8 5 * r b {1} * {1,3,5,7,9} * {1,3,7,9} {2,4,6,8} {5} {2,4,6,8} {1,3,5,7,9} {2,4,6,8} {2,4,6,8} {1,3,7,9} {5} {2,4} {2,4,6,8} {1,3,5,7} {1,3,5,7} {2,4} {5} {2,4,6,8} {1,3,5,7,9}
  • 47. 47 Example: Subset Construction r b 1 2,4 5 2 4,6 1,3,5 3 2,6 5 4 2,8 1,5,7 5 2,4,6,8 1,3,7,9 6 2,8 3,5,9 7 4,8 5 8 4,6 5,7,9 9 6,8 5 * r b {1} * {1,3,5,7,9} {2,4,6,8} {1,3,5,7,9} * {1,3,7,9} {2,4,6,8} {5} {2,4,6,8} {1,3,5,7,9} {2,4,6,8} {2,4,6,8} {1,3,7,9} {5} {2,4} {2,4,6,8} {1,3,5,7} {1,3,5,7} {2,4} {5} {2,4,6,8} {1,3,5,7,9}
  • 48. 48 NFA’s With ε-Transitions  We can allow state-to-state transitions on ε (null) input.  These transitions are done spontaneously, without looking at the input string.  A convenience at times, but still only regular languages are accepted.
  • 49. 49 Example: ε-NFA C E F A B D 1 1 1 0 0 0 ε ε ε 0 1 ε A {E} {B} ∅ B ∅ {C} {D} C ∅ {D} ∅ D ∅ ∅ ∅ E {F} ∅ {B, C} F {D} ∅ ∅ *
  • 50. 50 Closure of States  CL(q) = set of states you can reach from state q following only arcs labeled ε.  Example: CL(A) = {A}; CL(E) = {B, C, D, E}.  Closure of a set of states = union of the closure of each state.  Can be computed by doing a depth-first search that just follows ε-transitions. C E F A B D 1 1 1 0 0 0 ε ε ε
  • 51. 51 Equivalence of NFA and ε-NFA  Every NFA is an ε-NFA.  It just has no transitions on ε.  The converse requires us to take an ε-NFA with m states and construct an NFA that accepts the same language (also with m states).  We do so by combining ε–transitions with the next transition on a real input.
  • 52. 52 Picture of ε-Transition Removal Transitions on ε a a a Transitions on ε
  • 53. 53 Equivalence – (2)  Start with an ε-NFA with states Q, inputs Σ, start state q0, final states F, and transition function δE.  Construct an “ordinary” NFA with states Q, inputs Σ, start state q0, final states F’, and transition function δN.  If the ε-NFA has size m, then the resulting equivalent NFA will have size O(m2) in the worst case.
  • 54. 54 Compute δN as follows: 1. For each q in Q, let Sq = CL(q).  Step 1 takes O(m2) time (by doing a depth-first search from each q that just follows ε-transitions). 2. Let δN(q, a) be the union over all p in Sq of δE(p, a) for which δE(p, a) is defined.  Step 2 takes O(m) time for each state q, since the total size of all the δE(p, a) sets is O(m).  So, the total running time for ε-NFA-to-NFA conversion is O(m2). The resulting NFA could have size O(m2), since it will have the same number of nodes, but could have more edges.  F’ = set of states q such that CL(q) contains a state of F.
  • 55. 55 Example: ε-NFA-to-NFA 0 1 ε A {E} {B} ∅ B ∅ {C} {D} C ∅ {D} ∅ D ∅ ∅ ∅ E {F} ∅ {B, C} F {D} ∅ ∅ * ε-NFA 0 1 A {E} {B} B ∅ {C} C ∅ {D} D ∅ ∅ E {F} {C, D} F {D} ∅ * * * Since closure of E includes B and C; which have transitions on 1 to C and D. Since closures of B and E include final state D. Interesting closures: CL(B) = {B,D}; CL(E) = {B,C,D,E} NFA δN δE
  • 56. ε-NFA Matching via DFA Conversion can be Slow for Big Finite Automata  Given an ε-NFA, A, of size m, and an input string, T, of length n, we can test if T is in L(A) in O(2m + n) time: 1. Convert A into an equivalent DFA, B, using the above ε-NFA to NFA to DFA constructions.  Step 1 takes O(2m) time in the worst case and creates a DFA of size O(2m) in the worst case. 2. Test if T is in L(B) using the DFA simulation algorithm.  Step 2 takes O(2m + n) time.
  • 57. There is an Alternative Approach  Instead of converting the ε-NFA to a DFA, and then matching using the DFA, simulate the ε-NFA directly. Image from http://algs4.cs.princeton.edu
  • 58. ε-NFA Simulation  Given an ε-NFA, A, of size, m, and an input string, T, of length n, we can test if T is in L(A) in O(mn) time: 1. Read in the graph representation for A.  Step 1 takes O(m) time. 2. Let S = {q}, where q is the initial state for A. 3. For each character, c, in T (in order):  Let S = CL(S). This can be done in O(m) time by a DFS that only follows ε-transitions.  Let S be the union of d(q,c), for each q in S. This takes O(m) time, since the size of all the d(q,c) sets is O(m). 4. If S contains a final state, accept T.
  • 59. 59 Summary  DFA’s, NFA’s, and ε–NFA’s all accept exactly the same set of languages: the regular languages.  The NFA types are easier to design and may have exponentially fewer states than a DFA.  But only a DFA can be implemented in linear time, that is, O(m + n) time.  Implementing an ε-NFA by the conversion to DFA method takes O(2m + n) time.  Implementing an ε-NFA by the ε-NFA simulation method takes O(mn) time.