1 Dfa
1 Dfa
1 Course topics:
1. Finite automata / regular expressions
2. Context-free grammars
3. Turing Machines
4. Decidability / undecidability
5. NP-Completeness
2 Finite Automata
Automata: Plural of “automaton” (= machine)
TV:
start off on
switch off
switch on
turn off
turn on
switch off
standby
PC:
standby
switch off
switch on
stand by
switch off
switch on
start off on
hibernate
switch on
1 0
q0
0 q1
start
input string w = 0100 is accepted by M . M also accepts 0, 10, 110, 010, . . ., i.e. all inputs that ends
with 0.
Some denitions:
• Σ is a nite alphabet.
2
Transition diagram representation:
1 0
q0
0 q1
start
Q/Σ 0 1
→ q0 q1 q0
∗q1 q1 q0
Denition: A DFA M is said to accept an input string if its computation ends at an accept state.
eg. M accepts 0100.
Denition: The language of a DFA M , L(M ), is the set of all input strings accepted by M .
eg. L(M ) = {w|w ends with 0}
Examples:
q1 0,1
0
start q0
1
q2 0,1
0
1
q1 q3 1
0
0
start q0
1
q2 0,1
3
• L : {w|w begins and ends with 0}
0
1
q1 q3 1
0
0
start q0
1
q2 0,1
0 0
q0
1 q1
start
0 0
q0
1 q1
start
1 0
q0
0 q1
start
1 0
q0
0 q1
1 q2
start 0,1
4
• L : {w|w contains 010 as a substring}
1 0
q0
0 q1
1 q2
0 q3
start 0,1
1 1 1 1
Theorem 2.1 The language of the DFA M below is L = {w|w ∈ {0, 1}∗ and w does not have two consecutive 1s}.
M:
0 0,1
1
1
start A B C
5
Proof We have two sets S and T :
• S = “the language of M ”
• T = “the set of strings of 0s and 1s with no consecutive 1s”
To prove S = T , we need to prove both S ⊆ T and T ⊆ S. That is:
• if w ∈ S, then w ∈ T .
• if w ∈ T , then w ∈ S.
Inductive hypothesis for Part 1 (S ⊆ T ):
1. If δ(A, w) = A, then w has no consecutive 1s and does not end in 1.
2. If δ(A, w) = B, then w has no consecutive 1s and ends in a single 1.
Basis: |w| = 0; i.e. w =
• (1) holds since has no 1s at all.
• (2) holds vacuously, since δ(A, ) is not B.
Induction:
• Assume (1) and (2) are true for strings shorter than w, where |w| ≥ 1.
• Because w 6= , we can write w = xa, where a is the last symbol of w, and x is the string that
precedes.
• IH holds for x.
• Need to prove (1) and (2) for w = xa.
• (1) for w is: If δ(A, w) = A, then w has no consecutive 1s and does not end in 1.
• Since δ(A, w) = A, δ(A, x) must be A or B, and a must be 0 (look at the DFA).
• By the IH, x has no 11s.
• Thus, w has no 11s and does not end in 1.
• Now, prove (2) for w = xa: If δ(A, w) = B, then w has no 11s and ends in 1.
• Since δ(A, w) = B, δ(A, x) must be A, and a must be 1 (look at the DFA).
• By the IH, x has no 11s and does not end in 1.
• Thus, w has no 11s and ends in 1.
Inductive hypothesis for Part 2 (T ⊆ S):
• if w has no 11s, then w is accepted by M .
• Contrapositive: If w is not accepted by M , then w has 11.
Using the contrapositive:
• Because there is a unique transition from every state on every input symbol, each w gets the DFA to
exactly one state.
• The only way w is not accepted is if it gets to C.
• The only way to get to C (formally: δ(A, w) = C) is if w = x1y, x gets to B, and y is the tail of w
that follows what gets to C for the rst time.
• If δ(A, x) = B then surely x = z1 for some z.
• Thus, w = z11y and has 11.
6
2.3 Nonregular Languages
Some languages are nonregular.
• L2 = {w|w ∈ {(, )}∗ and w is balanced.}. Balanced parentheses are those sequences of parentheses
that can appear in an arithmetic expression. E.g.: (), ()(), (()), (()()) . . .
• Then w0 represents integer 2i, so we want δ(i mod 23, 0) = (2i) mod 23.
Example 2:
L4 = {w|w ∈ {0, 1}∗ and w, viewed as the reverse of a binary integer is divisible by 23}
• But there is a theorem that says the reverse of a regular language is also regular.
7
2.5 Implementation of DFA
enum STATES {A, B , C} s t a t e ;
int i ;
char w [ ] ; / / f r o m t h e u s e r
s t a t e = A; / / A i s s t a r t s t a t e
f o r ( i = 0 ; i < s t r l e n (w ) ; i ++){
switch ( s t a t e ){
case A:
i f (w[ i ]== ’ 0 ’ ) s t a t e =A ;
e l s e s t a t e =B ;
break ;
0 0,1 case B:
i f (w[ i ]== ’ 0 ’ ) s t a t e =A ;
1 e l s e s t a t e =C ;
1
start A B C break ;
case C:
0
s t a t e =C ;
break ;
}
}
i f ( s t a t e == A | | s t a t e == B) {
p r i n t f ( ” accepted .\ n” ) ;
}
else{
p r i n t f ( ” r e j e c t e d .\ n” ) ;
}
Recall L3 = {w|w ∈ {0, 1}∗ and w, viewed as a binary integer is divisible by 23}
if (w)2 = (i)10 ; then:
δ(0, w) = i mod 23. Thus
Q = {0, 1, . . . , 22}, q0 = {0} and F = {0}.
Note that (w0)2 = (2i)10 and (w1)2 = (2i + 1)10 .
Similarly :
δ(0, w1) = (2i + 1) mod 23.
δ(0, w1) = δ(δ(0, w), 1) = δ(i mod 23, 0) = (2i + 1) mod 23.
DFA (partial):
0
1 0
1 0
start 0 1 2 3 4 5 22