13.07.2015 Views

equivalenza di

equivalenza di

equivalenza di

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

SIMBOLI➜ Un simbolo è un’entità primitiva astratta non meglio definita.➜ Per ciò che ci concerne, un simbolo è atomico: se anche avesse unastruttura interna, noi non la osserviamo.➜ Le uniche caratteristiche rilevanti dei simboli:• l’identità, in modo da poter <strong>di</strong>stringuere un simbolo dall’altro;• la possibilità, almeno concettuale, <strong>di</strong> essere giustapposti (posti l’unoaccanto all’altro in modo da formare una sequenza[uni<strong>di</strong>mensionale]).➜ Esempi: lettere e caratteri numerici; ideogrammi; punti, linee e spazinell’alfabeto Morse; ban<strong>di</strong>ere per segnalazioni nautiche.IMBOLI 1


STRINGHE➜ Una stringa (o parola) è una sequenza finita <strong>di</strong> simboli giustapposti➜ Ad esempio, se a, b e c sono simboli, abcba è una stringa.➜ La lunghezza <strong>di</strong> una stringa w è il numero <strong>di</strong> occorrenze <strong>di</strong> simboli in we si denota con |w|.➜ Ad esempio, |abcba| = 5.➜ La stringa vuota ε è costituita da zero simboli: |ε| = 0.➜ Sia w = a 1 · · · a n una stringa (notare che, se n = 0, allora w = ε). Ognistringa della forma:• a 1 · · · a j , con j ∈ {1,...,n} è detta prefisso <strong>di</strong> w;• a i · · · a n , con i ∈ {1, ...,n} è detta suffisso <strong>di</strong> w;• a i · · · a j , con i,j ∈ {1,...,n}, cin i ≤ j, è detta sottostringa <strong>di</strong> w;• ε è sia prefisso che suffisso che sottostringa <strong>di</strong> w.TRINGHE 2


STRINGHE➜ I prefissi <strong>di</strong> abc sono ε, a, ab, e abc.➜ I suffissi <strong>di</strong> abc sono ε, c, bc, e abc.➜ Le sottostringhe <strong>di</strong> abc sono:εa ab abcbbcc➜ Un prefisso, un suffisso o una sottostringa <strong>di</strong> una stringa, quando nonsono la stringa stessa, sono detti propri.Esercizio: Sia data una stringa w <strong>di</strong> lunghezza |w| = n. Quanti sonoi suoi prefissi, i suoi suffissi e le sue sottostringhe? Quanti sono iprefissi dei suoi suffissi? Quanti i suffissi dei sui prefissi?TRINGHE 3


AUTOMI A STATI FINITI: DESCRIZIONE INFORMALEModello “meccanico”:s 1 s 2 s 3 · · ·⇑qTabella <strong>di</strong> transizione:abq 0 q 1 q 2q 1 q 1 q 0q 2 q 1 q 0UTOMI A STATI FINITI: DESCRIZIONE INFORMALE 7


AUTOMI A STATI FINITI: DESCRIZIONE INFORMALERappresentazione grafica me<strong>di</strong>ante grafo <strong>di</strong> transizione:✛ ✘★✥✤✜a✛ ✙q 1a ✒ ✧✦✣✢❅■ab ❅ ★✥✠❅★✥bq✲0q 2✛✧✦ b ✧✦UTOMI A STATI FINITI: DESCRIZIONE INFORMALE 8


AUTOMI A STATI FINITI DETERMINISTICI: DEFINIZIONE FORMALEUn automa a stati finiti deterministico (DFA) è una quintupla〈Q, Σ, δ, q 0 , F 〉 dove:• Q è un insieme finito <strong>di</strong> stati;• Σ è un alfabeto (alfabeto <strong>di</strong> input);• δ : Q × Σ −→ Q è la funzione <strong>di</strong> transizione;• q 0 è lo stato iniziale;• F ⊆ Q è l’insieme degli stati finali.Notazione: In genere, useremo p, q, r, . . . per denotare stati,P ,Q,R,S, . . . per insiemi <strong>di</strong> stati, a,b, . . . per denotare simboli <strong>di</strong> Σ,x,y,z,u,v,w, . . . per denotare stringhe. Il tutto con o senza pe<strong>di</strong>ci (adesempio, anche q 0 , q 1 , . . . sono stati).UTOMI A STATI FINITI DETERMINISTICI: DEFINIZIONE FORMALE 9


AUTOMI A STATI FINITI DETERMINISTICI: DEFINIZIONE FORMALEDalla funzione δ si ottiene in modo univoco ˆδ : Q × Σ ∗ −→ Q:⎧⎨ˆδ(q, ε) = q⎩ˆδ(q, wa) = δ (ˆδ(q, )w), aUna stringa x è detta essere accettata da un DFAM = 〈Q, Σ, δ, q 0 , F 〉 seˆδ(q 0 , x) ∈ F.Il linguaggio accettato da M, denotato da L(M) è l’insieme dellestringhe accettate, ovvero:L(M) = { x ∈ Σ ∗ ∣ ∣ ˆδ(q0 , x) ∈ F }UTOMI A STATI FINITI DETERMINISTICI: DEFINIZIONE FORMALE 10


AUTOMI A STATI FINITI E LINGUAGGI REGOLARIUn linguaggio L è detto regolare se è accettato da qualche DFA,ovvero se esiste M tale che L = L(M).∅ e Σ ∗ sono linguaggi regolari. Sia Σ = {s 1 , . . .,s n }: un automa M 0che riconosce il linguaggio ∅ (ovvero: nessuna stringa è accettata) èil seguente:s 1 . . . s nq 0 q 0 . . . q 0ove F = ∅. Infatti, poiché ∀x : x /∈ ∅, si ha che:∀x ∈ Σ ∗ : ˆδ(q 0 , x) /∈ FUTOMI A STATI FINITI E LINGUAGGI REGOLARI 11


Un automa per Σ ∗ , è invece l’automa M 1 :s 1 . . . s nq 0 q 0 . . . q 0ove F = {q 0 }. Si <strong>di</strong>mostra facilmente infatti, per induzione su |x| che∀x ∈ Σ ∗ : ˆδ(q 0 , x) = q 0UTOMI A STATI FINITI E LINGUAGGI REGOLARI 12


AUTOMI A STATI FINITI NON-DETERMINISTICIUn automa a stati finiti non-deterministico (NFA) è una quintupla〈Q, Σ, δ, q 0 , F 〉 dove➜ Q, Σ, q 0 e F ⊆ Q sono come per i DFA;➜ la funzione <strong>di</strong> transizione è ora del tipoδ: Q × Σ → ℘(Q).➜ In particolare, possiamo avere δ(q,a) = ∅ per qualche q ∈ Q ed a ∈ Σ.➜ Anche per gli NFA dalla funzione δ si ottiene in modo univoco lafunzione ˆδ: Q × Σ ∗ → ℘(Q):8< ˆδ(q, ε) = {q}: ˆδ(q, wa) = S p∈ˆδ(q,w) δ(p,a)UTOMI A STATI FINITI NON-DETERMINISTICI 13


