1 / 23

Algorithms and Computations Complexity Lecture 4 Growth of Functions Instructor : Haya Sammaneh

Algorithms and Computations Complexity Lecture 4 Growth of Functions Instructor : Haya Sammaneh. Recurrences.

yael-hodge
Download Presentation

Algorithms and Computations Complexity Lecture 4 Growth of Functions Instructor : Haya Sammaneh

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithms and Computations ComplexityLecture 4Growth of FunctionsInstructor : Haya Sammaneh

  2. Recurrences 􀂄 Describes the running time for a function contains recursion. 􀂄 Three methods to find the recurrences: 􀂅Substitution method. 􀂅Recursion-tree method. 􀂅The master method which provides the bounds for recurrences of the form T (n) = aT (n/b) + f (n).

  3. The substitution method 􀂄 Entails two steps: 􀂅 Guess the form of the solution. 􀂅 Use mathematical induction to find the constants and show that the solution works. 􀂄 Can be used to establish either upper or lower bounds on a recurrence.

  4. Example on 􀂄 Suppose T(1)= O(1) 􀂄 We guess that the solution is T (n) = O(n lg n). 􀂄 Prove that T (n) ≤ c nlg n for an appropriate choice of the constant c > 0. 􀂅 T (n/2) ≤ c (n/2)lg (n/2) = cn lg(n/2) + n =cn lg n - cn lg 2 + n because lg(n/2)= lg(n)-lg(2) T(n) =cn lg n - cn + n because lg(2)=1 ≤ cn lg n. 􀂅 where the last step holds as long as c ≥ 1.

  5. Substitution Method : Example • Let’s find the running time of the merge sort (let’s assume that n=2b, for some b).

  6. Substitution Method (Ex. 2) • (let’s assume that n=2b, for some b).

  7. Substitution Method (Ex. 3) • Let’s find the running time of this algorithm for a 2n x2n board.

  8. Avoiding pitfalls مخاطرand Changingvariables

  9. The recursion-tree method 􀂄 A straightforward way to devise a good guess. 􀂅Each node represents the cost of a single sub-problem somewhere in the set of recursive function invocations. 􀂅We sum the costs within each level of the tree and then we sum all the per-level costs to determine the total cost of all levels of the recursion.

  10. Example • If n= 43 (size of input) • T(n)=3 T(n/4)+cn2 • T(43)= 3 T(43/4)+c(43)2 • = 3T(42)+c(43)2 • = 3[3T(41)+c(42)2 ] +c(43)2 • = 9 T(41)+3 c(42)2 +c(43)2 • =9[ 3 T(1)+ c(4)2 ]+ 3 c(42)2 +c(43)2 • = 27 T(1) + 9 c(4)2 + 3 c(42)2 + 1c(43)2 • level =i =3 level=i=2 level=i= 1 level=i=0 33 node each with cost = T(1)3i node each with cost = c (n/4i)2 • # of level in this tree = log4 n= log4(43) = 3 levels

  11. The master method The master method applies to recurrences of the form T(n) = aT(n/b) + f (n) , where a ≥ 1, b > 1, and fis asymptotically positive.

  12. Three common cases Compare f (n) with nlogba: 1.f (n) =O(n logba – c) for some constant c > 0. •f (n) grows polynomially slower than nlogba (by an ncfactor). Solution:T(n) = Θ(nlogba) . 2.f (n)=Θ(nlogbalgkn) for some constant k ≥ 0. • f (n) andnlogbagrow at similar rates. Solution:T(n) = Θ(nlogbalgk+1n) .

  13. Three common cases (cont.) Compare f (n) with nlogba: 3.f (n) = Ω(nlogba+c) for some constant c > 0. • f (n) grows polynomially faster thannlogba(by an nc factor), and f (n) satisfies the regularity conditionthat a f (n/b) ≤ c f (n) for some constant c < 1. Solution:T(n) = Θ( f (n)) .

  14. The master method

  15. Understanding the master theory

  16. Examples Ex.T(n) = 4T(n/2) + n a = 4, b = 2 ⇒ nlogba= n2; f (n) = n. CASE 1: f (n) = O(n2– c) for c = 1. ∴ T(n) = Θ(n2). Ex.T(n) = 4T(n/2) + n2 a = 4, b = 2 ⇒ nlogba= n2; f (n) = n2. CASE 2: f (n) = Θ(n2lg0n), that is, k = 0. ∴ T(n) = Θ(n2lgn).

  17. Examples Ex.T(n) = 4T(n/2) + n3 a = 4, b = 2 ⇒ nlogba= n2; f (n) = n3. CASE 3: f (n) = Ω(n2 + c) for c = 1 and 4(n/2) 3 ≤ cn3 (reg. cond.) for c = 1/2. ∴ T(n) = Θ(n3). [ a f (n/b) ≤ c f (n) for some constant c < 1] Ex.T(n) = 4T(n/2) + n2/lgn a = 4, b = 2 ⇒ nlogba= n2; f (n) = n2/lgn. Master method does not apply.

More Related