Algorithms & Models of Computtion CS/ECE 374, Fll 2017 NFAs continued, Closure Properties of Regulr Lnguges Lecture 5 Tuesdy, Septemer 12, 2017 Sriel Hr-Peled (UIUC) CS374 1 Fll 2017 1 / 31
Regulr Lnguges, DFAs, NFAs Theorem Lnguges ccepted y DFAs, NFAs, nd regulr expressions re the sme. DFAs re specil cses of NFAs (trivil) NFAs ccept regulr expressions (we sw lredy) DFAs ccept lnguges ccepted y NFAs (tody) Regulr expressions for lnguges ccepted y DFAs (lter in the course) Sriel Hr-Peled (UIUC) CS374 2 Fll 2017 2 / 31
Regulr Lnguges, DFAs, NFAs Theorem Lnguges ccepted y DFAs, NFAs, nd regulr expressions re the sme. DFAs re specil cses of NFAs (trivil) NFAs ccept regulr expressions (we sw lredy) DFAs ccept lnguges ccepted y NFAs (tody) Regulr expressions for lnguges ccepted y DFAs (lter in the course) Sriel Hr-Peled (UIUC) CS374 2 Fll 2017 2 / 31
Prt I Equivlence of NFAs nd DFAs Sriel Hr-Peled (UIUC) CS374 3 Fll 2017 3 / 31
Equivlence of NFAs nd DFAs Theorem For every NFA N there is DFA M such tht L(M) = L(N). Sriel Hr-Peled (UIUC) CS374 4 Fll 2017 4 / 31
Forml Tuple Nottion for NFA Definition A non-deterministic finite utomt (NFA) N = (Q, Σ, δ, s, A) is five tuple where Q is finite set whose elements re clled sttes, Σ is finite set clled the input lphet, δ : Q Σ {ɛ} P(Q) is the trnsition function (here P(Q) is the power set of Q), s Q is the strt stte, A Q is the set of ccepting/finl sttes. δ(q, ) for Σ {ɛ} is suset of Q set of sttes. Sriel Hr-Peled (UIUC) CS374 5 Fll 2017 5 / 31
Extending the trnsition function to strings Definition For NFA N = (Q, Σ, δ, s, A) nd q Q the ɛrech(q) is the set of ll sttes tht q cn rech using only ɛ-trnsitions. Definition Inductive definition of δ : Q Σ P(Q): if w = ɛ, δ (q, w) = ɛrech(q) if w = where Σ δ (q, ) = p ɛrech(q) ( r δ(p,) ɛrech(r)) if w = x, δ (q, w) = p δ (q,x)( r δ(p,) ɛrech(r)) Sriel Hr-Peled (UIUC) CS374 6 Fll 2017 6 / 31
Forml definition of lnguge ccepted y N Definition A string w is ccepted y NFA N if δ N (s, w) A. Definition The lnguge L(N) ccepted y NFA N = (Q, Σ, δ, s, A) is {w Σ δ (s, w) A }. Sriel Hr-Peled (UIUC) CS374 7 Fll 2017 7 / 31
Simulting n NFA y DFA Think of progrm with fixed memory tht needs to simulte NFA N on input w. Wht does it need to store fter seeing prefix x of w? It needs to know t lest δ (s, x), the set of sttes tht N could e in fter reding x Is it sufficient? Yes, if it cn compute δ (s, x) fter seeing nother symol in the input. When should the progrm ccept string w? If δ (s, w) A. Key Oservtion: A DFA M tht simultes N should keep in its memory/stte the set of sttes of N Thus the stte spce of the DFA should e P(Q). Sriel Hr-Peled (UIUC) CS374 8 Fll 2017 8 / 31
Simulting n NFA y DFA Think of progrm with fixed memory tht needs to simulte NFA N on input w. Wht does it need to store fter seeing prefix x of w? It needs to know t lest δ (s, x), the set of sttes tht N could e in fter reding x Is it sufficient? Yes, if it cn compute δ (s, x) fter seeing nother symol in the input. When should the progrm ccept string w? If δ (s, w) A. Key Oservtion: A DFA M tht simultes N should keep in its memory/stte the set of sttes of N Thus the stte spce of the DFA should e P(Q). Sriel Hr-Peled (UIUC) CS374 8 Fll 2017 8 / 31
Simulting n NFA y DFA Think of progrm with fixed memory tht needs to simulte NFA N on input w. Wht does it need to store fter seeing prefix x of w? It needs to know t lest δ (s, x), the set of sttes tht N could e in fter reding x Is it sufficient? Yes, if it cn compute δ (s, x) fter seeing nother symol in the input. When should the progrm ccept string w? If δ (s, w) A. Key Oservtion: A DFA M tht simultes N should keep in its memory/stte the set of sttes of N Thus the stte spce of the DFA should e P(Q). Sriel Hr-Peled (UIUC) CS374 8 Fll 2017 8 / 31
Simulting n NFA y DFA Think of progrm with fixed memory tht needs to simulte NFA N on input w. Wht does it need to store fter seeing prefix x of w? It needs to know t lest δ (s, x), the set of sttes tht N could e in fter reding x Is it sufficient? Yes, if it cn compute δ (s, x) fter seeing nother symol in the input. When should the progrm ccept string w? If δ (s, w) A. Key Oservtion: A DFA M tht simultes N should keep in its memory/stte the set of sttes of N Thus the stte spce of the DFA should e P(Q). Sriel Hr-Peled (UIUC) CS374 8 Fll 2017 8 / 31
Simulting NFA Exmple the first revisited Previous lecture.. Rn NFA (N1) A B C D E on input. t = 0: t = 1: A B C D E t = 2: A B C D E t = 4: t = 5: A B C D E A B C D E t = 3: A B C D E A B C D E Sriel Hr-Peled (UIUC) CS374 9 Fll 2017 9 / 31
Exmple: DFA from NFA NFA: (N1) A B C D E DFA: A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E Sriel Hr-Peled (UIUC) CS374 10 Fll 2017 10 / 31
Suset Construction NFA N = (Q, Σ, s, δ, A). We crete DFA M = (Q, Σ, δ, s, A ) s follows: Q = P(Q) s = ɛrech(s) = δ (s, ɛ) A = {X Q X A } δ (X, ) = q X δ (q, ) for ech X Q, Σ. Sriel Hr-Peled (UIUC) CS374 11 Fll 2017 11 / 31
Suset Construction NFA N = (Q, Σ, s, δ, A). We crete DFA M = (Q, Σ, δ, s, A ) s follows: Q = P(Q) s = ɛrech(s) = δ (s, ɛ) A = {X Q X A } δ (X, ) = q X δ (q, ) for ech X Q, Σ. Sriel Hr-Peled (UIUC) CS374 11 Fll 2017 11 / 31
Suset Construction NFA N = (Q, Σ, s, δ, A). We crete DFA M = (Q, Σ, δ, s, A ) s follows: Q = P(Q) s = ɛrech(s) = δ (s, ɛ) A = {X Q X A } δ (X, ) = q X δ (q, ) for ech X Q, Σ. Sriel Hr-Peled (UIUC) CS374 11 Fll 2017 11 / 31
Suset Construction NFA N = (Q, Σ, s, δ, A). We crete DFA M = (Q, Σ, δ, s, A ) s follows: Q = P(Q) s = ɛrech(s) = δ (s, ɛ) A = {X Q X A } δ (X, ) = q X δ (q, ) for ech X Q, Σ. Sriel Hr-Peled (UIUC) CS374 11 Fll 2017 11 / 31
Exmple No ɛ-trnsitions 0, 1 0, 1 q 0 1 q 1 0, 1 0, 1 {q 1 } {} 0 0, 1 {q 0 } 1 {q 0,q 1 } Figure 4: NFA N Figure 5: DFA det(n) equivlent to N l now prove tht the DFA defined ove is correct. Tht is. L(N) =L(det(N)) d to show 8w 2. det(n) ccepts w i N ccepts w 8w 2. det(n) (s 0,w) 2 A 0 i N (s, w) \ A 6= ; 8w 2. det(n) (s 0,w) \ A 6= ; i N (s, w) \ A 6= ; the induction proof to go through we need to strengthen the clim s follows. 8w 2. det(n) (s0,w)= N (s, w) ords, this sys tht the stte of the DFA fter reding some string is exctly the set of sttes the e in fter reding the sme string. oof of the strengthened sttement is y induction on w. Sriel Hr-Peled (UIUC) CS374 12 Fll 2017 12 / 31
Exmple s 0 = N (s, ) A 0 = {X Q X \ A 6= ;} 0 ({q 1,q 2,...q k},)= N (q1,) [ N (q2,) [ [ N (qk,) or more concisely, 0 (X, ) = [ N (q, ) No ɛ-trnsitions An exmple NFA is shown in Figure 4 long with the DFA det(n) in Figure 5. q2x 0, 1 0, 1 {q 1} {} 0, 1 0, 1 q 0 1 q 1 0 0, 1 {q 0} 1 {q 0,q 1} Figure 4: NFA N Figure 5: DFA det(n) equivlent to N We will now prove tht the DFA defined ove is correct. Tht is Lemm 4. L(N) =L(det(N)) Proof. Need to show 8w 2. det(n) ccepts w i N ccepts w 8w 2. det(n) (s 0,w) 2 A 0 i N (s, w) \ A 6= ; 8w 2. det(n) (s 0,w) \ A 6= ; i N (s, w) \ A 6= ; Agin for the induction proof to go through we need to strengthen the clim s follows. 8w 2. det(n) (s0,w)= N (s, w) SrielIn Hr-Peled other words, (UIUC) this sys tht the stte of thecs374 DFA fter reding13 some string is exctly the set of sttes Fll the2017 13 / 31
Incrementl construction Only uild sttes rechle from s = ɛrech(s) the strt stte of M ε q 1 0 q 0 1 ε q 3 {q 0, q 1} 1 0 {q 2,q 3} 0, 1 0, 1 0, 1 {} q 2 {q 3} δ (X, ) = q X δ (q, ) Sriel Hr-Peled (UIUC) CS374 14 Fll 2017 14 / 31
Incrementl lgorithm Build M eginning with strt stte s == ɛrech(s) For ech existing stte X Q consider ech Σ nd clculte the stte Y = δ (X, ) = q X δ (q, ) nd dd trnsition. If Y is new stte dd it to rechle sttes tht need to explored. To compute δ (q, ) - set of ll sttes reched from q on string Compute X = ɛrech(q) Compute Y = p X δ(p, ) Compute Z = ɛrech(y ) = r Y ɛrech(r) Sriel Hr-Peled (UIUC) CS374 15 Fll 2017 15 / 31
Proof of Correctness Theorem Let N = (Q, Σ, s, δ, A) e NFA nd let M = (Q, Σ, δ, s, A ) e DFA constructed from N vi the suset construction. Then L(N) = L(M). Stronger clim: Lemm For every string w, δ N (s, w) = δ M (s, w). Proof y induction on w. Bse cse: w = ɛ. δ N (s, ɛ) = ɛrech(s). δ M (s, ɛ) = s = ɛrech(s) y definition of s. Sriel Hr-Peled (UIUC) CS374 16 Fll 2017 16 / 31
Proof of Correctness Theorem Let N = (Q, Σ, s, δ, A) e NFA nd let M = (Q, Σ, δ, s, A ) e DFA constructed from N vi the suset construction. Then L(N) = L(M). Stronger clim: Lemm For every string w, δ N (s, w) = δ M (s, w). Proof y induction on w. Bse cse: w = ɛ. δ N (s, ɛ) = ɛrech(s). δ M (s, ɛ) = s = ɛrech(s) y definition of s. Sriel Hr-Peled (UIUC) CS374 16 Fll 2017 16 / 31
Proof continued Lemm For every string w, δ N (s, w) = δ M (s, w). Inductive step: w = x (Note: suffix definition of strings) δ N (s, x) = p δ N (s,x) δ N (p, ) y inductive definition of δ N δ M (s, x) = δ M (δ M (s, x), ) y inductive definition of δ M By inductive hypothesis: Y = δ N (s, x) = δ M (s, x) Thus δ N (s, x) = p Y δ N (p, ) = δ M(Y, ) y definition of δ M. Therefore, δ N (s, x) = δ M(Y, ) = δ M (δ M (s, x), ) = δ M (s, x) which is wht we need. Sriel Hr-Peled (UIUC) CS374 17 Fll 2017 17 / 31
Proof continued Lemm For every string w, δ N (s, w) = δ M (s, w). Inductive step: w = x (Note: suffix definition of strings) δ N (s, x) = p δ N (s,x) δ N (p, ) y inductive definition of δ N δ M (s, x) = δ M (δ M (s, x), ) y inductive definition of δ M By inductive hypothesis: Y = δ N (s, x) = δ M (s, x) Thus δ N (s, x) = p Y δ N (p, ) = δ M(Y, ) y definition of δ M. Therefore, δ N (s, x) = δ M(Y, ) = δ M (δ M (s, x), ) = δ M (s, x) which is wht we need. Sriel Hr-Peled (UIUC) CS374 17 Fll 2017 17 / 31
Proof continued Lemm For every string w, δ N (s, w) = δ M (s, w). Inductive step: w = x (Note: suffix definition of strings) δ N (s, x) = p δ N (s,x) δ N (p, ) y inductive definition of δ N δ M (s, x) = δ M (δ M (s, x), ) y inductive definition of δ M By inductive hypothesis: Y = δ N (s, x) = δ M (s, x) Thus δ N (s, x) = p Y δ N (p, ) = δ M(Y, ) y definition of δ M. Therefore, δ N (s, x) = δ M(Y, ) = δ M (δ M (s, x), ) = δ M (s, x) which is wht we need. Sriel Hr-Peled (UIUC) CS374 17 Fll 2017 17 / 31
Proof continued Lemm For every string w, δ N (s, w) = δ M (s, w). Inductive step: w = x (Note: suffix definition of strings) δ N (s, x) = p δ N (s,x) δ N (p, ) y inductive definition of δ N δ M (s, x) = δ M (δ M (s, x), ) y inductive definition of δ M By inductive hypothesis: Y = δ N (s, x) = δ M (s, x) Thus δ N (s, x) = p Y δ N (p, ) = δ M(Y, ) y definition of δ M. Therefore, δ N (s, x) = δ M(Y, ) = δ M (δ M (s, x), ) = δ M (s, x) which is wht we need. Sriel Hr-Peled (UIUC) CS374 17 Fll 2017 17 / 31
Proof continued Lemm For every string w, δ N (s, w) = δ M (s, w). Inductive step: w = x (Note: suffix definition of strings) δ N (s, x) = p δ N (s,x) δ N (p, ) y inductive definition of δ N δ M (s, x) = δ M (δ M (s, x), ) y inductive definition of δ M By inductive hypothesis: Y = δ N (s, x) = δ M (s, x) Thus δ N (s, x) = p Y δ N (p, ) = δ M(Y, ) y definition of δ M. Therefore, δ N (s, x) = δ M(Y, ) = δ M (δ M (s, x), ) = δ M (s, x) which is wht we need. Sriel Hr-Peled (UIUC) CS374 17 Fll 2017 17 / 31
Prt II Closure Properties of Regulr Lnguges Sriel Hr-Peled (UIUC) CS374 18 Fll 2017 18 / 31
Regulr Lnguges Regulr lnguges hve three different chrcteriztions Inductive definition vi se cses nd closure under union, conctention nd Kleene str Lnguges ccepted y DFAs Lnguges ccepted y NFAs Regulr lnguge closed under mny opertions: union, conctention, Kleene str vi inductive definition or NFAs complement, union, intersection vi DFAs homomorphism, inverse homomorphism, reverse,... Different representtions llow for flexiility in proofs Sriel Hr-Peled (UIUC) CS374 19 Fll 2017 19 / 31
Regulr Lnguges Regulr lnguges hve three different chrcteriztions Inductive definition vi se cses nd closure under union, conctention nd Kleene str Lnguges ccepted y DFAs Lnguges ccepted y NFAs Regulr lnguge closed under mny opertions: union, conctention, Kleene str vi inductive definition or NFAs complement, union, intersection vi DFAs homomorphism, inverse homomorphism, reverse,... Different representtions llow for flexiility in proofs Sriel Hr-Peled (UIUC) CS374 19 Fll 2017 19 / 31
Exmple: PREFIX Let L e lnguge over Σ. Definition PREFIX(L) = {w wx L, x Σ } Theorem If L is regulr then PREFIX(L) is regulr. Let M = (Q, Σ, δ, s, A) e DFA tht recognizes L X = {q Q s cn rech q in M} Y = {q Q q cn rech some stte in A} Z = X Y Crete new DFA M = (Q, Σ, δ, s, Z) Clim: L(M ) = PREFIX(L). Sriel Hr-Peled (UIUC) CS374 20 Fll 2017 20 / 31
Exmple: PREFIX Let L e lnguge over Σ. Definition PREFIX(L) = {w wx L, x Σ } Theorem If L is regulr then PREFIX(L) is regulr. Let M = (Q, Σ, δ, s, A) e DFA tht recognizes L X = {q Q s cn rech q in M} Y = {q Q q cn rech some stte in A} Z = X Y Crete new DFA M = (Q, Σ, δ, s, Z) Clim: L(M ) = PREFIX(L). Sriel Hr-Peled (UIUC) CS374 20 Fll 2017 20 / 31
Exmple: PREFIX Let L e lnguge over Σ. Definition PREFIX(L) = {w wx L, x Σ } Theorem If L is regulr then PREFIX(L) is regulr. Let M = (Q, Σ, δ, s, A) e DFA tht recognizes L X = {q Q s cn rech q in M} Y = {q Q q cn rech some stte in A} Z = X Y Crete new DFA M = (Q, Σ, δ, s, Z) Clim: L(M ) = PREFIX(L). Sriel Hr-Peled (UIUC) CS374 20 Fll 2017 20 / 31
Exmple: PREFIX Let L e lnguge over Σ. Definition PREFIX(L) = {w wx L, x Σ } Theorem If L is regulr then PREFIX(L) is regulr. Let M = (Q, Σ, δ, s, A) e DFA tht recognizes L X = {q Q s cn rech q in M} Y = {q Q q cn rech some stte in A} Z = X Y Crete new DFA M = (Q, Σ, δ, s, Z) Clim: L(M ) = PREFIX(L). Sriel Hr-Peled (UIUC) CS374 20 Fll 2017 20 / 31
Exmple: PREFIX Let L e lnguge over Σ. Definition PREFIX(L) = {w wx L, x Σ } Theorem If L is regulr then PREFIX(L) is regulr. Let M = (Q, Σ, δ, s, A) e DFA tht recognizes L X = {q Q s cn rech q in M} Y = {q Q q cn rech some stte in A} Z = X Y Crete new DFA M = (Q, Σ, δ, s, Z) Clim: L(M ) = PREFIX(L). Sriel Hr-Peled (UIUC) CS374 20 Fll 2017 20 / 31
Exmple: PREFIX Let L e lnguge over Σ. Definition PREFIX(L) = {w wx L, x Σ } Theorem If L is regulr then PREFIX(L) is regulr. Let M = (Q, Σ, δ, s, A) e DFA tht recognizes L X = {q Q s cn rech q in M} Y = {q Q q cn rech some stte in A} Z = X Y Crete new DFA M = (Q, Σ, δ, s, Z) Clim: L(M ) = PREFIX(L). Sriel Hr-Peled (UIUC) CS374 20 Fll 2017 20 / 31
Exmple: PREFIX Let L e lnguge over Σ. Definition PREFIX(L) = {w wx L, x Σ } Theorem If L is regulr then PREFIX(L) is regulr. Let M = (Q, Σ, δ, s, A) e DFA tht recognizes L X = {q Q s cn rech q in M} Y = {q Q q cn rech some stte in A} Z = X Y Crete new DFA M = (Q, Σ, δ, s, Z) Clim: L(M ) = PREFIX(L). Sriel Hr-Peled (UIUC) CS374 20 Fll 2017 20 / 31
Exercise: SUFFIX Let L e lnguge over Σ. Definition SUFFIX(L) = {w xw L, x Σ } Prove the following: Theorem If L is regulr then PREFIX(L) is regulr. Sriel Hr-Peled (UIUC) CS374 21 Fll 2017 21 / 31
Prt III Regex to NFA Sriel Hr-Peled (UIUC) CS374 22 Fll 2017 22 / 31
Stge 0: Input A B C, Sriel Hr-Peled (UIUC) CS374 23 Fll 2017 23 / 31
Stge 1: Normlizing A B C, 2: Normlizing it. ǫ init A B C ǫ + AC Sriel Hr-Peled (UIUC) CS374 24 Fll 2017 24 / 31
Stge 2: Remove stte A ǫ init A B C ǫ AC + init ǫ A B C ǫ AC + Sriel Hr-Peled (UIUC) CS374 25 Fll 2017 25 / 31
Stge 4: Redrwn without old edges init B C ǫ AC + Sriel Hr-Peled (UIUC) CS374 26 Fll 2017 26 / 31
Stge 4: Removing B init B init B C ǫ + AC C ǫ + AC Sriel Hr-Peled (UIUC) CS374 27 Fll 2017 27 / 31
Stge 5: Redrw init + C ǫ + AC Sriel Hr-Peled (UIUC) CS374 28 Fll 2017 28 / 31
Stge 6: Removing C init + init ( + )( + ) ǫ + C ǫ + AC C ǫ + AC Sriel Hr-Peled (UIUC) CS374 29 Fll 2017 29 / 31
Stge 7: Redrw init ( + )( + ) AC Sriel Hr-Peled (UIUC) CS374 30 Fll 2017 30 / 31
Stge 8: Extrct regulr expression init ( + )( + ) AC Thus, this utomt is equivlent to the regulr expression ( + )( + ). Sriel Hr-Peled (UIUC) CS374 31 Fll 2017 31 / 31