Master Theorem Carola Wenk Slides courtesy of Charles Leiserson - - PowerPoint PPT Presentation

master theorem
SMART_READER_LITE
LIVE PREVIEW

Master Theorem Carola Wenk Slides courtesy of Charles Leiserson - - PowerPoint PPT Presentation

CS 3343 Fall 2010 Master Theorem Carola Wenk Slides courtesy of Charles Leiserson with small Slides courtesy of Charles Leiserson with small changes by Carola Wenk 9/16/2010 CS 3343 Analysis of Algorithms 1 The divide-and-conquer


slide-1
SLIDE 1

CS 3343 – Fall 2010

Master Theorem

Carola Wenk Slides courtesy of Charles Leiserson with small

9/16/2010 CS 3343 Analysis of Algorithms 1

Slides courtesy of Charles Leiserson with small changes by Carola Wenk

slide-2
SLIDE 2

The divide-and-conquer design paradigm

  • 1. Divide the problem (instance) into

subproblems. p a subproblems, each of size n/b 2 Conquer the subproblems by solving them

  • 2. Conquer the subproblems by solving them

recursively. 3 C bi b bl l ti

  • 3. Combine subproblem solutions.

Runtime for divide and combine is f(n)

9/16/2010 CS 3343 Analysis of Algorithms 2

slide-3
SLIDE 3

Example: merge sort Example: merge sort

  • 1. Divide: Trivial.
  • 2. Conquer: Recursively sort a=2

subarrays of size n/2=n/b y

  • 3. Combine: Linear-time merge, runtime

f(n)∈O(n) f(n)∈O(n) T(n) = 2T(n/2) + O(n) k di idi # subproblems subproblem size work dividing and combining

9/16/2010 CS 3343 Analysis of Algorithms 3

T(n) = aT(n/b) + f(n)

slide-4
SLIDE 4

The master method The master method

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

9/16/2010 CS 3343 Analysis of Algorithms 4

slide-5
SLIDE 5

Master theorem (summary) Master theorem (summary)

T(n) = aT(n/b) + f(n) CASE 1: f(n) = O(nlogba – ε) ⇒ T(n) = Θ(nlogba) . ⇒ T(n) Θ(n ) . CASE 2: f(n) = Θ(nlogba logkn) T( ) Θ( l l

k+1 )

⇒ T(n) = Θ(nlogba logk+1n) . CASE 3: f(n) = Ω(nlogba + ε) and af(n/b) ≤ c f(n) f( ) ( ) f( ) f( ) for some constant c < 1. T( ) Θ(f( ))

9/16/2010 CS 3343 Analysis of Algorithms 5

⇒ T(n) = Θ(f(n)) .

slide-6
SLIDE 6

Three common cases Three common cases

Compare f(n) with nlogba: Compare f(n) with n :

  • 1. f(n) = O(nlogba – ε) for some constant ε > 0.
  • f( ) gro s pol nomiall slo er than

logba

  • f(n) grows polynomially slower than nlogba

(by an nε factor). S l ti T( ) Θ( logba) Solution: T(n) = Θ(nlogba) .

  • 2. f(n) = Θ(nlogba logkn) for some constant k ≥ 0.

f( ) ( g )

  • f(n) and nlogba grow at similar rates.

Solution: T(n) = Θ(nlogba logk+1n)

9/16/2010 CS 3343 Analysis of Algorithms 6

Solution: T(n) = Θ(nlogba logk+1n) .

slide-7
SLIDE 7

Three common cases (cont ) Three common cases (cont.)

Compare f(n) with nlogba: Compare f(n) with n :

  • 3. f(n) = Ω(nlogba + ε) for some constant ε > 0.

l

  • f(n) grows polynomially faster than nlogba (by

an nε factor), and f(n) satisfies the regularity condition that af(n/b) ≤ c f(n) for some constant c < 1. Solution: T(n) = Θ( f(n)) .

9/16/2010 CS 3343 Analysis of Algorithms 7

slide-8
SLIDE 8

Examples Examples

E T( ) 4T( /2) ( )

  • Ex. T(n) = 4T(n/2) + sqrt(n)

a = 4, b = 2 ⇒ nlogba = n2; f(n) = sqrt(n). CASE 1: f(n) = O(n2 – ε) for ε = 1 5 CASE 1: f(n) = O(n2

ε) for ε = 1.5.

∴ T(n) = Θ(n2).

  • Ex. T(n) = 4T(n/2) + n2

a = 4 b = 2 ⇒ nlogba = n2; f(n) = n2 a 4, b 2 ⇒ n n ; f(n) n . CASE 2: f(n) = Θ(n2log0n), that is, k = 0. ∴ T(n) = Θ(n2logn).

9/16/2010 CS 3343 Analysis of Algorithms 8

( ) ( g )

slide-9
SLIDE 9

Examples Examples

  • Ex. T(n) = 4T(n/2) + n3
  • Ex. T(n) 4T(n/2) + n

a = 4, b = 2 ⇒ nlogba = n2; f(n) = n3. CASE 3: f(n) = Ω(n2 + ε) for ε = 1 f( ) ( ) and 4(n/2)3 ≤ cn3 (reg. cond.) for c = 1/2. ∴ T(n) = Θ(n3).

  • Ex. T(n) = 4T(n/2) + n2/logn

a = 4 b = 2 ⇒ nlogba = n2; f(n) = n2/logn a = 4, b = 2 ⇒ n gb = n ; f(n) = n /logn. Master method does not apply. In particular, for every constant ε > 0, we have log n ∈ o(nε).

9/16/2010 CS 3343 Analysis of Algorithms 9

for every constant ε 0, we have log n ∈ o(n ).

slide-10
SLIDE 10

Example: merge sort Example: merge sort

1 Divide: Trivial

  • 1. Divide: Trivial.
  • 2. Conquer: Recursively sort 2 subarrays.
  • 3. Combine: Linear-time merge.

T(n) = 2T(n/2) + O(n) T(n) = 2T(n/2) + O(n) # subproblems subproblem size work dividing and combining and combining nlogba = nlog22 = n1 = n ⇒ CASE 2 (k = 0) ⇒ T(n) = Θ(n logn) .

9/16/2010 CS 3343 Analysis of Algorithms 10

⇒ T(n) Θ(n logn) .

slide-11
SLIDE 11

Recurrence for binary search Recurrence for binary search

T( ) 1T( /2) + Θ(1) T(n) = 1T(n/2) + Θ(1) # b bl k di idi # subproblems subproblem size work dividing and combining nlogba = nlog21 = n0 = 1 ⇒ CASE 2 (k = 0) nlogba = nlog21 = n0 = 1 ⇒ CASE 2 (k = 0) ⇒ T(n) = Θ(logn) .

9/16/2010 CS 3343 Analysis of Algorithms 11