ΠΛΗ30 ΜΑΘΗΜΑ 2.1

download ΠΛΗ30 ΜΑΘΗΜΑ 2.1

of 29

  • date post

    18-Aug-2015
  • Category

    Education

  • view

    106
  • download

    1

Embed Size (px)

Transcript of ΠΛΗ30 ΜΑΘΗΜΑ 2.1

  1. 1. 30 2: 2.1:
  2. 2. . 1. 1. MergeSort( ) 2. QuickSort ( ) 3. QuickSelect ( ) 4. Strassen / . 2 , 30, 2.1: . 1.
  3. 3. . : (-) 3 , 30, 2.1: (QuickSort) k- Strassen
  4. 4. B. 4 , 30, 2.1: 2 , : ( 2.1) ( 2.2) ( 2.3) .
  5. 5. B. 1. 5 , 30, 2.1: : 1. : . 2. : ( ) 3. : , .
  6. 6. . 1. 1. MergeSort 6 , 30, 2.1: MergeSort ( ) : 1. : : ],...,[ 1 naaA = ],...,[ 2/11 naaA = ],...,[ 12/2 nn aaA += : 2. : MergeSort. 3. : Merge ( 1.4) 2/11 n 12/2 nn + )log(...)( 2 2)( nnn n TnT ==+ =
  7. 7. . 1. 2. QuickSort 7 , 30, 2.1: QuickSort ( ) : 1. : : 1 1 ],...,[ 1 naaA = 1 1 2 1 2. : ( 1 2) QuickSort. 3. : A = [A1]1[A2]
  8. 8. . 1. 2. QuickSort (1.) 8 , 30, 2.1: QuickSort: procedure QuickSort(A,start,finish) if startodigo) B[j]=A[k]; j=j-1 else B[i]=A[k]; i=i+1 end for B[i]=odigo; = return pos; end procedure
  9. 9. . 1. 2. QuickSort (2. ) 9 , 30, 2.1: Partition: B.1 B.2 1 2 3 4 5 6 7 8 9 7 4 11 18 20 6 1 1 2 3 4 5 6 7 8 7k i j 1 2 3 4 5 6 7 8 9 7 4 11 18 20 6 1 1 2 3 4 5 6 7 8 7 4k i j 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 B.3 B.4 B.5 B.6 B.7 1 2 3 4 5 6 7 8 9 7 4 11 18 20 6 1 1 2 3 4 5 6 7 8 7 4 11k i j 1 2 3 4 5 6 7 8 9 7 4 11 18 20 6 1 1 2 3 4 5 6 7 8 7 4 18 11k i j 1 2 3 4 5 6 7 8 9 7 4 11 18 20 6 1 1 2 3 4 5 6 7 8 7 4 20 18 11k i j 1 2 3 4 5 6 7 8 9 7 4 11 18 20 6 1 1 2 3 4 5 6 7 8 7 4 6 20 18 11k i j 1 2 3 4 5 6 7 8 9 7 4 11 18 20 6 1 1 2 3 4 5 6 7 8 7 4 6 1 20 18 11k i j 1 2 3 4 5 6 7 8 7 4 6 1 9 20 18 11
  10. 10. QuickSort (2. ) 10 , 30, 2.1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 3 10 13 7 4 11 9 6 1 14 5 2 3 10 13 18 19 22 20 1 2 3 4 5 6 7 8 9 10 11 12 7 4 11 9 6 1 14 5 2 3 10 13 4 6 1 5 2 3 7 13 10 14 9 11 14 15 16 19 22 20 19 20 22 1 2 3 4 5 6 4 6 1 5 2 3 1 2 3 4 5 6 8 9 10 11 12 13 10 14 9 11 10 9 11 13 14 15 16 20 22 20 221 2 3 4 5 6 10 9 11 13 14 20 22 16 22 22 1 2 3 1 2 3 1 3 2 5 6 5 6 5 6 8 9 10 10 9 11 9 10 11 12 14 14 6 6 6 2 3 3 2 2 3 2 2 2 8 9 9 10 11 11 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 9 10 11 13 14 18 19 20 22
  11. 11. . 1. 2. QuickSort (3. ) 11 , 30, 2.1: Partition (n) : . : )log(...)(2)( . nnn n TnT ==+ = : . : ? )log(...)( 2 2)( nnn n TnT ==+ = ( ) )(...)(1)( 2 . nnnTnT ==+=
  12. 12. . 1. 2. QuickSort (3. ) 12 , 30, 2.1: : : [ ] [ ] [ ]nTnTnnTnnTT nT )()0()1(...)()2()1()()1()0( )( +++++++++ = : T(n)=(nlogn) [ ] [ ] [ ] n nT )( = [ ] n nnnTTT nT )()(...)1()0(2 )( ++++ = [ ] )( )(...)1()0(2 )( n n nTTT nT + +++ =
  13. 13. . 1. 3. QuickSelect 13 , 30, 2.1: : n . k- : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 3 10 13 5- 5 10- 11 : : ( MergeSort) k- . : (nlogn) QuickSelect: : /: O(n)
  14. 14. . 1. 3. QuickSelect () 14 , 30, 2.1: o partition. k. Partition [A1] apos [A2] k=pos apos. kpos 2 (k-n1-1)- ( n1 1)
  15. 15. . 1. 3. QuickSelect (1.) 15 , 30, 2.1: : procedure QuickSelect(A,start,finish,k) if start>finish then return 0 elseelse pos=Partition(A,start,finish) if k=pos then return A[pos] else if kpos then return QuickSelect(A,pos+1,finish,k-pos) end if end if end procedure
  16. 16. 3. QuickSelect(2. k=12) 16 , 30, 2.1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 3 10 13 7 4 11 9 6 1 14 5 2 3 10 13 18 19 22 20 1 2 3 4 5 6 7 8 9 10 11 12 7 4 11 9 6 1 14 5 2 3 10 13 4 6 1 5 2 3 7 13 10 14 9 11 8 9 10 11 12 13 10 14 9 11 10 9 11 13 1410 9 11 13 14 12 14 14
  17. 17. . 1. 3. QuickSelect (3. ) 17 , 30, 2.1: Partition (n) : . : )(...)()( . nn n TnT ==+ = : . : . )(...)( 2 )( nn n TnT ==+ = ( ) )(...)(1)( 2 . nnnTnT ==+=
  18. 18. . 1. 3. QuickSelect (4. ) 18 , 30, 2.1: : 5- 5- . . ( ) ( ) . ..: 5- 9 ( ) 16 13 1 2 3 4 5 18 7 4 11 9 6 7 8 9 10 20 6 1 22 19 11 12 13 14 15 14 5 2 3 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 3 10 13 1 2 3 4 9 19 5 13
  19. 19. . 1. 3. QuickSelect (4. ) 19 , 30, 2.1: 5 : n T(n) n/5 ( (1)). n/5 . )()1( 5 n n = n/5 . 5- : , 3n/10 ( ). QuickSelect 7n/10 . )(...)( 5 )( . nn n TnT ==+ =
  20. 20. . 1. 3. QuickSelect (4. ) 20 , 30, 2.1: : procedure QuickSelect(A,start,finish,k) if start=finish then return A[start] elseelse m 5-. swap(A[m],A[start]) pos=Partition(A,start,finish) if k=pos then return A[pos] else if kpos then return QuickSelect(A,pos+1,finish,k-pos) end if end if end procedure
  21. 21. . 1. 3. QuickSelect (4. ) 21 , 30, 2.1: : n: (n) (n) Partition (n) 7n/10, (7n/10) : , T(n)=O(n) )(...)( 10 7 )( . nn n TnT ==+ =
  22. 22. . 1. 4. Strassen 22 , 30, 2.1: : nxn . ++ ++ = = == 2222122121221121 2212121121121111 2221 1211 2221 1211 babababa babababa bb bb aa aa C BAC : 12. (n2). Strassen: : : (n2,81) . : (n2,38) ++ 222212212122112122212221 bababababbaa
  23. 23. . 1. 4. Strassen O : 23 , 30, 2.1: procedure MultMatrix(A,B) for (i=1 to n) for (j=1 to n) C[i][j]=0; : C[i][j]=0; for (k=1 to n) C[i][j]=C[i][j]+A[i][k]*B[k][j]; end for end for end for return C end procedure ( ) )(...)1()1()( 3 nnnnnT ==+=
  24. 24. . 1. 4. Strassen Strassen 70: ( n ) : 24 , 30, 2.1: = 1211 AA AA A = 1211 BB BB B n/2 x n/2 O Strassen C=AxB : 2221 AA = 2221 BB B = 2221 1211 CC CC C
  25. 25. . 1. 4. Strassen 1 : : 25 , 30, 2.1: 2222122122 2122112121 2212121112 2112111111 BABAC BABAC BABAC BABAC += += += += nxn / . : 8 / n/2 x n/2 : . : 4 nx2 x nx2 n, 8 n/2 (n2) : 2222122122 BABAC += )(...)( 2 8)( 32 nn n TnT remMasterTheo ==+ =
  26. 26. . 1. 4. Strassen 1 : : 26 , 30, 2.1: 2222122122 2122112121 2212121112 2112111111 BABAC BABAC BABAC BABAC += += += += nxn / . : 8 / n/2 x n/2 : . : 4 nx2 x nx2 n, 8 n/2 (n2) : 2222122122 BABAC += )(...)( 2 8)( 32 nn n TnT remMasterTheo ==+ =
  27. 27. . 1. 4. Strassen 2 (Strassen): : 27 , 30, 2.1: ))(( ))(( 21123 11112 1112221122211 BBAAM BAM BAM BBBAAAM = = = ++= 742121 652112 3211 MMMMC MMMMC MMMMC MMC +++= ++= +++= += )( )( ))(( 22221121126 111222215 BBBBAM BAAAAM BBAAM += += += nxn / . : 7 / n/2 x n/2 : . : 24 nx2 x nx2 n, 7 n/2 (n2) : (n2,37). / (n2) ))(( 122221114 BBAAM = )(...)( 2 7)( 81,22 nn n TnT remMasterTheo ==+ = 542122 MMMMC +++=)( 21122211227 BBBBAM +=
  28. 28. . 1 : I. A n , n. 28 , 30, 2.1: II. B n n-1 , , O(n). III. n n/3 (n2) . ?
  29. 29. . 2 : I. A n , 1/4 , ( n/4) ( n) 29 , 30, 2.1: ( n/4) ( n) n. II. B n n/2 , , (n3). III. n n/2 O(n2) . ?