-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
198 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Confronto | ||
|
||
<p align="center"><img src="assets/01.png" alt="Confronto dei tempi del concatenamento e dell'indirizzamento aperto"></p> | ||
|
||
Dal grafico si nota che `T` è bene sia **ristrutturata** raddoppiando la dimensione, dopo un certo valore: | ||
- $\alpha \geq 2$ per il _concatenamento_, e richiederà $O(m + n)$ dato che vanno visitate le liste nelle celle | ||
- $\alpha \geq \frac{1}{2}$ per l'_indirizzamento aperto_, e richiederà $O(m)$ |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Taglio delle aste | ||
|
||
Nel problema del **taglio delle aste** si vuole cercare il **massimo guadagno** $r_n$ ottenibile dalla vendita di pezzi ricavati dal taglio di un'asta lunga $n$, i cui prezzi $p_i$ dipendono dalla lunghezza $i$ venduta. | ||
|
||
Per esempio se $n = 7$ e | ||
$$ | ||
\Set{(i, p_i) | i \leq n} = \{(1, 1), (2, 5), (3, 8), (4, 9), (5, 10), (6, 17), (7, 17)\} | ||
$$ | ||
conviene tagliare l'asta in pezzi da $2, 2, 3$ o $6, 1$ invece che $5, 2$ perchè si guadagna $18$ invece che $15$. | ||
|
||
## Ricorsione | ||
|
||
Un'asta lunga $n$ può essere **tagliata o meno** in ogni posizione $1 \leq i \leq n-1$, totalizzando | ||
$$ | ||
\underbrace{2 \cdot 2 \cdots 2}_{n-1} = 2^{n-1} | ||
$$ | ||
tagli e quindi portando la **complessità** a $\Theta(2^n)$. | ||
|
||
Il **ricavo** per l'asta $r_n$ sarà quindi definito ricorsivamente come: | ||
$$ | ||
r_n = \max(p_n, r_1 + r_{n-1}, r_2 + r_{n-2}, ..., r_{n-1} + r_1) | ||
$$ | ||
dove $p_n$ è il ricavo dell'**asta senza tagli** e $r_i + r_{n-i}$ è il ricavo dato dal **taglio su $i$**. | ||
|
||
Per il problema si dice che valga la proprietà di **sottostruttura ottima** perchè la **soluzione ottima** cercata, in questo caso $r_n$, è esprimibile da combinazioni di _soluzione ottime_ di sottoproblemi. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Programmazione dinamica | ||
|
||
La **programmazione dinamica** è una tecnica di **progettazione** di algoritmi che: | ||
- Sono **riducibili** a molteplici sottoproblemi annidati | ||
- Possiedono sottoproblemi detti **sovrapponibili**, ovvero che sono riutilizzati tra i vari sottoproblemi | ||
|
||
La tecnica si basa quindi sul **salvare** il risultato dei sottoproblemi così da avere la soluzione a disposizione. | ||
|
||
Per esempio, dall'albero ricorsivo della funzione di Fibonacci per $f(5)$ | ||
```dot process | ||
digraph { | ||
node [shape=circle] | ||
edge [dir=none] | ||
5 [color="#6a4c93"] | ||
4 [color="#ff595e"] | ||
{ | ||
node [color="#8ac926"] | ||
31 [label="3"] | ||
32 [label="3"] | ||
} | ||
{ | ||
node [color="#1982c4"] | ||
21 [label="2"] | ||
22 [label="2"] | ||
23 [label="2"] | ||
} | ||
{ | ||
node [shape=none width=0 height=0] | ||
11 [label="1"] | ||
12 [label="1"] | ||
13 [label="1"] | ||
14 [label="1"] | ||
15 [label="1"] | ||
01 [label="0"] | ||
02 [label="0"] | ||
03 [label="0"] | ||
} | ||
5 -> 4, 31 | ||
4 -> 32, 21 | ||
31 -> 22, 11 | ||
32 -> 23, 12 | ||
21 -> 13, 01 | ||
22 -> 14, 02 | ||
23 -> 15, 03 | ||
{ | ||
node [shape=point width=0] | ||
_0 | ||
_1 | ||
_2 | ||
_3 | ||
_4 | ||
_5 | ||
} | ||
{ | ||
edge [style=invis] | ||
{ | ||
rank=same | ||
4 -> _0 -> 31 | ||
} | ||
{ | ||
rank=same | ||
32 -> _1 -> 21 | ||
21 -> 22 | ||
22 -> _2 -> 11 | ||
} | ||
{ | ||
rank=same | ||
23 -> _3 -> 12 | ||
12 -> 13 | ||
13 -> _4 -> 01 | ||
01 -> 14 | ||
14 -> _5 -> 02 | ||
} | ||
{ | ||
rank=same | ||
15 -> 03 | ||
} | ||
{ | ||
edge [weight=100] | ||
5 -> _0 | ||
4 -> _1 | ||
31 -> _2 | ||
32 -> _3 | ||
21 -> _4 | ||
22 -> _5 | ||
} | ||
} | ||
} | ||
``` | ||
si nota che conviene _salvare_ il risultato di $f(2)$ e $f(3)$ per calcolarlo **una sola volta**. | ||
|
||
Durante l'implementazione si può scegliere tra due **tecniche di costruzione** di algoritmi: | ||
- **Top-down**, attraverso la **memoization**: salva le soluzioni in una tabella durante la ricorsione | ||
- **Bottom-up**: ordina i sottoproblemi in base alla dimensione e li risolve dal più piccolo, salvandoli |