1 Introducing Finite Automata: 1.1 Problems and Computation
1 Introducing Finite Automata: 1.1 Problems and Computation
M
q
2
if there is a sequence of states r
0
, r
1
, . . . r
k
such that
r
0
= q
1
,
for each i, (r
i
, w
i+1
) = r
i+1
, and
r
k
= q
2
.
Denition 4. For a DFA M = (Q, , , q
0
, F) and string w
, we say M accepts w i q
0
w
M
q
for some q F.
Useful Notation
5
Denition 5. For a DFA M = (Q, , , q
0
, F), let us dene a function
M
: Q
T(Q) such
that
M
(q, w) = q
Q[ q
w
M
q
.
We could say M accepts w i
M
(q
0
, w) F ,= .
Proposition 6. For a DFA M = (Q, , , q
0
, F), and any q Q, and w
, [
M
(q, w)[ = 1.
Acceptance/Recognition
Denition 7. The language accepted or recognized by a DFA M over alphabet is L(M) = w
q
0
q
00
q
p
1
0
1
0
0
1
0, 1
Figure 12: Automaton accepting strings having 001 as substring.
grep Problem
Problem
Given text T and string s, does s appear in T?
Nave Solution
=s?
=s?
=s?
=s?
=s?
T
1
T
2
T
3
. . . T
n
T
n+1
. . . T
t
Running time = O(nt), where [T[ = t and [s[ = n.
grep Problem
Smarter Solution
Solution
Build DFA M for L = w [ there are u, v s.t. w = usv
Run M on text T
Time = time to build M + O(t)!
Questions
9
Is L regular no matter what s is?
If yes, can M be built eciently?
Knuth-Morris-Pratt (1977): Yes to both the above questions.
Multiples
Problem
Design an automaton that accepts all strings w over 0, 1 such that w is the binary representation
of a number that is a multiple of 5.
Solution
What must be remembered? The remainder when divided by 5.
How do you compute remainders?
If w is the number n then w0 is 2n and w1 is 2n + 1.
(a.b + c) mod 5 = (a.(b mod 5) + c) mod 5
e.g. 1011 = 11 (decimal) 1 mod 5 10110 = 22 (decimal) 2 mod 5 10111 = 23 (decimal)
3 mod 5
Automaton for recognizing Multiples
q
0
q
1
q
4
q
2
q
3
0
1
0
1
1
0
1
0
0
1
Figure 13: Automaton recognizing binary numbers that are multiples of 5.
A One k-positions from end
Problem
10
Design an automaton for the language L
k
= w [ kth character from end of w is 1
Solution
What do you need to remember? The last k characters seen so far!
Formally, M
k
= (Q, 0, 1, , q
0
, F)
States = Q = w [ w 0, 1
and [w[ k
(w, b) =
wb if [w[ < k
w
2
w
3
. . . w
k
b if w = w
1
w
2
. . . w
k
q
0
=
F = 1w
2
w
3
. . . w
k
[ w
i
0, 1
11