Finite Automt Theory nd Forml Lnguges TMV027/DIT321 LP4 2018 Lecture 10 An Bove April 23rd 2018 Recp: Regulr Lnguges We cn convert between FA nd RE; Hence both FA nd RE ccept/generte regulr lnguges; More on lgebric lws for RE nd how to prove them; We use the Pumping lemm to show tht lnguge is NOT regulr; RL re closed under: Union, complement, intersection, difference, conctention, closure; Prefix, reversl; Closure properties cn be used both to prove tht lnguge IS regulr or tht lnguge is NOT regulr. April 23rd 2018, Lecture 10 TMV027/DIT321 1/25
Overview of Tody s Lecture Decision properties for RL; Equivlence of RL; Minimistion of utomt. Contributes to the following lerning outcome: Explin nd mnipulte the diff. concepts in utomt theory nd forml lng; Understnd the power nd the limittions of regulr lng nd context-free lng; Prove properties of lnguges, grmmrs nd utomt with rigorously forml mthemticl methods; Design utomt, regulr expressions nd context-free grmmrs ccepting or generting certin lnguge; Describe the lnguge ccepted by n utomt or generted by regulr expression or context-free grmmr; Simplify utomt nd context-free grmmrs; Determine if certin word belongs to lnguge; Differentite nd mnipulte forml descriptions of lng, utomt nd grmmrs. April 23rd 2018, Lecture 10 TMV027/DIT321 2/25 Decision Properties of Regulr Lnguges We wnt to be ble to nswer YES/NO to questions such s Is string w in the lnguge L? Is this lnguge empty? Are these 2 lnguges equivlent? In generl lnguges re infinite so we cnnot do mnul checking. Insted we work with the finite description of the lnguges (DFA, NFA. ǫ-nfa, RE). Which description is most convenient depends on the property nd on the lnguge. April 23rd 2018, Lecture 10 TMV027/DIT321 3/25
Testing Membership in Regulr Lnguges Given RL L nd word w over the lphbet of L, is w L? When L is given by FA we cn simply run the FA with the input w nd see if the word is ccepted by the FA. We hve seen n lgorithm simulting the running of DFA (nd you hve implemented lgorithms simulting the running of NFA nd ǫ-nfa, right? :-). Using derivtives (see exercises 4.2.3 nd 4.2.5) there is nice lgorithm checking membership on RE. Let M = L(R) nd w = 1... n. Let \R = D R = {x x M} (in the book dm d ). D w R = D n (... (D 1 R)...). It cn then be shown tht w M iff ǫ D w R. April 23rd 2018, Lecture 10 TMV027/DIT321 4/25 Testing Emptiness of Regulr Lnguges given FA Given FA for lnguge, testing whether the lnguge is empty or not mounts to checking if there is pth from the strt stte to finl stte. Let D = (Q, Σ, δ, q 0, F ) be DFA. Recll the notion of ccessible sttes: Acc = {ˆδ(q 0, x) x Σ }. Proposition: Given D s bove, then D = (Q Acc, Σ, δ Q Acc, q 0, F Acc) is DFA such tht L(D) = L(D ). In prticulr, L(D) = if F Acc =. (Actully, L(D) = iff F Acc = since if ˆδ(q 0, x) F then ˆδ(q 0, x) F Acc.) April 23rd 2018, Lecture 10 TMV027/DIT321 5/25
Testing Emptiness of Regulr Lnguges given FA A recursive lgorithm to test whether stte is ccessible/rechble is s follows: Bse cse: The strt stte q 0 is rechble from q 0. Recursive step: If q is rechble from q 0 nd there is n rc from q to p (with ny lbel, including ǫ) then p is lso rechble from q 0. (This step is repeted until no new rechble stte is found.) (This lgorithm is n instnce of grph-rechbility.) If the set of rechble sttes contins t lest one finl stte then the RL is NOT empty. Exercise: Progrm this! April 23rd 2018, Lecture 10 TMV027/DIT321 6/25 Testing Emptiness of Regulr Lnguges given RE Given RE for the lnguge we cn insted define the function: isempty : RE Bool isempty( ) = True isempty(ǫ) = Flse isempty() = Flse isempty(r 1 + R 2 ) = isempty(r 1 ) isempty(r 2 ) isempty(r 1 R 2 ) = isempty(r 1 ) isempty(r 2 ) isempty(r ) = Flse April 23rd 2018, Lecture 10 TMV027/DIT321 7/25
Functionl Representtion of Testing Emptiness for RE dt RExp = Empty Epsilon Atom Plus (RExp ) (RExp ) Conct (RExp ) (RExp ) Str (RExp ) isempty :: RExp -> Bool isempty Empty = True isempty (Plus e1 e2) = isempty e1 && isempty e2 isempty (Conct e1 e2) = isempty e1 isempty e2 isempty _ = Flse April 23rd 2018, Lecture 10 TMV027/DIT321 8/25 Other Testing Algorithms on Regulr Expressions Tests if RE genertes ǫ. hsepsilon : RE Bool hsepsilon( ) = Flse hsepsilon(ǫ) = True hsepsilon() = Flse hsepsilon(r 1 + R 2 ) = hsepsilon(r 1 ) hsepsilon(r 2 ) hsepsilon(r 1 R 2 ) = hsepsilon(r 1 ) hsepsilon(r 2 ) hsepsilon(r ) = True April 23rd 2018, Lecture 10 TMV027/DIT321 9/25
Other Testing Algorithms on Regulr Expressions Tests if R genertes t most ǫ: L(R) {ǫ}. tmosteps : RE Bool tmosteps( ) = True tmosteps(ǫ) = True tmosteps() = Flse tmosteps(r 1 + R 2 ) = tmosteps(r 1 ) tmosteps(r 2 ) tmosteps(r 1 R 2 ) = isempty(r 1 ) isempty(r 2 ) (tmosteps(r 1 ) tmosteps(r 2 )) tmosteps(r ) = tmosteps(r) April 23rd 2018, Lecture 10 TMV027/DIT321 10/25 Other Testing Algorithms on Regulr Expressions Tests if regulr expression genertes n infinite lnguge. infinite : RE Bool infinite( ) = Flse infinite(ǫ) = Flse infimote() = Flse infinite(r 1 + R 2 ) = infinite(r 1 ) infinite(r 2 ) infinite(r 1 R 2 ) = (infinite(r 1 ) (isempty(r 2 )) ( (isempty(r 1 )) infinite(r 2 )) infinite(r ) = (tmosteps(r)) April 23rd 2018, Lecture 10 TMV027/DIT321 11/25
Testing Equivlence of Regulr Lnguges We hve seen how one cn prove tht 2 RE re equl, hence the lnguges they represent re equivlent (but this is not n esy process). We will see now how to test when 2 DFA describe the sme lnguge. April 23rd 2018, Lecture 10 TMV027/DIT321 12/25 Testing Equivlence of Sttes in DFA How to nswer the question do sttes p nd q behve in the sme wy? Definition: We sy tht sttes p nd q re equivlent if for ll w, ˆδ(p, w) is n ccepting stte iff ˆδ(q, w) is n ccepting stte. Note: We do not require tht ˆδ(p, w) = ˆδ(q, w)! Definition: If p nd q re not equivlent, then they re distinguishble. Tht is, there exists t lest one w such tht one of ˆδ(p, w) nd ˆδ(q, w) is n ccepting stte nd the other is not. April 23rd 2018, Lecture 10 TMV027/DIT321 13/25
Exmple: Identifying Distinguishble Pirs Let us find the distinguishble pirs in the following DFA. q 0 b q 1 q 2 b b q 3 q 4, b, b q 5, b q 0 q 1 q 2 q 3 q 4 q 5 X X X X X q 4 X X X q 3 X X X q 2 X X q 1 If p is ccepting nd q is not, then the word ǫ distinguish them. δ(q 1, ) = q 3 nd δ(q 5, ) = q 5. Since (q 3, q 5 ) is distinguishble so must be (q 1, q 5 ). Wht bout δ(q 2, ) nd δ(q 5, )? Wht bout the pirs (q 0, q 3 ) nd (q 0, q 4 ) with the input? Finlly, let us consider the pirs (q 3, q 4 ) nd (q 1, q 2 ). April 23rd 2018, Lecture 10 TMV027/DIT321 14/25 Tble-Filling Algorithm This lgorithm finds pirs of sttes tht re distinguishble. Any 2 sttes tht we do not find distinguishble re equivlent (see slide 17). Let D = (Q, Σ, δ, q 0, F ) be DFA. The tble-filling lgorithm is s follows: Bse cse: If p is n ccepting stte nd q is not, then (p, q) re distinguishble. Recursive step: Let p nd q be such tht for some, δ(p, ) = r nd δ(q, ) = s with (r, s) known to be distinguishble. Then (p, q) re lso distinguishble. (This step is repeted until no new distinguishble pir is found.) (If w distinguishes r nd s then w must distinguish p nd q since ˆδ(p, w) = ˆδ(r, w) nd ˆδ(q, w) = ˆδ(s, w).) April 23rd 2018, Lecture 10 TMV027/DIT321 15/25
Exmple: Tble-Filling Algorithm Let us fill the tble of distinguishble pirs in the following DFA. q 0 q 1 q 2 q 3 q 4 q 5 q 0 q 1 q 2 q 3 q 4 q 5 X X X X q 4 X X X q 3 X X q 2 X X q 1 X Let us consider the bse cse of the lgorithm. Let us consider the pir (q 0, q 5 ). Let us consider the pir (q 0, q 2 ). Let us consider (q 2, q 3 ) nd (q 3, q 5 ). Finlly, let us consider the remining pirs. April 23rd 2018, Lecture 10 TMV027/DIT321 16/25 Equivlent Sttes Theorem: Let D = (Q, Σ, δ, q 0, F ) be DFA. If 2 sttes re not distinguishble by the tble-filling lgorithm then the sttes re equivlent. Proof: Let us ssume there is bd pir (p, q) such tht p nd q re distinguishble but the tble-filling lgorithm doesn t find them so. If there re bd pirs, let (p, q ) be bd pir with the shortest string w = 1 2... n tht distinguishes 2 sttes. Note w is not ǫ otherwise (p, q ) is found distinguishble in the bse step. Let δ(p, 1 ) = r nd δ(q, 1 ) = s. Sttes r nd s re distinguished by 2... n since this string tkes r to ˆδ(p, w) nd s to ˆδ(q, w). Now string 2... n distinguishes 2 sttes nd is shorter thn w which is the shortest string tht distinguishes bd pir. Then (r, s) cnnot be bd pir nd hence it must be found distinguishble by the lgorithm. Then the recursive step should hve found (p, q ) distinguishble. This contrdicts the ssumption tht bd pirs exist. April 23rd 2018, Lecture 10 TMV027/DIT321 17/25
Testing Equivlence of Regulr Lnguges We cn use the tble-filling lgorithm to test equivlence of regulr lnguges. Let M nd N be 2 regulr lnguges. Let D M = (Q M, Σ, δ M, q M, F M ) nd D N = (Q N, Σ, δ N, q N, F N ) be their corresponding DFA. Let us ssume Q M Q N = (esy to obtin by renming). Construct D = (Q M Q N, Σ, δ,, F M F N ) (initil stte irrelevnt). δ is the union of δ M nd δ N s function (we need to mke δ totl!). One should now check if the pir (q M, q N ) is equivlent. If so, string is ccepted by D M iff it is ccepted by D N. Hence M nd N re equivlent lnguges. April 23rd 2018, Lecture 10 TMV027/DIT321 18/25 Equivlence of Sttes: An Equivlence Reltion The reltion stte p is equivlent to stte q, denoted p q, is n equivlence reltion. Reflexive: p. p p; Symmetric: p q. p q q p; Trnsitive: p q r. p q q r p r. (See Theorem 4.23 for proof of the trnsitivity prt.) Exercise: Prove these properties! April 23rd 2018, Lecture 10 TMV027/DIT321 19/25
Prtition of Sttes Let D = (Q, Σ, δ, q 0, F ) be DFA. The tble-filling lgo. defines the equivlence of sttes reltion over Q. This is n equivlence reltion so we cn define the quotient Q/. This quotient gives us prtition into clsses of mutully equivlent sttes. Exmple: The prtition for the exmple in slide 14 is the following (note the singleton clsses!) {q 0 } {q 1, q 2 } {q 3, q 4 } {q 5 } Exmple: The prtition for the exmple in slide 16 is the following {q 0, q 3 } {q 1, q 4 } {q 2, q 5 } Note: Clsses might lso hve more thn 2 elements. April 23rd 2018, Lecture 10 TMV027/DIT321 20/25 Exmple: Minimistion of DFA How to use the prtition into clsses of equivlent sttes to minimise DFA? Exmple: The miniml DFA corresponding to the DFA in slide 14 is, b, b, b, b q 0 q 1 q 2 q 3 q 4 q 5 Exmple: The miniml DFA corresponding to the DFA in slide 16 is q 0 q 3 q 1 q 4 q 2 q 5 Exercise: Progrm the minimistion lgorithm! April 23rd 2018, Lecture 10 TMV027/DIT321 21/25
Minimistion of DFA: The Algorithm Let D = (Q, Σ, δ, q 0, F ) be DFA. Q/ llows to build n equivlent DFA with the minimum nr. of sttes. This minimum DFA is unique (modulo the nme of the sttes). The lgorithm for building the minimum DFA D = (Q, Σ, δ, q 0, F ) is: 1 Eliminte ny non ccessible stte; 2 Prtition the remining sttes using the tble-filling lgorithm; 3 Use ech clss s single stte in the new DFA; 4 The strt stte is the clss contining q 0 ; 5 The finl sttes re ll those clsses contining elements in F ; 6 δ (S, ) = T if given ny q S, δ(q, ) = p for some p T. (Actully, the prtition gurntees tht q S. p T. δ(q, ) = p.) April 23rd 2018, Lecture 10 TMV027/DIT321 22/25 Does the Minimistion Algorithm Give Miniml DFA? Given DFA D, the minimistion lgorithm gives us DFA D with the miniml number of sttes with respect to those of D. But, could there exist DFA A completely unrelted to D, lso ccepting the sme lnguge nd with less sttes thn those in D? Section 4.4.4 in the book shows by contrdiction tht A cnnot exist. Theorem: If D is DFA nd D the DFA constructed from D with the minimistion lgorithm described before, then D hs s few sttes s ny DFA equivlent to D. April 23rd 2018, Lecture 10 TMV027/DIT321 23/25
Cn we Minimise NFA? One could find smller NFA, but not with this lgorithm. 0, 1 Exmple: Consider the following NFA q 0 0 q 1 1 0 q 2 The tble-filling lgorithm does not find equivlent sttes in this cse. However, the following is smller nd equivlent NFA for the lnguge. 0, 1 q 0 0 q 1 April 23rd 2018, Lecture 10 TMV027/DIT321 24/25 Overview of Next Lecture Sections 5 5.2.2, nd notes on Inductive sets nd induction: Context-free grmmrs; Derivtions; Prse trees; Proofs in grmmrs. April 23rd 2018, Lecture 10 TMV027/DIT321 25/25