Outline: Great Theoretical Ideas in CS
Outline: Great Theoretical Ideas in CS
V. Adamchik CS 15-251
Outline
Carnegie Mellon University
DFA
Finite Automata Regular Languages
0n1n is not regular
Union Theorem
Kleene’s Theorem
NFA
Application: KMP
1
The machine processes a string and accepts
it if the process ends in a double circle
The unique string of length 0 will be denoted
by ε and will be called the empty or null string
1
The Language L(M) of Machine M The Language L(M) of Machine M
0 0 0
0,1
1
q0 q1
q0
1
1
What language does this DFA decide/accept?
L(M) = All strings of 0s and 1s
EXAMPLE
Determine the language
An automaton that accepts all recognized by
and only those strings that
contain 001
1 0,1
0 0,1 0
1
0 0 1
{0} {00} {001}
1 L(M)={1,11,111, …}
2
Membership problem
Determine the language
decided by
Determine whether some
word belongs to the language.
0 0,1
0 1 0,1
L(M)={1, 01}
3
Theorem: L = {0n1n : n∈ℕ} is not regular Theorem: L = {0n1n : n∈ℕ} is not regular
Theorem: L = {0n1n : n∈ℕ} is not regular Theorem: L = {0n1n : n∈ℕ} is not regular
Full proof: Full proof:
Suppose M is a DFA deciding L with, say, k states. So on input 0s1s ∈ L, M will reach an accepting state.
4
Regular Languages Equivalence of two DFAs
qodd 0 qodd 0
M2 M2
0,1 0,1 0,1 0,1
p0 p1 p2 p0 p1 p2
0,1 0,1
5
Union Theorem Union Theorem
qeven 0 qeven 0
M1 M1
qodd 0 qodd 0
M2 M2
0,1 0,1 0,1 0,1
p0 p1 p2 p0 p1 p2
0,1 0,1
qeven 0 qeven 0
M1 M1
qodd 0 qodd 0
M2 M2
0,1 0,1 0,1 0,1
p0 p1 p2 p0 p1 p2
0,1 0,1
qeven 0 qeven 0
M1 M1
qodd 0 qodd 0
M2 M2
0,1 0,1 0,1 0,1
p0 p1 p2 p0 p1 p2
0,1 0,1
6
Union Theorem Union Theorem
Q = pairs of states, one from M1 and one from M2
Make a DFA keeping
track of both at once.
qeven 0 = { (q1, q2) | q1 Q1 and q2 Q2 }
M1
= Q1 Q2
0
1 1 0 0
qeven, p0 qeven, p1 qeven, p2
qodd 0
1 1
M2
0,1 0,1 qodd, p0 0 qodd, p1 0 qodd, p2
p0 p1 p2
0,1
1 1
A* UA k
k 0
Deduction rules:
All binary strings of an even Given L1, L2, can obtain L1 ⋃ L2
number of 1s Given L1, L2, can obtain L1 ⋅ L2
Given L, can obtain L*
7
The Kleene Theorem (1956) Reverse
Reverse: AR = { w1 …wk | wk …w1 A}
Every regular language over Σ can be How to construct a DFA for the reversal
constructed from ∅ and {a}, a ∈ Σ, using only of a language?
the operations union, concatenation 1 0,1
The direction in which we
and the Kleene star. read a string should be 0
irrelevant. q0 q1
1 0,1
If we flip transitions
around we might not get 0
a DFA. q0 q1
8
Find the language recognized by this What does it mean that for an NFA to
NFA recognize a string?
0
0 0 0
s1 s1 s3
s3 0
0
1 1
1 1
s0 0,1 s0 0,1
0 0 1
1
s2 s4 s2 s4
What does it mean that for a NFA to Find the language recognized by this
recognize a string? NFA
0
Here we are going formally define this.
1
1
In other words,
if we ask if there is a NFA that is not
CMU prof.
equivalent to any DFA. The answer is No.
emeritus
Rabin Scott
9
NFA vs. DFA Pattern Matching
Pattern Matching
Build DFA from pattern
Input: Text T, length n. Pattern P, length k.
Output: Does P occur in T? The alphabet is {a, b}.
The pattern is a a b a a a b b.
b b
a
0 1 0 1 2
a a
10
DFA Construction DFA Construction
aabaaabb aabaaabb
b a b a
b a
b
0 1 2 3 0 1 2 3 4
a a a a
b b
b
b b
b a b a
b b
0 1 2 3 4 5 0 1 2 3 4 5 6
a a a a a a a a a
b b b
b b
b
b a
b
a
b a b
b a 0 1 2 3 4 5 6 7 8
a a a a b
b b b
0 1 2 3 4 5 6 7 b
a a a a a
b a
b b
b
11
The Knuth-Morris-Pratt Algorithm (1976) The KMP Algorithm - Motivation
1970 Cook published a paper about a possibility of Algorithm compares the
existence of a linear time algorithm pattern to the text in
left-to-right, but shifts a b a a b x
Knuth and Pratt developed an algorithm the pattern more
intelligently than the
a b a a b a
Morris discovered the same algorithm brute-force algorithm. j
When a mismatch
a b a a b a
Pittsburgh native, occurs, we compute the
CMU professor. length of the longest
No need to Resume
prefix of P that is a repeat these comparing
proper suffix of P. comparisons here
Languages
DFAs
The regular operations
0n1n is not regular
Union Theorem
Kleene’s Theorem
Here’s What NFAs
You Need to Application: KMP
Know…
12