Theory of Computation (I) Yijia Chen Fudan University
Instructor Yijia Chen Homepage: http://basics.sjtu.edu.cn/~chen Email: yijiachen@fudan.edu.cn
Textbook Introduction to the Theory of Computation Michael Sipser, MIT Third Edition, 2012.
Part One. Automata and Languages
Regular Languages
Finite automata Definition A (deterministic) finite automaton (DFA) is a 5-tuple ( Q, Σ, δ, q 0, F ), where 1. Q is a finite set called the states, 2. Σ is a finite set called the alphabet, 3. δ : Q Σ Q is the transition function, 4. q 0 Q is the start state, and 5. F Q is the set of accept states.
Formal definition of computation Let M = ( Q, Σ, δ, q 0, F ) be a finite automaton and let w = w 1w 2 w n be a string with w i Σ for all i [n]. Then M accepts w if a sequence of states r 0, r 1,..., r n in Q exists with: 1. r 0 = q 0, 2. δ(r i, w i+1 ) = r i+1 for i = 0,..., n 1, and 3. r n F. We say that M recognizes A if A = { w M accepts w }.
Regular languages Definition A language is called regular if some finite automaton recognizes it.
The regular operators Definition Let A and B be languages. We define the regular operations union, concatenation, and star as follows: Union: A B = { x x A or x B }. Concatenation: A B = { xy x A and y B }. Star: A = { x 1x 2... x k k 0 and each xi A }.
Closure under union Theorem The class of regular languages is closed under the union operation. In other words, if A 1 and A 2 are regular languages, so is A 1 A 2.
Proof (1) For i [2] let M i = ( Q i, Σ i, δ i, q i, F i ) recognize Ai. We can assume without loss of generality Σ 1 = Σ 2: Let a Σ 2 Σ 1. We add δ 1(r, a) = r trap, where r trap is a new state with δ 1(r trap, w) = r trap for every w.
Proof (2) We construct M = ( Q, Σ, δ, q 0, F ) to recognize A 1 A 2: 1. Q = Q 1 Q 2 = { (r 1, r } 2) r 1 Q 1 and r 2 Q 2. 2. Σ = Σ 1 = Σ 2. 3. For each (r 1, r 2) Q and a Σ we let δ ( (r 1, r 2), a ) = ( δ 1(r 1, a), δ 2(r 2, a) ). 4. q 0 = (q 1, q 2). 5. F = (F 1 Q 2) (Q 1 F 2) = { (r 1, r } 2) r 1 F 1 or r 2 F 2.
Closure under concatenation Theorem The class of regular languages is closed under the concatenation operation. In other words, if A 1 and A 2 are regular languages, so is A 1 A 2. We prove the above theorem by nondeterministic finite automata.
Nondeterminism Definition A nondeterministic finite automaton (NFA) is a 5-tuple ( Q, Σ, δ, q 0, F ), where 1. Q is a finite set of states, 2. Σ is a finite alphabet, 3. δ : Q Σ ε P(Q) is the transition function, where Σ ε = Σ {ε}, 4. q 0 Q is the start state, and 5. F Q is the set of accept states.
Formal definition of computation Let N = ( Q, Σ, δ, q 0, F ) be an NFA and let w = y 1y 2 y m be a string with y i Σ ε for all i [m]. Then N accepts w if a sequence of states r 0, r 1,..., r m in Q exists with: 1. r 0 = q 0, 2. r i+1 δ(r i, y i+1 ) for i = 0,..., m 1, and 3. r m F.
Equivalence of NFAs and DFAs Theorem Every NFA has an equivalent DFA, i.e., they recognize the same language.
Proof (1) Let N = ( Q, Σ, δ, q 0, F ) be the NFA recognizing some language A. We construct a DFA M = ( Q, Σ, δ, q 0, F ) recognizing the same A. First assume N has no ε arrows. 1. Q = P(Q). 2. Let R Q and a Σ. Then we define 3. q 0 = {q 0}. δ (R, a) = { q Q q δ(r, a) for some r R }. 4. F = { R Q R F }.
Proof (2) Now we allow ε arrows. For every R Q, i.e., R Q, let E(R) = { q Q q can be reached from R by traveling along 0 and more ε arrows }. 1. Q = P(Q). 2. Let R Q and a Σ. Then we define δ (R, a) = { q Q ( ) } q E δ(r, a) for some r R. 3. q 0 = E ( {q ) 0}. 4. F = { R Q } R F.
Corollary A language is regular if and on if some nondeterministic finite automaton recognizes it.
Second proof of the closure under union For i [2] let N i = ( Q i, Σ i, δ i, q i, F i ) recognize Ai. We construct an N = ( Q, Σ, δ, q 0, F ) to recognize A 1 A 2: 1. Q = {q 0} Q 1 Q 2. 2. q 0 is the start state. 3. F = F 1 F 2. 4. For any q Q and any a Σ ε δ 1(q, a) q Q 1 δ 2(q, a) q Q 2 δ(q, a) = {q 1, q 2} q = q 0 and a = ε q = q 0 and a ε.
Closure under concatenation Theorem The class of regular languages is closed under the concatenation operation.
Proof For i [2] let N i = ( Q i, Σ i, δ i, q i, F i ) recognize Ai. We construct an N = ( Q, Σ, δ, q 1, F 2 ) to recognize A1 A 2: 1. Q = Q 1 Q 2. 2. The start state q 1 is the same as the start state of N 1. 3. The accept states F 2 are the same as the accept states of N 2. 4. For any q Q and any a Σ ε δ 1(q, a) q Q 1 F 1 δ 1(q, a) q F 1 and a ε δ(q, a) = δ 1(q, a) {q 2} q F 1 and a = ε δ 2(q, a) q Q 2.
Closure under star Theorem The class of regular languages is closed under the star operation.
Proof Let N 1 = ( Q 1, Σ, δ 1, q 1, F 1 ) recognize Ai. We construct an N = ( Q, Σ, δ, q 0, F ) to recognize A 1 : 1. Q = {q 0} Q 1. 2. The start state q 0 is the new start state. 3. F = {q 0} F 1. 4. For any q Q and any a Σ ε δ 1(q, a) q Q 1 F 1 δ 1(q, a) q F 1 and a ε δ(q, a) = δ 1(q, a) {q 1} q F 1 and a = ε {q 1} q = q 0 and a = ε q = q 0 and a ε.
Regular expression Definition We say that R is a regular expression if R is 1. a for some a Σ, 2. ε, 3., 4. (R 1 R 2), where R 1 and R 2 are regular expressions, 5. (R 1 R 2), where R 1 and R 2 are regular expressions, 6. (R 1 ), where R 1 is a regular expressions. We often write R 1R 2 instead of (R 1 R 2) if no confusion arises.
Language defined by regular expressions regular expression R language L(R) a {a} ε {ε} (R 1 R 2) L(R 1) L(R 2) (R 1 R 2) L(R 1) L(R 2) (R1 ) L(R 1)
Equivalence with finite automata Theorem A language is regular if and only if some regular expression describes it.
The languages defined by regular expressions are regular 1. R = a: Let N = ( {q 1, q 2}, Σ, δ, q 1, {q 2} ), where δ(q 1, a) = {q 2} and δ(r, b) = for all r q 1 or b a. 2. R = ε: Let N = ( {q 1}, Σ, δ, q 1, {q 1} ), where δ(r, b) = for all r and b. 3. R = : Let N = ( {q 1}, Σ, δ, q 1, ), where δ(r, b) = for all r and b. 4. R = R 1 R 2: L(R) = L(R 1) L(R 2). 5. R = R 1 R 2: L(R) = L(R 1) L(R 2). 6. R = R 1 : L(R) = L(R 1).