Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
aa:lab:sol:3 [2023/10/27 11:20]
ioana.dabelea created
aa:lab:sol:3 [2023/10/31 18:51] (current)
alexandra.udrescu01 [Exercițiul 2]
Line 1: Line 1:
-1.+===== Exercițiul ​=====
   * $ f_{111}(M) = TRUE \iff  M[111] \rightarrow TRUE$   * $ f_{111}(M) = TRUE \iff  M[111] \rightarrow TRUE$
  
Line 16: Line 16:
  
 Demonstraţii:​ Demonstraţii:​
-  - $ f_{h}(M, w) = TRUE \Rightarrow M[w] \text{se termină\Rightarrow ​\text{vom returna x == 111\Rightarrow ​\text{când x este 111}, M^* \text{va tranziţiona în Y} => M^*[111] \rightarrow TRUE \Rightarrow f_{111}(M^*) = TRUE$ +  - $ f_{h}(M, w) = TRUE \Rightarrow M[w] se termină ​ $\Rightarrow ​vom returna x == 111 $\Rightarrow ​când x este 111, M^* va tranziţiona în Y $\Rightarrow ​M^*[111] \rightarrow TRUE \Rightarrow f_{111}(M^*) = TRUE$ 
-  - $ f_{111}(M^*) = TRUE \Rightarrow M^*[111] \rightarrow TRUE \Rightarrow ​\text{am ajuns la return\Rightarrow ​\text{run M[w] din }M^* \text{nu a ciclat\Rightarrow ​\text{M[w] s-a oprit\Rightarrow f_{h}(M, w) = TRUE$+  - $ f_{111}(M^*) = TRUE \Rightarrow M^*[111] \rightarrow TRUE \Rightarrow ​am ajuns la return ​$\Rightarrow ​run M[w] nu a ciclat ​\Rightarrow ​M[w] s-a oprit $\Rightarrow f_{h}(M, w) = TRUE$
  
 Din cele 2 direcţii ale demonstraţiei $\Rightarrow f_{h} \le_m f_{111}$. Din cele 2 direcţii ale demonstraţiei $\Rightarrow f_{h} \le_m f_{111}$.
  
 +------
   * $ f_{p}(M) = TRUE \iff \forall w, M \text{ decide dacă w e palindrom} $   * $ f_{p}(M) = TRUE \iff \forall w, M \text{ decide dacă w e palindrom} $
  
Line 36: Line 36:
  
 Demonstraţii:​ Demonstraţii:​
-  - $ f_{h}(M, w) = TRUE \Rightarrow M[w] \text{se termină\Rightarrow ​\text{o sa verificăm dacă x este palindrom\Rightarrow ​\text{când x este palindrom}, M^* \text{va tranziţiona în Y ​\Rightarrow f_{p}(M^*) = TRUE$ +  - $ f_{h}(M, w) = TRUE \Rightarrow M[w] se termină ​$\Rightarrow ​o sa verificăm dacă x este palindrom ​$\Rightarrow ​când x este palindrom, ​M^* va tranziţiona în Y  ​\Rightarrow f_{p}(M^*) = TRUE$ 
-  - $ f_{p}(M^*) = TRUE \Rightarrow M^*[x] ​\text{decide daca x este palindrom ​\forall x \Rightarrow ​\text{am ajuns la return\Rightarrow ​\text{run M[w] din }M^* \text{nu a ciclat\Rightarrow ​\text{M[w] s-a oprit\Rightarrow f_{h}(M, w) = TRUE$+  - $ f_{p} (M^*) = TRUE \Rightarrow M^*[x] ​decide daca x este palindrom ​ ​$  ​\forall x \Rightarrow ​am ajuns la return ​$  ​\Rightarrow ​run M[w] din  ​$ ​M^* nu a ciclat ​$  ​\Rightarrow ​M[w] s-a oprit $  ​\Rightarrow f_{h} (M, w) = TRUE$
  
 Din cele 2 direcţii ale demonstraţiei $\Rightarrow f_{h} \le_m f_{p}$. Din cele 2 direcţii ale demonstraţiei $\Rightarrow f_{h} \le_m f_{p}$.
 +------
   * $ f_{rev}(M) = TRUE \iff \forall w, M \text{ computează inversul lui w}$   * $ f_{rev}(M) = TRUE \iff \forall w, M \text{ computează inversul lui w}$
  
Line 55: Line 55:
  
 Demonstraţii:​ Demonstraţii:​
-  - $ f_{h}(M, w) = TRUE \Rightarrow M[w] \text{se termină\Rightarrow ​\text{o sa calculeze rev(x) deoarece ajunge la return\Rightarrow f_{rev}(M^*) = TRUE$ +  - $ f_{h} (M, w) = TRUE \Rightarrow M[w]  ​$ ​se termină ​$  ​\Rightarrow ​ ​$ ​o sa calculeze rev(x) deoarece ajunge la return ​$  ​\Rightarrow f_{rev} (M^*) = TRUE$ 
-  - $ f_{rev}(M^*) = TRUE \Rightarrow M^*[x] ​\text{calculeaza rev(x) ​\forall x \forall x \Rightarrow ​\text{am ajuns la return\Rightarrow ​\text{run M[w] din }M^* \text{nu a ciclat\Rightarrow ​\text{M[w] s-a oprit\Rightarrow f_{h}(M, w) = TRUE$+  - $ f_{rev} (M^*) = TRUE \Rightarrow M^*[x] ​ ​$ ​calculeaza rev(x) ​ ​$  ​\forall x \forall x \Rightarrow ​ ​$ ​am ajuns la return ​$  ​\Rightarrow ​ ​$ ​run M[w] din  ​$ ​M^*  ​$ ​nu a ciclat ​$  ​\Rightarrow ​ ​$ ​M[w] s-a oprit $  ​\Rightarrow f_{h} (M, w) = TRUE$
  
 Din cele 2 direcţii ale demonstraţiei $\Rightarrow f_{h} \le_m f_{rev}$. Din cele 2 direcţii ale demonstraţiei $\Rightarrow f_{h} \le_m f_{rev}$.
 +------
   * $ f_{own}(M) = TRUE \iff M[enc(M)] \rightarrow TRUE$   * $ f_{own}(M) = TRUE \iff M[enc(M)] \rightarrow TRUE$
  
Line 76: Line 76:
  
 Demonstraţii:​ Demonstraţii:​
-  - $ f_{h}(M, w) = TRUE \Rightarrow M[w] \text{se termină\Rightarrow M^*[enc(M^*)] ​\text{se termina\Rightarrow f_{own}(M^*) = TRUE$ +  - $ f_{h} (M, w) = TRUE \Rightarrow M[w]  ​$ ​se termină ​$  ​\Rightarrow M^*[enc(M^*)] ​ ​$ ​se termina ​$  ​\Rightarrow f_{own} (M^*) = TRUE$ 
-  - $ f_{own}(M^*) = TRUE \Rightarrow M^*[enc(M^*)] ​\text{se termina\Rightarrow ​\text{run M[w] din }M^* \text{nu a ciclat\Rightarrow ​\text{M[w] s-a oprit\Rightarrow f_{h}(M, w) = TRUE$+  - $ f_{own} (M^*) = TRUE \Rightarrow M^*[enc(M^*)] ​ ​$ ​se termina ​$  ​\Rightarrow ​ ​$ ​run M[w] din  ​$ ​M^*  ​$ ​nu a ciclat ​$  ​\Rightarrow ​ ​$ ​M[w] s-a oprit $  ​\Rightarrow f_{h} (M, w) = TRUE$
  
 Din cele 2 direcţii ale demonstraţiei $\Rightarrow f_{h} \le_m f_{own}$. Din cele 2 direcţii ale demonstraţiei $\Rightarrow f_{h} \le_m f_{own}$.
  
 +------
   * $ f_{finite}(M) = TRUE \iff M \text{ se oprește pentru un număr finit de cuvinte}$   * $ f_{finite}(M) = TRUE \iff M \text{ se oprește pentru un număr finit de cuvinte}$
  
Line 99: Line 99:
  
 Demonstraţii:​ Demonstraţii:​
-  - $ f_{h}(M, w) = TRUE \Rightarrow M[w] \text{ ​se termină in n paşi\Rightarrow M^*[w] ​\text{se opreşte ​\forall x \text{, |x| < n}\text{ciclează ​\forall x \text{, |x| >= n\Rightarrow f_{finite}(M^*) = TRUE$ +  - $ f_{h} (M, w) = TRUE \Rightarrow M[w]  ​$  ​se termină in n paşi $  ​\Rightarrow M^*[w] ​ ​$ ​se opreşte ​ ​$  ​\forall x  ​$ ​, |x| < n  ​$ ​ciclează ​ ​$  ​\forall x  ​$ ​, |x| >= n $  ​\Rightarrow f_{finite} (M^*) = TRUE$ 
-  - $ f_{finite}(M^*) = TRUE \Rightarrow M^*[x] ​\text{ ​se opreşte după un număr finit de cuvinte ​}\Rightarrow ​\text{M[w] s-a oprit după un număr finit de paşi\Rightarrow f_{h}(M, w) = TRUE$+  - $ f_{finite} (M^*) = TRUE \Rightarrow M^*[x] ​ ​$  ​se opreşte după un număr finit de cuvinte ​ ​$ ​\Rightarrow ​ ​$ ​M[w] s-a oprit după un număr finit de paşi $  ​\Rightarrow f_{h} (M, w) = TRUE$
  
 Din cele 2 direcţii ale demonstraţiei $\Rightarrow f_{h} \le_m f_{finite}$. Din cele 2 direcţii ale demonstraţiei $\Rightarrow f_{h} \le_m f_{finite}$.
 +------
   * $ f_{set}(A, M) = TRUE \iff \forall w \in A, M[w] \text{ halts}$   * $ f_{set}(A, M) = TRUE \iff \forall w \in A, M[w] \text{ halts}$
  
Line 121: Line 121:
  
 Demonstraţii:​ Demonstraţii:​
-  - $ f_{h}(M, w) = TRUE \Rightarrow M[w] \text{se termină\Rightarrow ​\text{cum M^* \text{se poate opri doar pentru x = 101\Rightarrow f_{set}(A, M^*) = TRUE$ +  - $ f_{h} (M, w) = TRUE \Rightarrow M[w]  ​$ ​se termină ​$  ​\Rightarrow ​ ​$ ​cum  ​$  ​M^*  ​$ ​se poate opri doar pentru x = 101 $  ​\Rightarrow ​\forall w f_{set} (A, M^*) = TRUE$ 
-  - $ f_{set}(M^*) = TRUE \Rightarrow M^*[x] ​\text{o să se oprească pentru orice x din A\Rightarrow ​\text{M[w] s-a oprit\Rightarrow f_{h}(M, w) = TRUE$+  - $ f_{set} (M^*) = TRUE \Rightarrow M^*[x] ​ ​$ ​o să se oprească pentru orice x din A $  ​\Rightarrow ​ ​$ ​M[w] s-a oprit $  ​\Rightarrow f_{h} (M, w) = TRUE$
  
-Din cele 2 direcţii ale demonstraţiei $\Rightarrow f_{h} \le_m f_{set}$. 
  
 +Din cele 2 direcţii ale demonstraţiei $\Rightarrow f_{h} \le_m f_{set}$.
 +------
   * $ f_{x}(M) = TRUE \iff \exists w, M[w] \text{ scrie un x pe bandă la un moment dat}$   * $ f_{x}(M) = TRUE \iff \exists w, M[w] \text{ scrie un x pe bandă la un moment dat}$
  
Line 140: Line 141:
  
 Demonstraţii:​ Demonstraţii:​
-  - $ f_{h}(M, w) = TRUE \Rightarrow M[w] \text{se termină\Rightarrow ​\text{cum M^* \text{printează x la final\Rightarrow f_{x}(M^*) = TRUE$ +  - $ f_{h} (M, w) = TRUE \Rightarrow M[w]  ​$ ​se termină ​$  ​\Rightarrow ​ ​$ ​cum  ​$  ​M^*  ​$ ​printează x la final $  ​\Rightarrow f_{x} (M^*) = TRUE$ 
-  - $ f_{x}(M^*) = TRUE \Rightarrow M^*[x] ​\text{ajunge să printeze x pe bandă\Rightarrow ​\text{M[w] s-a oprit\Rightarrow f_{h}(M, w) = TRUE$+  - $ f_{x} (M^*) = TRUE \Rightarrow M^*[x] ​ ​$ ​ajunge să printeze x pe bandă ​$  ​\Rightarrow ​ ​$ ​M[w] s-a oprit $  ​\Rightarrow f_{h} (M, w) = TRUE$
  
-Din cele 2 direcţii ale demonstraţiei $\Rightarrow f_{h} \le_m f_{x}$. 
  
 +Din cele 2 direcţii ale demonstraţiei $\Rightarrow f_{h} \le_m f_{x}$.
 +------
   * $ f_{eq}(M_1, M_2) = TRUE \iff  \forall w, M_1[w] \equiv M_2[w]$; i.e. mașinile au același comportament (fie acceptă, fie resping, fie computează aceeași valoare, fie nu se termină) pentru orice cuvânt.   * $ f_{eq}(M_1, M_2) = TRUE \iff  \forall w, M_1[w] \equiv M_2[w]$; i.e. mașinile au același comportament (fie acceptă, fie resping, fie computează aceeași valoare, fie nu se termină) pentru orice cuvânt.
  
Line 164: Line 166:
  
 Demonstraţii: ​ Demonstraţii: ​
-  - $ f_{h}(M, w) = TRUE \Rightarrow M[w] \text{se termină\Rightarrow M_{2} \text{acceptă orice x, iar M_{1}[x] ​\text{deja acceptă orice x din construcţie\Rightarrow M_1[w] \equiv M_2[w] \Rightarrow f_{eq}(M_{1},​ M_{2}) = TRUE$ +  - $ f_{h} (M, w) = TRUE \Rightarrow M[w]  ​$ ​se termină ​$  ​\Rightarrow M_{2}   $ acceptă orice x, iar  ​$  ​M_{1} [x]  ​$ ​deja acceptă orice x din construcţie ​$  ​\Rightarrow M_1[w] \equiv M_2[w] \Rightarrow f_{eq} (M_{1} , M_{2} ) = TRUE$ 
-  - $ f_{eq}(M_{1},​ M_{2}) = TRUE \Rightarrow M_1[w] \equiv M_2[w] \Rightarrow ​\text{stim din construcţie căM_{1} \text{acceptă orice x\Rightarrow M_{2} \text{acceptă orice x\Rightarrow M[w] \text{se opreşte\Rightarrow f_{h}(M, w) = TRUE$+  - $ f_{eq} (M_{1} , M_{2} ) = TRUE \Rightarrow M_1[w] \equiv M_2[w] \Rightarrow ​ ​$ ​stim din construcţie că $  ​M_{1}   $ acceptă orice x $  ​\Rightarrow M_{2}   $ acceptă orice x $  ​\Rightarrow M[w]  ​$ ​se opreşte ​$  ​\Rightarrow f_{h} (M, w) = TRUE$ 
  
 Din cele 2 direcţii ale demonstraţiei $\Rightarrow f_{h} \le_m f_{eq}$. Din cele 2 direcţii ale demonstraţiei $\Rightarrow f_{h} \le_m f_{eq}$.
 +
 +===== Exercițiul 2 =====
 +  * $ f_{all} \le_m f_{eq}$
 +
 +$ M \to t \to M_{1}, M_{2}$
 +Trebuie să construim o transformare t, astfel încât să obţinem 2 maşini cu acelaşi comportament doar dacă M[w] se opreşte **pentru orice** w
 +
 +$ M_{1}[x]: $
 +<​code>​
 + ​acceptă
 +</​code>​
 +
 +$ M_{2}[x]: $
 +<​code>​
 +   run M[x]
 +   ​acceptă
 +</​code>​
 +**Atenţie** acest exercitiu are transformarea diferită faţă de exerciţiul 1, subpunctul 8.
 +
 +Demonstraţii:​
 +  - $ f_{all} (M) = TRUE \Rightarrow \forall x, M[x]  $ se termină $  \Rightarrow M_{2}   ​$ ​ acceptă orice x, iar $  M_{1}   $ deja acceptă orice x din construcţie $  \Rightarrow M_1[w] \equiv M_2[w] \Rightarrow f_{eq} (M_{1} , M_{2} ) = TRUE$
 +  - $ f_{eq} (M_{1} , M_{2} ) = TRUE \Rightarrow M_1[w] \equiv M_2[w] ​ $ , ştim din construcţie că  $  M_{1}  $  acceptă orice x $  \Rightarrow M_{2}   ​$ ​ acceptă orice x $  \Rightarrow M[x]  $ se opreşte pentru orice x $  \Rightarrow f_{all} (M, w) = TRUE$ 
 +
 +
 +------
 +  * $ f_{any} \le_m f_{eq}$
 +
 +$ M \to t \to M_{1}, M_{2}$
 +
 +$ M_{1}[x]: $
 +<​code>​
 + ​acceptă
 +</​code>​
 +
 +$ M_{2}[x]: $
 +<​code>​
 +   k = 1
 +   ​inputs = [0]
 +   while true {
 +    for w in inputs:
 +     ​step(M[w]) // rulam o singura tranzitie
 +     ​if(M[w] s-a oprit)
 +      accepta
 +    k++
 +    inputs.append(k) ​
 +   }
 +</​code>​
 +
 +Demonstraţii:​
 +  - $ f_{any}(M) = M \Rightarrow \exists w, M[w]  $ se termină $ \Rightarrow M_{2}  $  acceptă orice x, iar $ M_{1}  $ deja acceptă orice x din construcţie $ \Rightarrow M_1[w] \equiv M_2[w] \Rightarrow f_{eq}(M_{1},​ M_{2}) = TRUE$
 +  - $ f_{eq}(M_{1},​ M_{2}) = TRUE \Rightarrow M_1[w] \equiv M_2[w] ​ $ , ştim din construcţie că $ M_{1}  $  acceptă orice x $ \Rightarrow M_{2}  $  acceptă orice x $ \Rightarrow \exists w  $  astfel încât $ M[w]  $  se opreşte $ \Rightarrow f_{any}(M, w) = TRUE$ 
 +
 +------
 +  * $ f_{p} \le_m f_{eq}$
 +
 +$ M \to t \to M_{1}, M_{2}$
 +
 +$ M_{1}[x]: $
 +<​code>​
 + ​acceptă
 +</​code>​
 +
 +$ M_{2}[x]: $
 +<​code>​
 +   run M[x]
 +   if M decide corect daca x este palindrom:
 +    accepta
 +   else:
 +    nu accepta
 +</​code>​
 +
 +Demonstraţii:​
 +  - $ f_{p} = TRUE \Rightarrow \forall x M  $ decide daca e palindrom $ \Rightarrow M_{2}  $  acceptă orice x, iar $ M_{1}  $ deja acceptă orice x din construcţie $ \Rightarrow M_1[w] \equiv M_2[w] \Rightarrow f_{eq}(M_{1},​ M_{2}) = TRUE$
 +  - $ f_{eq}(M_{1},​ M_{2}) = TRUE \Rightarrow M_1[w] \equiv M_2[w] ​ $ , ştim din construcţie că $ M_{1}  $  acceptă orice x $ \Rightarrow M_{2}  $  acceptă orice x $ \Rightarrow M  $  decide corect dacă x este palindrom $ \Rightarrow f_{p} = TRUE$
 +
 +--------
 +  * $ f_{own} \le_m  f_{eq}$
 +
 +$ M \to t \to M_{1}, M_{2}$
 +
 +$ M_{1}[x]: $
 +<​code>​
 + ​acceptă
 +</​code>​
 +
 +$ M_{2}[x]: $
 +<​code>​
 +   if x == enc(M):
 +    run M[x]
 +   ​accepta
 +</​code>​
 +
 +Demonstraţii:​
 +  - $ f_{own} = TRUE \Rightarrow M[enc(M)] ​ $  se termină $ \Rightarrow M_{2}  $  acceptă orice x, iar $ M_{1}  $ deja acceptă orice x din construcţie $ \Rightarrow M_1[w] \equiv M_2[w] \Rightarrow f_{eq}(M_{1},​ M_{2}) = TRUE$
 +  - $ f_{eq}(M_{1},​ M_{2}) = TRUE \Rightarrow M_1[w] \equiv M_2[w] ​ $ , ştim din construcţie că $ M_{1}  $  acceptă orice x $ \Rightarrow M_{2}  $  acceptă orice x $ \Rightarrow M[enc(M)] ​ $  se termină $ \Rightarrow f_{own} = TRUE$
 +
 +===== Exercițiul 3 =====
 +Demonstrăm că $ \le_m$ e reflexivă
 +
 +Fie o problemă $ f$ şi o transformare $ t: \Sigma^* \rightarrow \Sigma^*, t(w) = w$
 +
 +$ f(w) = True \iff f(t(w)) = TRUE$ deci $ f \le_m f$ 
 +
 +
 +Demonstrăm că $ \le_m$ e tranzitivă
 +
 +Fie probleme de decizie f, g, h astfel încât:
 +
 +$ f \le_m g$ (deci există o transformare $ t_{1}$ a.î. $ f(w) = TRUE \iff g(t_{1}(w)) = TRUE$)
 +
 +$ g \le_m h$ (deci există o transformare $ t_{2}$ a.î. $ g(w) = TRUE \iff h(t_{2}(w)) = TRUE$) ​
 +
 +
 +Luăm transformarea $ t = t_{1} \circ t_{2}$:
 +
 +$ f(w) = TRUE \iff h(t_{2}(t_{1}(w))) = TRUE$
 +
 +$ f(w) = TRUE \iff h(t(w))) = TRUE \Rightarrow f \le_m h$
 +
 +