Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 8 Non-Deterministic Finite Automata 0,1 0,1 0 0,ε q q 1 q 2 3 1 q 4 an NFA may have more than one transition labeled with the same symbol, an NFA may have no transitions labeled with a certain symbol, and an NFA may have transitions labeled with ε, theempty string. Comment: Every DFA is also a non-deterministic finite automata (NFA).
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 9 Non-Deterministic Computation 0,1 0,1 0 0,ε q q 1 q 2 3 1 q 4 What happens when more than one transition is possible? the machine splits into multiple copies each branch follows one possibility together, branches follow all possibilities. If the input doesn t appear, that branch dies. Automaton accepts if some branch accepts. What does an ε transition do?
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 10 Non-Deterministic Computation 0,1 0,1 0 0,ε q q q 1 2 3 1 q 4 What happens on string 1001?
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 11 The String 1001 0,1 0,1 symbol 1 q 1 0 0,ε q q q 1 2 3 1 q 4 0 q 1 0 q 1 q 2 q 3 1 q q 1 q 2 3 q 3 q q q 1 4 4
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 12 Why Non-Determinism? Theorem (to be proved soon): Deterministic and non-deterministic finite automata accept exactly the same set of languages. Q.: Sowhy do we need them? A.: NFAs are often easier to design than equivalent DFAs. Example: Design a finite automaton that accepts all strings with a 1 in their third-to-the-last position? NFA? DFA?
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 13 Solving with NFA 0,1 1 0,1 q q 1 q 2 3 0,1 q 4 Guesses which symbol is third from the last, and checks that indeed it is a 1. If guess is premature, that branch dies, and no harm occurs.
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 15 Solving with DFA 0 1 0
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 14 Solving with DFA Have 8 states, encoding the last three letters. A state for each string in {0, 1} 3. add transitions on modifying the suffix, give the new letter. Mark as accepting, the strings 1
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 16 NFA Formal Definition Transition function δ is going to be different. Let P(Q) denote the powerset of Q. Let Σ ε denote Σ {ε}. A non-deterministic finite automaton is a 5-tuple (Q, Σ, δ,q 0,F), where Q is a finite set called the states, Σ is a finite set called the alphabet, δ : Q Σ ε P(Q) is the transition function, q 0 Q is the start state, and F Q is the set of accept states.
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 17 Example 0,1 0,1 0 0,ε 1 q q 1 q 2 3 where Q = {q 1,q 2,q 3,q 4 }, Σ = {0, 1}, q 4 N 1 =(Q, Σ, δ,q 1,F) δ is 0 1 ε q 1 {q 1,q 2 } {q 1 } q 2 {q 3 } {q 3 } q 3 {q 4 } q 4 {q 4 } {q 4 } q 1 is the start state, and F = {q 4 }.
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 18 Formal Model of Computation Let M =(Q, Σ, δ,q 0,F) be an NFA, and w be a string over Σ ε that has the form y 1 y 2 y m where y i Σ ε. u be the string over Σ obtained from w by omitting all occurances of ε. Suppose there is a sequence of states (in Q), r 0,...,r n,suchthat r 0 = q 0 r i+1 δ(r i,y i+1 ), 0 i<n r n F Then we say that M accepts u.
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 19 Equivalence of NFA s and DFA s Given an NFA, N, weconstructadfa, M, thataccepts the same language. To begin with, we make things easier by ignoring ε transitions. Make DFA simulate all possible NFA states. As consequence of the construction, if the NFA has k states, the DFA has 2 k states (an exponential blow up).
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 20 Equivalence of NFA s and DFA s Let N =(Q, Σ, δ,q 0,F) be the NFA accepting A. Construct a DFA M =(Q, Σ, δ,q 0,F ). Q = P(Q). For R Q and a Σ, let q 0 = {q 0} δ (R, a) = {q Q q δ(r, a) for some r R} F = {R Q R contains an accept state of N} Notice: F is a set whose elements are subsets of Q, so (as expected) F is a subset of P(Q).
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 21 Equivalence of NFA s and DFA s Extend the transition function to work on words: δ(q, w 1 w n )=δ(δ(q, w 1 w n 1 ),w n ). Define an equivalence between subset of states in the NFA and the states in DFA Inductive Claim: For any word y = y 1 y 2 y m Σ of length m, δ(q 0,y) is equivalent to δ (q 0,y). Proof by induction on m. Base of the induction. Inductive step. Example.
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 22 Example: NFA DFA Non-Deterministic Automata:
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 23 Example: NFA DFA Deterministic Automata - set of states:
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 24 Example: NFA DFA Deterministic Automata - transitions from q 0 :
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 25 Example: NFA DFA Deterministic Automata:
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 26 Dealing with ε-transitions For any state R of M, definee(r) to be the collection of states reachable from R by ε transitions only. E(R) = {q Q q can be reached from some r R by 0 or more ε transitions} Define transition function: δ (R, a) = {q Q q E(δ(r, a)) for some r R} Change start state to Example. q 0 = E({q 0})
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 27 Example: Removing ε-transitions Non-Deterministic Automata with ε-tansitions
Slides modified Yishay Mansour on modification by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 28 Example: Removing ε-transitions Non-Deterministic Automata without ε-tansitions
The Regular Operations Let A and B be languages. The union operation: A B = {x x A or x B} The concatenation operation: A B = {xy x A and y B} The star operation: A = {x 1 x 2...x k k 0 and each x i A}
The Regular Operations Examples Let A= {good, bad} and B = {boy, girl}. Union A B = {good, bad, boy, girl} Concatenation A B = {goodboy, goodgirl, badboy, badgirl} Star A = {ε, good, bad, goodgood, goodbad, badbad, badgood,...}
Regular Expressions Anotationforbuildinguplanguagesbydescribingthemas expressions, e.g. (0 1)0. 0 and 1 are shorthand for {0} and {1} so (0 1) = {0, 1}. 0 is shorthand for {0}. concatenation, like multiplication, is implicit, so 0 10 is shorthand for the set of all strings over Σ = {0, 1} having exactly a single 1. Q.: Whatdoes(0 1)0 stand for? Remark: Regularexpressionsareoftenusedintexteditors or shell scripts.
More Examples Let Σ be an alphabet. The regular expression Σ is the language of one-symbol strings. Σ is all strings. Σ 1 all strings ending in 1. 0Σ Σ 1 strings starting with 0 or ending in 1. Just like in arithmetic, operations have precedence: star first concatenation next union last parentheses used to change default order
Regular Expressions Formal Definition Syntax: R is a regular expression if R is of form a for some a Σ ε (R 1 R 2 ) for regular expressions R 1 and R 2 (R 1 R 2 ) for regular expressions R 1 and R 2 (R1 ) for regular expression R 1
Regular Expressions Formal Definition Let L(R) be the language denoted by regular expression R. R L(R) a {a} ε {ε} (R 1 R 2 ) L(R 1 ) L(R 2 ) (R 1 R 2 ) L(R 1 ) L(R 2 ) (R 1 ) L(R 1 ) Q.: What sthedifferencebetween and ε? Q.: Isn tthisdefinitioncircular?
Remarkable Fact Thm.: A language, L, is described by a regular expression, R, if and only if L is regular. = construct an NFA accepting R. = Given a regular language, L, constructan equivalent regular expression.
Regular Languages, Revisited By definition, a language is regular if it is accepted by some DFA. Corollary: Alanguageisregularifandonlyifitisaccepted by some NFA. This is an alternative way of characterizing regular languages. We will now use the equivalence to show that regular languages are closed under the regular operations (union, concatenation, star).
Claim: Closure Under Union If A 1 and A 2 are regular languages, so is A 1 A 2. Approach to Proof: some M 1 accepts A 1 some M 2 accepts A 2 construct M that accepts A 1 A 2. Attempted Proof Idea: first simulate M 1, and if M 1 doesn t accept, then simulate M 2.
Claim: Closure Under Union If A 1 and A 2 are regular languages, so is A 1 A 2. Approach to Proof: some M 1 accepts A 1 some M 2 accepts A 2 construct M that accepts A 1 A 2. Attempted Proof Idea: first simulate M 1, and if M 1 doesn t accept, then simulate M 2. What s wrong with this?
Claim: Closure Under Union (reminder) If A 1 and A 2 are regular languages, so is A 1 A 2. Approach to Proof: some M 1 accepts A 1 some M 2 accepts A 2 construct M that accepts A 1 A 2. in our construction, states of M were Cartesian product of M 1 and M 2 states.
Closure Under Union (alternative proof) N 1 N 2
Regular Languages Closed Under Union! N 1! N 2
Regular Languages Closed Under Union Suppose N 1 =(Q 1, Σ, δ 1,q 1,F 1 ) accept L 1,and N 2 =(Q 2, Σ, δ 2,q 2,F 2 ) accept L 2. Define N =(Q, Σ, δ,q 0,F): Q = q 0 Q 1 Q 2 Σ is the same, q 0 is the start state F = F 1 F 2 δ (q, a) = δ 1 (q, a) q Q 1 δ 2 (q, a) q Q 2 {q 1,q 2 } q = q 0 and a = ε q = q 0 and a ε
What About Concatenation? Thm: IfL 1, L 2 are regular languages, so is L 1 L 2. Example: L 1 = {good, bad} and L 2 = {boy, girl}. L 1 L 2 = {goodboy, goodgirl, badboy, badgirl} This is much harder to prove. Idea: SimulateM 1 for a while, then switch to M 2. Problem: Butwhen do you switch? Seems hard to do with DFAs. This leads us into non-determinism.
Regular Languages Closed UnderConcatenation N 1 N 2
Regular LanguagesClosed Under Concatenation N 1! N 2! Remark: Final states are exactly those of N 2.
Regular LanguagesClosed Under Concatenation Suppose N 1 =(Q 1, Σ, δ 1,q 1,F 1 ) accept L 1,and N 2 =(Q 2, Σ, δ 2,q 2,F 2 ) accept L 2. Define N =(Q, Σ, δ,q 1,F 2 ): Q = Q 1 Q 2 q 1 is the start state of N F 2 is the set of accept states of N δ 1 (q, a) q Q 1 and q/ F 1 δ δ 1 (q, a) q Q 1 and a ε (q, a) = δ 1 (q, a) {q 2 } q F 1 and a = ε δ 2 (q, a) q Q 2
Regular Languages Closed Under Star N 1 N 1!! Oops - bad construction (really? why?). How do we fix it?
Regular Languages Closed Under Star N 1 accepts R 1.WannabuildNFAforR =(R 1 ). q 0 ε ε Ahaa - abetterconstruction!
Regular Languages Closed Under Star Suppose N 1 =(Q 1, Σ, δ 1,q 1,F 1 ) accepts L 1. Define N =(Q, Σ, δ, q 0,F): Q = {q 0 } Q 1 q 0 is the new start state. F = {q 0 F 1 } δ 1 (q, a) q Q 1 and q/ F 1 δ 1 (q, a) q F 1 and a ε δ (q, a) = δ 1 (q, ε) {q 1 } q F 1 and a = ε {q 1 } q = q 0 and a = ε q = q 0 and a ε
Summary Regular languages are closed under union concatenation star Non-deterministic finite automata are equivalent to deterministic finite automata but much easier to use in some proofs and constructions.
Given R, Build NFA Accepting It (= ) a 1. R = a,for some a Σ 2. R = ε 3. R =
Given R, Build NFA Accepting It (= ) N 1!! N 2 N 1!! N 2 R =(R 1 R 2 ) R =(R 1 R 2 ) R =(R 1 ) q 0 ε ε
Approximately Correct Examples a a b b ab b! a! b! a ab U a only approximately?)! a (why
Automaton Expression L(q,S,r): expression for all paths (in A) from state q to state r passing through (intermediate) states S only Defined recursively L(q,,r) = sum (union) of all a for which there s an edge q to r labeled a, plus ε if q=r L(q,S {s},r) = [L(q,S,r)+L(q,S,s)L(s,S,s)*L(s,S,r)]
What We Just Completed Thm.: A language, L, is described by a regular expression, R, if and only if L is regular. = construct an NFA accepting R. = Given a regular language, L, constructan equivalent regular expression
Algorithmic Questions for NFAs Q.: GivenanNFA,N, andastrings, iss L(N)? Answer: ConstructtheDFAequivalenttoN and run it on w. Q.: IsL(N) =? Answer: This is a reachability question in graphs: Is there a path in the states graph of N from the start state to some accepting state. There are simple, efficient algorithms for this task.
More Algorithmic Questions for NFAs Q.: IsL(N) =Σ? Answer: CheckifL(N) =. Q.: GivenN 1 and N 2,isL(N 1 ) L(N 2 )? Answer: CheckifL(N 2 ) L(N 1 )=. Q.: GivenN 1 and N 2,isL(N 1 )=L(N 2 )? Answer: CheckifL(N 1 ) L(N 2 ) and L(N 2 ) L(N 1 ). In the future, we will see that for stronger models of computations, many of these problems cannot be solved by any algorithm.