Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a...

25
1 Heaps and HeapSort 2 6 5 7 9

Transcript of Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a...

Page 1: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

1

Heaps and HeapSort2

65

79

Page 2: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

2

Priority Queue Abstract Data Type (ADT)

A priority queue stores a collection of items, each of which has a given priority.An item is a pair(key, element), where the key is the priority. Assume smaller key = higher priority.Main methods of the Priority Queue ADTn insertItem(k, e)

inserts an item with key k and element e

n removeMin()removes the item with smallest key and returns its element

Additional methodsn minKey()

returns, but does not remove, the smallest key of an item

n minElement()returns, but does not remove, the element of an item with smallest key

n size(), isEmpty()Applications:n Standby flyersn Auctionsn Stock market

Page 3: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

3

Sorting with Priority QueuesWe can use a priority queue to sort a set of elementsHow?

Page 4: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

4

Sorting with Priority QueuesWe can use a priority queue to sort a set of elementsn Insert the elements one

by one with a series of insertItem(e, e) operations

n Remove the elements in sorted order with a series of removeMin() operations

The running time of this sorting method depends on the priority queue implementationWhat are some possible implementations?

PQ-Sort(S)P = priority queuewhile S.isEmpty == false

e = S. get(0)S.remove(0)P.insertItem(e, e)

while P.isEmpty() == falsee = P.removeMin()S.add(e)

Page 5: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

5

List-Based Priority QueueImplementation with an unsorted list

Performance:n insertItem takes O(1) time

since we can insert the item at the end of the list.

n removeMin, minKey and minElement take O(n) time since we may have to traverse the entire list to find the smallest key.

Implementation with a sorted list

Performance:n insertItem takes O(n) time

since we have to find where to insert the item and possibly move existing items

n The time for minKey and minElement is O(1).

n The time for removeMin depends on how the list is implemented, even thought the min is the first item in the list.

w O(n) if it’s an arrayw O(1) if it’s a linked list

4 5 2 3 1 1 2 3 4 5

Page 6: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

6

PQ-Sort with Unsorted List PQRunning time:n Inserting the elements into the priority queue with n

insertItem operations takes O(n) timen Removing the elements in sorted order from the priority

queue with n removeMin operations takes time:1 + 2 + …+ n

O(n2) time(An aside: Phase 2 is like SelectionSort.)

Page 7: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

7

PQ-Sort with Sorted List PQ Running time:

n Inserting the elements into the priority queue with ninsertItem operations takes time:

1 + 2 + …+ nn Removing the elements in sorted order from the priority

queue with a series of n removeMin operations takes O(n2) time if using an array, O(n) time if using a linked list.

In either case, O(n2) time.(An aside: Phase 1 is like Insertion Sort.)

Page 8: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

8

HeapsA heap is a binary tree storing keys at its nodes and satisfying the following properties:n Heap Property for “Min Heap”:

for every node v other than the root:

key(v) ³ key(parent(v))n Complete Binary Tree:

w all levels are full except possibly the last one

w all the nodes at the last level are as far left as possible

n Important: A heap storing nkeys has height O(log n)

2

65

79

last node

Page 9: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

9

Heaps and Priority QueuesWe can use a heap to implement a priority queueWe store a <key, value> pair at each nodeWe keep track of the position of the last nodeFor simplicity, we show only the keys in subsequent pictures

(2, Sue)

(6, Mark)(5, Pat)

(9, Jeff) (7, Anna)

Page 10: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

10

Insertion into HeapMethod insertItem of the priority queue ADT corresponds to the insertion of a key k to the heapThe insertion algorithm consists of three steps:n Find the insertion point z

(the new last node)n Store k at zn Restore the heap property

(discussed next)

2

65

79

insertion node

z

2

65

79 1z

Page 11: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

11

UpheapAfter the insertion of a new key k, the heap property may be violatedAlgorithm upheap restores the heap property by swapping k along an upward path from the insertion nodeUpheap terminates when the key k reaches the root or a node whose parent has a key less than or equal to kSince a heap has height O(log n), upheap runs in O(log n) time

1

25

79 6z

2

15

79 6z

2

