fundamental algorithms
play

Fundamental Algorithms Chapter 2b: Recurrences Dirk Pfl uger - PowerPoint PPT Presentation

Technische Universit at M unchen Fundamental Algorithms Chapter 2b: Recurrences Dirk Pfl uger Winter 2010/11 D. Pfl uger: Fundamental Algorithms Chapter 2b: Recurrences, Winter 2010/11 1 Technische Universit at M unchen


  1. Technische Universit¨ at M¨ unchen Fundamental Algorithms Chapter 2b: Recurrences Dirk Pfl¨ uger Winter 2010/11 D. Pfl¨ uger: Fundamental Algorithms Chapter 2b: Recurrences, Winter 2010/11 1

  2. Technische Universit¨ at M¨ unchen Recurrences Definition A recurrence is an (in-)equality that defines (or characterizes) a function in terms of its values on smaller arguments. Examples: • Time complexity of MergeSort: � c 1 for n ≤ 1 T MS ( n ) = � n � 2 T MS + c 2 n for n ≥ 2 2 • or, given in Θ -notation: � Θ( n ) for n ≤ 1 T MS ( n ) = � n � 2 T MS + Θ( n ) for n ≥ 2 2 • inequality for the BFPRT algorithm (see later. . . ): � Θ( n ) for n ≤ C T BF ( n ) ≤ �� n � 7 �� � T BF + T BF 10 n + 6 + O ( n ) for n > C 5 D. Pfl¨ uger: Fundamental Algorithms Chapter 2b: Recurrences, Winter 2010/11 2

  3. Technische Universit¨ at M¨ unchen The Substitution Method step 1: guess the type of the solution step 2: find the respective parameters, and prove that the resulting function satisfies the recurrence Example: (MergeSort recurrence) � c 1 for n ≤ 1 T MS ( n ) = � n � 2 T MS + c 2 n for n ≥ 2 2 1. guess solution: T ( n ) = an log 2 n + bn 2. determine the correct values for the parameters a and b D. Pfl¨ uger: Fundamental Algorithms Chapter 2b: Recurrences, Winter 2010/11 3

  4. Technische Universit¨ at M¨ unchen Solving the MergeSort Recurrence via Substitution For n ≤ 1 : T MS ( n ) = c 1 ! • T MS ( 1 ) = a · 1 · log 2 ( 1 ) + b · 1 = c 1 • can only be true, if b := c 1 � n � For n > 1 : T MS ( n ) = 2 T MS + c 2 n 2 • insert T MS ( n ) = an log 2 n + c 1 n into equation: � n � a n � + c 1 n � an log 2 n + c 1 n = + c 2 n 2 2 log 2 2 2 an log 2 n + c 1 n = an ( log 2 n − 1 ) + c 1 n + c 2 n ⇔ 0 = − an + c 2 n ⇔ a = c 2 ⇔ • therefore: T MS ( n ) = c 2 n log 2 n + c 1 n D. Pfl¨ uger: Fundamental Algorithms Chapter 2b: Recurrences, Winter 2010/11 4

  5. Technische Universit¨ at M¨ unchen The Recursion-Tree Method (or Iteration Method) General Steps: 1. draw a tree of all recursive function calls 2. state the local costs for each node (function call) of the tree 3. sum up the costs of all nodes on each level of the tree Possible Results: • a sum of costs-per-level that can be added up easily • an easier recurrence for the costs-per-level • a good guess for the substitution method Example: → MergeSort recurrence D. Pfl¨ uger: Fundamental Algorithms Chapter 2b: Recurrences, Winter 2010/11 5

  6. Technische Universit¨ at M¨ unchen The Master Theorem Prerequisites: • constants a , b ≤ 1 ( a , b ∈ R ); a function f ( n ) • recurrence given by � n � T ( n ) = aT + f ( n ) , T ( 1 ) ∈ Θ( 1 ) b Then, T ( n ) can be bounded asymptotically as follows: n log b a − ǫ � n log b a � � � 1. if f ( n ) ∈ O for some ǫ > 0, then T ( n ) ∈ Θ n log b a log n n log b a � � � � 2. if f ( n ) ∈ Θ , then T ( n ) ∈ Θ � n log b a + ǫ � 3. if f ( n ) ∈ Ω for some ǫ > 0, and � n � if af ≤ cf ( n ) for some constant c < 1 and all n > n 0 , b then T ( n ) ∈ Θ ( f ( n )) Proof : see textbook (Cormen et al.) D. Pfl¨ uger: Fundamental Algorithms Chapter 2b: Recurrences, Winter 2010/11 6

  7. Technische Universit¨ at M¨ unchen The Master Theorem – Remarks Interpreting the Master Theorem: � n � , a solution is T 0 ( n ) = n log b a • for f ( n ) = 0, i.e., T ( n ) = aT b = an log b a � n � n � log b a � = n log b a = T 0 ( n ) aT 0 = a b b a • The master theorem compares the non-recursive part of the costs, f ( n ) , with this solution T 0 ( n ) • case 1: f ( n ) ∈ O ( T 0 ( n ) · n − ǫ ) , � n log b a � ⇒ costs of recursion dominate, and T ( n ) ∈ Θ • case 2: f ( n ) ∈ Θ ( T 0 ( n )) , n log b a log n � � ⇒ costs are balanced, and T ( n ) ∈ Θ • case 3: f ( n ) ∈ Ω ( T 0 ( n ) · n ǫ ) , ⇒ costs f ( n ) dominate, and T ( n ) ∈ Θ ( f ( n )) D. Pfl¨ uger: Fundamental Algorithms Chapter 2b: Recurrences, Winter 2010/11 7

  8. Technische Universit¨ at M¨ unchen The Master Theorem – Remarks (2) Floor and Ceil: � n � n � n � the fraction n � � • if in aT b occurs as or , the theorem still b b b holds �� n �� n �� �� • situations as in T + T (compare MergeSort 2 2 � n � recurrence) are also covered ⇒ 2 T 2 ⇒ the master theorem will cover many (but not all) divide-and-conquer recurrences Technicalities of the Proof: • case 1: f ( n ) ∈ O ( T 0 ( n )) is not sufficient: f ( n ) needs to be polynomially smaller than T 0 ( n ) • case 3: f ( n ) ∈ Ω ( T 0 ( n )) is not sufficient: f ( n ) needs to be polynomially larger than T 0 ( n ) D. Pfl¨ uger: Fundamental Algorithms Chapter 2b: Recurrences, Winter 2010/11 8

  9. Technische Universit¨ at M¨ unchen The Master Theorem – Examples MergeSort: T ( n ) = 2 T ( n 2 ) + f ( n ) , where f ( n ) ∈ Θ( n ) • a = 2 and b = 2, therefore T 0 ( n ) = n log 2 2 = n • case 2 applies: f ( n ) ∈ Θ( n ) , therefore T ( n ) ∈ Θ( n log ( n )) Expensive Merge: T ( n ) = 2 T ( n 2 ) + f ( n ) , but f ( n ) ∈ Θ( n 2 ) • again a = 2 and b = 2, thus T 0 ( n ) = n log 2 2 = n • now f ( n ) ∈ Ω( n 1 + ǫ ) for any 0 < ǫ < 1 • therefore case 3 applies: T ( n ) ∈ Θ( f ( n )) = Θ( n 2 ) Odd-Even MergeSort: T ( n ) = 2 T ( n 2 ) + f ( n ) , with f ( n ) ∈ Θ( n log n ) • still a = 2 and b = 2, thus T 0 ( n ) = n log 2 2 = n • now, f ( n ) ∈ Ω( n ) , but f ( n ) �∈ Ω( n 1 + ǫ ) for any ǫ > 0 • thus, the Master theorem does not apply D. Pfl¨ uger: Fundamental Algorithms Chapter 2b: Recurrences, Winter 2010/11 9

Recommend


More recommend