Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
aa:lab:sol:8 [2023/12/10 15:26] dmihai created |
aa:lab:sol:8 [2025/12/04 12:08] (current) mihnea.gheorghe fix notation consistency |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Soluții Laboratorul 8 ====== | ||
| - | <note> | + | ===== Soluții ===== |
| - | Vă reamintim următoarele definiții: | + | |
| + | === 1. Definiții și axiome === | ||
| - | (1) $ f \in NP: \exists N$ o Mașină Turing Nondeterministă care decide $ f$ în timp polinomial | + | \( |
| + | \mathrm{insert} : \mathbb{E} \times \mathrm{BTree} \to \mathrm{BTree} | ||
| + | \) | ||
| - | (2) $ f \in P: \exists N$ o Mașină Turing Deterministă care decide $ f$ în timp polinomial | + | \( |
| + | \mathrm{insert}(x, \mathrm{Nil}) | ||
| + | \overset{(\text{INS1})}{=} | ||
| + | \mathrm{Node}(x, \mathrm{{Nil}}, \mathrm{Nil}) | ||
| + | \) | ||
| - | (3) $math[f \le_P g: \exists t: \Sigma^* \rightarrow \Sigma^*, \text{ s.t. } \left\{ \begin{array}{ll} t \text{ e computabilă în timp polinomial de o MTD } \\ \forall w \in \Sigma^\star, f(w) = g(t(w)) \\ \end{array} \right.] | + | \( |
| + | \mathrm{insert}(x, \mathrm{Node}(y,l,r)) | ||
| + | \overset{(\text{INS2})}{=} | ||
| + | \mathrm{Node}(y, \mathrm{insert}(x,l), r), x \le y \ | ||
| + | \) | ||
| - | (4) $ f \in NPH: \forall g \in NP, g \le_P f$ | + | \( |
| + | \mathrm{insert}(x, \mathrm{Node}(y,l,r)) | ||
| + | \overset{(\text{INS3})}{=} | ||
| + | \mathrm{Node}(y, l, \mathrm{insert}(x,r)), x > y | ||
| + | \) | ||
| - | (5) $ f \in NPC: f \in NPH \land f \in NP$ | + | --- |
| - | (6) $ SAT \in NPC$ | + | \( |
| + | \mathrm{min} : \mathrm{BTree} \setminus \{\mathrm{Nil}\} \to \mathbb{E} | ||
| + | \) | ||
| - | </note> | + | \( |
| - | 0. Dacă $ f \le_P g$ și $ g \le_P h$, atunci $ f \le_P h$ | + | \mathrm{min}(\mathrm{Node}(x, \mathrm{Nil}, r)) |
| + | \overset{(\text{MIN1})}{=} | ||
| + | x | ||
| + | \) | ||
| - | Propoziția ne spune că relația $ \le_P$ e **tranzitivă**. | + | \( |
| + | \mathrm{min}(\mathrm{Node}(y, \mathrm{Node}(x,l,r), r')) | ||
| + | \overset{(\text{MIN2})}{=} | ||
| + | \mathrm{min}(\mathrm{Node}(x,l,r)) | ||
| + | \) | ||
| - | Există o transformare $ t_1$ de la $ f$ la $ g$ computabilă în timp $ O(n^i). | + | --- |
| - | Există o transformare $ t_2$ de la $ g$ la $ h$ computabilă în timp $ O(n^j). | + | |
| - | Compunerea lor e o transformare computabilă în timp $ O(n^ij)$ și e o transformare de la $ f$ la $ h$. | + | |
| - | 1. Dacă $ f \in NP$ și $ g \le_P f$, atunci $ g \in NP$ | + | \( |
| + | \mathrm{max} : \mathrm{BTree} \setminus \{\mathrm{Nil}\} \to \mathbb{E} | ||
| + | \) | ||
| - | Considerăm definiția (3); dacă transformarea e computabilă în timp polinomial determinist, e computabilă și în timp polinomial nondeterminist, e.g.$ N_t$ computează $ t$ în timp $ O(n^i)$. | + | \( |
| + | \mathrm{max}(\mathrm{Node}(x, l, \mathrm{Nil})) | ||
| + | \overset{(\text{MAX1})}{=} | ||
| + | x | ||
| + | \) | ||
| - | Considerăm definiția (2); $ N_f$ decide $ f$ în $ O(n^j)$. | + | \( |
| + | \mathrm{max}(\mathrm{Node}(y, l', \mathrm{Node}(x,l,r))) | ||
| + | \overset{(\text{MAX2})}{=} | ||
| + | \mathrm{max}(\mathrm{Node}(x,l,r)) \\ | ||
| + | \) | ||
| - | Compunând mașinile pentru $ t$ și $ f$, obținem o MTN $ N_g$ care decide $ g$ în timp $ O(n^ij)$. | + | === 2. Soluție Python === |
| - | 2. Dacă $ f \in P$ și $ g \le_P f$, atunci $ g \in NP$ | + | <file python bst_path.py> |
| + | import random | ||
| - | Considerând definițiile (1) și (3), putem compune cele două mașini într-o mașină deterministă $ D_g$ care decide $ g$ în timp $ O(n^ij)$. | + | class Node: |
| - | Deci $ g \in P$. | + | def __init__(self, x): |
| - | Dar știm că $ P \subseteq NP$, deci $ g \in NP$. | + | self.x = x |
| + | self.left = None | ||
| + | self.right = None | ||
| - | 3. Dacă $ f \in NPC$ și $ g \le_P f$, atunci $ g \in NPC$ | + | def insert(root, x): |
| + | if root is None: | ||
| + | return Node(x) | ||
| + | if x <= root.x: | ||
| + | root.left = insert(root.left, x) | ||
| + | else: | ||
| + | root.right = insert(root.right, x) | ||
| + | return root | ||
| - | Din (5) și (4) reiese că orice funcție din $ NP$ se reduce la $ f$. | + | def search_path_length(root, x): |
| - | Faptul că $ g$ se reduce la $ f$ nu înseamnă că orice funcție se reduce la $ g$, deci propoziția e falsă. | + | steps = 0 |
| + | current = root | ||
| + | while current is not None: | ||
| + | steps += 1 | ||
| + | if x == current.x: | ||
| + | return steps | ||
| + | elif x < current.x: | ||
| + | current = current.left | ||
| + | else: | ||
| + | current = current.right | ||
| + | return steps | ||
| - | Din definițiile noastre nu reiese imediat nici că $ g \in NP$. | + | def average_search_path(n): |
| - | Aici ne putem folosi de argumentul de la primul exercițiu. | + | """Construiește un BST dintr-o permutare aleatoare și măsoară media.""" |
| + | perm = list(range(1, n + 1)) | ||
| + | random.shuffle(perm) | ||
| - | 4. Dacă $ f \in NPH$ și $ g \le_P f$, atunci $ g \in NPC$ | + | root = None |
| + | for x in perm: | ||
| + | root = insert(root, x) | ||
| - | Același argument ca la exercițiul anterior; de data asta nu putem argumenta nici măcar că $ g \in NP$. | + | lengths = [search_path_length(root, x) for x in perm] |
| + | return sum(lengths) / n | ||
| - | 5. Dacă $ f \in NPC$ și $ f \le_P g$, atunci $ g \in NPC$ | + | def experiment(trials=2000, n=100): |
| + | total = 0 | ||
| + | for _ in range(trials): | ||
| + | total += average_search_path(n) | ||
| + | return total / trials | ||
| - | Din tranzitivitatea relațiti $ \le_P$, reiese că orice funcție din $ NP$ se reduce la $ g$. | + | for n in [10, 20, 50, 100, 200, 500, 1000]: |
| - | Dar asta înseamnă doar că $ g \in NPH$; $ g$ ar putea să nu fie în $ NP$, deci propoziția e falsă. | + | avg = experiment(trials=1000, n=n) |
| + | print(f"n = {n} → lungime medie ≈ {avg:.3f}") | ||
| + | </file> | ||
| - | 6. Dacă $ f \in NPC$ și $ f \le_P g$, atunci $ g \in NPH$ | ||
| - | Același argument de mai sus; deci propoziția e adevărată. | + | === 3. a) \(\forall x \in \mathbb{E}, \forall t \in \mathrm{BTree} : \mathrm{isBST}(t) \implies \mathrm{isBST}(\mathrm{insert}(x,t))\) === |
| + | Vrem să demonstrăm | ||
| + | \( | ||
| + | \mathrm{isBST}(t) \implies \mathrm{isBST}(\mathrm{insert}(x,t)), \forall t \in \mathrm{BTree}, \forall x \in \mathbb{E} | ||
| + | \) | ||
| - | 7. Dacă $ f \in NP$ și $ f \le_P g$, atunci $ g \in NPC$ | + | --- |
| - | Propoziția e falsă; ca $ g \in NPC$, ar trebui ca **toate** funcțiile din $ NP$ să se reducă la ea și $ g \in NP$, dar nu știm asta despre $ g$. Propoziția e falsă. | + | **Caz de bază:** \(t = \mathrm{Nil}\) |
| - | 8. Dacă $ f \in NP$ și $ f \le_P g$, atunci $ g \in NPH$ | + | \( |
| + | \mathrm{insert}(x, \mathrm{Nil}) \overset{(\text{INS1})}{=} \mathrm{Node}(x, \mathrm{Nil}, \mathrm{Nil}), \forall x \in \mathbb{E} | ||
| + | \) | ||
| - | Propoziția e falsă; ca $ g \in NPH$, ar trebui ca **toate** funcțiile din $ NP$ să se reducă la ea; dar știm asta doar despre o funcție anume $ f$. | + | \( |
| + | \begin{aligned} | ||
| + | \mathrm{isBST}(\mathrm{Nil}) | ||
| + | &\overset{(\text{BST})}{=} \mathrm{isBSTBetween}(\mathrm{Nil}, -\infty, +\infty) \\ | ||
| + | &\overset{(\text{BST1})}{=} \text{true}, \forall x \in \mathbb{E} | ||
| + | \end{aligned} | ||
| + | \) | ||
| + | \( | ||
| + | \begin{aligned} | ||
| + | \mathrm{isBST}(\mathrm{Node}(x, \mathrm{Nil}, \mathrm{Nil})) | ||
| + | &\overset{(\text{BST})}{=} \mathrm{isBSTBetween}(\mathrm{Node}(x, \mathrm{Nil}, \mathrm{Nil}), -\infty, +\infty) \\ | ||
| + | &\overset{(\text{BST2})}{=} (-\infty \le x \le +\infty) | ||
| + | \;\land\; | ||
| + | \mathrm{isBSTBetween}(\mathrm{Nil}, -\infty, x) | ||
| + | \;\land\; | ||
| + | \mathrm{isBSTBetween}(\mathrm{Nil}, x, +\infty) \\ | ||
| + | &\overset{(\overline{\mathbb{E}})}{=} \text{true} \;\land\; | ||
| + | \mathrm{isBSTBetween}(\mathrm{Nil}, -\infty, x) | ||
| + | \;\land\; | ||
| + | \mathrm{isBSTBetween}(\mathrm{Nil}, x, +\infty) \\ | ||
| + | &\overset{(\text{BST1})}{=} \text{true}, \forall x \in \mathbb{E} | ||
| + | \end{aligned} | ||
| + | \) | ||
| - | 9. Dacă $ f \le_P SAT$, atunci $ f \in NPC$ | + | Deci cazul de bază este adevărat \(\forall x \in \mathbb{E}\). |
| - | Din definiția (6) și exercițiul 1, reiese că $ f$ e în $ NP$; dar nu putem spune dacă e $ NPC$. | + | --- |
| - | E important de menționat că valoarea de adevăr a propoziției nu depinde de relația dintre $ P$ și $ NP$. | + | **Pas de inducție**: \(t = \mathrm{Node}(y, l, r)\) |
| - | Dacă $ P = NP$, atunci $ NPC = NP \setminus \{f_{FALSE}, f_{TRUE}\}$. | + | |
| - | Deci putem oferi un contraexemplu pentru propoziția de mai sus: $ f = f_{TRUE}$ și $ t$ e funcția care pentru orice input creează formula $ x$ (evident satisfiabilă de interpretarea $ T(x) = TRUE$. | + | |
| - | Observați că $ f$ nu e $ NPC$, nici dacă $ P = NP$. | + | |
| - | 10. Dacă $ SAT \le_P f$, atunci $ f \in NPC$ | + | Presupunem \(\mathrm{isBST}(t)\) |
| + | \( | ||
| + | \overset{(\text{BST})}{\Leftrightarrow} | ||
| + | \mathrm{isBSTBetween}(t, -\infty, +\infty) | ||
| + | \overset{(\text{BST2})}{\Leftrightarrow} | ||
| + | (-\infty \le y \le +\infty) \;\land\; | ||
| + | \mathrm{isBSTBetween}(l, -\infty, y) \;\land\; | ||
| + | \mathrm{isBSTBetween}(r, y, +\infty) \ | ||
| + | (\text{II}) | ||
| + | \) | ||
| - | Din (6) și tranzitivitatea relației $ \le_P$ reiese că orice funcție din $ NP$ se reduce la $ f$. | + | \( |
| - | Dar asta înseamnă doar că $ f \in NPH$. Propoziția e falsă. | + | \begin{aligned} |
| + | \forall lo, y \in \mathbb{E}: &\quad \mathrm{isBSTBetween}(l, lo, y) | ||
| + | \overset{(\text{II1})}{\implies} | ||
| + | \mathrm{isBSTBetween}(\mathrm{insert}(x,l), lo, y) \\ | ||
| + | \forall y, hi \in \mathbb{E}: &\quad \mathrm{isBSTBetween}(r, y, hi) | ||
| + | \overset{(\text{II2})}{\implies} | ||
| + | \mathrm{isBSTBetween}(\mathrm{insert}(x,r), y, hi) | ||
| + | \end{aligned} | ||
| + | \) | ||
| + | --- | ||
| + | |||
| + | **Caz 1: \(x \le y\)** | ||
| + | |||
| + | \( | ||
| + | \mathrm{insert}(x, \mathrm{Node}(y,l,r)) | ||
| + | \overset{(\text{INS2})}{=} | ||
| + | \mathrm{Node}(y, \mathrm{insert}(x,l), r) | ||
| + | \) | ||
| + | |||
| + | \( | ||
| + | \begin{aligned} | ||
| + | \mathrm{isBSTBetween}(\mathrm{Node}(y, \mathrm{insert}(x,l), r), lo, hi) | ||
| + | &\overset{(\text{BST2})}{=} | ||
| + | (lo \le y \le hi) \;\land\; | ||
| + | \mathrm{isBSTBetween}(\mathrm{insert}(x,l), lo, y) \;\land\; | ||
| + | \mathrm{isBSTBetween}(r, y, hi) \\ | ||
| + | &\overset{(\text{II1})}{=} | ||
| + | (lo \le y \le hi) \;\land\; | ||
| + | \mathrm{isBSTBetween}(l, lo, y) \;\land\; | ||
| + | \mathrm{isBSTBetween}(r, y, hi) \\ | ||
| + | &\overset{(\text{II})}{=} | ||
| + | \text{true} | ||
| + | \end{aligned} | ||
| + | \) | ||
| + | |||
| + | --- | ||
| + | |||
| + | **Caz 2: \(x > y\)** | ||
| + | |||
| + | \( | ||
| + | \mathrm{insert}(x, \mathrm{Node}(y,l,r)) | ||
| + | \overset{(\text{INS3})}{=} | ||
| + | \mathrm{Node}(y, l, \mathrm{insert}(x,r)) | ||
| + | \) | ||
| + | |||
| + | \( | ||
| + | \begin{aligned} | ||
| + | \mathrm{isBSTBetween}(\mathrm{Node}(y, l, \mathrm{insert}(x,r)), lo, hi) | ||
| + | &\overset{(\text{BST2})}{=} | ||
| + | (lo \le y \le hi) \;\land\; | ||
| + | \mathrm{isBSTBetween}(l, lo, y) \;\land\; | ||
| + | \mathrm{isBSTBetween}(\mathrm{insert}(x,r), y, hi) \\ | ||
| + | &\overset{(\text{II2})}{=} | ||
| + | (lo \le y \le hi) \;\land\; | ||
| + | \mathrm{isBSTBetween}(l, lo, y) \;\land\; | ||
| + | \mathrm{isBSTBetween}(r, y, hi) \\ | ||
| + | &\overset{(\text{II})}{=} | ||
| + | \text{true} | ||
| + | \end{aligned} | ||
| + | \) | ||
| + | |||
| + | --- | ||
| + | |||
| + | **Deci** \(\mathrm{isBST}(t) \implies \mathrm{isBST}(\mathrm{insert}(x,t)), \forall x \in \mathbb{E}, \forall t \in \mathrm{BTree}\) | ||