Master Theorem - Introduction to Algorithms - Lecture Slides, Slides of Computer Science

These are the Lecture Slides of Introduction to Algorithms which includes Expensive Operations, Sort Edges, Running Time, Upshot, Union, Makeset, Disjoint Set, Disjoint Set Union, Naïve Implementation etc. Key important points are: Master Theorem, Introduction to Heapsort, Solving Recurrences Continued, Merge Sort, Sorted Subarrays, Single Sorted Subarray, Requires, Require Allocating, Statement, Substitution Method

Typology: Slides

2012/2013

Uploaded on 03/23/2013

dhruv
dhruv 🇮🇳

4.3

(12)

194 documents

1 / 56

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Solving Recurrences Continued
The Master Theorem
Introduction to heapsort
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38

Partial preview of the text

Download Master Theorem - Introduction to Algorithms - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Solving Recurrences Continued

The Master Theorem

Introduction to heapsort

Review: Merge Sort

MergeSort(A, left, right) { if (left < right) { mid = floor((left + right) / 2); MergeSort(A, left, mid); MergeSort(A, mid+1, right); Merge(A, left, mid, right); } } // Merge() takes two sorted subarrays of A and // merges them into a single sorted subarray of A. // Code for this is in the book. It requires O(n) // time, and does require allocating O(n) space

Review: Solving Recurrences

  • Substitution method
  • Iteration method
  • Master method

Review: Solving Recurrences

  • The substitution method
    • A.k.a. the “making a good guess method”
    • Guess the form of the answer, then use induction to find the constants and show that solution works
    • Run an example : merge sort
      • T(n) = 2T(n/2) + cn
      • We guess that the answer is O(n lg n)
      • Prove it by induction
    • Can similarly show T(n) = Ω(n lg n), thus Θ(n lg n)
  • T(n) = aT(n/b) + cn a(aT(n/b/b) + cn/b) + cn a 2 T(n/b 2 ) + cna/b + cn a 2 T(n/b 2 ) + cn(a/b + 1) a 2 (aT(n/b 2 /b) + cn/b 2 ) + cn(a/b + 1) a 3 T(n/b 3 ) + cn(a 2 /b 2 ) + cn(a/b + 1) a 3 T(n/b 3 ) + cn(a 2 /b 2 + a/b + 1) … a kT(n/b k) + cn(a k-1/b k-1^ + a k-2/b k-2^ + … + a 2 /b 2 + a/b + 1)

( ) cn n b

n aT T n

  • So we have
    • T(n) = a kT(n/b k) + cn(a k-1/b k-1^ + ... + a 2 /b 2 + a/b + 1)
  • For k = log (^) b n
    • n = b k
    • T(n) = a kT(1) + cn(a k-1/b k-1^ + ... + a 2 /b 2 + a/b + 1) = a kc + cn(a k-1/b k-1^ + ... + a 2 /b 2 + a/b + 1) = ca k^ + cn(a k-1/b k-1^ + ... + a 2 /b 2 + a/b + 1) = cna k^ /b k^ + cn(a k-1/b k-1^ + ... + a 2 /b 2 + a/b + 1) = cn(a k/b k^ + ... + a 2 /b 2 + a/b + 1)

( ) cn n b

n aT T n

  • So with k = logb n
    • T(n) = cn(a k/b k^ + ... + a^2 /b 2 + a/b + 1)
  • What if a < b?

( ) cn n b

n aT T n

  • So with k = logb n
    • T(n) = cn(a k/b k^ + ... + a^2 /b 2 + a/b + 1)
  • What if a < b?
    • Recall that Σ(xk^ + xk-1^ + … + x + 1) = (x k+1^ -1)/(x-1)

( ) cn n b

n aT T n

  • So with k = logb n
    • T(n) = cn(a k/b k^ + ... + a^2 /b 2 + a/b + 1)
  • What if a < b?
    • Recall that Σ(xk^ + xk-1^ + … + x + 1) = (x k+1^ -1)/(x-1)
    • So:
    • T(n) = cn ·Θ(1) = Θ(n)

( ) cn n b

n aT T n

( a b ) a b

a b a b

a b b

a b

a b

a k k k

k k

k − < − = − −

        • = + − + −

− 1

1 1

1 1 1 1 1 1 1

1 

  • So with k = logb n
    • T(n) = cn(a k/b k^ + ... + a^2 /b 2 + a/b + 1)
  • What if a > b?

( ) cn n b

n aT T n

  • So with k = logb n
    • T(n) = cn(a k/b k^ + ... + a^2 /b 2 + a/b + 1)
  • What if a > b?
    • T(n) = cn · Θ(a k^ / b k)

( ) cn n b

n aT T n

(( ) k )

k k

k k

k a b a b

a b b

a b

a b

a (^) = Θ −

        • = + − −

− 1

1 1 1 1

1 

  • So with k = logb n
    • T(n) = cn(a k/b k^ + ... + a^2 /b 2 + a/b + 1)
  • What if a > b?
    • T(n) = cn · Θ(a k^ / b k) = cn · Θ(a log n^ / b log n) = cn · Θ(a log n^ / n)

( ) cn n b

n aT T n

(( ) k )

k k

k k

k a b a b

a b b

a b

a b

a (^) = Θ −

        • = + − −

− 1

1 1 1 1

1 

  • So with k = logb n
    • T(n) = cn(a k/b k^ + ... + a^2 /b 2 + a/b + 1)
  • What if a > b?
    • T(n) = cn · Θ(a k^ / b k) = cn · Θ(a log n^ / b log n) = cn · Θ(a log n^ / n) recall logarithm fact: a log n^ = n log a = cn · Θ(n log a^ / n) = Θ(cn · nlog a^ / n)

( ) cn n b

n aT T n

(( ) k )

k k

k k

k a b a b

a b b

a b

a b

a (^) = Θ −

        • = + − −

− 1

1 1 1 1

1 

  • So with k = logb n
    • T(n) = cn(a k/b k^ + ... + a^2 /b 2 + a/b + 1)
  • What if a > b?
    • T(n) = cn · Θ(a k^ / b k) = cn · Θ(a log n^ / b log n) = cn · Θ(a log n^ / n) recall logarithm fact: a log n^ = n log a = cn · Θ(n log a^ / n) = Θ(cn · nlog a^ / n) = Θ(n log a^ )

( ) cn n b

n aT T n

(( ) k )

k k

k k

k a b a b

a b b

a b

a b

a (^) = Θ −

        • = + − −

− 1

1 1 1 1

1 