AUTOMI A STATI FINITI NON-DETERMINISTICI: ESEMPIO➜ Una stringa x è accettata da un NFA M = 〈Q, Σ, δ,q 0 , F 〉 seˆδ(q 0 , x) ∩ F ≠ ∅.➜ Il linguaggio accettato da M è l’insieme delle stringhe accettate, ovvero:L(M) = ˘ x ∈ Σ ∗ ˛˛ ˆδ(q0 , x) ∩ F ≠ ∅ ¯UTOMI A STATI FINITI NON-DETERMINISTICI: ESEMPIO 14


AUTOMI A STATI FINITI NON-DETERMINISTICI: ESEMPIO✤✜ ✤✜ ✛✘q 20 ✲ q 4✣✢ ✣✢ ✚✙0✤✜ ✤✜ ✒❅ 1❅❅❘q10 ✲ q 3✣✢ ❅ ✣✢❅❅❘0 ✤✜ ✒ 0q 1✣✢UTOMI A STATI FINITI NON-DETERMINISTICI: ESEMPIO 15


EQUIVALENZA TRA DFA E NFA➜ Un DFA è banalmente convertibile in un NFA.➜ Il teorema <strong>di</strong> Rabin-Scott (1959) mostra che un NFA è sempreconvertibile in un DFA.Teorema 1 Sia M = 〈Q, Σ, δ, q 0 , F 〉 un NFA. Allora esiste un DFAM ′ tale che L(M) = L(M ′ ).QUIVALENZA TRA DFA E NFA 16


EQUIVALENZA TRA DFA E NFA: TEOREMA DI RABIN-SCOTTDimostrazione: Si definisca M ′ = 〈Q ′ , Σ ′ , δ ′ , q 0, ′ F ′ 〉 come segue:• Σ ′ = Σ;• Q ′ = ℘(Q);• q 0 ′ = {q 0 };• F ′ = {P ⊆ Q : P ∩ F ≠ ∅};• δ ′ (P, a) = ⋃ p∈Pδ(p, a), per P ∈ ℘(Q).QUIVALENZA TRA DFA E NFA: TEOREMA DI RABIN-SCOTT 17


EQUIVALENZA TRA DFA E NFA: TEOREMA DI RABIN-SCOTTMostriamo per induzione sulla lunghezza della stringa <strong>di</strong> input x cheˆδ(q 0 , x) = ˆδ ′ (q ′ 0, x)Base: Per |x| = 0 il risultato è banale, poiché q ′ 0 = {q 0 } e x = ε.Passo: Supponiamo che l’ipotesi induttiva valga per tutte le stringhex tali che |x| ≤ m. Sia xa una stringa <strong>di</strong> lunghezza m + 1. Allora:ˆδ ′ (q ′ 0, xa) = δ ′ (ˆδ ′ (q ′ 0, x), a)[per def. <strong>di</strong> ˆ· nei DFA]= δ ′ (ˆδ(q 0 , x), a) [per ip. ind.]= ⋃δ(p, a) [per def. <strong>di</strong> δ ′ ]p∈ˆδ(q 0 ,x)= ˆδ(q 0 , xa) [per def. <strong>di</strong> ‘ˆ·’ negli NFA]QUIVALENZA TRA DFA E NFA: TEOREMA DI RABIN-SCOTT 18


EQUIVALENZA TRA DFA E NFA: TEOREMA DI RABIN-SCOTTIl teorema segue dal fatto che:x ∈ L(M) ⇐⇒ ˆδ(q 0 , x) ∩ F ≠ ∅⇐⇒ ˆδ ′ (q ′ 0, x) ∩ F ≠ ∅[def. <strong>di</strong> linguaggio NFA][proprietà sopra]⇐⇒ ˆδ ′ (q ′ 0, x) ∈ F ′ [per def. <strong>di</strong> F ′ ]⇐⇒ x ∈ L(M ′ )[per def. <strong>di</strong> linguaggio DFA]QUIVALENZA TRA DFA E NFA: TEOREMA DI RABIN-SCOTT 19


EQUIVALENZA TRA DFA E NFA: ESERCIZIEsercizio: Si determini il DFA equivalente al NFA:0 1q 0 {q 0 } {q 0 , q 1 }q 1 {q 1 } {q 0 , q 2 }q 2 {q 1 , q 2 } {q 0 , q 1 , q 2 }ove F = {q 2 }. Qual è il linguaggio accettato?Esercizio: Si descriva un NFA a 4 stati che riconosce il linguaggiodelle stringhe <strong>di</strong> 0 e 1 con terzultimo elemento a 0. Si passi poi alDFA equivalente e lo si confronti con quello ottenuto nell’eserciziosopra.QUIVALENZA TRA DFA E NFA: ESERCIZI 20


AUTOMI CON ε-TRANSIZIONIUn NFA con ε-transizioni è una quintupla 〈Q, Σ, δ, q 0 , F 〉:➜ Q, Σ, q 0 e F ⊆ Q sono come per gli NFA;➜ δ : Q × (Σ ∪ {ε}) −→ ℘(Q).➜ L’idea è che da uno stato è permesso passare ad un altro stato anchesenza “leggere” caratteri <strong>di</strong> input.➜ La definizione <strong>di</strong> ˆδ : Q × Σ ∗ −→ ℘(Q) fa riferimento alle funzioniausiliarie ε-step: Q → ℘(Q). . .ε-step(S) = ˘ q ∈ Q ˛˛ ∃p ∈ S . q ∈ δ(p,ε)¯;ε-step 0 (S) = S;ε-step n+1 (S) = ε-step`ε-step n (S)´;UTOMI CON ε-TRANSIZIONI 21


AUTOMI CON ε-TRANSIZIONI (CONT.)➜ . . . e ε-closure: Q → ℘(Q) e ε-closure: ℘(Q) → ℘(Q):ε-closure(S) = [ ε-step i ;i∈Nε-closure(p) = ε-closure`{p}´.➜ ˆδ si può ora definire nel modo seguente:8< ˆδ(q, ε) = ε-closure(q),: ˆδ(q, wa) = S p∈ˆδ(q,w) ε-closure(δ(p,a)).UTOMI CON ε-TRANSIZIONI (CONT.) 22


AUTOMI CON ε-TRANSIZIONI (CONT.)✛✘aq ✲✚✙✛✘εq ′ ✲✚✙✛✘q ′′✚✙δ(q, a) = {q ′ } ≠ {q ′ , q ′′ } =⋃p∈ˆδ(q,ε)ε-closure(δ(p, a)) = ˆδ(q, a)UTOMI CON ε-TRANSIZIONI (CONT.) 23


