Appunti per il modulo di algoritmi e strutture dati - Sezione di ...
Appunti per il modulo di algoritmi e strutture dati - Sezione di ...
Appunti per il modulo di algoritmi e strutture dati - Sezione di ...
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
★<br />
✥<br />
✑ a : b ❜<br />
✑<br />
❜❜❜❜❜<br />
✑ ✧ ✦<br />
✑ < ><br />
★✑✑.<br />
✥<br />
★<br />
✥<br />
a : c<br />
✧✁<br />
❏✦<br />
✧✁<br />
❏✦<br />
✁<br />
★✁<br />
< > ❏<br />
✥★<br />
❏<br />
✁<br />
★✁<br />
<<br />
✥<br />
> ❏<br />
✥★<br />
❏ ✥<br />
b : c a : b<br />
✧ ✦✧<br />
✁ ❇ ✁ ❇<br />
✁ < > ❇❇<br />
✁<br />
✁ < ❇❇<br />
✁<br />
abc acb cab<br />
✦✧<br />
✦✧<br />
✦<br />
✁ ❇ ✁ ❇<br />
✁<<br />
> ❇❇ ✁ > ❇❇<br />
✁<br />
✁<br />
bac bca cba<br />
Figura 13: Albero <strong>di</strong> decisione <strong>di</strong> Selection-Sort<br />
Teorema 10.1<br />
Un albero binario <strong>di</strong> livello k ha al massimo 2 k foglie.<br />
Dim. L’albero che ha <strong>il</strong> massimo numero <strong>di</strong> foglie è quello b<strong>il</strong>anciato, che ha 2 k foglie <strong>per</strong> <strong>il</strong> teorema 6.1.<br />
Come conseguenza, ogni albero binario con s foglie ha livello maggiore o uguale log2 s. Quin<strong>di</strong> ogni<br />
albero binario con s foglie deve avere almeno un cammino <strong>di</strong> lunghezza maggiore o uguale a log2 s. Allora<br />
log2 s è un limite inferiore alla lunghezza massima dei cammini <strong>per</strong> tutti gli alberi con almeno s foglie.<br />
Si può anche <strong>di</strong>mostrare che log2 s è un limite inferiore alla lunghezza me<strong>di</strong>a dei cammini dalla ra<strong>di</strong>ce ad<br />
una foglia <strong>per</strong> tutti gli alberi con almeno s foglie.<br />
Quin<strong>di</strong>, dato un problema, se in<strong>di</strong>chiamo con s(n) <strong>il</strong> numero <strong>di</strong> soluzioni come funzione della <strong>di</strong>mensione<br />
dei <strong>dati</strong> n, abbiamo che <strong>il</strong> numero massimo e <strong>il</strong> numero me<strong>di</strong>o <strong>di</strong> decisioni sono limitati inferiormente<br />
da log2 s(n).<br />
Ut<strong>il</strong>izziamo questo risultato <strong>per</strong> trovare un limite inferiore al tempo degli <strong>algoritmi</strong> <strong>di</strong> or<strong>di</strong>namento<br />
<strong>di</strong> n elementi basati su confronti. In questo caso le possib<strong>il</strong>i <strong>di</strong>sposizioni degli elementi sono s(n) = n!.<br />
Quin<strong>di</strong> ogni albero <strong>di</strong> decisione deve avere almeno n! foglie e ogni albero deve avere almeno un cammino<br />
<strong>di</strong> lunghezza log(n!). Si può <strong>di</strong>mostrare che n! è quasi uguale a ( n<br />
e )n . Abbiamo log(n!) = log(( n<br />
e )n ) =<br />
n log( n)<br />
= n log n − n log e. Quin<strong>di</strong> la complessità dell’or<strong>di</strong>namento è limitata inferiormente da n log n e<br />
e<br />
cioè è Ω(n log n). Di conseguenza gli <strong>algoritmi</strong> mergesort, quicksort e heapsort sono ottimi.<br />
Riba<strong>di</strong>amo che <strong>il</strong> ragionamento con gli alberi <strong>di</strong> decisione riguarda soltanto i casi in cui gli <strong>algoritmi</strong><br />
sono basati su confronti e non abbiamo nessuna informazione sugli elementi da trattare. Per esempio,<br />
l’or<strong>di</strong>namento <strong>di</strong> n elementi che comprendono soltanto valori compresi tra 1 e n è <strong>di</strong> or<strong>di</strong>ne lineare (ve<strong>di</strong><br />
prossima sezione).<br />
È importante anche notare che non è detto che sia possib<strong>il</strong>e trovare <strong>per</strong> ogni problema un algoritmo con<br />
tempo uguale al limite inferiore trovato con gli alberi <strong>di</strong> decisione: <strong>per</strong> esempio, <strong>il</strong> problema <strong>di</strong> cercare<br />
un elemento in un insieme <strong>di</strong> n elementi è Ω(log n). Infatti le possib<strong>il</strong>i soluzioni sono che l’elemento<br />
cercato sia <strong>il</strong> primo, <strong>il</strong> secondo, ecc. e quin<strong>di</strong> sono n. Tuttavia anche l’algoritmo migliore, se non abbiamo<br />
informazioni sugli elementi, fa necessariamente almeno n confronti. Se invece gli elementi sono or<strong>di</strong>nati,<br />
è possib<strong>il</strong>e, come vedremo, raggiungere <strong>il</strong> limite inferiore.<br />
Gli alberi binari b<strong>il</strong>anciati corrispondono ad <strong>algoritmi</strong> ottimi sia <strong>per</strong> <strong>il</strong> caso peggiore che <strong>per</strong> <strong>il</strong> caso<br />
me<strong>di</strong>o. Se infatti consideriamo un albero binario <strong>di</strong> decisione quasi b<strong>il</strong>anciato B con s foglie, abbiamo<br />
che B ha livello log2 s e quin<strong>di</strong> la lunghezza me<strong>di</strong>a dei cammini dalla ra<strong>di</strong>ce ad una foglia è log2 s. Quin<strong>di</strong><br />
46