Języki, automaty i obliczenia/Ćwiczenia 7: Twierdzenie Kleene'ego. Własności języków i gramatyk regularnych: Różnice pomiędzy wersjami
Nie podano opisu zmian |
m Zastępowanie tekstu – „.</math>” na „</math>” |
||
(Nie pokazano 18 wersji utworzonych przez 3 użytkowników) | |||
Linia 1: | Linia 1: | ||
== | ==Ćwiczenia 7== | ||
{{cwiczenie| | {{cwiczenie|1|| | ||
Niech <math> | Niech <math>A = \{a,b\}</math>. Dla automatów<center><math>{\mathcal B} = (S_{\mathcal B},A,f_{\mathcal B},s_{\mathcal B},F_{\mathcal B}),\;\;\;\; {\mathcal C} = | ||
(S_{\mathcal C},A,f_{\mathcal C},s_{\mathcal C},F_{\mathcal C})</math></center> | (S_{\mathcal C},A,f_{\mathcal C},s_{\mathcal C},F_{\mathcal C})</math></center> | ||
<center><math> | <center><math>S_{\mathcal B} = \{s_{\mathcal B},s_1,s_2\}, | ||
\;\;F_{\mathcal B} = \{s_2\},\;\;\; S_{\mathcal C} = \{s_{\mathcal C},s\},\;\; F_{\mathcal C} = \{s\} | \;\;F_{\mathcal B} = \{s_2\},\;\;\; S_{\mathcal C} = \{s_{\mathcal C},s\},\;\; F_{\mathcal C} = \{s\}</math>,</center> | ||
gdzie | gdzie | ||
<center><math> | <center><math>\begin{array} {c|c|c|c|} f_{\mathcal B} &s_{\mathcal B} &s_1 & s_2 \\ | ||
\hline a & s_2 & s_1 & s_2 \\ \hline b & s_1 & s_1 & s_2\\ | \hline a & s_2 & s_1 & s_2 \\ \hline b & s_1 & s_1 & s_2\\ | ||
\hline \end{array} | \hline \end{array} \begin{array} {c|c|c|} f_{\mathcal C} &s_{\mathcal C} &s \\ \hline a & s & s \\ | ||
\hline b & s_{\mathcal C} & s_{\mathcal C} \\ \hline \end{array} | \hline b & s_{\mathcal C} & s_{\mathcal C} \\ \hline \end{array}</math></center> | ||
skonstruuj automat <math> | skonstruuj automat <math>\mathcal A</math> taki, że <center><math>L({\mathcal A}) = L({\mathcal B}) \cap L({\mathcal C})</math>,</center> | ||
}} | }} | ||
Rozwiązanie | <div class="mw-collapsible mw-made=collapsible mw-collapsed"><span class="mw-collapsible-toogle mw-collapsible-toogle-default style="font-variant:small-caps">Rozwiązanie </span><div class="mw-collapsible-content" style="display:none"><math>L({\mathcal A})= (S_{\mathcal B}\times S_{\mathcal C},A,f_{\mathcal A},(s_{\mathcal B},s_{\mathcal C}), | ||
<math> | |||
F_{\mathcal B}\times F_{\mathcal C})</math>. Funkcja przejść zadana jest grafem | F_{\mathcal B}\times F_{\mathcal C})</math>. Funkcja przejść zadana jest grafem | ||
rys1< | <center> | ||
[[File:ja-lekcja07-c-rys1.svg|250x250px|thumb|center|Rysunek 1]] | |||
</center> | |||
Zauważ, że otrzymany automat nie jest minimalny. Jakie języki akceptują automaty <math> | Zauważ, że otrzymany automat nie jest minimalny. Jakie języki akceptują automaty <math>{\mathcal A}</math>, <math>{\mathcal B}</math> i <math>{\mathcal C}</math>? | ||
</div></div> | |||
{{cwiczenie|2|| | |||
Niech <math>A = \{a,b\}</math>. Dla automatu <center><math>{\mathcal B} = (S_{\mathcal | |||
B},A,f_{\mathcal B},s_{\mathcal B},F_{\mathcal B})</math>,</center> | |||
Niech <math> | |||
B},A,f_{\mathcal B},s_{\mathcal B},F_{\mathcal B}) | |||
gdzie | gdzie | ||
<center><math> | <center><math>S_{\mathcal B} = \{s_{\mathcal B},s_1,s_2\}, | ||
\;\;F_{\mathcal B} = \{s_1\} | \;\;F_{\mathcal B} = \{s_1\}</math>,</center> | ||
a funkcja przejść zdefiniowana jest | a funkcja przejść zdefiniowana jest | ||
następująco: | następująco: | ||
<center><math> | <center><math>\begin{array} {c|c|c|c|} f_{\mathcal B} &s_{\mathcal B} &s_1 & s_2 \\ | ||
\hline a & s_1 & s_2 & s_2 \\ \hline b & s_1 & s_{\mathcal{B}} & s_1\\ | \hline a & s_1 & s_2 & s_2 \\ \hline b & s_1 & s_{\mathcal{B}} & s_1\\ | ||
\hline \end{array} | \hline \end{array}</math></center> | ||
skonstruuj automat deterministyczny <math> | skonstruuj automat deterministyczny <math>\mathcal A</math> taki, że | ||
<center><math> | <center><math>L({\mathcal A}) = L({\mathcal B})^*</math>.</center> | ||
}} | }} | ||
<div class="mw-collapsible mw-made=collapsible mw-collapsed"><span class="mw-collapsible-toogle mw-collapsible-toogle-default style="font-variant:small-caps">Wskazówka </span><div class="mw-collapsible-content" style="display:none">Pomocne może być użycie automatu z pustymi przejściami. | |||
</div></div> | |||
<div class="mw-collapsible mw-made=collapsible mw-collapsed"><span class="mw-collapsible-toogle mw-collapsible-toogle-default style="font-variant:small-caps">Rozwiązanie </span><div class="mw-collapsible-content" style="display:none"> | |||
Automat <math>\mathcal{B}</math> pokazany jest na rysunku 2. | |||
<center> | |||
[[File:ja-lekcja07-c-rys2.svg|250x150px|thumb|center|Rysunek 2]] | |||
</center> | |||
Automat | |||
pokazany jest na rysunku [[ | |||
Aby skonstruować automat akceptujący język <math>L(\mathcal{B})^*</math>, | |||
[[ | wystarczy dodać przejście <math>f(s_1,1)=s_{\mathcal{B}}</math> (dlaczego?). | ||
Automat z pustymi przejściami akceptujący <math>L(\mathcal{B})^*</math> | |||
pokazany jest na rysunku 3. | |||
<center> | |||
[[File:ja-lekcja07-c-rys3.svg|250x150px|thumb|center|Rysunek 3]] | |||
</center> | |||
Po usunięciu pustych przejść otrzymujemy automat z rysunku 4: | |||
<center> | |||
[[File:ja-lekcja07-c-rys4.svg|250x250px|thumb|center|Rysunek 4]] | |||
</center> | |||
Teraz wystarczy skonstruować równoważny automat deterministyczny. | Teraz wystarczy skonstruować równoważny automat deterministyczny. | ||
</div></div> | |||
{{cwiczenie| | {{cwiczenie|3|| | ||
Dane są dwa automaty nad tym samym alfabetem <math> | Dane są dwa automaty nad tym samym alfabetem <math>A</math><br> | ||
<math> | <math>\mathcal{A}=(S,f,s_{0},T)</math> i <math>\mathcal{B}=(Q,g,t_{0},F)</math>. Udowodnij, | ||
że istnieje liczba <math> | że istnieje liczba <math>p\in{\Bbb N}_{0}</math> taka, że jeśli dla każdego słowa <math>w</math> o długości <math>|w|\leq p</math> spełniona jest implikacja | ||
<math> | <math>w\in L(\mathcal{A})\Rightarrow w\in L(\mathcal{B})</math>, | ||
to | to | ||
<center><math> | <center><math>L(\mathcal{A})\subseteq L(\mathcal{B})</math></center> | ||
}} | }} | ||
<div class="mw-collapsible mw-made=collapsible mw-collapsed"><span class="mw-collapsible-toogle mw-collapsible-toogle-default style="font-variant:small-caps">Rozwiązanie </span><div class="mw-collapsible-content" style="display:none"> | |||
Niech <math> | Niech <math>\mathcal{C}</math> będzie automatem takim, że <math>L(\mathcal{C})=L(\mathcal{A})\cup L(\mathcal{B})</math>. Wówczas, | ||
<center><math> | <center><math>L(\mathcal{C})\subseteq L(\mathcal{B})\Leftrightarrow L(\mathcal{A})\subseteq L(\mathcal{B})</math>.</center> | ||
Udowodnimy więc inkluzję <math> | Udowodnimy więc inkluzję <math>L(\mathcal{C})\subseteq L(\mathcal{B})</math>, gdzie | ||
<center><math> | <center><math>\mathcal{C}=(S\times Q,f\times g,(s_0,t_0), (S\times F \cup T\times Q))</math>.</center> | ||
Przyjmijmy <math> | Przyjmijmy <math>p=|S| \cdot |Q|</math>. Niech <math>w \in L(\mathcal{C})</math> | ||
# Jeśli <math> | # Jeśli <math>|w|\leq p</math>, to z założenia <math>w \in L(\mathcal{B})</math> | ||
# Jeśli <math> | # Jeśli <math>|w|>p</math>, to z lematu o pompowaniu wynika, że <math>\exists v_1 v_2 \in A^*</math>, <math>\exists u \in A^+</math> takie, że <math>w=v_1 uv_2</math>, <math>v_1 v_2 \in L(\mathcal{C})</math> i <math>|v_1v_2|<|w|</math>. | ||
<center><math> | <center><math>(f\times g)((s_0,t_0),w) = (f\times g)((s_0,t_0),v_1 v_2)=(f(s_0,v_1v_2),g(t_0,v_1v_2)) | ||
\in S\times F \cup T\times Q | \in S\times F \cup T\times Q </math></center> | ||
Mamy dwie możliwości: | Mamy dwie możliwości: | ||
:(a) <math>g(t_0,v_1v_2) \in F</math>, co oznacza <math>v_1v_2 \in L(\mathcal{B})</math>, <math>w \in | |||
L(\mathcal{B})</math>, | L(\mathcal{B})</math>, | ||
Jeśli <math> | :(b) <math>f(s_0,v_1v_2)\in T</math>, co oznacza <math>v_1v_2 \in L(\mathcal{A})</math>. <br> | ||
Jeśli <math> | : Jeśli <math>|v_1v_2|<p</math>, to <math>v_1v_2 \in L(\mathcal{B})</math>, <math>w \in L(\mathcal{B})</math>.<br> | ||
: Jeśli <math>|v_1v_2|>p</math>, to powtarzamy punkt 2. | |||
Po każdym kroku długość rozważanego słowa silnie maleje, więc po skończonej ilości kroków uzyskamy oczekiwany efekt.<br> | Po każdym kroku długość rozważanego słowa silnie maleje, więc po skończonej ilości kroków uzyskamy oczekiwany efekt.<br> | ||
Zauważ, że z zadania tego wynika | Zauważ, że z zadania tego wynika | ||
rozstrzygalność problemu równoważności automatów. Algorytmiczny aspekt tego problemu rozważymy w ćwiczeniach | rozstrzygalność problemu równoważności automatów. Algorytmiczny aspekt tego problemu rozważymy w ćwiczeniach 8. | ||
</div></div> | |||
{{cwiczenie| | {{cwiczenie|4|| | ||
Niech <math> | Niech <math>A</math> będzie dowolnym alfabetem, a <math>L \subset A^*</math> językiem regularnym. Udowodnij, że język | ||
<math> | <math>L'=\{a^{|w|} :w \in L\}</math> jest też językiem regularnym. | ||
}} | }} | ||
Rozwiązanie | <div class="mw-collapsible mw-made=collapsible mw-collapsed"><span class="mw-collapsible-toogle mw-collapsible-toogle-default style="font-variant:small-caps">Rozwiązanie </span><div class="mw-collapsible-content" style="display:none"> | ||
x \in A\ | Rozważmy homomorfizm <math>h:A^* \longrightarrow \{a\}^*</math> taki, że <math>\forall | ||
x \in A\quad f(x)=a</math>. Dla tak zdefiniowanego <math>h</math> język <math>L'</math> jest homomorficzym obrazem języka regularnego <math>L'=h(L)</math>, | |||
a więc jest regularny. | a więc jest regularny. | ||
</div></div> | |||
{{cwiczenie| | {{cwiczenie|5|| | ||
Udowodnij, że następujące języki nie są regularne: | Udowodnij, że następujące języki nie są regularne: | ||
# <math> | # <math>L = \{a^n : n \; \text{nie jest liczbą pierwszą;} \}</math>, | ||
# <math> | # <math>L=\left\{ a^{n}b^{m}:0\leq n,m;\;n\neq m\right\}</math>. | ||
}} | }} | ||
<div class="mw-collapsible mw-made=collapsible mw-collapsed"><span class="mw-collapsible-toogle mw-collapsible-toogle-default style="font-variant:small-caps">Rozwiązanie punktu 1</span><div class="mw-collapsible-content" style="display:none"> | |||
\; \text{ jest liczbą pierwszą | Skorzystamy z faktu, że język <math>L' = \{a^n : n | ||
ćwiczenie | \; \text{ jest liczbą pierwszą;} \}</math> nie jest akceptowany ( | ||
<center><math> | ćwiczenie 8), a więc nie jest regularny. | ||
<center><math>a^*=L \cup L', L\cap L' = \emptyset, \;\text {a więc}\;L'=\overline{L}</math></center> | |||
Ponieważ klasa języków regularnych jest zamknięta ze względu na | Ponieważ klasa języków regularnych jest zamknięta ze względu na | ||
uzupełnienie, to <math> | uzupełnienie, to <math>L \notin \mathcal{REG}</math>. | ||
</div></div> | |||
<div class="mw-collapsible mw-made=collapsible mw-collapsed"><span class="mw-collapsible-toogle mw-collapsible-toogle-default style="font-variant:small-caps">Rozwiązanie punktu 2</span><div class="mw-collapsible-content" style="display:none"> | |||
(patrz | Język <math>L'=\left\{ a^{n}b^{n}:0\leq n \right\}</math> nie jest akceptowany | ||
<center><math> | (patrz [[Języki, automaty i obliczenia/Wykład 6: Automat niedeterministyczny. Lemat o pompowaniu#przyklad_3_1|3.1. z wykładu 6]]), a więc nie jest regularny. | ||
<center><math>a^*b^* =L \cup L',L\cap L' = \emptyset,\;\text {a więc}\;L'=a^* b^* \cap\overline{L}</math>.</center> | |||
Ponieważ klasa języków regularnych jest zamknięta ze względu na | Ponieważ klasa języków regularnych jest zamknięta ze względu na | ||
uzupełnienie i przecięcie, to <math> | uzupełnienie i przecięcie, to <math>L \notin \mathcal{REG}</math>. | ||
</div></div> | |||
<center>ZADANIA DOMOWE</center> | |||
{{cwiczenie|6|| | |||
Niech <math>A = \{a,b\}</math>. Skonstruuj automat <math>\mathcal A</math>, taki że | |||
1. <math>L({\mathcal A}) = L({\mathcal B}) \cup L({\mathcal C})</math>, gdzie <center><math>{\mathcal B} = | |||
Niech <math> | |||
(S_{\mathcal B},A,f_{\mathcal B},s_{\mathcal B},F_{\mathcal B}),\;\;\;\; {\mathcal C} = | (S_{\mathcal B},A,f_{\mathcal B},s_{\mathcal B},F_{\mathcal B}),\;\;\;\; {\mathcal C} = | ||
(S_{\mathcal C},A,f_{\mathcal C},s_{\mathcal C},F_{\mathcal C}) | (S_{\mathcal C},A,f_{\mathcal C},s_{\mathcal C},F_{\mathcal C})</math>,</center> | ||
<center><math> | <center><math>S_{\mathcal B} = | ||
\{s_{\mathcal B},s_1,s_2\}, \;\;F_{\mathcal B} = \{s_2\},\;\;\; S_{\mathcal C} = | \{s_{\mathcal B},s_1,s_2\}, \;\;F_{\mathcal B} = \{s_2\},\;\;\; S_{\mathcal C} = | ||
\{s_{\mathcal C},{s'}_1,{s'}_2\},\;\; F_{\mathcal C} = \{{s'}_2\} | \{s_{\mathcal C},{s'}_1,{s'}_2\},\;\; F_{\mathcal C} = \{{s'}_2\}</math>,</center> | ||
<center><math> | <center><math>\begin{array} {c|c|c|c|} f_{\mathcal B} &s_{\mathcal B} &s_1 & s_2 \\ | ||
\hline a & s_2 & s_1 & s_2 \\ \hline b & s_1 & s_1 & s_2\\ \hline \end{array} | \hline a & s_2 & s_1 & s_2 \\ \hline b & s_1 & s_1 & s_2\\ \hline \end{array} | ||
\begin{array} {c|c|c|c|} f_{\mathcal C} &s_{\mathcal C} &{s'}_1&{s'}_2 \\ \hline a & {s'}_1 & {s'}_1& {s'}_2 \\ | \begin{array} {c|c|c|c|} f_{\mathcal C} &s_{\mathcal C} &{s'}_1&{s'}_2 \\ \hline a & {s'}_1 & {s'}_1& {s'}_2 \\ | ||
\hline b & {s'}_2 & {s'}_1& {s'}_2 \\ \hline \end{array} | \hline b & {s'}_2 & {s'}_1& {s'}_2 \\ \hline \end{array}</math></center> | ||
2. <math>L({\mathcal A}) = L({\mathcal B})^*</math>, gdzie <center><math>{\mathcal B} = | |||
(S_{\mathcal B},A,f_{\mathcal B},s_{\mathcal B},F_{\mathcal B}),</math></center> | (S_{\mathcal B},A,f_{\mathcal B},s_{\mathcal B},F_{\mathcal B})</math>,</center> | ||
<center><math>S_{\mathcal B} = \{s_{\mathcal B},s_1,s_2,s_3\}, \;\;F_{\mathcal | |||
B} = \{s_2\}</math>,</center> | |||
<center><math> | <center><math>\begin{array} {c|c|c|c|c|} f_{\mathcal B} &s_{\mathcal B} &s_1 & s_2 & s_3\\ \hline a & s_1 & s_3 & s_3 & s_3 \\ | ||
\hline b & s_3 & s_2 & s_3 &s_3\\ \hline \end{array} | \hline b & s_3 & s_2 & s_3 &s_3\\ \hline \end{array}</math></center> | ||
Podaj dwie konstrukcje: | Podaj dwie konstrukcje: | ||
Linia 166: | Linia 178: | ||
}} | }} | ||
{{cwiczenie| | {{cwiczenie|7|| | ||
Skonstruuj minimalny automat <math> | Skonstruuj minimalny automat <math>\mathcal{A}</math>, taki że | ||
<math> | <math>L(\mathcal{A})=L(\mathcal{B})^*</math>, gdzie <math>\mathcal{B}</math> opisany jest | ||
poniższym grafem: | poniższym grafem:}} | ||
<center> | |||
[[File:ja-lekcja07-c-rys5.svg|250x250px|thumb|center|Rysunek 5]] | |||
</center> | |||
{{cwiczenie|8|| | |||
{{cwiczenie| | |||
Udowodnij, że następujące języki nie są regularne: | Udowodnij, że następujące języki nie są regularne: | ||
# <math> | # <math>L=\left\{ a^{n}b^{m}c^n:0<n,m\right\}</math>, | ||
# <math> | # <math>L = \{(ab)^n(bc)^n : n \geq 0 \}</math>. <br> | ||
}} | |||
Wykorzystaj fakt, że język <math> | <div class="mw-collapsible mw-made=collapsible mw-collapsed"><span class="mw-collapsible-toogle mw-collapsible-toogle-default style="font-variant:small-caps">Wskazówka do punktu 2 </span><div class="mw-collapsible-content" style="display:none"> | ||
Wykorzystaj fakt, że język <math>\left\{ a^{n}b^{n}:n \geq 0\right\}</math> nie jest regularny oraz | |||
domkniętość klasy języków regularnych ze względu na homomorfizm. | domkniętość klasy języków regularnych ze względu na homomorfizm. | ||
</div></div> | |||
{{cwiczenie|9|| | |||
{{cwiczenie| | |||
Zbuduj automat akceptujący język będący ogółem skończonych sekwencji | Zbuduj automat akceptujący język będący ogółem skończonych sekwencji | ||
binarnych, w których liczba zer jest podzielna przez dwa, a liczba | binarnych, w których liczba zer jest podzielna przez dwa, a liczba | ||
jedynek przez 3, a następnie gramatykę generującą ten język. | jedynek przez 3, a następnie gramatykę generującą ten język. | ||
}} | |||
<div class="mw-collapsible mw-made=collapsible mw-collapsed"><span class="mw-collapsible-toogle mw-collapsible-toogle-default style="font-variant:small-caps">Wskazówka </span><div class="mw-collapsible-content" style="display:none"> | |||
\{0, 1\}</math>, <math> | Stany automatu niech będą postaci <math>(x,y)</math>, gdzie <math>x \in | ||
\{0, 1\}</math>, <math>y \in \{0, 1, 2\}</math>. Stan początkowy to <math>(0,0)</math>. Określ | |||
funkcję przejść w ten sposób, że po przeczytaniu przez automat słowa | funkcję przejść w ten sposób, że po przeczytaniu przez automat słowa | ||
zawierającego <math> | zawierającego <math>k</math> symboli 0 oraz <math>l</math> symboli 1 automat znajdzie się | ||
w stanie <math> | w stanie <math>(k \mod 2, l \mod 3)</math>. Zastanów się, który stan będzie | ||
stanem końcowym. Na podstawie automatu określ gramatykę. | stanem końcowym. Na podstawie automatu określ gramatykę. | ||
</div></div> | |||
Aktualna wersja na dzień 11:25, 5 wrz 2023
Ćwiczenia 7
Ćwiczenie 1
gdzie
Ćwiczenie 2
gdzie
a funkcja przejść zdefiniowana jest następująco:
skonstruuj automat deterministyczny taki, że
Ćwiczenie 3
Dane są dwa automaty nad tym samym alfabetem
i . Udowodnij,
że istnieje liczba taka, że jeśli dla każdego słowa o długości spełniona jest implikacja
,
to
Ćwiczenie 4
Niech będzie dowolnym alfabetem, a językiem regularnym. Udowodnij, że język jest też językiem regularnym.
Ćwiczenie 5
Udowodnij, że następujące języki nie są regularne:
- ,
- .
Ćwiczenie 6
Niech . Skonstruuj automat , taki że
1. , gdzie
Podaj dwie konstrukcje:
- opartą na dowodzie twierdzenia Kleene'ego,
- z wykorzystaniem automatu z pustymi przejściami.
Ćwiczenie 7
Skonstruuj minimalny automat , taki że , gdzie opisany jest
poniższym grafem:
Ćwiczenie 8
Udowodnij, że następujące języki nie są regularne:
- ,
- .
Ćwiczenie 9
Zbuduj automat akceptujący język będący ogółem skończonych sekwencji binarnych, w których liczba zer jest podzielna przez dwa, a liczba jedynek przez 3, a następnie gramatykę generującą ten język.