403: Algorithms and Data Structures Heapsort and Priority Queues - - PowerPoint PPT Presentation

403 algorithms and data structures heapsort and priority
SMART_READER_LITE
LIVE PREVIEW

403: Algorithms and Data Structures Heapsort and Priority Queues - - PowerPoint PPT Presentation

403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer Science 1 Some slides borrowed from David Luebke Context We defined heaps almost complete binary trees A[ Parent ( i )] A[ i ]


slide-1
SLIDE 1

403: Algorithms and Data Structures Heapsort and Priority Queues

Fall 2016 UAlbany Computer Science

Some slides borrowed from David Luebke

1

slide-2
SLIDE 2

Context

  • We defined heaps

– ”almost” complete binary trees – A[Parent(i)] ≥ A[i] for all nodes i > 1

  • Heap operaIons: Heapify()

– Fix a single violaIon of the heap property – “Float” values down the tree – O(logn), where n is the heap size

  • What is the base of the log?

16 14 10 8 7 9 3 2 4 1

16 14 10 8 7 9 3 2 4 1 A = =

2

slide-3
SLIDE 3

Heap OperaIons: BuildHeap()

  • Input: Array A[1…n]
  • Required: Convert A into a heap
  • Idea: build a heap in a bo[om-up manner by

running Heapify() on successive subarrays

3

slide-4
SLIDE 4

Heap OperaIons: BuildHeap()

  • Fact: for array of length n, all elements in range

A[⎣n/2⎦ + 1 .. n] are leaves (Why?)

– Lec(⎣n/2⎦ + 1 ) = 2* (⎣n/2⎦ + 1) > n

  • Another fact: Leaves are (trivially) heaps
  • Walk backwards through the array from ⎣n/2⎦ to

1, calling Heapify() on each node.

– Order of processing guarantees that the children of node i are heaps when i is processed – Why is this important?

4

slide-5
SLIDE 5

BuildHeap()

// given an unsorted array A, make A a heap BuildHeap(A) { heap_size(A) = length(A); for (i = ⎣length[A]/2⎦ downto 1) Heapify(A, i); }

5

slide-6
SLIDE 6

BuildHeap() Example

  • Work through example

A = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7}

4 1 3 2 16 9 10 14 8 7

6

slide-7
SLIDE 7

Crude Analysis of BuildHeap()

  • Each call to Heapify() takes O(lg n) Ime
  • There are O(n) such calls (specifically, ⎣n/2⎦)
  • Thus the running Ime is O(n lg n)

– Is this a correct asympto2c upper bound? – Is this an asympto2cally 2ght bound?

  • A Ighter bound is O(n)

– How can this be? Is there a flaw in the above reasoning?

7

slide-8
SLIDE 8

Analyzing BuildHeap(): Tight

  • To Heapify() a subtree takes O(h) Ime where

h is the height of the subtree

– h = O(lg m), m = # nodes in subtree – IntuiIon: The height of most subtrees is small , i.e. O(log n) is too “generous”

  • Fact 1: an n-element heap has at most ⎡n/2h+1⎤

nodes of height h

– Proof?

  • Using Fact 1 we can show that BuildHeap()

takes O(n) Ime

– Proof?

8

slide-9
SLIDE 9

Heapsort

  • Given BuildHeap(), an in-place sorIng

algorithm is easily constructed:

– Maximum element is at A[1] – Discard by swapping with element at A[n]

  • Decrement heap_size[A]
  • A[n] now contains correct value

– Restore heap property at A[1] by calling Heapify() – Repeat, always swapping A[1] for A[heap_size(A)]

9

slide-10
SLIDE 10

Example

10

16 14 10 8 7 9 3 2 4 1

slide-11
SLIDE 11

Heapsort

Heapsort(A) { BuildHeap(A); for (i = length(A) downto 2) { Swap(A[1], A[i]); heap_size(A) -= 1; Heapify(A, 1); } }

11

slide-12
SLIDE 12

Analyzing Heapsort

  • The call to BuildHeap() takes O(n) Ime
  • Each of the n - 1 calls to Heapify() takes

O(lg n) Ime

  • Thus the total Ime taken by HeapSort()

= O(n) + (n - 1) O(lg n) = O(n) + O(n lg n) = O(n lg n)

12

slide-13
SLIDE 13

Priority Queues

  • Heapsort is a nice algorithm, but in pracIce

Quicksort (coming up) usually wins

  • But the heap data structure is incredibly

useful for implemenIng priority queues

– A data structure for maintaining a set S of elements, each with an associated value or key – Supports the operaIons Insert(), Maximum(), and ExtractMax() – What might a priority queue be useful for?

13

slide-14
SLIDE 14

14

slide-15
SLIDE 15

Assassin's prioriIzed TODO manager

15

slide-16
SLIDE 16

Priority Queue OperaIons

  • Insert(S, x) inserts the element x into set S
  • Maximum(S) returns the element of S with

the maximum key

  • ExtractMax(S) removes and returns the

element of S with the maximum key

  • How could we implement these opera2ons

using a heap?

16

slide-17
SLIDE 17

Priority Queue OperaIons

  • Insert(S, x)

– Increment heap size and add x at the end – move the new element “upwards” (reverse-heapify) – O(log n)

  • Maximum(S)

– return S[1] – Time complexity?

  • ExtractMax(S) removes and returns the element
  • f S with the maximum key

– save S[1], place S[heap_size(S)] in S[1]; Heapify(S,1) – Time?

17

slide-18
SLIDE 18

Heap vs All (for Priority queues)

18

Data Structure Pre- processing Insert Max Extract Max Linked List O(n) O(1) O(n) O(n) Sorted Array O(n logn) O(n) (shicing) O(1) O(1) Heap O(n) O(log n) O(1) O(log n)

slide-19
SLIDE 19

Announcements

  • Read through Chapter 6

– Next class: Build heap, Heap Sort, Priority Queues

  • HW2 due next Wednesday

19