8/27/2009
Sofya Raskhodnikova
Intro to Theory of Computation
LECTURE 2 Theory of Computation• Finite Automata• Operations on languages• Nondeterminism
L2.1Sofya Raskhodnikova; based on slides by Nick Hopper
Homework 0 dueHomework 1 handed out
JFLAP http://www.cs.duke.edu/csed/jflap/
L2.28/27/2009 Sofya Raskhodnikova; based on slides by Rakesh Verma
aaaaaaaaaaaa
Formal definition of FA
L2.38/27/2009 Sofya Raskhodnikova; based on slides by Nick Hopper
Q = {q0, q1, q2, q3}Σ = {0,1}
: Q Σ → Q transition function*q0 Q is start stateF = {q1, q2} Q accept states
M = (Q, Σ, , q0, F) where
M q2
00,1
00
1
1
1
q0
q1
q3
0 1
q0 q0 q1
q1 q2 q2
q2 q3 q2
q3 q0 q2
*
A language is regular if it is recognized by a finite automaton
L = { w | w contains 001} is regular
L = { w | w has an even number of 1s} is regular
The language of M, L(M), is the set of strings that M accepts
Language of FA
8/27/2009 L1.4Sofya Raskhodnikova; based on slides by Nick Hopper
8/27/2009 Sofya Raskhodnikova; based on slides by Nick Hopper L1.5
Regular languages
Many interesting programs accept regular languages
NETWORK PROTOCOLS
COMPILERS
GENETIC TESTING
ARITHMETIC
INTERNET TRANSMISSION CONTROL PROTOCOL
Let TCPS = { w | w is a complete TCP Session}Theorem. TCPS is regular
8/27/2009 Sofya Raskhodnikova; based on slides by Nick Hopper L1.6
COMMENTS : Are delimited by /* */ Cannot have NESTED /* */ Must be CLOSED by */ */ is ILLEGAL outside a comment
COMMENTS = {strings with legal comments}
Theorem. COMMENTS is regular.
Compilers
8/27/2009 Sofya Raskhodnikova; based on slides by Nick Hopper L1.7
DNA SEQUENCES are strings over the alphabet {A,C,G,T}
GENES are special substrings
A GENETIC TEST searches a DNA SEQUENCE for a GENE
Theorem. Every GENETIC TEST is regular.
Genetic testing
8/27/2009 Sofya Raskhodnikova; based on slides by Nick Hopper L1.8
LET 3 =
• A string over 3 has three ROWS• Each ROW b0b1b2…bN represents the integer
b0 + 2b1 + … + 2NbN.• Let ADD = {S | ROW1 + ROW2 = ROW3 }
Theorem. ADD is regular.
{ [ ],[ ],[ ],[ ], [ ],[ ],[ ],[ ]}
000
001
010
011
100
101
110
111
Arithmetic
8/27/2009 Sofya Raskhodnikova; based on slides by Nick Hopper L1.9
Sofya Raskhodnikova; based on slides by Nick Hopper
Operations on languages
L2.108/27/2009
Union: A B = { w | w A or w B }
Intersection: A B = { w | w A and w B }
Complement: A = { w | w A }
Reverse: AR = { w1 …wk | wk …w1 A }
Concatenation: A B = { vw | v A and w B }
Star: A* = { w1 …wk | k ≥ 0 and each wi A }
Regular
Union: A B = { w | w A or w B }
Concatenation: A B = { vw | v A and w B }
Star: A* = { w1 …wk | k ≥ 0 and each wi A }
Sofya Raskhodnikova; based on slides by Nick Hopper
Closure properties of the class of regular languages
L2.118/27/2009
THEOREM. The class of regular languages is closed under all 6 operations.
If A and B are regular, applying any of these operation yields a regular language.
Sofya Raskhodnikova; based on slides by Nick Hopper
Closure properties
L2.128/27/2009
Union: A B = { w | w A or w B }
Complement: A = { w | w A }
Sofya Raskhodnikova; based on slides by Nick Hopper
Closure under union
L2.138/27/2009
Theorem. The union of two regular languages is also a regular language
Proof: Let
M1 = (Q1, Σ, 1, q0, F1) be finite automaton for L1
and
M2 = (Q2, Σ, 2, q0, F2) be finite automaton for L2
Construct a finite automaton M = (Q, Σ, , q0, F) that recognizes L = L1 L2
1
2
Sofya Raskhodnikova; based on slides by Nick Hopper
Example
L2.148/27/2009
q0 q1
00
1
1
p0 p1
11
0
0
M1
M2
M = ?
Sofya Raskhodnikova; based on slides by Nick Hopper
Proof (continued)
L2.158/27/2009
Idea: Run both M1 and M2 at the same time!
Q = pairs of states, one from M1 and one from M2
= { (q1, q2) | q1 Q1 and q2 Q2 }
= Q1 Q2
q0 = (q0, q0)1 2
F = { (q1, q2) | q1 F1 or q2 F2 }
( (q1,q2), ) = (1(q1, ), 2(q2, ))
Sofya Raskhodnikova; based on slides by Nick Hopper
Example (continued)
L2.168/27/2009
q0 q1
0 0
1
1
p0 p1
11
0
0
M1
M2
q0,p0 q1,p0
q0,p1 q1,p1
0000
1
1
1
1
M unionintersection
Sofya Raskhodnikova; based on slides by Nick Hopper
Closure properties
L2.178/27/2009
Union: A B = { w | w A or w B }
Intersection: A B = { w | w A and w B }
Complement: A = { w | w A }
Reverse: AR = { w1 …wk | wk …w1 A }
Sofya Raskhodnikova; based on slides by Nick Hopper
Closure under reverse
L2.188/27/2009
Theorem. The reverse of a regular language is also regular language
Proof: Let L be a regular language and
M be a finite automaton that recognizes it.
Construct a finite automaton MR recognizing LR
Idea: Define MR as M with the arrows reversedSwap start and accept states
Sofya Raskhodnikova; based on slides by Nick Hopper
Closure under reverse
L2.198/27/2009
MR IS NOT ALWAYS A DFA!
It may have many start states
Some states may have too many outgoing edges, or none
Sofya Raskhodnikova; based on slides by Nick Hopper
Example
L2.208/27/2009
1 0
1
0 1
0,1
0
L2.218/27/2009 Sofya Raskhodnikova; based on slides by Nick Hopper
NONDETERMINISM
1 0
1
0 1
0,1
0
What happens with 100?
Nondeterministic Finite Automaton (NFA) accepts if there is a way to make it reach an accept state
Nondeterminism
Ways to think about nondeterminism
• parallel computation
• tree of possible computations
• guessing the “right” choice
L2.22
DeterministicComputation
NondeterministicComputation
accept or reject accept
reject
8/27/2009 Sofya Raskhodnikova; based on slides by Nick Hopper
Sofya Raskhodnikova; based on slides by Nick Hopper
Example
L2.238/27/2009
1
0
0
L(M)={1,00}
ε
ε
Sofya Raskhodnikova; based on slides by Nick Hopper
Example
L2.248/27/2009
0,1
0,ε 1
0,1
1
L(M)={w | w contains 101 or 11}
Formal Definition
8/25/2009
Q is the set of states
Σ is the alphabet
q0 Q is the start state
F Q is the set of accept states
An NFA is a 5-tuple M = (Q, Σ, , q0, F)
L1.25Sofya Raskhodnikova; based on slides by Nick Hopper
: Q Σε → P(Q) is the transition function
P(Q) is the set of subsets of Q and Σε = Σ {ε}
Sofya Raskhodnikova; based on slides by Nick Hopper
Example
L2.278/27/2009
0,1
0,ε 1
0,1
1
(q1,ε) =
N = (Q, Σ, , q0, F)
Q = {q0, q1, q2, q3}
Σ = {0,1}
F = {q3}
(q0,1) = {q0, q1}
{q1,q2}
(q2,0) =
q1 q2 q3q0