240 likes | 433 Views
Theory of Computation. 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 www.peerinstruction4cs.org . Extra Credit Quiz.
E N D
Theory of Computation 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 www.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. • TRUE • FALSE
They’re really good guessers! Nondeterministic Finite AutomataNFA What is deterministic?
DFA or NFA? DFA NFA Both DFA and NFA
DFA or NFA? DFA NFA Both DFA and NFA
Tracing in an NFA “100” • What are the two sequences of states on the input “100”? • (q0,q0,q1,q2[accept]), (q0,q1,q2[accept]) Final: Accept • (q0,q0,q1,q2[accept]), (q0,q1,q2[reject]) Final: Accept • (q0,q0,q1,q2[accept]), (q0,q1,q2[reject]) Final: Reject • (q0,q0,q1,q2[reject]), (q0,q1,q2[reject]) Final: Reject
DFAs vs. NFAs DFAs 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”) • 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
Formal Definition of an NFA • A DFA M1 is defined as a 5-tuple as follows: • M1 = (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) NEED TO USE THIS FOR PROOFS---CANNOT MAKE GENERAL STATEMENTS BY DRAWING SPECIFIC EXAMPLES!
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) • TRUE • FALSE NEED TO USE THIS FOR PROOFS---CANNOT MAKE GENERAL STATEMENTS BY DRAWING SPECIFIC EXAMPLES!
A different (easier!) way to prove what we just proved Reg. Langs. Closed Under Union(Why NFAs are So Useful in proofs)
Final form for your homework/test: Thm. The class of regular languages is closed under the union operation. • Proof: • Given: Two regular languages L1, L2. • Want to show: L1 U L2 is regular. • Because L1 and L2 are regular, we know there exist DFAs M1 = (Q1,Σ,δ1,q01,F1) and M2 = (Q2,Σ,δ2,q02,F2) that recognize L1 and L2. We construct a DFA M = (Q,Σ,δ,q0,F), s.t.: • Q = Q1x Q2 • δ((x,y),c) = (δ1(x,c),δ2 (y,c)), for c in Σ and (x,y) in Q • q0 = (q01, q02) • F = {(x,y) in Q | x in F1or y in F2} • M recognizes L1 U L2. • Correctness: ___________________________________ • A DFA recognizes L1 U L2, so L1 U L2 is regular, and the class of regular languages is closed under union. Q.E.D. Could you come up with this, and write it correctly, in a short amount of time on an exam??
Final form for your homework/test: Thm. The class of regular languages is closed under the union operation. • Proof: • Given: Two regular languages L1, L2. • Want to show: L1 U L2 is regular. • Because L1 and L2 are regular, we know there exist DFAs M1 = (Q1,Σ,δ1,q01,F1) and M2 = (Q2,Σ,δ2,q02,F2) that recognize L1 and L2. We construct an NFAM = (Q,Σ,δ,q0,F), s.t.: • Q = • δ(x,c) = • q0 = • F = • M recognizes L1 U L2. • Correctness: ___________________________________ • An NFA recognizes L1 U L2, so L1 U L2 is regular, and the class of regular languages is closed under union. Q.E.D. Could you come up with this, and write it correctly, in a short amount of time on an exam??
Another construction proof Equivalence of Finite AutomataNFA & DFA
Tracing in NFA (Fig. 1.27 in your book) Run this NFA on input 010110 Fill in the missing row of states: • q2, q3, q4, q4, q4 • q1, q2, q4, q4, q4 • q1, q2, q3, q4, q4 • None of the above • I don’t understand this at all
Tracing in NFA (Fig. 1.27 in your book) Run this NFA on input 010110 • 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|! (e) I don’t understand this at all
Thm. 1.39: Every NFA has an equivalent DFA. Talk about sigma • Given: NFA M = (Q,Σ,δ,q0,F) • Want: DFA M’ = (Q’,Σ,δ’,q0’,F’) s.t. L(M) = L(M’). • Construction: //need to make a DFA that simulates nondeterminism within the constraints of determinism (!!) • Q’ = • Σ • δ’ • q0’ = • F’ = • A DFA recognizes L(M), then every NFA has an equivalent DFA. Q.E.D. P(Q) d’(R,a) = {q | q in d(r,a) for some r in R OR q can be reached by following 1+ epsilon edges from some r in R} q’0 = {q0} U {q in Q | q can be reached by following 1+ epsilon edges from q0} { R in Q’ | R contains at least one accept state of M}
Thm. 1.39: Every NFA has an equivalent DFA. • We also know every DFA isan 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!
Extremely useful Regular ExpressionsPattern Matching
From the Reading Quiz • Let L be the language of this regular expression: 1*0 • Which of the following is NOT in L? • 10 • 100 • 110 • 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? • Some strings in L have equal numbers of a’s and b’s • All strings in L have more b’s than a’s • L contains “aaaaaa” • a‘s never follow b’s in any string in L • 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? • L is the empty set • L contains “a” • aaab is not in L • None or more than one of the above