Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. Based on a work at Cynthia Lee, UCSDCreative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported Licensewww.peerinstruction4cs.org
Extra Credit Quiz If there is somebody in your group who seems really smart, it is a good idea to just let that person do all the talking like a “mini-teacher,” so that you can learn from them. a)TRUE b)FALSE
NONDETERMINISTIC FINITE AUTOMATA NFA They’re really good guessers!
DFA or NFA? a)DFA b)NFA c)Both DFA and NFA
DFA or NFA? a)DFA b)NFA c)Both DFA and NFA
Tracing in an NFA What are the two sequences of states on the input “100”? a)(q0,q0,q1,q2[accept]), (q0,q1,q2[accept]) Final: Accept b)(q0,q0,q1,q2[accept]), (q0,q1,q2[reject]) Final: Accept c)(q0,q0,q1,q2[accept]), (q0,q1,q2[reject]) Final: Reject d)(q0,q0,q1,q2[reject]), (q0,q1,q2[reject]) Final: Reject “100”
DFAs vs. NFAs DFAs For each character in the alphabet, exactly one transition leaving every state Computation is “deterministic,” i.e. determined by the input, i.e., the same every time for a given input NFAs There may be 0, 1, or many transitions leaving a single state for the same input character Transition function defined on “epsilon” in addition to alphabet characters There may be several different ways to reach an accept state for a single string—the computation may not determined by the input (“nondeterministic”)
Formal Definition of an NFA A DFA M 1 is defined as a 5-tuple as follows: M 1 = (Q, Σ, δ, q0, F), where: – Q is a finite set of states – Σ is a finite set of characters, the alphabet – δ: Q x Σ -> P (Q), the transition function – q0, a member of Q, the start state – F, a subset of Q, the accept state(s) 8
Nondeterminism Because NFAs are non-deterministic, the outcome (accept/reject) of the computation may be different from run to run (i.e., isn’t determined by the input) a)TRUE b)FALSE 9
REG. LANGS. CLOSED UNDER UNION (WHY NFAS ARE SO USEFUL IN PROOFS) A different (easier!) way to prove what we just proved
Thm. The class of regular languages is closed under the union operation. Proof: Given: Two regular languages L 1, L 2. Want to show: L 1 U L 2 is regular. Because L 1 and L 2 are regular, we know there exist DFAs M 1 = (Q 1,Σ,δ 1,q 01,F 1 ) and M 2 = (Q 2,Σ,δ 2,q 02,F 2 ) that recognize L 1 and L 2. We construct a DFA M = (Q,Σ,δ,q 0,F), s.t.: – Q = Q 1 x Q 2 – δ((x,y),c) = (δ 1 (x,c), δ 2 (y,c)), for c in Σ and (x,y) in Q – q 0 = (q 01, q 02 ) – F = {(x,y) in Q | x in F 1 or y in F 2 } M recognizes L 1 U L 2. Correctness: ___________________________________ A DFA recognizes L 1 U L 2, so L 1 U L 2 is regular, and the class of regular languages is closed under union. Q.E.D.
Thm. The class of regular languages is closed under the union operation. Proof: Given: Two regular languages L 1, L 2. Want to show: L 1 U L 2 is regular. Because L 1 and L 2 are regular, we know there exist DFAs M 1 = (Q 1,Σ,δ 1,q 01,F 1 ) and M 2 = (Q 2,Σ,δ 2,q 02,F 2 ) that recognize L 1 and L 2. We construct an NFA M = (Q,Σ,δ,q 0,F), s.t.: – Q = – δ(x,c) = – q 0 = – F = M recognizes L 1 U L 2. Correctness: ___________________________________ An NFA recognizes L 1 U L 2, so L 1 U L 2 is regular, and the class of regular languages is closed under union. Q.E.D.
Back to our working example
EQUIVALENCE OF FINITE AUTOMATA NFA & DFA Another construction proof
Tracing in NFA Fill in the missing row of states: a)q2, q3, q4, q4, q4 b)q1, q2, q4, q4, q4 c)q1, q2, q3, q4, q4 d)None of the above e)I don’t understand this at all (Fig in your book) Run this NFA on input
Tracing in NFA Each row is a set of states that we are in at the “same time” – {q1} – {q1,q2,q3} – {q1,q3} – {q1,q2,q3,q4} – {q1,q3,q4} Recall that when we did the union closure proof with DFAs, we were always in a pair of states at the “same time”—same concept What are all the possible unique sets of states? (a) QxQ (b) |Q| 2 (c) P (Q) (d) |Q|! (Fig in your book) Run this NFA on input (e) I don’t understand this at all
Thm. 1.39: Every NFA has an equivalent DFA. Given: NFA M = (Q,Σ,δ,q 0,F) Want: DFA M’ = (Q’,Σ,δ’,q 0 ’,F’) s.t. L(M) = L(M’). Construction: //need to make a DFA that simulates nondeterminism within the constraints of determinism (!!) – Q’ = – Σ – δ’ – q 0 ’ = – F’ = A DFA recognizes L(M), then every NFA has an equivalent DFA. Q.E.D.
Thm. 1.39: Every NFA has an equivalent DFA. We also know every DFA is an NFA. Corollary of these two facts: The class of languages recognized by DFAs and the class of languages recognized by NFAs are the same class – The Class of Regular Languages It may be surprising that adding something as powerful and magic-seeming as instantaneous, 100% accurate guessing to the DFA model could turn out to not increase the power of the model! – This course is full of surprises!
REGULAR EXPRESSIONS PATTERN MATCHING Extremely useful
From the Reading Quiz Let L be the language of this regular expression: 1*0 Which of the following is NOT in L? a)10 b)100 c)110 d)They’re all in L
Regular Expressions and UNIX/Linux Shell Wildcard 1*0 = “Any number of 1’s (including no 1’s), followed by a single 0” – “100” not accepted by that RE This may be confusing to those who are used to *’s usage in, say, UNIX/Linux shell—VERY DIFFERENT – In UNIX/Linux * means “replace with anything here” 1*0 matches “100” and “1blahblahblah0” – Typical shell usage: “ls *.jpg” (to list all JPEG files) – This is very useful to know! Just don’t confuse it with REs in this course
Regular Expressions Let L be the language of this regular expression: ((a U Ø) + b*)* Which of the following is NOT true of L? a)Some strings in L have equal numbers of a’s and b’s b)All strings in L have more b’s than a’s c)L contains “aaaaaa” d)a‘s never follow b’s in any string in L e)None or more than one of the above
Regular Expressions Let L be the language of this regular expression: aØb* Which of the following is NOT true of L? a)L is the empty set b)L contains “a” c)aaab is not in L d)None or more than one of the above