12
CS 5633 Analysis of Algorithms Chapter 6: Slide – 1 Chapter 6: Heapsort Max Heaps Heapsort Priority Queues

Chapter6: Heapsortbylander/cs5633/notes/heapsort.pdf · Max-Heapify parent left right figure 6.1⊲ max-heapify figure 6.2 heapsort behavior 1 behavior 2 behavior 3 heap-extract-max

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter6: Heapsortbylander/cs5633/notes/heapsort.pdf · Max-Heapify parent left right figure 6.1⊲ max-heapify figure 6.2 heapsort behavior 1 behavior 2 behavior 3 heap-extract-max

CS 5633 Analysis of Algorithms Chapter 6: Slide – 1

Chapter 6: Heapsort

Max HeapsHeapsortPriority Queues

Page 2: Chapter6: Heapsortbylander/cs5633/notes/heapsort.pdf · Max-Heapify parent left right figure 6.1⊲ max-heapify figure 6.2 heapsort behavior 1 behavior 2 behavior 3 heap-extract-max

Parent, Left, Right

⊲ parent left right

figure 6.1

max-heapify

figure 6.2

heapsort

behavior 1

behavior 2

behavior 3

heap-extract-max

priority queues

set-key and insert

CS 5633 Analysis of Algorithms Chapter 6: Slide – 2

Parent(i)return ⌊i/2⌋

Left(i)return 2i

Right(i)return 2i+ 1

Max-heap property: A[Parent(i)] ≥ A[i] forevery node i other than the root.

Page 3: Chapter6: Heapsortbylander/cs5633/notes/heapsort.pdf · Max-Heapify parent left right figure 6.1⊲ max-heapify figure 6.2 heapsort behavior 1 behavior 2 behavior 3 heap-extract-max

Figure 6.1

parent left right

⊲ figure 6.1

max-heapify

figure 6.2

heapsort

behavior 1

behavior 2

behavior 3

heap-extract-max

priority queues

set-key and insert

CS 5633 Analysis of Algorithms Chapter 6: Slide – 3

Page 4: Chapter6: Heapsortbylander/cs5633/notes/heapsort.pdf · Max-Heapify parent left right figure 6.1⊲ max-heapify figure 6.2 heapsort behavior 1 behavior 2 behavior 3 heap-extract-max

Max-Heapify

parent left right

figure 6.1

⊲ max-heapify

figure 6.2

heapsort

behavior 1

behavior 2

behavior 3

heap-extract-max

priority queues

set-key and insert

CS 5633 Analysis of Algorithms Chapter 6: Slide – 4

Max-Heapify(A, i)l ← Left(i)r ← Right(i)if l ≤ A.heap-size and A[l] > A[i]then largest← lelse largest← i

if r ≤ A.heap-size and A[r] > A[largest]then largest← r

if largest 6= ithen exchange A[i]↔ A[largest]

Max-Heapify(A, largest)

Page 5: Chapter6: Heapsortbylander/cs5633/notes/heapsort.pdf · Max-Heapify parent left right figure 6.1⊲ max-heapify figure 6.2 heapsort behavior 1 behavior 2 behavior 3 heap-extract-max

Figure 6.2

parent left right

figure 6.1

max-heapify

⊲ figure 6.2

heapsort

behavior 1

behavior 2

behavior 3

heap-extract-max

priority queues

set-key and insert

CS 5633 Analysis of Algorithms Chapter 6: Slide – 5

Page 6: Chapter6: Heapsortbylander/cs5633/notes/heapsort.pdf · Max-Heapify parent left right figure 6.1⊲ max-heapify figure 6.2 heapsort behavior 1 behavior 2 behavior 3 heap-extract-max

Heapsort

parent left right

figure 6.1

max-heapify

figure 6.2

⊲ heapsort

behavior 1

behavior 2

behavior 3

heap-extract-max

priority queues

set-key and insert

CS 5633 Analysis of Algorithms Chapter 6: Slide – 6

Build-Max-Heap(A)A.heap-size← A.length

for i← ⌊A.length/2⌋ downto 1Max-Heapify(A, i)

Heapsort(A)Build-Max-Heap(A)for i← A.length downto 2

exchange A[1]↔ A[i]A.heap-size← A.heap-size− 1Max-Heapify(A, 1)

Page 7: Chapter6: Heapsortbylander/cs5633/notes/heapsort.pdf · Max-Heapify parent left right figure 6.1⊲ max-heapify figure 6.2 heapsort behavior 1 behavior 2 behavior 3 heap-extract-max

Build-Max-Heap Behavior, Part 1

parent left right

figure 6.1

max-heapify

figure 6.2

heapsort

⊲ behavior 1

behavior 2

behavior 3

heap-extract-max

priority queues

set-key and insert

CS 5633 Analysis of Algorithms Chapter 6: Slide – 7

# of Max-Heapify calls

1 ✲ 3(1) calls

���

❅❅❅