65

79 1z

Page 12: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

12

Removal from a HeapMethod removeMin of the priority queue ADT corresponds to the removal of the root key from the heapThe removal algorithm consists of three steps:n Remove the root keyn Move the key of the last

node w to the rootn Restore the heap property

(discussed next)

2

65

79

last node

w

7

65

9

Page 13: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

13

DownheapAfter replacing the root key with the key k of the last node, the heap-order property may be violatedAlgorithm downheap restores the heap-order property by swapping key k with one of its children along a downward path from the root. Which one?Downheap terminates when key k reaches a node whose children have keys greater than or equal to kSince a heap has height O(log n), downheap runs in O(log n) time

7

65

9

5

67

9

Page 14: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

14

Heap Implementation UsingArrayLists or Arrays

We can represent a heap with nkeys by means of an array or ArrayList of length n + 1For the node at index in the left child is at index 2in the right child is at index 2i + 1n the parent is at index floor(i/2)

Links between nodes are not explicitly storedThe cell at index 0 is not usedOperation insertItem corresponds to inserting at index n + 1 and upheapingOperation removeMin corresponds to moving the item at index n to index 1 and downheaping

2

65

79

2 5 6 9 71 2 3 4 50

Page 15: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

15

Given a list S of n items (keys), remove the items in S and make a heap-based priority queue from the n keys

Remove the n keys from the heap one at a time and add them to S

HeapSort

Page 16: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

16

Insert n keys one at a time

How long does it take to insert key i?

And:

Top-Down Heap Construction

lgi ≈ n lgni=0

n

lgi

Page 17: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

17

We can construct a heap storing n given keys using a bottom-up construction with log nphasesPairs of heaps with 2i -1keys are merged into heaps with 2i+1-1 keys

Bottom-Up Heap Construction

2i -1 2i -1

2i+1-1

Page 18: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

18

Merging Two HeapsWe are given two heaps and a key kWe create a new heap with the root node storing k and with the two heaps as subtreesWe perform downheap to restore the heap-order property

7

3

58

2

64

3

58

2

64

2

3

58

4

67

Page 19: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

19

Example

1516 124 96 2023

25

1516

5

124

11

96

27

2023

Page 20: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

20

Example (contd.)

25

1516

5

124

11

96

27

2023

15

2516

4

125

6

911

20

2723

Page 21: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

21

Example (contd.)7

15

2516

4

125

8

6

911

20

2723

4

15

2516

5

127

6

8

911

20

2723

Page 22: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

22

Example (end)4

15

2516

5

127

10

6

8

911

20

2723

5

15

2516

7

1210

4

6

8

911

20

2723

Page 23: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

23

Visual AnalysisWe visualize the worst-case time using proxy paths that first go right and then repeatedly go left until the bottom of the heap is reached (this path may differ from the actual downheap path)Since no edge is traversed more than once, the total number of nodes of the proxy paths is O(n)Thus, bottom-up heap construction runs in O(n) time Bottom-up heap construction is faster than n successive insertions and speeds up the first phase of heap-sort

Page 24: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

Non-Visual AnalysisIn phase i = 1, 2,… are (n+1)/2i+1 pairs of heaps being combinedEach pair has a downheap path length of iLength of all downheap paths during phase i is (n+1)(i/2i+1)< n(i/2i)Sum these over the lg n phases:

And use this fact:

So:

And bottom-up heap construction is O(n) time

n i2 i

=i=1

lg n

∑ n i2 i

=i=1

lg n

∑ n i 12( )i

i=1

lg n

ix i =x

1− x( )2i=0

∑ , x <1

n i 12( )i <

i=1

lg n

∑ n i 12( )i

i=0

∑ = 2n

Page 25: Heaps and HeapSortsmajerci/teaching/cs2200/2020spring/Lectures/05-heapsort.pdfHeaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap

25

Given a list S of n items (keys), remove the items in S and make a heap-based priority queue from the n keys: O(n) time

Remove the n keys from the heap one at a time and add them to S: O(?) time

So: O(n lg n) time

Total: O(n lg n) time

HeapSort

lgi ≈ n lgni=0

n