AUTOMI CON ε-TRANSIZIONI (CONT.)➜ Il linguaggio accettato da un ε-NFA è definito comeL(M) = ˘ x ∈ Σ ∗ ˛˛ ˆδ(q0 , x) ∩ F ≠ ∅ ¯.➜ Osservazione: per gli ε-NFA si potrebbe assumere che l’insieme Fabbia esattamente un elemento.➜ Osservazione: ˆδ(q,x) = ε-closure(ˆδ(q, x)).UTOMI CON ε-TRANSIZIONI (CONT.) 24


EQUIVALENZA DI ε-NFA E NFA➜ Ogni NFA è, per definizione, un caso particolare <strong>di</strong> un ε-NFA.➜ Ma gli ε-NFA non sono più potenti degli NFA:Teorema 2 Sia M = 〈Q, Σ, δ, q 0 , F 〉 un ε-NFA. Allora esiste un NFAM ′ tale che L(M) = L(M ′ ).Dimostrazione: Definisco M ′ = 〈Q ′ , Σ ′ , δ ′ , q ′ 0, F ′ 〉 come segue:• Q ′ = Q,• Σ ′ = Σ,• q 0 ′ = q 0 ,⎧⎨• F ′ F ∪ {q 0 }, se ε-closure(q 0 ) ∩ F ≠ ∅,=⎩F, altrimenti,• δ ′ (q, a) = ˆδ(q, a).QUIVALENZA DI ε-NFA E NFA 25


EQUIVALENZA DI ε-NFA E NFA➜ Dobbiamo mostrare che ˆδ(q 0 ,x) ∩ F ≠ ∅ ⇐⇒ ˆδ ′ (q 0, ′ x) ∩ F ′ ≠ ∅.➜ Trattiamo <strong>di</strong>stintamente i casi x = ε e x ≠ ε.Se x = ε:➜ ˆδ(q 0 , ε) = ε-closure(q 0 ) e ˆδ ′ (q 0, ′ ε) = {q 0 }, per definizione.➜ (=⇒) Se ε-closure(q 0 ) ∩ F ≠ ∅ allora, per def. <strong>di</strong> F ′ vale che q 0 ∈ F ′ ;dunque {q 0 } ∩ F ′ ≠ ∅.➜ (⇐=) Sia ora {q 0 } ∩ F ′ ≠ ∅. Allora q 0 ∈ F ′ . Due casi sono possibili:1. se q 0 ∈ F allora ε-closure(q 0 ) ∩ F ≠ ∅ (in quantoq 0 ∈ ε-closure(q 0 )).2. Altrimenti, se q 0 ∈ F ′ \ F per definizione <strong>di</strong> F ′ si ha cheε-closure(q 0 ) ∩ F ≠ ∅.QUIVALENZA DI ε-NFA E NFA 26


EQUIVALENZA DI ε-NFA E NFA (CONT.)➜ Dobbiamo mostrare che ˆδ(q 0 ,x) ∩ F ≠ ∅ ⇐⇒ ˆδ ′ (q ′ 0, x) ∩ F ′ ≠ ∅.Se x ≠ ε:➜ Mostriamo, per induzione su |x| ≥ 1, che ˆδ ′ (q ′ 0, x) = ˆδ(q 0 ,x):➜ Base: |x| = 1. Allora x = a per qualche simbolo a ∈ Σ. Ma alloraˆδ ′ (q ′ 0, a) = δ ′ (q ′ 0,a) = ˆδ(q 0 , a) per definizione.QUIVALENZA DI ε-NFA E NFA (CONT.) 27


