No Slide Title

Download Report

Transcript No Slide Title

Finite Automata
CPSC 388
Ellen Walker
Hiram College
Machine Model of Computation
String
Machine
Accept or reject
• Strings in the language are accepted
• Strings not in the language are rejected
Deterministic Finite Automaton
Transition
0
0
1
Odd
Even
Initial State
1
Accept State
Accepts strings with an odd number of 1’s
(e.g. 1011, 110111, 10, 01110)
Parts of a DFA
• An alphabet (S)
• A finite set of states (S)
• A deterministic set of transitions from
one state to the next (T : S x S  S)
• One start state (s0  S)
• One or more accepting states (A  S)
Deterministic Transition Table
0
0
1
Odd
Even
1
Even
Odd*
0
Even
Odd
1
Odd
Even
Testing some strings
•
•
•
•
•
•
1001 reject
0101 reject
10101 accept
0
reject
1
accept
010101011 accept
Results
Data for DFA in C++
Class State{
Public:
string name;
int index; // unique integer
bool isFinal; //true if final state
}
State start;
State next [MAXSTATES][MAXCHARS];
Implementing DFA in C++
//Construct all the states, set isFinal
//Create next array from transitions
int machine(istream &sin){
//sin.get() reads a single character from sin
for(State S=start; c=sin.get();
S=next[S.index][c]);
//Action for S would go here...
return S.isFinal;
}
What Language is This?
a
b
S2
S1
a
b
S3
a, b
Draw DFA for:
•
•
•
•
•
•
•
{aa, ab, bb}
(ab)*
(a+ab+aab)*b
All strings with 1 a and 2 b’s
All strings containing ab
All strings that don’t contain ab
All strings that don’t contain abc
Deterministic vs.
Non-Deterministic
• Deterministic: exactly one new state for
a given state,character pair
• Non-deterministic: 0 or more new
states for each state,character pair
– Assume machine will always make the
right choice!
– Add e-transitions (move w/o symbol)
NFA: All strings containing “ab”
a, b
S1
s1
s2
s3*
a
a, b
S2
a
{s1,s2}
{}
{s3}
b
b
{s1}
{s3}
{s3}
S3
Are NFA’s More Powerful?
• Yes if ...
– There is at least one language that can be
accepted by an NFA but no DFA
• No if …
– For every NFA, we can make a DFA that
accepts the same language
From NFA to DFA
• Each DFA state corresponds to a set of
states in the NFA
– DFA has max 2n states if NFA has n states
(still finite)
• Transition from {s1,s2} on a is union of
transitions from s1 on a and s2 on a
Conversion Algorithm
• Copy the transitions from the start state.
• Choose one of the “new state” sets.
Create a new row for that set, and
construct its transitions (by unioning
transitions from original table)
• Repeat until every set in the table has a
row
Example: DFA for (a|b)*ab(a|b)*
• Initial NFA table
s1
s2
s3*
a
{s1,s2}
{}
{s3}
b
{s1}
{s3}
{s3}
• Copy start state, create transition for
{s1,s2}
a
b
s1
{s1,s2}
{s1,s2}
{s1,s2}
{s1}
{s1,s3}
Final NFA
s1
{s1,s2}
{s1,s3}*
{s1,s2,s3} *
a
{s1,s2}
{s1,s2}
{s1,s2,s3}
{s1,s2,s3}
a
b
{s1}
{s1,s3}
{s1,s3}
{s1,s3}
b
a
b
b
S1
a
S12
a
S13
S123
b
Minimizing DFA
• If 2 states have different isFinal, they
are different
• If 2 states have transitions on the same
character to states that are known
different, they are different
• Otherwise, the states are the same and
can be merged (repeat until all different)
Minimization Example
s1
{s1,s2}
{s1,s3}*
{s1,s2,s3} *
a
{s1,s2}
{s1,s2}
{s1,s2,s3}
{s1,s2,s3}
b
{s1}
{s1,s3}
{s1,s3}
{s1,s3}
s1 != {s1,s3} s1 != {s1,s2,s3} (final)
s1 != {s1,s2} (transition on b)
{s1,s2} != {s1,s3} {s1,s2} != {s1,s2,s3} (final)
We cannot say {s1,s3} != {s1,s2,s3}, so merge!
Why minimize?
• Implement machine with smaller table
• Compare two machines
– If both are minimized, if both compute the
same language, then the machines will be
identical except for state names
DFA and Regular Expression
• The set of languages accepted by
DFA’s = the set of languages accepted
by regular expressions
– For every r.e. we can make a DFA that
accepts the same language
– For every DFA we can make a r.e. that
accepts the same language
For every r.e. there is a DFA...
• If we can construct an NFA, that’s good
enough
• Prove by induction:
– Base cases: , e, a (element of S) -- build
an NFA for each.
– Step cases: r | s , rs , r* -- build resulting
NFA from NFA’s from r and/or s