CS 154 Formal Languages and Computability Assignment #2 Solutions Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak www.cs.sjsu.edu/~mak
Assignment #2: Question 1 o Use JFLAP to construct the transition graph for the DFA that accepts all strings (and only those strings) on the alphabet {a, b, c} that have an odd number of a s. 2
Assignment #2: Question 2 o Use JFLAP to construct the transition graph for the DFA that accepts all strings (and only those strings) on the alphabet {a, b, c} that have the symbols in alphabetical order. 3
Assignment #2: Question 3 o Describe in words the strings that the following DFA accepts and demonstrate your answer with some sample strings. n All strings that contain an even number of 0 s or an even number of 1 s. 4
Assignment #2: Question 4 o Describe in words the strings that the following DFA accepts and demonstrate your answer with some sample strings. n All strings that contain either two consecutive 0 s or two consecutive 1 s. 5
Assignment #2: Question 5 o Describe in words the strings that the following NFA accepts and demonstrate your answer with some sample strings. n All strings that contain all 1 s or end with a 0. 6
Assignment #2: Question 6 o NFA start state q 0 has λ-transitions to states q 1 and q 2, so label the DFA start state {q 0, q 1, q 2 }. 7
Assignment #2: Question 6, cont d o NFA: δ(q 0, 0) = ϕ and δ(q 1, 0) = ϕ and δ(q 2, 0) = {q 2, q 3 }, so DFA δ({q 0, q 1, q 2 }, 0) = {q 2, q 3 }. Perform the union. o NFA: δ(q 0, 1) = {q 0 } and δ(q 1, 1) = ϕ and δ(q 2, 1) = {q 2 }, so DFA δ({q 0, q 1, q 2 }, 1) = {q 0, q 2 }. 8
Assignment #2: Question 6, cont d o NFA: δ(q 0, 0) = ϕ and δ(q 2, 0) = {q 2, q 3 }, so DFA δ({q 0, q 2 }, 0) = {q 2, q 3 }. o NFA: δ(q 0, 1) = {q 0 } and δ(q 2, 1) = {q 2 }, so DFA δ({q 0, q 2 }, 1) = {q 0, q 2 }. 9
Assignment #2: Question 6, cont d o NFA: δ(q 2, 0) = {q 2, q 3 } and δ(q 3, 0) = ϕ, so DFA δ({q 2, q 3 }, 0) = {q 2, q 3 }. o NFA: δ(q 2, 1) = {q 2 } and δ(q 3, 1) = ϕ, so DFA δ({q 2, q 3 }, 1) = {q 2 }. 10
Assignment #2: Question 6, cont d o NFA: δ(q 2, 0) = {q 2, q 3 } so DFA δ({q 2 }, 0) = {q 2, q 3 }. o NFA: δ(q 2, 1) = {q 2 } so DFA δ({q 2 }, 1) = {q 2 }. 11
Assignment #2: Question 6, cont d o Since NFA q 0 and q 3 are final states, DFA {q 0, q 1, q 2 }, {q 0, q 2 }, and {q 2, q 3 } must be final states. 12
Assignment #2: Question 7 o Construct the NFA where Σ = {0, 1}, q 0 is the starting state, and q 2 is the final state. 0 1 q 0 q 0, q 1 q 2 q 1 q 2 q 1 q 2 q 1 q 2 13
Assignment #2: Question 8 o NFA: start state q 0, so DFA start state {q 0 } 14
Assignment #2: Question 8, cont d o NFA: δ(q 0, 0) = {q 0, q 1 }, so DFA δ({q 0 }, 0) = {q 0, q 1 } o NFA: δ(q 0, 1) = {q 2 }, so DFA δ({q 0 }, 1) = {q 2 } 15
Assignment #2: Question 8, cont d o NDA: δ(q 2, 0) = {q 1 }, so DFA δ({q 2 }, 0) = {q 1 } o NDA: δ(q 2, 1) = {q 2 }, so DFA δ({q 2 }, 1) = {q 2 } 16
Assignment #2: Question 8, cont d o NDA: δ(q 1, 0) = {q 2 }, so DFA δ({q 1 }, 0) = {q 2 } o NDA: δ(q 1, 1) = {q 1 }, so DFA δ({q 1 }, 1) = {q 1 } 17
Assignment #2: Question 8, cont d o NFA: δ(q 0, 0) = {q 0, q 1 } and δ(q 1, 0) = {q 2 }, so DFA δ({q 0, q 1 }, 0) = {q 0, q 1, q 2 } o NFA: δ(q 0, 1) = {q 2 } and δ(q 1, 1) = {q 1 }, so DFA δ({q 0, q 1 }, 1) = {q 1, q 2 } 18
Assignment #2: Question 8, cont d o NDA: δ(q 1, 0) = {q 2 } and δ(q 2, 0) = {q 1 }, so DFA δ({q 1, q 2 }, 0) = {q 1, q 2 } o NDA: δ(q 1, 1) = {q 1 } and δ(q 2, 1) = {q 2 }, so DFA δ({q 1, q 2 }, 1) = {q 1, q 2 } 19
Assignment #2: Question 8, cont d o NDA: δ(q 0, 0) = {q 0, q 1 } and δ(q 1, 0) = {q 2 } and δ(q 2, 0) = {q 1 }, so DFA δ({q 0, q 1, q 2 }, 0) = {q 0, q 1, q 2 } o NDA: δ(q 0, 1) = {q 2 } and δ(q 1, 1) = {q 1 } and δ(q 2, 1) = {q 2 }, so DFA δ({q 0, q 1, q 2 }, 1) = {q 1, q 2 } 20
Assignment #2: Question 8, cont d o NDA final state q 2, so DFA final states {q 2 }, {q 1, q 2 }, and {q 0, q 1, q 2 } 21
o Assignment #2: Question 9 Construct the DFA where Σ = {a, b}, q 0 is the starting state, and q 2 is the final state. a b q 0 q 2 q 2 q 1 q 2 q 2 q 2 q 3 q 3 q 3 q 3 q 1 22
Assignment #2: Question 10 o State q 2 is final: 0 1 3 2 o From states q 0 and q 1, all strings lead to final state q 2 : o 0 1 3 2 No further partitioning is possible. 23
Assignment #2: Question 10, cont d o Original: δ(q 0, w) = δ(q 1, w) = q 2 for all w in Σ Minimized: δ*({q 0, q 1 }, w) = {q 2 } o Original: δ(q 3, b) = q 1 Minimized: δ({q 3 }, b) = {q 0, q 1 } 24