2 3 ✲ 2(2) calls

✁✁✁

❆❆❆

✁✁✁

❆❆❆

4 5 6 7 ✲ 0 calls

Total: 7 calls

Page 8: Chapter6: Heapsortbylander/cs5633/notes/heapsort.pdf · Max-Heapify parent left right figure 6.1⊲ max-heapify figure 6.2 heapsort behavior 1 behavior 2 behavior 3 heap-extract-max

Build-Max-Heap Behavior, Part 2

parent left right

figure 6.1

max-heapify

figure 6.2

heapsort

behavior 1

⊲ behavior 2

behavior 3

heap-extract-max

priority queues

set-key and insert

CS 5633 Analysis of Algorithms Chapter 6: Slide – 8

# of Max-Heapify calls

1 ✲ 4(1) calls

���

❅❅❅

2 3 ✲ 3(2) calls

✁✁✁

❆❆❆

✁✁✁

❆❆❆

4 5 6 7 ✲ 2(4) calls

✄✄✄

✄✄✄

✄✄✄

✄✄✄

❈❈❈

❈❈❈

❈❈❈

❈❈❈

8 9 10 11 12 13 14 15 ✲ 0 calls

Total: 18 calls

Page 9: Chapter6: Heapsortbylander/cs5633/notes/heapsort.pdf · Max-Heapify parent left right figure 6.1⊲ max-heapify figure 6.2 heapsort behavior 1 behavior 2 behavior 3 heap-extract-max

Build-Max-Heap Behavior, Part 3

parent left right

figure 6.1

max-heapify

figure 6.2

heapsort

behavior 1

behavior 2

⊲ behavior 3

heap-extract-max

priority queues

set-key and insert

CS 5633 Analysis of Algorithms Chapter 6: Slide – 9

Assuming n = 2k − 1 for k ≥ 1, the recurrence is:T (1) = 0T (n) = T (⌊n/2⌋) + ⌊n/2⌋+ ⌈n/4⌉

A simplification T (n) = T (n/2) + 3n/4 is Θ(n)by the Master Theorem.

Can show T (n) < 2n by mathematical induction.Basis n = 1: T (1) = 0 < 2Induction:Assuming T (n) < 2n, show T (2n+1) < 2(2n+1)

T (2n+ 1) = T (n) + n+ ⌈n/2⌉ < 2n+ n+ n =4n < 2(2n+ 1)

Page 10: Chapter6: Heapsortbylander/cs5633/notes/heapsort.pdf · Max-Heapify parent left right figure 6.1⊲ max-heapify figure 6.2 heapsort behavior 1 behavior 2 behavior 3 heap-extract-max

Heap-Extract-Max

parent left right

figure 6.1

max-heapify

figure 6.2

heapsort

behavior 1

behavior 2

behavior 3

⊲ heap-extract-

max

priority queues

set-key and insert

CS 5633 Analysis of Algorithms Chapter 6: Slide – 10

Heap-Maximum(A)return A[1]

Heap-Extract-Max(A)if A.heap-size < 1then error “heap underflow”

max← A[1]A[1]← A[A.heap-size]A.heap-size← A.heap-size− 1Max-Heapify(A, 1)return max

Page 11: Chapter6: Heapsortbylander/cs5633/notes/heapsort.pdf · Max-Heapify parent left right figure 6.1⊲ max-heapify figure 6.2 heapsort behavior 1 behavior 2 behavior 3 heap-extract-max

Priority Queues

parent left right

figure 6.1

max-heapify

figure 6.2

heapsort

behavior 1

behavior 2

behavior 3

heap-extract-max

⊲ priority queues

set-key and insert

CS 5633 Analysis of Algorithms Chapter 6: Slide – 11

A max-priority queue supports the followingoperations:

� Insert(Q, x): Insert x into the Q.� Maximum(Q): Return the maximum of the

Q.� Extract-Max(Q): Remove the maximum

of the Q and return it.� Set-Key(Q, x, k): Set the key of x to k

(differs from book’s Increase-Key).

Page 12: Chapter6: Heapsortbylander/cs5633/notes/heapsort.pdf · Max-Heapify parent left right figure 6.1⊲ max-heapify figure 6.2 heapsort behavior 1 behavior 2 behavior 3 heap-extract-max

Set-Key and Insert

parent left right

figure 6.1

max-heapify

figure 6.2

heapsort

behavior 1

behavior 2

behavior 3

heap-extract-max

priority queues

⊲ set-key and

insert

CS 5633 Analysis of Algorithms Chapter 6: Slide – 12

Max-Heap-Set-Key(A, i, key)A[i] ← key

if A[Parent(i)] < key

then while i > 1 and A[Parent(i)] < key

exchange A[i]↔ A[Parent(i)]i← Parent(i)

else Max-Heapify(A, i)

Max-Heap-Insert(A, key)A.heap-size← A.heap-size+ 1Max-Heap-Set-Key(A,A.heap-size, key)