CS 373: Theory of Computation: Manoj Prabhakaran Mahesh Viswanathan Fall 2008
CS 373: Theory of Computation: Manoj Prabhakaran Mahesh Viswanathan Fall 2008
Fall 2008
1
1 Introduction
1.1 Problems and Computation
Decision Problems
Decision Problems
Given input, decide “yes” or “no”
• In this course, we will study decision problems because aspects of computability are captured
by this special class of problems
• But some programs do not need larger state for larger instances!
• Finite state: A fixed upper bound on the size of the state, independent of the size of the input
2
• Not enough memory to hold the entire input
– “Streaming input”: automaton runs (i.e., changes state) on seeing each bit of input
An Automatic Door
Front Rear
pad pad
door
rear front
both rear
neither front both
closed open
neither
Finite Automata
Details
Automaton
A finite automaton has: Finite set of states, with start/initial and accepting/final states; Transitions
from one state to another on reading a symbol from the input.
Computation
Start at the initial state; in each step, read the next symbol of the input, take the transition (edge)
labeled by that symbol to a new state.
Acceptance/Rejection: If after reading the input w, the machine is in a final state then w is
accepted; otherwise w is rejected.
3
0 0
1
q0 q1
1
Example: Computation
0 0
1
q0 q1
1
1.3 Examples
Example I
0, 1
q0
4
Example II
0 1
1
q0 q1
0
Example III
0 0
1
q0 q1
1
Example IV
1
q0 q1
1
0 0 0 0
1
q3 q2
1
Figure 7: Automaton accepts strings having an odd number of 1s and odd number of 0s
1.4 Applications
Finite Automata in Practice
• grep
5
• Thermostats
• Coke Machines
• Elevators
• Security Properties
2 Formal Definitions
2.1 Alphabets, Strings and Languages
Alphabet
Definition 1. An alphabet is any finite, non-empty set of symbols. We will usually denote it by Σ.
Example 2. Examples of alphabets include {0, 1} (binary alphabet); {a, b, . . . , z} (English alpha-
bet); the set of all ASCII characters; {moveforward, moveback, rotate90}.
Strings
• The length of string u (denoted by |u|) is the number of symbols in u. Example, || = 0,
|011010| = 6.
• u is a prefix of v if there is a string w such that v = uw. Example ‘cat0 is a prefix of ‘catnap0 .
Languages
Definition 4. • For alphabet Σ, Σ∗ is the set of all strings over Σ. Σn is the set of all strings
of length n.
• A language over Σ is a set L ⊆ Σ∗ . For example L = {1, 01, 11, 001} is a language over {0, 1}.
6
Set Notation
We will often define languages using the set builder notation. Thus, L = {w ∈ Σ∗ | p(w)} is the
collection of all strings w over Σ that satisfy the property p.
Example 5. • L = {w ∈ {0, 1}∗ | |w| is even} is the set of all even length strings over {0, 1}.
• L = {w ∈ {0, 1}∗ | there is a u such that wu = 10001} is the set of all prefixes of 10001.
• What the transition from each state and input symbol is.
• q0 ∈ Q initial state
• F ⊆ Q final/accepting states
Computation
7
Definition 7. For a DFA M = (Q, Σ, δ, q0 , F ), string w = w1 w2 · · · wk , where for each i wi ∈ Σ,
w
and states q1 , q2 ∈ Q, we say q1 −→M q2 if there is a sequence of states r0 , r1 , . . . rk such that
• r0 = q 1 ,
• rk = q 2 .
w
Definition 8. For a DFA M = (Q, Σ, δ, q0 , F ) and string w ∈ Σ∗ , we say M accepts w iff q0 −→M q
for some q ∈ F .
Proposition 11. For a DFA M , string w, and state q1 , there is exactly one state q2 such that
w
q1 −→M q2 .
Proof. By induction on |w|.
uv
Proposition 12. For DFA M , strings u and v, and states q1 and q3 , q1 −→M q3 if and only if
u v
there is a state q2 such that q1 −→M q2 and q2 −→M q3 .
0 0
1
q0 q1
Example 13. 1
δ(q0 , 0) = q0 δ(q0 , 1) = q1
δ(q1 , 0) = q1 δ(q1 , 1) = q0
8
0 1
q0 q0 q1
q1 q1 q0
Language of Modd
Proposition 14. L(Modd ) = {w ∈ {0, 1}∗ | w has an odd number of 1s}, where Modd is as defined
before.
• Induction Step w = u0: If w has an odd number of 1s then u has an odd number of 1s, and
u 0 w
so (by induction hypothesis) q0 −→M q1 . Since q1 −→M q1 , q0 −→M q1 .
• Induction Step w = u1: If w has an odd number of 1s then to show that M is in q1 after w,
we need to argue that M is in q0 after u.
Need to prove a stronger statement.
• Proving that if w has an odd number of 1s then M is in state q1 is not sufficient to show that
L(Modd ) is the set of strings that has an odd number of 1s because it does not show that only
strings with an odd number of 1s is accepted!
Corrected Proof
w
Proof. We will prove by induction on |w| that after reading w, q0 −→M q1 if and only if w has an
odd number of 1s.
• Base Case: When w = , w has an even number of 1s and M is in state q0 after w.
• Induction Step w = u0: w has an odd number of 1s iff u has an odd number of 1s, iff (by ind.
u w 0 0
hyp.) q0 −→M q1 iff q0 −→M q1 (since q0 −→M q0 and q1 −→M q1 ).
9
• Induction Step w = u1: w has an odd number of 1s iff u has an even number of 1s iff
u w 1 1
q0 −→M q0 (ind. hyp.) iff q0 −→M q1 (since q0 −→M q1 and q1 −→M q0 ).
Proof Template
Given a DFA M having n states {q0 , q1 , . . . qn−1 } with initial state q0 , to prove that L(M ) = L, we
do the following.
3. Show that ∪qi ∈F Li = L; the collection of all strings that reach a final state is exactly L.
3 Designing DFAs
3.1 General Method
Typical Problem
Problem
Given a language L, design a DFA M that accepts L, i.e., L(M ) = L.
How does one go about it?
Methodology
• Imagine yourself in the place of the machine, reading symbols of the input, and trying to
determine if it should be accepted.
• Remember at any point you have only seen a part of the input, and you don’t know when it
ends.
• Figure out what to keep in memory. It cannot be all the symbols seen so far: it must fit into
a finite number of bits.
10
3.2 Examples
Strings containing 0
Problem
Design an automaton that accepts all strings over {0, 1} that contain at least one 0.
Solution
What do you need to remember? Whether you have seen a 0 so far or not!
1 0, 1
qnoz 0 qzer
Problem
Design an automaton that accepts all strings over {0, 1} that have an even length.
Solution
What do you need to remember? Whether you have seen an odd or an even number of symbols.
0, 1
qe qo
0, 1
Pattern Recognition
Problem
Design an automaton that accepts all strings over {0, 1} that have 001 as a substring, where u is a
substring of w if there are w1 and w2 such that w = w1 uw2 .
Solution
What do you need to remember? Whether you
11
• have just seen 0
• have just seen 00
• have seen the entire pattern 001
1 0 0, 1
0
q q0 0 q00 1 qp
1
grep Problem
Problem
Given text T and string s, does s appear in T ?
Naı̈ve Solution
=s?
z }| {
=s?
z }| {
=s?
z }| {
=s?
z }| {
=s?
z }| {
T1 T2 T3 . . . Tn Tn+1 . . . Tt
grep Problem
Smarter Solution
Solution
• Build DFA M for L = {w | there are u, vs.t. w = usv}
• Run M on text T
Time = time to build M + O(t)!
Questions
12
• Is L regular no matter what s is?
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 do you need to remember? The remainder when divided by 5.
How do you compute remainders?
q1 0
0 0
1 1 q2
1
q0 1
0 q3
q4 0
Figure 13: Automaton recognizing strings encoding binary number that are multiples of 5.
Problem
13
Design an automaton for the language Lk = {w | kth character from end of w is 1}
Solution
What do you need to remember? The last k characters seen so far!
Formally, Mk = (Q, {0, 1}, δ, q0 , F )
• q0 = hi
Proof
Let M , with initial state q0 , recognize Lk and assume (for contradiction) that M has < 2k states.
Proof (contd)
Proof
Let i be the first position where w0 and w1 differ. Without loss of generality assume that w0 has
0 in the ith position and w1 has 1.
k
z }| {
i−1
w0 0 = . . . 0 . . . 0i−1
w1 0i−1 = |{z} . . . 0i−1
. . . 1 |{z}
i−1 k−i
w0 0i−1 6∈ Lk and w1 0i−1 ∈ Lk . Thus, M cannot accept both w0 0i−1 and w1 0i−1 .
Proof (contd)
. . . Almost there
Proof
14
w w
So far, w0 0i−1 6∈ Ln , w1 0i−1 ∈ Ln , q0 −→
0 1
M q, and q0 −→M q.
w 0i−1
0 0i−1
q0 −→ M q1 iff q −→M q1
w 0i−1
1
iff q0 −→ M q1
15