La presentazione è in caricamento. Aspetta per favore

La presentazione è in caricamento. Aspetta per favore

Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl.

Presentazioni simili


Presentazione sul tema: "Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl."— Transcript della presentazione:

1 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Algoritmi come soluzioni di problemi computazionali. INTRODUZIONE Esempio1: problema dellordinamento. Input: a 1,a 2,...,a n Output: a' 1,a' 2,...,a' n permutazione (riarrangiamento) di a 1,a 2,...,a n tale che a' 1 a' 2... a' n.

2 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Soluzione1: Algoritmo InsertionSort. InsertionSort (A) n lunghezza[A] for j 2 to n do inserisce A[j] nella sequenza ordinata A[1..j-1] x A[j] i j - 1 while i 1 and x < A[i] do A[i+1] A[i] i i – 1 A[i+1] x

3 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl InsertionSort (A) n lunghezza[A] for j 2 to n do inserisce A[j] nella sequenza ordinata A[1..j-1] x A[j] i j – 1 while i 1 and x < A[i] do A[i+1] A[i] i i – 1 A[i+1] x void InsertionSort (vector A) { int i,j,n = A.size(); tipo x; for (j = 1; j < n; j++) { // inserisce A[j] nella sequenza // ordinata A[0..j-1] x = A[j]; i = j – 1; while (i >= 0 && x < A[i]) { A[i+1] = A[i]; i--; } A[i+1] = x; }

4 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl x 5 8 4 7 1 3 6 5 x 8 4 7 1 3 622 5 x 4 7 1 3 682 5 8 x 7 1 3 64 2 x 5 8 7 1 3 6 2 5 x 4 7 1 3 6 2 4 5 8 x 1 3 67 2 4 5 x 8 1 3 6 5 2 8 4 7 1 3 6 2 5 8 4 7 1 3 6 2 4 5 8 7 1 3 6 InsertionSort (A) n lunghezza (A) for j 2 to n do inserisce A[j] nella sequenza ordinata A[1..j-1] x A[j] i j – 1 while i 1 and x < A[i] do A[i+1] A[i] i i – 1 A[i+1] x

5 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl 2 4 5 7 8 x 3 61 x 2 4 5 7 8 3 6 1 2 4 5 7 8 x 63 1 2 x 4 5 7 8 6 1 2 3 4 5 7 8 x6 1 2 3 4 5 x 7 8 1 2 3 4 5 6 7 8 2 4 5 7 8 1 3 6 1 2 4 5 7 8 3 6 1 2 3 4 5 7 8 6 InsertionSort (A) n lunghezza (A) for j 2 to n do inserisce A[j] nella sequenza ordinata A[1..j-1] x A[j] i j – 1 while i 1 and x < A[i] do A[i+1] A[i] i i – 1 A[i+1] x 2 4 5 x 8 1 3 6

6 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Analisi di InsertionSort: correttezza InsertionSort (A) n lunghezza [A] A contiene a 1,a 2,..., a n for j 2 to n do A[1..j-1] è un permutazione ordinata di a 1,..., a j-1 x A[j] i j - 1 A[1..i] è un permutazione ordinata di a 1,..., a j-1 x = a j e i = j-1 Correttezza InsertionSort

7 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl A[1..i] è un permutazione ordinata di a 1,..., a j-1 x = a j e i = j-1 while i 1 and x < A[i] do A[1..i]A[i+2..j] è permutazione ordinata di a 1,..., a j-1, x = a j, 1 i < j e x < A[i] A[i+1] A[i] A[1..i-1]A[i+1..j] è permutazione ordinata di a 1,..., a j-1, x = a j, 1 i < j e x < A[i+1] i i – 1 A[1..i]A[i+2..j] è permutazione ordinata di a 1,..., a j-1, x = a j, 0 i < j e x < A[i+2] A[1..i]A[i+2..j] è permutazione ordinata di a 1,..., a j-1, x = a j, i < j e o i = 0 oppure A[i] x

8 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl A[1..i]A[i+2..j] è permutazione ordinata di a 1,..., a j-1, x = a j, i < j e o i = 0 oppure A[i] x A[1..i]·x·A[i+2..j] è permutazione ordinata di a 1,..., a j A[i+1] x A[1..j] è permutazione ordinata di a 1,..., a j A[1..j] è permutazione ordinata di a 1,..., a j e j = n A[1..n] è permutazione ordinata di a 1,..., a n

9 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Analisi di InsertionSort: complessità InsertionSort (A) n lunghezza(A) for j 2 to n do x A[j] i j - 1 while i 1 and x < A[i] do A[i+1] A[i] i i – 1 A[i+1] x

10 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Analisi di InsertionSort: complessità InsertionSort (A) n lunghezza(A) for j 2 to n do x A[j] i j - 1 while i 1 and x < A[i] do A[i+1] A[i] i i – 1 A[i+1] x

11 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl caso migliore:

12 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl caso peggiore:

13 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl caso medio:

14 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Soluzione2: Algoritmo MergeSort. MergeSort (A,p,r) if p < r then q (p+r)/2 MergeSort (A,p,q) MergeSort (A,q+1,r) Merge(A,p,q,r)

15 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Merge (A,p,q,r) n 1 q – p + 1 n 2 r – q for i 1 to n 1 do L[i] A[p + i – 1] for j 1 to n 2 do R[j] A[q + j] L[n 1 + 1] R[n 2 + 1]

16 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl i j 1 for k p to r do if L[i] R[j] then A[k] L[i] i i + 1 else A[k] R[j] j j + 1

17 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl 5 2 8 0 4 7 1 9 3 2 6 0 47255 285 2 80 4 75 2 8 0 4 71 9 3 2 62 61 9 31 93629140 5 2 8 0 4 7 1 9 3 2 6 5 2 8 0 4 71 9 3 2 6 5 2 80 4 72 61 9 3 5 28 25 0 471 93 4091 62 0 1 2 2 3 4 5 6 7 8 9 0 2 4 5 7 8 2 5 8 2 5 52 8 1 2 3 6 9 0 4 72 61 3 9 0 471 93 4091 62

18 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl MergeSort (A,p,r) Analisi: correttezza A[p..r] contiene una sequenza a p,...,a r di n = r – p + 1 elementi if p < r then altrimenti n 1 e a p,...,a r è già ordinata q (p+r)/2 n 1 = q – p + 1 < n ed n 2 = r – q < n MergeSort (A,p,q) MergeSort (A,q+1,r) A[p..q] è una permutazione ordinata di a p,...,a q A[q+1..r] è una permutazione ordinata di a q+1,...,a r Merge(A,p,q,r) A[p..r] è una permutazione ordinata di a p,...,a r

19 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Merge (A,p,q,r) Analisi: correttezza A[p..q] contiene a p,...,a q ordinata A[q+1..r] contiene a q+1,...,a r ordinata n 1 q – p + 1 n 2 r – q for i 1 to n 1 do L[i] A[p + i – 1] for j 1 to n 2 do R[j] A[q + j] L[1..n 1 ] contiene a p,...,a q R[1.. n 2 ] contiene a q+1,...,a r L[n 1 +1] R[n 2 +1] L[1..n 1 +1] contiene a p,...,a q, ed R[1.. n 2 +1] contiene a q+1,...,a r,

20 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl i j 1 for k p to r do i n 1 +1, j n 2 +1, k = p+i+j-2 A[p..k-1] è permutazione ordinata di L[1..i-1] R[1..j-1] ed A[p..k-1] min(L[i],R[j]) if L[i] R[j] then i n 1 A[k] L[i] i i + 1 else j n 2 A[k] R[j] j j + 1 i = n 1 +1, j = n 2 +1, k = r+1 A[p..r] è una permutazione ordinata di L[1..n 1 ] R[1.. n 2 ] A[p..r] è una permutazione ordinata di a p,...,a r

21 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Analisi: complessità Merge (A,p,q,r) n 1 q – p + 1 n 2 r – q for i 1 to n 1 do L[i] A[p + i – 1] for j 1 to n 2 do R[j] A[q + j] L[n 1 +1] R[n 2 +1]

22 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl i j 1 for k p to r do if L[i] R[j] then A[k] L[i] i i + 1 else A[k] R[j] j j + 1

23 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Analisi: complessità MergeSort (A,p,r) if p < r then q (p+r)/2 MergeSort (A,p,q) MergeSort (A,q+1,r) Merge(A,p,q,r)

24 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl

25 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Analisi: complessità MergeSort (A,p,r) if p < r then q (p+r)/2 MergeSort (A,p,q) MergeSort (A,q+1,r) Merge(A,p,q,r)

26 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl

27 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl dunque esiste N tale che per ogni n > N. Quindi, qualunque siano i valori delle costanti a, b, c, a', b' e c' lalgoritmo MergeSort è superiore a InsertSort per array di dimensione sufficientemente grande.

28 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Possiamo dire che cresce come n 2 mentre cresce come n log 2 n. nn2n2 n log 2 n IS n 2 ns MS n log 2 n ns 1010033 0.1 s0.033 s 10010000664 10 s0.664 s 100010 6 99651ms 10 s 1000010 8 1328770.1s 133 s 10 6 10 12 2·10 7 17m20ms 10 9 10 18 3·10 10 70anni30s

29 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl dunque esiste N tale che per ogni n > N. Quindi, qualunque siano i valori delle costanti a, b, c, a', b' e c' lalgoritmo InsertSort è superiore a MergeSort per array (quasi) ordinati e sufficientemente grandi.

30 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl InsertSort è pure superiore a MergeSort per array di piccola dimensione in quanto le costanti a, b, c in sono generalmente molto maggiori delle costanti a', b' e c' in Questo suggerisce una modifica di MergeSort in cui per ordinare porzioni di array di dimensione minore di una certa costante k si usa InsertSort invece di usare ricorsivamente MergeSort.

31 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Soluzione3: Algoritmo MergeInsSort. MergeInsSort MergeInsSort (A,p,r) if p < r then if r-p+1 < 32 then InsertSort(A,p,r) else q (p+r)/2 MergeInsSort (A,p,q) MergeInsSort (A,q+1,r) Merge(A,p,q,r)

32 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl E possibile ridurre le costanti di MergeSort utilizzando una versione bottom-up iterativa invece della versione top-down ricorsiva. La versione bottom-up iterativa è la seguente: Allinizio larray è suddiviso in n segmenti di 2 0 = 1 elementi che sono ovviamente ordinati. Ripete quindi la seguente operazione: se larray è suddiviso in segmenti ordinati di 2 k elementi ciascuno usa Merge per riunire a due a due tali segmenti ottenendo un array suddiviso in segmenti ordinati di 2 k+1 elementi ciascuno. Quando 2 k n larray è ordinato.

33 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Soluzione4: Algoritmo MergeSortBin. MergeSortBin (A) n length(A), b 1 while b < n do A è ordinato a blocchi lunghi b MergeBin(A,B,b) b 2·b B è ordinato a blocchi lunghi b MergeBin(B,A,b) b 2·b A è ordinato

34 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl MergeBin (A,B,b) n length(A), k 0 while k < n do A[k+1..n] è ordinato a blocchi lunghi b B[1..k] è ordinato a blocchi lunghi 2·b i k p min(i+b,n) j p q min(j+b,n) while i+1 p and j+1 q do k k + 1 if A[i+1] A[j+1] then i i + 1 B[k] A[i] else j j + 1 B[k] A[j] while i+1 p do k k + 1 i i + 1 B[k] A[i] while j+1 q do k k + 1 j j + 1 B[k] A[j]

35 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl 52804719326138570b = 1 2 50 84 71 92 31 65 73 80b = 20 1 2 4 5 7 8 91 2 3 3 5 6 7 80b = 80 1 1 2 2 3 3 4 5 5 6 7 7 8 8 90b = 160 0 1 1 2 2 3 3 4 5 5 6 7 7 8 8 9b = 320 0 1 1 2 2 3 3 4 5 5 6 7 7 8 8 9b = 640 2 5 81 4 7 91 2 3 63 5 7 80b = 4

36 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Algoritmo MergeSortBin su file MergeSortBinFile (A) A file b 1 do A è ordinato a blocchi lunghi b Distribuisci(A, B, C, b) nb Riunisci(B, C, A, b) A è ordinato a blocchi lunghi 2·b b 2·b A è ordinato a blocchi lunghi b e contiene nb blocchi while nb > 1 MergeSort binario

37 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Distribuisci (A, B, C, b) rewind(A) while not eof(A) do i 1 while i b and not eof(A) do read(A, x), write(B, x) i i +1 i 1 while i b and not eof(A) do read(A, y), write(C, y) i i +1

38 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl Riunisci (B,C,A,b) rewind(B), fB eof(B), if not fB then read(B, x) rewind(C), fC eof(C), if not fC then read(C, y) nb 0 while not fC do i 1, j 1, nb nb+1 while i b and not fB and j b and not fC do if x y then write(A, x), fB eof(B), if not fB then read(B, x) else write(A, y), fC eof(C), if not fC then read(C, y)

39 Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl while i b and not fB do write(A, x), fB eof(B), if not fB then read(B, x) while j b and not fC do write(A, y), fC eof(C), if not fC then read(C, y) if not fB then nb nb +1 while not fB do write(A, x), fB eof(B), if not fB then read(B, x) return nb


Scaricare ppt "Introduzione agli algoritmi e strutture dati 3/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2010 – The McGraw-Hill Companies srl."

Presentazioni simili


Annunci Google