Nondeterminism Nondeterministic Finite Automt Nondeterminism Subset Construction A nondeterministic finite utomton hs the bility to be in severl sttes t once. Trnsitions from stte on n input symbol cn be to ny set of sttes. Modified from Jeff Ullmn s slides 2 Nondeterminism (2) Strt in one strt stte. Accept if ny sequence of choices leds to finl stte. Intuitively: the NFA lwys guesses right. Exmple: Moves on Chessbord Sttes = squres. Inputs = r (move to n djcent red squre) nd b (move to n djcent blck squre); digonl is djcent. Strt stte, finl stte re in opposite corners. 4 Exmple: Chessbord (2) 2 4 6 8 9 b 2 4 9 2,4 2 4,6,, 4 2,8,, 2,4,6,8,,,9 Forml NFA A finite set of sttes, typiclly Q. An input lphbet, typiclly Σ. A trnsition function, typiclly δ. A strt stte in Q, typiclly q. A set of finl sttes F Q. Accept, since finl stte reched 6
Trnsition Function of n NFA δ(q, ) is set of sttes. Extend to strings s follows: Bsis: δ(q, ) = {q} Induction: δ(q, w) = the union over ll sttes p in δ(q, w) of δ(p, ) Lnguge of n NFA A string w is ccepted by n NFA if δ(q, w) contins t lest one finl stte. The lnguge of the NFA is the set of strings it ccepts. Cn NFAs do more thn DFAs? 8 Exmple: Lnguge of n NFA 2 4 6 8 9 Equivlence of DFA s, NFA s For our chessbord NFA we sw tht rbb is ccepted. If the input consists of only b s, the set of ccessible sttes lterntes between {} nd {,,,9}, so only even-length, nonempty strings of b s re ccepted. Wht bout strings with t lest one r? 9 A DFA D cn be turned into n NFA N tht ccepts the sme lnguge. If δ D (q, ) = p, then let the NFA hve δ N (q, ) = {p}. Then the NFA is lwys in set contining exctly one stte the stte the DFA is in fter reding the sme input. Equivlence (2) Subset Construction Surprisingly, for ny NFA there is DFA tht ccepts the sme lnguge. Proof is the subset construction. The number of sttes of the DFA cn be exponentil in the number of sttes of the NFA. Thus, NFA s ccept exctly the regulr lnguges. Given n NFA with sttes Q, inputs Σ, trnsition function δ N, stte stte q, nd finl sttes F, construct equivlent DFA with: Stte set 2 Q (Set of subsets of Q). Inputs Σ. Strt stte {q }. Finl sttes = ll those with member of F. 2 2
Criticl Point The DFA sttes hve nmes tht re sets of NFA sttes. But s DFA stte, n expression like {p,q} must be red s single symbol, not s set. Anlogy: clss of objects whose vlues re sets of objects of nother clss, e.g. the stte of hsh tble Subset Construction (2) The trnsition function δ D is defined by: δ D ({q,,q k }, ) is the union over ll i =,,k of δ N (q i, ). Exmple: We ll construct the DFA equivlent of our chessbord NFA. 4 Exmple: Subset Construction 2,4 2 4,6,, 4 2,8,, 2,4,6,8,,,9 {} {2,4} {} {2,4} {} Alert: Wht we re doing here is the lzy form of DFA construction, where we only construct stte Exmple: Subset Construction 2,4 2 4,6,, 4 2,8,, 2,4,6,8,,,9 {} {2,4} {} {2,4} {2,4,6,8} {,,,} {} {2,4,6,8} {,,,} if we re forced to. (how mny?) 6 Exmple: Subset Construction Exmple: Subset Construction 2,4 2 4,6,, 4 2,8,, 2,4,6,8,,,9 {} {2,4} {} {2,4} {2,4,6,8} {,,,} {} {2,4,6,8} {,,,9} {2,4,6,8} {,,,} {,,,9} 2,4 2 4,6,, 4 2,8,, 2,4,6,8,,,9 {} {2,4} {} {2,4} {2,4,6,8} {,,,} {} {2,4,6,8} {,,,9} {2,4,6,8} {2,4,6,8} {,,,,9} {,,,} {,,,9} {,,,,9} 8
Exmple: Subset Construction Exmple: Subset Construction 2,4 2 4,6,, 4 2,8,, 2,4,6,8,,,9 {} {2,4} {} {2,4} {2,4,6,8} {,,,} {} {2,4,6,8} {,,,9} {2,4,6,8} {2,4,6,8} {,,,,9} {,,,} {2,4,6,8} {,,,,9} {,,,9} {,,,,9} 2,4 2 4,6,, 4 2,8,, 2,4,6,8,,,9 {} {2,4} {} {2,4} {2,4,6,8} {,,,} {} {2,4,6,8} {,,,9} {2,4,6,8} {2,4,6,8} {,,,,9} {,,,} {2,4,6,8} {,,,,9} {,,,9} {2,4,6,8} {} {,,,,9} 9 2 Exmple: Subset Construction 2,4 2 4,6,, 4 2,8,, 2,4,6,8,,,9 {} {2,4} {} {2,4} {2,4,6,8} {,,,} {} {2,4,6,8} {,,,9} {2,4,6,8} {2,4,6,8} {,,,,9} {,,,} {2,4,6,8} {,,,,9} {,,,9} {2,4,6,8} {} {,,,,9} {2,4,6,8} {,,,,9} 2 Proof of Equivlence: Subset Construction Show by induction on w tht δ N (q, w) = δ D ({q }, w) IH(n): for ll w of length n, δ N (q, w) = δ D ({q }, w) Bsis: Show IH(). Only string of length is, nd δ N (q, ) = {q } δ D ({q }, ) = {q } 22 Induction Step () Assume n nd IH (n) to show IH(n+) Let w be n rbitrry string of length n+ Need to show δ N (q, w) = δ D ({q }, w) Let w = x, so x =n, (use IH(n) on x) Let S = δ N (q, x); By IH(N), S = δ D ({q }, x) δ N (q, w) = δ N (q, x) = the union over ll sttes p in S of δ N (p, ) Induction Step (2) Assume n nd IH (n) to show IH(n+) w = x; x =n; δ N (q, x)=s=δ D ({q }, x) δ N (q, w) = union over ll sttes p in S of δ N (p, ) δ D ({q }, w) = δ D ({q }, x) = δ D ( δ D ({q }, x), ) def. ext. δ D = δ D ( S, ) by def. S = the union over ll sttes p in S of δ N (p, ) by def. δ D by def. extension of δ N So δ 2 N (q, w) = δ D ({q }, w) 24 4
NFA s With -Trnsitions Exmple: -NFA We cn llow stte-to-stte trnsitions on input. These trnsitions re done spontneously, without looking t the input string. A convenience t times, but still only regulr lnguges re ccepted. A B E C F D A {E} {B} B {C} {D} C {D} D E {F} {B, C} F {D} 2 26 Closure of Sttes CL(q) = set of sttes you cn rech from stte q following only rcs lbeled. Exmple: CL(A) = {A}; CL(E) = {B, C, D, E}. A B C D Closure of set of sttes = union of the closure of ech stte. E F 2 Extended Delt Bsis: δ (q, ) = CL(q). Induction: δ(q, x) is computed s follows:. Strt with (q, x) = S. δ 2. Tke the union of CL(δ(p, )) for ll p in S. Intuition: δ (q, w) is the set of sttes you cn rech from q following pth lbeled w. And notice tht δ(q, ) is not 28 tht set of sttes, for symbol. Exmple: Extended Delt δ (A, ) = CL(A) = {A}. A B C D (A, ) = CL({E}) = {B, C, D, E}. δ δ(a, ) = CL({C, D}) = {C, D}. Lnguge of n -NFA is the set of strings w such tht δ(q, w) contins finl stte. E F 29 Equivlence of NFA, -NFA Every NFA is n -NFA. It just hs no trnsitions on. Converse requires us to tke n -NFA nd construct n NFA tht ccepts the sme lnguge. We do so by combining trnsitions with the next trnsition on rel input. Wrning: This tretment is bit different from tht in the text.
Picture of -Trnsition Removl Picture of -Trnsition Removl Text goes from here To here, nd performs the subset construction Trnsitions on Trnsitions on Trnsitions on Trnsitions on 2 Picture of -Trnsition Removl We ll go from here Trnsitions on To here, with no subset construction Trnsitions on Equivlence (2) Strt with n -NFA with sttes Q, inputs Σ, strt stte q, finl sttes F, nd trnsition function δ E. Construct n ordinry NFA with sttes Q, inputs Σ, strt stte q, finl sttes F, nd trnsition function δ N. 4 Equivlence () Equivlence (4) Compute δ N (q, ) s follows:. Let S = CL(q). 2. δ N (q, ) is the union over ll p in S of δ E (p, ). F = the set of sttes q such tht CL(q) contins stte of F. Intuition: δ N incorportes trnsitions before using but not fter. Prove by induction on w tht CL(δ N (q, w)) = δ E (q, w). Thus, the -NFA ccepts w if nd only if the ordinry NFA does. 6 6
Interesting closures: CL(B) = {B,D}; CL(E) = {B,C,D,E} A {E} {B} B {C} {D} C {D} D E {F} {B, C} F {D} -NFA Exmple: -NFAto-NFA Since closures of B nd E include finl stte D. A {E} {B} B {C} C {D} D E {F} {C, D} F {D} Since closure of E includes B nd C; which hve trnsitions on to C nd D. Summry DFA s, NFA s, nd NFA s ll ccept exctly the sme set of lnguges: the regulr lnguges. The NFA types re esier to design nd my hve exponentilly fewer sttes thn DFA. But only DFA cn be implemented! 8