Mastering Recurrences: Analyzing the Running Time of Heapify() and Heapsort Algorithms, Lecture notes of Algorithms and Programming

The master theorem and its application to the analysis of heapify() and heapsort algorithms. The basics of the master theorem, its use in analyzing recursive functions, and the advantages of merge sort and insertion sort. Additionally, it discusses the implementation of heaps as complete binary trees and arrays, basic heap operations, and heapify() example. Lastly, it analyzes the running time of heapify() and heapsort algorithms.

Typology: Lecture notes

2015/2016

Uploaded on 10/21/2016

Mouad1231
Mouad1231 🇩🇿

4

(2)

12 documents

1 / 56

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
David Luebke 1
10/21/16
CS 332: Algorithms
Solving Recurrences Continued
The Master Theorem
Introduction to heapsort
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 Mastering Recurrences: Analyzing the Running Time of Heapify() and Heapsort Algorithms and more Lecture notes Algorithms and Programming in PDF only on Docsity!

CS 332: Algorithms

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

k

T(n/b

k

) + cn(a

k-

/b

k-

  • a

k-

/b

k-

  • … + a

2

/b

2

  • a/b + 1)

cn n

b

n

aT

T n

So we have

■ T(n) = a

k

T(n/b

k

) + cn(a

k-

/b

k-

  • ... + a

2

/b

2

  • a/b + 1)

● For k = log

b

n

■ n = b

k

■ T(n) = a

k

T(1) + cn(a

k-

/b

k-

  • ... + a

2

/b

2

  • a/b + 1)

= a

k

c + cn(a

k-

/b

k-

  • ... + a

2

/b

2

  • a/b + 1)

= ca

k

  • cn(a

k-

/b

k-

  • ... + a

2

/b

2

  • a/b + 1)

= cna

k

/b

k

  • cn(a

k-

/b

k-

  • ... + 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 = log

b

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 = log

b

n

■ T(n) = cn(a

k

/b

k

  • ... + a

2

/b

2

  • a/b + 1)

What if a < b?

■ Recall that (x

k

  • x

k-

  • … + x + 1) = (x

k+

-1)/(x-1)

cn n

b

n

aT

T n

So with k = log

b

n

■ T(n) = cn(a

k

/b

k

  • ... + a

2

/b

2

  • a/b + 1)

What if a < b?

■ Recall that (x

k

  • x

k-

  • … + x + 1) = (x

k+

-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 = log

b

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 = log

b

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 = log

b

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 = log

b

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 · n

log 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 = log

b

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 · n

log 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