algorithm control structures analysis of algorithms
play

Algorithm Control Structures Analysis of Algorithms Sequencing - PowerPoint PPT Presentation

Algorithm Control Structures Analysis of Algorithms Sequencing If-Then-Else For loop Algorithm Control Structures While loop Recursive calls http://www.cp.eng.chula.ac.th/faculty/spj Sequencing Sequencing


  1. Algorithm Control Structures Analysis of Algorithms � Sequencing � If-Then-Else � �For� loop Algorithm Control Structures � �While� loop � Recursive calls http://www.cp.eng.chula.ac.th/faculty/spj Sequencing Sequencing Θ ( n ) block #1 t 1 block #1 Θ ( n 2 ) t 1 + t 2 = max( t 1 , t 2 ) Θ ( n 2 ) block #2 block #2 t 2 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  2. Sequencing Sequencing Θ ( n ) Ο ( n ) block #1 block #1 Ο ( n 2 ) Θ ( n 2 ) Ο ( n 2 ) Θ ( n 2 ) block #2 block #2 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Sequencing If-Then-Else Ο ( n 2 ) block #1 Θ ( n 2 ) t 2 max( t 1 , t 2 ) Θ ( n 2 ) block #1 t 1 block #2 block #2 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  3. �For� Loop �For� Loop m m   = Θ   for ( i = 1 to m ) for ( i = 1 to m ) ∑ Θ ( 1 ) ∑ 1 m   { s += A[i][j] � t i 1 i 1 = = i P(i) i 1  = Θ ( m ) } ��� P(i) ������� t i http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj �For� Loop �For� Loop m m m m i m ∑∑ Θ ( 1 ) = ∑ Θ ( ) ∑∑ Θ ( 1 ) = ∑ Θ ( i ) m for ( i = 1 to m ) for ( i = 1 to m ) i 1 j 1 i 1 i 1 j 1 i 1 for ( j = 1 to m ) = = = for ( j = 1 to i ) = = = m m   s += A[i][j] s += A[i][j] = Θ   ∑ ∑ O ( m ) m =   1 i i 1 = = 2 m   ( ) = Θ m =   O ∑ m   i 1 = 2 O ( m ) = http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  4. �For� Loop �For� Loop m i m 1 m i m 1 − − ∑∑ Θ ( 1 ) = ∑ Θ ( i ) Θ ( 1 ) ∑∑ = ∑ Θ ( i ) for ( i = 1 to m ) for ( i = 2 to m-1 ) i 1 j 1 i 1 for ( j = 1 to i ) = = = for ( j = 3 to i ) 2 3 i j i 2 = = = m   m 1 − s += A[i][j] s += A[i][j]   = Θ   ∑ i = Θ   ∑ i     i 1 = i 2 = 2 +  ( m 1 )    m m = Θ   = Θ  + Θ  ( ) m    2  2   2 2 = Θ ( ) m = Θ ( ) m http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj �While� Loop �While� Loop: Insertion Sort while ( n > 0 ) { Insertion_Sort( A[1..n] ) … for j = 2 to n n = n - 1 { } key = A[j] Θ ( n ) i = j-1 while ( n > 0 ) { while i>0 and A[i]>key … { n = n / 2 i = 0; j = n Ο ( i ) Θ ( i ) A[i+1] = A[i] } while ( i < j ) { i = i-1 Θ (log n ) … } i++; j--; A[i+1] = key } } Θ ( n ) http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  5. �While� Loop: Insertion Sort �While� Loop: Insertion Sort Insertion_Sort( A[1..n] ) Insertion_Sort( A[1..n] ) for j = 2 to n for j = 2 to n { { key = A[j] key = A[j] i = j-1 i = j-1 while i>0 and A[i]>key while i>0 and A[i]>key { { n Ο ( n 2 ) Ο ( j ) Ο ( i ) Σ Ο ( j ) Ο ( j ) A[i+1] = A[i] A[i+1] = A[i] i = i-1 i = i-1 j= 2 } } A[i+1] = key A[i+1] = key } } http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj �While� Loop: Euclid�s GCD �While� Loop: Euclid�s GCD ��� n ≥ k ������������ n mod k < n / 2 ���� GCD( k, n ) GCD( k, n ) { { t k n 1: ��� k ≤ n / 2 while k > 0 while k > 0 88 128 { { ≤ n / 2 n mod k < k 88 40 88 t = k t = a 40 8 40 k = n mod k k = n mod k 2: ��� k > n / 2 8 0 8 n = t n = t n / k < 2 , int( n / k ) = 1 } } return n return n n mod k = n - k * int( n / k ) } } Ο ( log n ) = n - k < n - n / 2 = n / 2 http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  6. �While� Loop: FindMin_BST Recursive Calls Position FindMin_BST( TREE T ) waste( n ) { { Position p; Θ (1 ) if ( n = 0 ) return 0 for ( i = 1 to n ) p = T; Θ ( n 2 ) for (j = 1 to i ) if ( p != NULL ) { print i,j,n while ( p->left != NULL ) { p = p->left; for( i = 1 to 3 ) } 3 T ( n / 2 ) waste( n/2 ) } } return p; } Ο ( h ) Ο ( n ) T ( n ) = 3 T ( n / 2 ) + Θ ( n 2 ) http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj �Guessing�+ Induction Theorem T ( n ) = T (0.7 n ) + T (0.2 n ) + O( n ) k ∑ 1 α < If then the solution to the recurrence i Guess: T ( n ) = O( n ), T ( n ) ≤ cn k i 1 = = ∑ T ( n ) T ( n ) O ( n ) α + i Proof: i 1 = is T ( n ) = O( n ) Basis: obvious Induction: T ( n ) ≤ 0.7 cn + 0.2 cn + O( n ) T ( n ) = T (0.3 n )+ T (0.2 n )+ T (0.09 n )+ T (0.4 n ) + O( n ) = 0.9 cn + O( n ) ≤ 0.9 cn + dn = O( n ) = cn ( choose d = 0.1 c ) http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  7. Recursion Trees Recursion Trees n 2 n 2 n n ( n / 2) 2 ( n / 2) 2 ( n / 2) 2 (3/4) n 2 n / 3 2 n / 3 n log 1.5 n lg n (9/16) n 2 ( n / 4) 2 ( n / 4) 2 ( n / 4) 2 ( n / 4) 2 ( n / 4) 2 ( n / 4) 2 ( n / 4) 2 ( n / 4) 2 ( n / 4) 2 n / 9 2 n / 9 2 n / 9 4 n / 9 n ... ... T ( n ) = 3 T ( n / 2 ) + Θ ( n 2 ) Θ ( n 2 ) T ( n ) = T ( n / 3 ) + T (2 n /3 ) + n Θ ( n log n ) T ( n /2) = 3 T ( n /4 ) + Θ (( n /2) 2 ) http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Master Method Master Method � T ( n ) = aT ( n / b ) + f ( n ) a ≥ 1, b > 1 � T ( n ) = aT ( n / b ) + f ( n ) a ≥ 1, b ≥ 1 � Let c = log b a � Let c = log b a � If f ( n ) = Ο ( n c - ε ) for some constant ε > 0, then � If f ( n ) = Ω ( n c + ε ) for some constant ε > 0 T ( n ) = Θ ( n c ) � and if a f ( n /b) ≤ d f ( n ) for some constant d < 1 � � and all sufficiently large n , then � If f ( n ) = Θ ( n c ) for some constant ε > 0, then T ( n ) = Θ ( f ( n ) ) � T ( n ) = Θ ( n c log n ) � http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  8. Master Method Master Method : Example � T ( n ) = aT ( n / b ) + f ( n ) a ≥ 1, b > 1 � T ( n ) = 9 T ( n / 3 ) + n � a = 9, b = 3, c = log b a = 2, n c = n 2 � Let c = log b a � f ( n ) = n = Ο ( n 2 - 0.1 ) � f ( n ) = Ο ( n c - ε ) T ( n ) = Θ ( n c ) → � T ( n ) = Θ ( n c ) = Θ ( n 2 ) � f ( n ) = Θ ( n c ) T ( n ) = Θ ( n c log n ) → � f ( n ) = Ω ( n c + ε ) T ( n ) = Θ ( f ( n ) ) → http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj Master Method : Example Master Method : Example � T ( n ) = T ( n / 3 ) + 1 � T ( n ) = 3 T ( n / 4 ) + n log n � a = 1, b = 3, c = log b a = 0, n c = 1 � a = 3, b = 4, c = log b a < 0.793, n c < n 0.793 � f ( n ) = 1 = Θ ( n c ) = Θ ( 1 ) � f ( n ) = n log n = Ω ( n 0.793 ) � T ( n ) = Θ ( n c log n ) = Θ ( log n ) � a f ( n / b ) = 3 (( n /4) log ( n /4) ) ≤ (3/4) n log n = d f ( n ) � T ( n ) = Θ ( f ( n ) ) = Θ ( n log n ) http://www.cp.eng.chula.ac.th/faculty/spj http://www.cp.eng.chula.ac.th/faculty/spj

  9. Conclusion � �������ก��������ก���� blocks ��� control structures � �������������� block � ������������������������ blocks ��� control structure � ������������������������� asymptotic http://www.cp.eng.chula.ac.th/faculty/spj

Recommend


More recommend