theory of computation (fall 2014): lecture 02: deterministic finite automata; how dfas work; dfa...
TRANSCRIPT
Theory of Computation
Vladimir Kulyukin
www.vkedco.blogspot.com
Deterministic Finite Automata
Outline
● Blocks World Problem: A DFA for a Robotic Camera & Arm
● How DFAs Work● DFA Transition Function● Formal Languages Accepted by DFAs● Sample DFA Problems● Sample DFA Implementation
Blocks World Problem
A B
Suppose we want to design a control system for a robotic camera & arm unit that should build two-block towers: a on b or b on a.
Blocks World Problem
A
B
Goal State 01: A on B
B
A
Goal State 02: B on A
Robotic Arm & Camera Operations
• puton(X, Y) – put A on top of Y• clear(X) – verify that X's top is clear• on(X, Y) – verify that X is on top of Y
Finite Automaton for Blocks World Problem
on(A, T)on(B, T)clear(A)clear(B)
on(A, B)on(B, T)clear(A)
on(B, A)on(A, T)clear(B)
puton(A, B)
puton(A, T)
puton(B, A)
puton(B, T)
Back to Automata & Languages
The robot control for our problem can be solved with a finite state machine, i.e., it is a regular language.
Deterministic Finite Automata
• A DFA can be informally defined as a directed graph whose nodes are states and whose edges are transitions on specific symbols
• A DFA has a unique start state and a set (possibly empty) of final or accepting states
• A DFA processes the input string one symbol at a time. When the last symbol is read, the DFA reaches a state which is either final or not. If the state is final, the DFA accepts (recognizes) the string. If the state is not final, the DFA rejects the string
DFA: Formal Definition
states. (final) accepting ofset theis
state;start theis
;: function;n transitioa is
alphabet;an is
states; ofset finite a is
:where
,,,,, i.e. tuple,-5 a is DFA A
0
0
F
Q
FqQMM
Example 1: DFA M
q1q0
a
b
b
a
q0 is the start state.q1 is the final state.
Example 1: DFA M
01
100010
1
10
0
,
;,;,;, .4
; .3
;, .2
;, 1.
where,,,,,
qbq
qaqqbqqaq
qF
ba
qqQ
FqQM
How DFAs Work
Example: DFA M
a b
q0 q1 q0
q1 q1 q0
The transition function, δ, can be represented as a table:
How DFA M Works
q0
b a a b a
tape.on the
symbolleftmost thebefore placed is head The
How DFA M Works
q0
b a a b a
right. cell one move and in remain , read ,In 00 qbq
How DFA M Works
q0
b a a b a
right. cell one move and into go , read ,In 10 qaq
How DFA M Works
q1
b a a b a
right. cell one move and in remain , read ,In 11 qaq
How DFA M Works
q1
b a a b a
right. cell one move and into go , read ,In 01 qbq
How DFA M Works
q0
b a a b a
right. cell one move and into go , read ,In 10 qaq
How DFA M Works
q1
b a a b a
final. is because
ecognized,accepted/r is Thus,
1q
baaba
Observations on DFA Definition
● Q may not be empty: a DFA must have at least one state, i.e., the start state
● δ is a function, i.e., it must be for every state in Q and every symbol in Σ
● F may be empty, in which case the DFA accepts the empty language
● F can be equal to Q, in which case the DFA accepts Σ*
δ* :DFA Transition Function
The δ* Function
axqxaq
,,, .2
;, .1
:statesFA tostrings maps
**
*
*
Back to Example DFA M
Fqaqabq
abbqabbq
abbqabbqbbaq
bba
100
00*
0*
0*
0*
,,,
,,,,,,,
,,,,,,
accepted?it Is . isinput theSuppose
Languages Accepted by DFAs
DFA String Acceptance
FxqFqQM
x
, ifonly and if ,,,,
DFA aby ecognizedaccepted/r is stringA
0*
0
*
DFA’s Language: L(M)
.,|Then
.,,,,such that DFA a be Let
0**
0
FxqxML
FqQMM
Regular Languages
A language L is regular if and only if there exits a DFA M such that L(M) = L.
Sample DFA Problems
Typical DFA Problems
● Given a DFA M and a set of strings, determine which strings are in the language of M, i.e. L(M)?
● Given a DFA M, state in the set former notation, state L(M)?
● Draw a DFA diagram given the formal definition of the DFA
Problem 01
q0 q1
a
a
b
Which of the following strings are in L(M)?1. b2. ε3. ab4. abba5. ababaaaaba
b
Solution 01
Fqababaaaabaq
Fqaqabqabbq
abbaq
ababq
aabbqabbaq
MLab
Fqbqbaqabq
ML
MLb
10*
0111
0*
0*
0*
0*
110*
0*
, .5
,,,,,,
,,,,
,,,
,,, .4
So, .,,,, .3
.2
.1
Problem 02
q0 q1
a,b
a,b
What is the language accepted by this DFA M?
Solution 02
.,12 and ,|
:answer Formal
odd. islength whose,over strings All
:answer Informal
* NnnxbaxxML
ba
Problem 03
q0 q1
ba
a
b
What is the language accepted by this DFA M?
Solution 03
*, and |
:answer Formal
in end that ,over strings All
:answer Informal
bayyaxxML
aba
Problem 04
odd is in s' ofnumber |, .2
,| 1.
:it acceptsthat
DFA a draw languages, following theofeach For
*
*
xabax
baxx
Solution 04.1
q0
a,b
Solution 04.2
q0 q1
b b
a
a
Problem 05
01
01
10
10
1010
,
;,
;,
;,
,,,,,,,
:DFA thisDraw
qbq
qaq
qbq
qaq
qqbaqq
Solution 05
q0 q1
a,b
a,b
Sample DFA Implementation
A DFA: Odd Numbers of Zeros
q0 q1
0
0
q2
any symbol other than 0any symbol other than 0
Java Implementation: Part 1
import java.io.*;
public class OddNumZeros {
private static final int q0 = 0; private static final int q1 = 1; private static final int q2 = 2; // this is the sink
private int state; // current state …
}
Java Implementation: Part 2
static private int delta(int s, char c) { switch (s) { case q0: switch (c) { case '0': return q1; default: return q2; // go to the sink } case q1: switch (c) { case '0': return q0; default: return q2; // go to the sink } default: return q2; // go to the sink }}
Java Implementation: Part 3
public void deltaStar(String instr) { char c; for(int i = 0; i < instr.length(); i++) { c = instr.charAt(i); state = delta(state, c); } }
public boolean isInputAccepted() { return state == q1; }
Java Implementation: Part 4public void reset() { state = q0; }
public static void main(String[] args) throws IOException {
OddNumZeros dfa = new OddNumZeros(); BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String instr = in.readLine(); while ( instr != null ) { dfa.reset(); dfa.deltaStar(instr); if ( dfa.isInputAccepted() ) { System.out.println(instr + " accepted"); } else { System.out.println(instr + " rejected"); } instr = in.readLine(); }
}
References & Reading Suggestions
● A. Brooks Weber. Ch. 2 Formal Language: A Practical Introduction, Franklin, Beedle & Associates, Inc
● Davis, Weyuker, Sigal. Ch. 9. Computability, Complexity, and Languages, 2nd Edition, Academic Press