EQUIVALENZA DI ε-NFA E NFA (CONT.)➜ Passo: Assumiamo che la tesi valga per tutte le stringhe x tali che1 ≤ |x| ≤ m. Sia xa una stringa <strong>di</strong> lunghezza m + 1. Allora:[ˆδ ′ (q 0, ′ xa) = δ ′ (p, a) [def. <strong>di</strong> ˆδ ′ ]p∈ˆδ ′ (q0 ′[,x)= ˆδ(p,a) [def. <strong>di</strong> δ ′ ]p∈ˆδ ′ (q0 ′ ,x)= [ ˆδ(p, a)[ip. ind.]p∈ˆδ(q 0 ,x)= [p∈ˆδ(q 0 ,x) r∈ˆδ(p,ε)p∈ˆδ(q 0 ,x)[ε-closure(δ(r,a))[def. <strong>di</strong> ˆδ]= [ε-closure(δ(p,a)) [ˆδ(q 0 , x) chiuso per ε-closure]= ˆδ(q 0 , xa) [def. <strong>di</strong> ˆδ]QUIVALENZA DI ε-NFA E NFA (CONT.) 28


EQUIVALENZA DI ε-NFA E NFA (CONT.)➜ Dobbiamo mostrare che ˆδ(q 0 ,x) ∩ F ≠ ∅ ⇐⇒ ˆδ ′ (q ′ 0, x) ∩ F ′ ≠ ∅.Se x ≠ ε:➜ Poiché F ⊆ F ′ e ˆδ ′ (q ′ 0, x) = ˆδ(q 0 , x) l’implicazione (=⇒) derivaimme<strong>di</strong>atamente.➜ Per l’altra implicazione, l’unico problema si avrebbe nel caso:1. q 0 ∈ ˆδ ′ (q ′ 0, x),2. q ′ 0 ∈ F ′ ,3. q 0 /∈ F .Poiché ˆδ ′ (q ′ 0, x) = ˆδ(q 0 , x), si ha che q 0 ∈ ˆδ(q 0 , x). Ma, per definizione <strong>di</strong>ˆδ, anche ogni elemento della sua ε-closure appartiene a ˆδ(q 0 , x). Taleε-closure, poiché q 0 ∈ F ′ , interseca F .QUIVALENZA DI ε-NFA E NFA (CONT.) 29


EQUIVALENZA DI ε-NFA E NFA (CONT.)Corollario 3 Le classi <strong>di</strong> linguaggi riconosciute da DFA, NFA eε-NFA coincidono (linguaggi regolari).QUIVALENZA DI ε-NFA E NFA (CONT.) 30


EQUIVALENZA DI ε-NFA E NFA (ESEMPIO)✛✘✤✜✤✜✛✘✤✜✤✜✲00q 1 q 2 q 5✲0q 1 q 2 ✲ q 5✚✙✣✢✣✢✣✢ ✚✙0✣✢0✣✢ ✚✙✻✻❅✤✜ε✤✜ε❄ ✤✜ε✤✜ ✛✘ ✒ ❅❅❅❘ ✤✜ ❅❅❘ ✒ ✤✜0 0q 0 q 3 ✲ q 4 q 0 ✲ q 3 ✲ q 40✣✢ ✣✢ ✣✢ ✣✢ ✚✙0 0✣✢ ✣✢➜ Esempio della trasformazione da ε-NFA in NFA.➜ Il numero degli stati rimane costante, ma q 0 viene aggiunto agli statifinali.QUIVALENZA DI ε-NFA E NFA (ESEMPIO) 31


RAPPRESENTAZIONE DEI LINGUAGGI➜ Il problema della rappresentazione finita <strong>di</strong> linguaggi infiniti è affrontabileda tre <strong>di</strong>stinti punti <strong>di</strong> vista:1. riconoscitivo-analitico, quello che già conosciamo, in cui il linguaggioè visto come l’insieme delle stringhe riconosciute o accettate dastrutture finite dette automi;2. generativo-sintetico, in cui il linguaggio è visto come l’insieme dellestringhe generate da strutture finite dette grammatiche;3. algebrico, in cui il linguaggio è rappresentato da un’espressionealgebrica o è la soluzione <strong>di</strong> un sistema <strong>di</strong> relazioni algebriche.➜ Le espressioni regolari, che ora vedremo, ricadono nell’approccio <strong>di</strong> tipoalgebrico.APPRESENTAZIONE DEI LINGUAGGI 32


OPERAZIONI SUI LINGUAGGISia Σ un alfabeto e L, L 1 , L 2 insiemi <strong>di</strong> stringhe <strong>di</strong> Σ ∗ .➜ La concatenazione <strong>di</strong> L 1 e L 2 è l’insieme:L 1 L 2 = { xy ∈ Σ ∗ | x ∈ L 1 , y ∈ L 2 }.➜ La chiusura (<strong>di</strong> Kleene) <strong>di</strong> L, è l’insieme:L ∗ = [ L i ,i≥0doveL 0 = {ε},L i+1 = LL i .PERAZIONI SUI LINGUAGGI 33


OPERAZIONI SUI LINGUAGGI (CONT.)➜ La chiusura positiva <strong>di</strong> L è l’insiemeL + = [ i≥1L i .➜ Nota: L + = LL ∗ .➜ Altre operazioni sui linguaggi che menzioneremo nel seguito sono lenormali operazioni insiemistiche <strong>di</strong> unione, intersezione e complementorispetto a Σ ∗ .PERAZIONI SUI LINGUAGGI (CONT.) 34


ESPRESSIONI REGOLARISia Σ un alfabeto. Le espressioni regolari su Σ e gli insiemi che essedenotano sono definiti ricorsivamente nel modo seguente:1. ∅ è una espressione regolare che denota l’insieme vuoto.2. ε è una espressione regolare che denota l’insieme {ε}.3. Per ogni simbolo a ∈ Σ, a è una espressione regolare chedenota l’insieme {a}.4. Se r e s sono espressioni regolari denotanti rispettivamente gliinsiemi R ed S, allora (r + s), (rs), e (r ∗ ) sono espressioniregolari che denotano gli insiemi R ∪ S, RS, e R ∗rispettivamente.Se r è una espressione regolare, in<strong>di</strong>cheremo con L(r) il linguaggiodenotato da r.SPRESSIONI REGOLARI 35


ESPRESSIONI REGOLARI (CONT.)Possiamo dare una definizione più formale del linguaggio denotatoda un’espressione regolare: se r ed s sono espressioni regolari,L(∅) = ∅,L(ε) = {ε},L(a) = {a}, per ogni a ∈ Σ,L(r + s) = L(r) ∪ L(s),L(rs) = L(r)L(s),L(r ∗ ) = L(r) ∗ .SPRESSIONI REGOLARI (CONT.) 36


ESPRESSIONI REGOLARI (ESEMPI)➜ Sia Σ = {0,1}. Il linguaggio associato all’espressione regolarer = 1(0 + 1) ∗ 00 èL(r) = L(1)(L(0) ∪ L(1)) ∗ L(0)L(0) = { 1x00 | x ∈ Σ ∗ },ovvero l’insieme delle stringhe binarie rappresentanti un multiplo <strong>di</strong> 4.➜ Sia Σ l’insieme dei caratteri ASCII. Una stringa w è un identificatore C oC++ se e solo se w ∈ L(r) conr = (+· · ·+++· · ·+Þ+)(+· · ·+++· · ·+Þ++¼+· · ·+) ∗ ,ovvero w è una sequenza non vuota <strong>di</strong> lettere, cifre decimali ounderscore in cui il primo carattere non è una cifra decimale.SPRESSIONI REGOLARI (ESEMPI) 37


EQUIVALENZA TRA DFA ED ESPRESSIONI REGOLARITeorema 4 (McNaughton & Yamada, 1960) Sia r una espressioneregolare. Allora esiste un ε-NFA M tale che L(M) = L(r).Dimostrazione: Costruiremo un ε-NFA siffatto, con un unico statofinale, per induzione sulla struttura dell’espressione regolare r.Base: Ci sono tre casi base:➜ l’automa:✓✏ ✎☞q 0✍✌✒✑riconosce il linguaggio {ε};QUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI 38


EQUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI (CONT.)➜ l’automa✓✏q 0✒✑✓✏ ✎☞q 1✒✑✍✌riconosce il linguaggio ∅;➜ l’automa✓✏ ✓✏a✎☞q 0✲ q1✒✑ ✒✑ ✍✌riconosce il linguaggio {a}.QUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI (CONT.) 39


EQUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI (CONT.)Passo: Anche qui abbiamo tre casi da analizzare:➜ r = r 1 + r 2 . Per i = 1,2, sia M i , con stato iniziale q i 0 e stato finale q i fl’automa che riconosce L(r i ). Il seguente automa, con stato iniziale q 0 estato finale q f riconosce il linguaggio L(r) = L(r 1 ) ∪ L(r 2 ):✒✒✑ε ✫✓✏q 0✒✑✬✓✏◗ε ◗ ◗✬✓✏q 1 0q 2 0M 1M 2✒✑✫✩✓✏ ✎☞q 1 f✒✑ ✍✌❅ ε✪❅ ❅❘ ✓✏ ✎☞q 2 fε ✒q✍✌f✩✒✑✓✏ ✎☞✒✑ ✍✌✪QUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI (CONT.) 40


EQUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI (CONT.)➜ r = r 1 r 2 . Per i = 1, 2, sia M i , con stato iniziale q i 0 e stato finale q i fl’automa che riconosce L(r i ). L’esistenza <strong>di</strong> tali automi è assicuratadall’ipotesi induttiva. Il seguente automa, con stato iniziale q 0 e statofinale q f riconosce il linguaggio L(r 1 )L(r 2 ):✬✓✏ ✓✏εq 0✲ q01✒✑ ✒✑✫M 1✩✬✓✏ ✎☞ ✓✏εq ✲ q0✍✌2 f1✒✑ ✒✑✪✫M 2✩✓✏ ✎☞ ✓✏ ✎☞εq 2 ✲fq f✒✑ ✍✌ ✒✑ ✍✌✪QUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI (CONT.) 41


EQUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI (CONT.)➜ r = r ∗ 1. Sia M 1 , con stato iniziale q 1 0 e stato finale q 1 f l’automa chericonosce L(r 1 ). L’esistenza <strong>di</strong> tale automa è assicurata dall’ipotesiinduttiva. Il seguente automa, con stato iniziale q 0 e stato finale q friconosce L(r) = (L(r 1 )) ∗ :ε✬✬✓✏ ✓✏ ❄εq 0✲ q01✒✑ ✒✑✫ε✫M 1✩✩✓✏ ✎☞ ✓✏ ✎☞εq ✲ qff✒✑ ✍✌1 ✒✑ ✍✌✪✻✪Le <strong>di</strong>mostrazioni che tali automi riconoscono esattamente i linguaggia loro assegnati è lasciata per esercizio.QUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI (CONT.) 42


EQUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI (CONT.)Teorema 5 Sia M un DFA. Allora esiste una espressione regolare rtale che L(M) = L(r).Dimostrazione: Sia M = 〈Q, Σ, δ, q 0 , F 〉 con Q = {1, 2, . . ., n} eq 0 = 1.Con r (k)ij denotiamo l’espressione regolare che definisce l’insieme <strong>di</strong>stringhe che permettono <strong>di</strong> spostarsi dallo stato i allo stato j nel DFAM, senza passare attraverso stati maggiori <strong>di</strong> k (salvo i e j).Definiamo induttivamente r (k)ij :Base: con k = 0 ci sono solo due possibilità:1. una transizione dallo stato i allo stato j;2. un cammino <strong>di</strong> lunghezza 0 e i = j.QUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI (CONT.) 43


EQUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI (CONT.)Se i ≠ j allora è possibile solo il primo caso, quin<strong>di</strong>:i. se non c’è nessuna transizione da i a j, allora r (0)ij = ∅;ii. se c’è esattamente una transizione da i a j per il simbolo a,allora r (0)ij = a;iii. se ci sono p > 1 simboli a 1 , a 2 , . . .,a p corrispondenti adaltrettante transizioni da i a j, allora r (0)ij = a 1 + a 2 + · · · + a p .Se i = j, allora è possibile avere un cammino <strong>di</strong> lunghezza 0. Ledefinizioni vanno dunque così mo<strong>di</strong>ficate:i. r (0)ijii. r (0)ijiii. r (0)ij= ε (nessun simbolo a);= ε + a (un solo simbolo a);= ε + a 1 + a 2 + · · · + a p (p simboli).QUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI (CONT.) 44


EQUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI (CONT.)Passo: Sia k > 0. Per ogni coppia <strong>di</strong> stati i e j, consideriamo tutti icammini da i a j che attraversano stati non maggiori <strong>di</strong> k (possonoessere infiniti). Per ognuno <strong>di</strong> questi ci sono due casi:a. Il cammino non passa attraverso lo stato k: dunque, per ipotesiinduttiva, la stringa corrispondente è in r (k−1)ij ;b. Il cammino passa attraverso lo stato k almeno una volta. Inquesto caso spezzeremo il cammino in tre parti:1. il primo andrà dallo stato i allo stato k senza passareattraverso k, e corrisponderà per i. i. ad una stringa in r (k−1)ik;2. l’ultimo andrà dallo stato k allo stato j senza passareattraverso k, e corrisponderà per i. i. ad una stringa in r (k−1)kj;QUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI (CONT.) 45


EQUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI (CONT.)3. la parte centrale sarà a sua volta la concatenazione <strong>di</strong> zero o piùframmenti che vanno da k a k senza passare da k. Ognuno <strong>di</strong>questi frammenti corrisponde per i. i. ad una stringa in r (k−1) . Laparte centrale corrisponderà quin<strong>di</strong> ad una stringa inCombinando i casi a. e b., otteniamo(kkr (k−1)kk) ∗.r (k)ij= r (k−1)ij+ r (k−1)ik((k−1)) ∗r (k−1) rkk kj.Se F = {f 1 , . . .,f h }, l’espressione cercata èr = r (n)1f 1+ · · · + r (n)1f h.QUIVALENZA TRA DFA ED ESPRESSIONI REGOLARI (CONT.) 46


LINGUAGGI REGOLARI E NON➜ Abbiamo visto <strong>di</strong>versi meto<strong>di</strong> per stabilire se un dato linguaggio èregolare (esibizione <strong>di</strong> un DFA, NFA, ε-NFA o <strong>di</strong> una espressioneregolare).➜ Ci chie<strong>di</strong>amo ora: tutti i linguaggi sono regolari?➜ In caso contrario: come si può <strong>di</strong>mostrare che un linguaggio non èregolare?➜ (Non essere riusciti a trovare un automa o un’espressione regolareadeguati, chiaramente, non <strong>di</strong>mostra nulla).INGUAGGI REGOLARI E NON 47


IL “PUMPING LEMMA” PER LINGUAGGI REGOLARILemma 6 (Bar-Hillel, Perles, Shamir, 1961) Sia L un linguaggioregolare. Allora esiste una costante n ∈ N tale che, per ogni z ∈ Ltale che |z| ≥ n, esistono tre stringhe u, v, w tali che:1. z = uvw,2. |uv| ≤ n,3. |v| > 0, e4. per ogni i ≥ 0 vale che uv i w ∈ L.“PUMPING LEMMA” PER LINGUAGGI REGOLARI 48


IL “PUMPING LEMMA” PER LINGUAGGI REGOLARI (CONT.)Dimostrazione:➜ Sia M = 〈{q 0 , . . .,q n−1 },Σ, δ, q 0 ,F 〉 un DFA tale che L = L(M).➜ Sia z = a 1 · · · a m ∈ L arbitraria con m ≥ n (se una tale z non esistesse,il lemma varrebbe banalmente).➜ Per i = 1, ...,n (ricordando che n ≤ m) si consideri l’evoluzione deglistati ˆδ(q 0 , a 1 · · · a i ):q 0 , ˆδ(q 0 , a 1 ), ˆδ(q 0 , a 1 a 2 ), . . ., ˆδ(q 0 , a 1 · · · a n )| {z }n+1➜ Poiché gli stati dell’automa sono n per ipotesi, esiste (almeno) uno stato¯q raggiunto (almeno) due volte:con i 2 > i 1 .ˆδ(q 0 ,a 1 · · · a i1 ) = ¯q= ˆδ(q 0 , a 1 · · · a i1 · · · a i2 )“PUMPING LEMMA” PER LINGUAGGI REGOLARI (CONT.) 49


IL “PUMPING LEMMA” PER LINGUAGGI REGOLARI (CONT.)➜ Si pongonou = a 1 · · · a i1 ,v = a i1 +1 · · · a i2 ,w = a i2 +1 · · · a m .➜ Abbiamo |uv| ≤ n e |v| > 0 per costruzione.➜ Sappiamo che ˆδ(q 0 , u) = ¯q e ˆδ(q 0 , uv) = ¯q. Usando l’esercizio 3.8 si hache ˆδ(¯q, v) = ¯q.➜ Per ipotesi abbiamo che ˆδ(q 0 , uvw) = q ′ per qualche q ′ ∈ F .➜ Siccome ˆδ(q 0 , uv) = ¯q, usando ancora l’esercizio 3.8 si ha cheˆδ(¯q, w) = q ′ .➜ Per induzione su n ≥ 0 si mostra (farlo per esercizio) che ˆδ(q 0 ,uv i ) = ¯q.➜ Si può quin<strong>di</strong> concludere che ˆδ(q 0 , uv i w) = q ′ ∈ F , come volevasi.“PUMPING LEMMA” PER LINGUAGGI REGOLARI (CONT.) 50


IL “PUMPING LEMMA” PER LINGUAGGI REGOLARI (CONT.)Corollario 7 La costante n ∈ N del “Pumping Lemma” può esserepresa come il minimo numero <strong>di</strong> stati degli automi che riconosconoL.Dimostrazione: Imme<strong>di</strong>ata dalla <strong>di</strong>mostrazione del Lemma.“PUMPING LEMMA” PER LINGUAGGI REGOLARI (CONT.) 51


L “PUMPING LEMMA” PER LINGUAGGI REGOLARI (OSSERVAZIONE)➜ Il lemma asserisce, dato un linguaggio regolare L, la veri<strong>di</strong>cità dellaformula∃n ∈ N . ∀z ∈ L :“|z| ≥ n → ∃u, v,w. `z = uvw ∧ |uv| ≤ n ∧ |v| > 0 ∧ ∀i ∈ N : uv i w ∈ L´”➜ Il “Pumping Lemma” può essere usato per mostrare che un datolinguaggio non è regolare (qualora non lo sia!). Per fare ciò, bisognamostrare che vale la negazione della formula <strong>di</strong> cui sopra, ovvero <strong>di</strong>∀n ∈ N : ∃z ∈ L .“|z| ≥ n ∧ ∀u, v, w: `z = uvw ∧ |uv| ≤ n ∧ |v| > 0 ∧ ∃i ∈ N . uv i w /∈ L´”“PUMPING LEMMA” PER LINGUAGGI REGOLARI (OSSERVAZIONE) 52


IL “PUMPING LEMMA” PER LINGUAGGI REGOLARI (ESEMPIO)➜ Il linguaggio L = { 0 i 1 i | i ≥ 0 } non è regolare.➜ Si prenda un numero naturale n arbitrario e si scelga, tra tutte lestringhe in L <strong>di</strong> lunghezza maggiore o uguale a n, la stringaz = 0 n 1 n ∈ L.➜ Bisogna ora mostrare che prese comunque u, v e w tali che z = uvw,|uv| ≤ n e |v| > 0 esiste almeno un i ≥ 0 tale per cui uv i w /∈ L.➜ Essendo |uv| ≤ n, tali mo<strong>di</strong> <strong>di</strong> sud<strong>di</strong>videre z sono tutti riconducibili alseguente schema, con a + b + c = n e b > 0:u = 0 a ,v = 0 b ,w = 0 c 1 n ,➜ “Pompando” v si ottengono stringhe al <strong>di</strong> fuori del linguaggio, da cuil’assurdo. Ad esempio, con i = 0 vale che uv 0 w = 0 a 0 c 1 n /∈ L, poichéa + c < n.“PUMPING LEMMA” PER LINGUAGGI REGOLARI (ESEMPIO) 53


PROPRIETÀ DI CHIUSURA DEI LINGUAGGI REGOLARITeorema 8 I linguaggi regolari sono chiusi rispetto alle operazioni <strong>di</strong>unione, concatenazione e chiusura <strong>di</strong> Kleene.Dimostrazione: Imme<strong>di</strong>ata usando i risultati visti sulle espressioniregolari.Teorema 9 I linguaggi regolari sono chiusi rispetto alla operazione<strong>di</strong> complementazione. Ovvero, se L ⊆ Σ ∗ è regolare, anche¯L = Σ ∗ \ L è regolare.Dimostrazione: Sia M = 〈Q, Σ, δ, q 0 , F 〉 il DFA che riconosce L.Allora, banalmente, M ′ = 〈Q, Σ, δ, q 0 , Q \ F 〉 riconosce ¯L.Corollario 10 I linguaggi regolari sono chiusi rispettoall’intersezione.Dimostrazione: Imme<strong>di</strong>ata dal fatto che L 1 ∩ L 2 = (¯L 1 ∪ ¯L 2 ).ROPRIETÀ DI CHIUSURA DEI LINGUAGGI REGOLARI 54


RISULTATI DI DECIDIBILITÀTeorema 11 (Appartenenza) Il problema dell’appartenenza perlinguaggi regolari (data una descrizione del linguaggio regolare L[me<strong>di</strong>ante e.r. o automa] e una stringa x, decidere se x ∈ L o meno)è deci<strong>di</strong>bile.Dimostrazione: Imme<strong>di</strong>ata.Teorema 12 (Vuoto-infinito) L’insieme delle stringhe accettate daun DFA M con n stati è:1. non vuoto se e solo se accetta una stringa <strong>di</strong> lunghezza inferiorea n;2. infinito se e solo se l’automa accetta una stringa <strong>di</strong> lunghezza l,n ≤ l < 2n.ISULTATI DI DECIDIBILITÀ 55


Dimostrazione <strong>di</strong> 1:RISULTATI DI DECIDIBILITÀ (CONT.)➜ (⇐=) Se M accetta una stringa (<strong>di</strong> lunghezza inferiore a n) allora L ènon vuoto.➜ (=⇒) Supponiamo L ≠ ∅. Allora M accetta almeno una stringa z,|z| = m.➜ Se m < n, la tesi è provata.➜ Altrimenti, per il pumping lemma, z = uvw con |v| ≥ 1, e uv 0 w = uwè accettata da M.➜ Se |uw| < n la tesi è provata.➜ Altrimenti si proceda iterativamente ripartendo con z = uw (cheha lunghezza strettamente minore a m). . .➜ . . . dopo al più m − n iterazioni si otterrà una stringa <strong>di</strong> lunghezzainferiore a n.ISULTATI DI DECIDIBILITÀ (CONT.) 56


Dimostrazione <strong>di</strong> 2:RISULTATI DI DECIDIBILITÀ (CONT.)➜ (⇐=) Supponiamo M accetti una stringa z <strong>di</strong> lunghezza l conn ≤ l < 2n.➜ Per il pumping lemma, z = uvw, |v| ≥ 1 e { uv i w | i ∈ N } ⊆ L(M).➜ { uv i w | i ∈ N } è un insieme infinito, come volevasi.➜ (=⇒) Sia L(M) infinito.➜ Allora esiste z ∈ L(M) tale che |z| = m ≥ 2n.➜ Per il pumping lemma z = uvw con |uv| ≤ n, |v| ≥ 1 (dunque|uw| ≥ n) e z ′ = uw ∈ L(M).➜ Se |z ′ | < 2n, allora la tesi è <strong>di</strong>mostrata.➜ Altrimenti, si reiteri il proce<strong>di</strong>mento partendo dalla stringaz ′ = uw (più corta <strong>di</strong> z). . .➜ . . . in un numero finito <strong>di</strong> passi si trova la stringa cercata.ISULTATI DI DECIDIBILITÀ (CONT.) 57


RISULTATI DI DECIDIBILITÀ (CONT.)Corollario 13 Sia M DFA. Allora i problemi L(M) = ∅ e L(M) èinfinito sono entrambi deci<strong>di</strong>bili.Teorema 14 (Equivalenza) Dati due DFA M 1 e M 2 , il problema <strong>di</strong>stabilire se L(M 1 ) = L(M 2 ) è deci<strong>di</strong>bile.Dimostrazione: L(M 1 ) = L(M 2 ) è insiemisticamente equivalente a(L(M1 ) ∩ L(M 2 ) ) ∪ ( L(M 1 ) ∩ L(M 2 ) ) = ∅.Sappiamo che esiste un automa M 3 tale cheL(M 3 ) = ( L(M 1 ) ∩ L(M 2 ) ) ∪ ( L(M 1 ) ∩ L(M 2 ) ) .Il risultato segue dalla deci<strong>di</strong>bilità del problema del vuoto.ISULTATI DI DECIDIBILITÀ (CONT.) 58


RELAZIONI E CLASSI DI EQUIVALENZA➜ Dato un insieme S, una relazione <strong>di</strong> <strong>equivalenza</strong> R ⊆ S × S(univocamente, a meno <strong>di</strong> ridenominazione degli in<strong>di</strong>ci) una partizione<strong>di</strong> S = S i∈I S i dove, per ogni i,j ∈ I con i ≠ j, si ha che:1. S i ≠ ∅;2. S i ∩ S j = ∅;3. ∀a, b ∈ S i : a R b;4. ∀a ∈ S i : ∀b ∈ S j : ¬(a R b).➜ Le S i sono dette classi <strong>di</strong> <strong>equivalenza</strong>.➜ Se a ∈ S i allora con la notazione [a] R (o semplicemente [a] quando R èchiara dal contesto) si denota la classe S i .➜ Se I è un insieme finito R si <strong>di</strong>ce <strong>di</strong> in<strong>di</strong>ce finito (|I|) su S.➜ Date due relazioni <strong>di</strong> <strong>equivalenza</strong> R 1 e R 2 sullo stesso insieme S, R 1 èun raffinamento <strong>di</strong> R 2 se ogni classe <strong>di</strong> <strong>equivalenza</strong> della partizioneindotta da R 1 è sottoinsieme <strong>di</strong> qualche classe <strong>di</strong> <strong>equivalenza</strong> dellapartizione indotta da R 2 .ELAZIONI E CLASSI DI EQUIVALENZA 59


RELAZIONI E CLASSI DI EQUIVALENZA (ESEMPIO)➜ Sia S = {2,3, 4,5}.➜ SianoP 1 = ˘{2,3,5}, {4}¯P 2 = ˘{2}, {3,5}, {4}¯le partizioni <strong>di</strong> S ottenute a partire dalle relazioni <strong>di</strong> <strong>equivalenza</strong>R 1 = ˘(x, y) ˛˛ x e y sono entrambi primi o uguali tra loro ¯,R 2 = ˘(x, y) ˛˛ x e y sono entrambi primi e <strong>di</strong>spari, o uguali tra loro¯.➜ R 2 è un raffinamento <strong>di</strong> R 1 .ELAZIONI E CLASSI DI EQUIVALENZA (ESEMPIO) 60


DUE RELAZIONI INDOTTE DA LINGUAGGI E AUTOMI➜ Sia L ⊆ Σ ∗ un linguaggio qualsiasi.➜ La relazione R L ⊆ Σ ∗ × Σ ∗ è definita daR L = ˘(x, y) ˛˛ ∀z ∈ Σ ∗ : xz ∈ L ↔ yz ∈ L ¯.➜ Sia M = 〈Q,Σ, δ,q 0 , F 〉 un DFA.➜ La relazione R M ⊆ Σ ∗ × Σ ∗ è definita daR M = ˘(x, y) ˛˛ ˆδ(q0 , x) = ˆδ(q 0 ,y) ¯.➜ Si definisce il linguaggioL q = ˘ x ∈ Σ ∗ ˛˛ ˆδ(q0 , x) = q ¯il linguaggio associato allo stato q dell’automa.➜ Le classi <strong>di</strong> <strong>equivalenza</strong> <strong>di</strong> R M sono esattamente i linguaggi associatiad ogni stato dell’automa M.UE RELAZIONI INDOTTE DA LINGUAGGI E AUTOMI 61


DUE RELAZIONI INDOTTE DA LINGUAGGI E AUTOMI (CONT.)Lemma 15 R L e R M sono relazioni <strong>di</strong> <strong>equivalenza</strong>.Dimostrazione: Fare per esercizio.➜ Una relazione R ⊆ Σ ∗ × Σ ∗ che gode della proprietàx R y =⇒ ∀z ∈ Σ ∗ : xz R yzsi <strong>di</strong>ce invariante a destra (rispetto alla concatenazione).➜ Dall’esercizio 3.8 segue che R M è invariante a destra.➜ Anche R L è invariante a destra:➜ Siano x, y ∈ Σ ∗ tali che x R L y e z ∈ Σ ∗ ,➜ Se non avessimo xz R L yz allora esisterebbe w tale che xzw ∈ L eyzw /∈ L (o viceversa).➜ Ma in tal caso, con z ′ = zw, mostreremmo che x ̸R L y: assurdo.UE RELAZIONI INDOTTE DA LINGUAGGI E AUTOMI (CONT.) 62


IL TEOREMA DI MYHILL-NERODETeorema 16 (Myhill-Nerode, 1957–58) I seguenti enunciati sonoequivalenti:1. L ⊆ Σ ∗ è accettato da un qualche DFA;2. L è l’unione <strong>di</strong> alcune classi <strong>di</strong> <strong>equivalenza</strong> <strong>di</strong> Σ ∗ indotte da unarelazione invariante a destra e <strong>di</strong> in<strong>di</strong>ce finito;3. R L è <strong>di</strong> in<strong>di</strong>ce finito.TEOREMA DI MYHILL-NERODE 63


IL TEOREMA DI MYHILL-NERODE (DIM. DI (1) =⇒ (2))➜ Sia L accettato da un DFA M = 〈Q,Σ, δ,q 0 , F 〉. Mostreremo che R M èla relazione che sod<strong>di</strong>sfa il punto (2).➜ Per definizione <strong>di</strong> linguaggio riconosciuto da un automa,L = [ ˘ ˛˛ x ∈ Σ∗ ˆδ(q0 , x) = q ¯.q∈F➜ Siccome R M è invariante a destra, gli insiemiL q = ˘ x ∈ Σ ∗ ˛˛ ˆδ(q0 , x) = q ¯costituiscono le classi <strong>di</strong> <strong>equivalenza</strong> della partizione indotta da R M .TEOREMA DI MYHILL-NERODE (DIM. DI (1) =⇒ (2)) 64


IL TEOREMA DI MYHILL-NERODE (DIM. DI (2) =⇒ (3))➜ Mostreremo che ogni relazione <strong>di</strong> <strong>equivalenza</strong> R che sod<strong>di</strong>sfa (2) è unraffinamento <strong>di</strong> R L .➜ Sia x ∈ Σ ∗ : vogliamo mostrare che [x] R ⊆ [x] RL .➜ Sia y ∈ [x] R (dunque x R y). Poiché R è invariante a destra per ipotesi,allora xz R yz per ogni z ∈ Σ ∗ .➜ Poiché L è unione <strong>di</strong> classi <strong>di</strong> <strong>equivalenza</strong> <strong>di</strong> R, ciò implica che ogniqualvolta vRw si ha che v ∈ L sse w ∈ L. Pertanto per ogni z ∈ Σ ∗ ,xz ∈ L sse yz ∈ L. Ma allora x R L y per definizione, dunque y ∈ [x] RL .➜ L’in<strong>di</strong>ce <strong>di</strong> R L è minore <strong>di</strong> quello <strong>di</strong> R, che per ipotesi è finito.TEOREMA DI MYHILL-NERODE (DIM. DI (2) =⇒ (3)) 65


IL TEOREMA DI MYHILL-NERODE (DIM. DI (3) =⇒ (1))Si costruisce un DFA M ′ = 〈Q ′ , Σ ′ , δ ′ , q ′ 0, F ′ 〉 che riconosce L. Sia• Q ′ l’insieme (finito per ipotesi) <strong>di</strong> classi <strong>di</strong> <strong>equivalenza</strong> <strong>di</strong> R L ,• Σ ′ lo stesso <strong>di</strong> L,• δ ′ ([x], a) = [xa] (la definizione ha senso in<strong>di</strong>pendentemente dallascelta <strong>di</strong> x in quanto R L è invariante a destra),• q ′ 0 = [ε],• F ′ = {[x] : x ∈ L}.Si tratta <strong>di</strong> mostrare che L(M ′ ) = L. Si verifica per induzione su|y| ≥ 0 che ˆδ ′ ([x], y) = [xy]. Pertanto si ha che:ˆδ ′ (q ′ 0, x) = ˆδ ′ ([ε], x) = [εx] = [x]TEOREMA DI MYHILL-NERODE (DIM. DI (3) =⇒ (1)) 66


e dunquex ∈ L(M ′ ) sse ˆδ ′ (q ′ 0, x) ∈ F ′ sse [x] ∈ F sse x ∈ L .TEOREMA DI MYHILL-NERODE (DIM. DI (3) =⇒ (1)) 67


MINIMIZZAZIONE DI DFATeorema 17 Per ogni linguaggio regolare L esiste un automa M conminimo numero <strong>di</strong> stati tale che L = L(M), unico a meno <strong>di</strong>isomorfismo (ovvero ridenominazione <strong>di</strong> stati).Dimostrazione:➜ Sia L accettato da un DFA M. Σ ∗ è partizionato da R M negli insiemi <strong>di</strong>linguaggi accettati dai singoli stati <strong>di</strong> M.➜ Dal Teorema <strong>di</strong> Myhill-Nerode (1 → 2), R M raffina R L e pertanto|Q| ≥ |Q ′ | (M ′ = (Q ′ , ...) è l’automa costruito come nella<strong>di</strong>mostrazione (3 → 1) del Teorema).➜ Dunque l’automa M ′ ha il minor numero <strong>di</strong> stati possibile.INIMIZZAZIONE DI DFA 68


MINIMIZZAZIONE DI DFA (CONT.)➜ Supponiamo ora che |Q| = |Q ′ | e definiamo f ⊆ Q × Q ′ in modo chef(q) = [x] ⇐⇒ ˆδ(q0 , x) = q.➜ f è una funzione: supponiamo f(q) = [y 1 ] e f(q) = [y 2 ]. Alloraˆδ(q 0 , y 1 ) = q e ˆδ(q 0 , y 2 ) = q. Ma allora y 1 R M y 2 . Poiché R M èraffinamento <strong>di</strong> R L si ha anche [y 1 ] = [y 2 ].➜ f è suriettiva, in quanto per ogni x ∈ Σ ∗ si ha che esiste q = ˆδ(q 0 ,x)tale che f(q) = [x].➜ Poiché |Q| = |Q ′ | ciò implica che f è biiettiva e dunque è una funzione<strong>di</strong> rinomina dei no<strong>di</strong>.INIMIZZAZIONE DI DFA (CONT.) 69


MINIMIZZAZIONE DI DFA (CONT.)➜ Per mostrare che i due automi sono isomorfi rimane da mostrare che ilseguente <strong>di</strong>agramma commuta, per ogni q ∈ Q e per ogni a ∈ Σ:q↓ aq ′f−→f−→[x]↓ a[xa]➜ In altri termini, dati f(q) = [x], δ(q,a) = q ′ , δ ′ ([x], a) = [xa], dobbiamomostrare che f(q ′ ) = [xa].➜ Infatti:f(q ′ ) = [xa] ⇐⇒ q ′ = ˆδ(q 0 ,xa) [def. <strong>di</strong> f]⇐⇒ q ′ = δ(ˆδ(q 0 , x), a)⇐⇒ q ′ = δ(q,a)[def. <strong>di</strong> ˆδ][vero per ipotesi]INIMIZZAZIONE DI DFA (CONT.) 70

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!