cmpt 225

54
CMPT 225 Priority Queues and Heaps

Upload: heidi

Post on 19-Feb-2016

31 views

Category:

Documents


0 download

DESCRIPTION

Priority Queues and Heaps. CMPT 225. Remember Queues?. A queue should implement at least the first two of these operations: insert – insert item at the back of the queue remove – remove an item from the front peek – return the item at the front of the queue without removing it - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CMPT 225

CMPT 225Priority Queues and Heaps

Page 2: CMPT 225

Remember Queues?

October 2004 John Edgar 2

A queue should implement at least the first two of these operations: insert – insert item at the back of the

queue remove – remove an item from the front peek – return the item at the front of the

queue without removing it It is assumed that these operations

will be implemented efficiently That is, in constant time

Page 3: CMPT 225

Queue Implementations

Either with an array

Or with a linked list

October 2004 John Edgar 3

4 18 25 2 50 1 2 3 4 5 6 7

front 4

6

back7

3

Page 4: CMPT 225

FIFO

Queues are first-in first-out (FIFO) Priority queues are a fancier type of

queue Maintains an ordering of items in the

queue, not necessarily first-in first-out

October 2004 John Edgar 4

Page 5: CMPT 225

Priority Queues

September 2004 John Edgar 5

6

5

4

32

1

Page 6: CMPT 225

Priority Queues Items in a priority queue are given a

priority value Which could be numerical or something

else The highest priority item is removed

first Uses include

System requests Data structure to support Dijkstra’s

AlgorithmSeptember 2004 John Edgar 6

Page 7: CMPT 225

Priority Queue Problem Can items be inserted and removed

efficiently from a priority queue? Using an array, or Using a linked list?

Note that items are not removed based on the order in which they are inserted

September 2004 John Edgar 7

Now we’ll see how we can do these efficiently (using a different data structure)

Page 8: CMPT 225

ADT Priority Queue

Items in a priority queue have a priority Not necessarily numerical Could be lowest first or highest first

The highest priority item is removed first

Priority queue operations Insert Remove in priority queue order

▪ Both operations should be performed in at most O(log n) time

October 2004 John Edgar 8

Page 9: CMPT 225

Priority Queue Implementation

September 2004 John Edgar 9

Page 10: CMPT 225

Implementing a Priority Queue Items have to be removed in priority order

This can only be done efficiently if the items are ordered in some way

One option would be to use a balanced binary search tree Binary search trees are fully ordered and

insertion and removal can be implemented in O(log n) time▪ Some operations (e.g. removal) are complex▪ Although operations are O(logn) they require quite a

lot of structural overhead There is a much simpler binary tree

solution October 2004 John Edgar 10

Page 11: CMPT 225

Heaps A heap is binary tree with two properties Heaps are complete

All levels, except the bottom, are completely filled in

The leaves on the bottom level are as far to the left as possible.

Heaps are partially ordered The value of a node is at least as large as its

children’s values, for a max heap or The value of a node is no greater than its

children’s values, for a min heapOctober 2004 John Edgar 11

Page 12: CMPT 225

Complete Binary Trees

October 2004 John Edgar 12

complete binary trees

incomplete binary trees

Page 13: CMPT 225

Partially Ordered Tree – max heap

October 2004 John Edgar 13

98

4186

13 65

9 10

32 29

44 23 21 17

Heaps are not fully ordered, an inorder traversal would result in:9, 13, 10, 86, 44, 65, 23, 98, 21, 32, 17, 41, 29

Page 14: CMPT 225

Priority Queues and Heaps A heap can implement a priority queue The item at the top of the heap must

always be the highest priority value Because of the partial ordering property

Implement priority queue operations: Insertions – insert an item into a heap Removal – remove and return the heap’s root For both operations preserve the heap property

October 2004 John Edgar 14

Page 15: CMPT 225

Heap ImplementationUsing an Array

September 2004 John Edgar 15

Page 16: CMPT 225

Heap Implementation Heaps can be

implemented using arrays

There is a natural method of indexing tree nodes Index nodes from top to

bottom and left to right as shown on the right

Because heaps are complete binary trees there can be no gaps in the arrayOctober 2004 John Edgar 16

0

1 2

3 4 5 6

Page 17: CMPT 225

Referencing Nodes

It will be necessary to find the index of the parents of a node Or the children of a node

The array is indexed from 0 to n – 1 Each level's nodes are indexed from:

▪ 2level – 1 to 2level+1 – 2 (where the root is level 0) The children of a node i, are the array

elements indexed at 2i + 1 and 2i + 2 The parent of a node i, is the array element

indexed at floor((i – 1) / 2)October 2004 John Edgar 17

Page 18: CMPT 225

Heap Array Example

index

0 1 2 3 4 5 6 7 8 9 10 11 12

value 98 86 41 13 65 32 29 9 10 44 23 21 17

October 2004 John Edgar 18

98

4186

13 65

9 10

32 29

44 23 21 17

Heap

Underlying Array

0

1 2

3 54 6

7 8 9 10 11 12

Page 19: CMPT 225

Heap Insertion

September 2004 John Edgar 19

Page 20: CMPT 225

Heap Insertion

On insertion the heap properties have to be maintained; remember that A heap is a complete binary tree and A partially ordered binary tree

There are two general strategies that could be used to maintain the heap properties Make sure that the tree is complete and then

fix the ordering or Make sure the ordering is correct first Which is better?

October 2004 John Edgar 20

Page 21: CMPT 225

Heap Insertion Sketch The insertion algorithm first ensures that

the tree is complete Make the new item the first available (left-

most) leaf on the bottom level i.e. the first free element in the underlying

array Fix the partial ordering

Compare the new value to its parent Swap them if the new value is greater than the

parent Repeat until this is not the case

▪ Referred to as bubbling up, or trickling up

October 2004 John Edgar 21

Page 22: CMPT 225

Heap Insertion Example

October 2004 John Edgar 22

Insert 81

index

0 1 2 3 4 5 6 7 8 9 10 11 12 13

value 98

86 41 13 65 32 29 9 10 44 23 21 17

98

4186

13 65

9 10

32 29

44 23 21 17

Page 23: CMPT 225

Heap Insertion Example

October 2004 John Edgar 23

Insert 81

index

0 1 2 3 4 5 6 7 8 9 10 11 12 13

value 98

86 41 13 65 32 29 9 10 44 23 21 17

98

4186

13 65

9 10

32 29

44 23 21 17

(13-1)/2 = 6

81 is less than 98 so finished

8129

index

0 1 2 3 4 5 6 7 8 9 10 11 12 13

value 98

86 41 13 65 32 29 9 10 44 23 21 17 81

81

index

0 1 2 3 4 5 6 7 8 9 10 11 12 13

value 98

86 41 13 65 32 81 9 10 44 23 21 17 29

81

41

index

0 1 2 3 4 5 6 7 8 9 10 11 12 13

value 98

86 81 13 65 32 41 9 10 44 23 21 17 29

Page 24: CMPT 225

Heap Removal

September 2004 John Edgar 24

Page 25: CMPT 225

Heap Removal

Make a temporary copy of the root’s data Similarly to the insertion algorithm, first

ensure that the heap remains complete Replace the root node with the right-most leaf i.e. the highest (occupied) index in the array

Swap the new root with its largest valued child until the partially ordered property holds i.e. bubble down

Return the root’s dataOctober 2004 John Edgar 25

Page 26: CMPT 225

Heap Removal Example

index

0 1 2 3 4 5 6 7 8 9 10 11 12

value 98 86 41 13 65 32 29 9 10 44 23 21 17

October 2004 John Edgar 26

98

4186

13 65

9 10

32 29

44 23 21 17

Remove (root)

Page 27: CMPT 225

Heap Removal Example

index

0 1 2 3 4 5 6 7 8 9 10 11 12

value 98 86 41 13 65 32 29 9 10 44 23 21 17

October 2004 John Edgar 27

98

4186

13 65

9 10

32 29

44 23 21 17

Remove (root)

replace root with right-most leaf17

index

0 1 2 3 4 5 6 7 8 9 10 11 12

value 17 86 41 13 65 32 29 9 10 44 23 21

Page 28: CMPT 225

17

index

0 1 2 3 4 5 6 7 8 9 10 11 12

value 17 86 41 13 65 32 29 9 10 44 23 21

Heap Removal Example

October 2004 John Edgar 28

86

4186

13 65

9 10

32 29

44 23 21

Remove (root)

swap with larger child

??

children of root: 2*0+1, 2*0+2 = 1, 2

17

index

0 1 2 3 4 5 6 7 8 9 10 11 12

value 86 17 41 13 65 32 29 9 10 44 23 21

Page 29: CMPT 225

41

index

0 1 2 3 4 5 6 7 8 9 10 11 12

value 86 17 41 13 65 32 29 9 10 44 23 21

86

17

index

0 1 2 3 4 5 6 7 8 9 10 11 12

value 86 65 41 13 17 32 29 9 10 44 23 21

Heap Removal Example

October 2004 John Edgar 29

65

13 65

9 10

32 29

44 23 21

Remove (root)

children of 1: 2*1+1, 2*1+2 = 3, 4

17

? ?

swap with larger child

Page 30: CMPT 225

17

index

0 1 2 3 4 5 6 7 8 9 10 11 12

value 86 65 41 13 17 32 29 9 10 44 23 21

41

index

0 1 2 3 4 5 6 7 8 9 10 11 12

value 86 65 41 13 44 32 29 9 10 17 23 21

86

Heap Removal Example

October 2004 John Edgar 30

13

9 10

32 29

44 23 21

Remove (root)

children of 4: 2*4+1, 2*4+2 = 9, 10

swap with larger child

17

44

65

??

Page 31: CMPT 225

Bubbles

September 2004 John Edgar 31

Page 32: CMPT 225

Bubble Up and Bubble Down Helper functions are usually written for

preserving the heap property bubbleUp ensures that the heap property is

preserved from the start node up to the root bubbleDown ensures that the heap property is

preserved from the start node down to the leaves

These functions may be implemented recursively or iteratively

October 2004 John Edgar 32

Page 33: CMPT 225

BubbleDown Algorithm

Go to terminal

October 2004 John Edgar 33

Page 34: CMPT 225

Removal Algorithm

October 2004 John Edgar 34

Page 35: CMPT 225

Insertion Algorithm

October 2004 John Edgar 35

Lab next week

Page 36: CMPT 225

BubbleUp Algorithm

Lab next week

October 2004 John Edgar 36

Page 37: CMPT 225

Heap Efficiency For both insertion and removal the heap

performs at most height swaps For insertion at most height comparisons

▪ To bubble up the array For removal at most height * 2 comparisons

▪ To bubble down the array (have to compare two children)

Height of a complete binary tree is log2(n) Both insertion and removal are therefore

O(logn)

October 2004 John Edgar 37

Page 38: CMPT 225

Sorting with Heaps

September 2004 John Edgar 38

Page 39: CMPT 225

Sorting with Heaps Observation 1: Removal of a node from a

heap can be performed in O(logn) time Observation 2: Nodes are removed in

order Conclusion: Removing all of the nodes

one by one would result in sorted output Analysis: Removal of all the nodes from a

heap is a O(n*logn) operation

October 2004 John Edgar 39

Page 40: CMPT 225

But … A heap can be used to return sorted data

In O(n*logn) time However, we can’t assume that the data

to be sorted just happens to be in a heap! Aha! But we can put it in a heap. Inserting an item into a heap is a O(logn)

operation so inserting n items is O(n*logn) But we can do better than just repeatedly

calling the insertion algorithm

October 2004 John Edgar 40

Page 41: CMPT 225

Heapifying Data To create a heap from an unordered array

repeatedly call bubbleDown Any subtree in a heap is itself a heap Call bubbleDown on elements in the upper ½ of

the array Start with index n/2 and work up to index 0

▪ i.e. from the last non-leaf node to the root bubbleDown does not need to be called on

the lower half of the array (the leaves) Since bubbleDown restores the partial ordering

from any given node down to the leaves

October 2004 John Edgar 41

Page 42: CMPT 225

Heapify Example

October 2004 John Edgar 42

89

2329

36 48

27 70

94 13

76 37 42 58

Assume unsorted input is contained in an array as shown here (indexed from top to bottom and left to right)

0

1 2

3 54 6

7 8 9 10

11

12

Page 43: CMPT 225

Heapify Example

October 2004 John Edgar 43

89

2329

36 48

27 70

94 13

76 37 42 58

0

1 2

3 54 6

7 8 9 10

11

12

94

42 58

n = 12, (n-1)/2 = 5bubbleDown(5)

Page 44: CMPT 225

94

42 58

Heapify Example

October 2004 John Edgar 44

89

2329

36 48

27 70

13

76 37

0

1 2

3 54 6

7 8 9 10

11

12

bubbleDown(5)bubbleDown(4)

76

48 37

n = 12, (n-1)/2 = 5

Page 45: CMPT 225

76

48 37

94

42 58

Heapify Example

October 2004 John Edgar 45

89

2329

36

27 70

13

0

1 2

3 54 6

7 8 9 10

11

12

bubbleDown(5)bubbleDown(4)

70

27 36

bubbleDown(3)

n = 12, (n-1)/2 = 5

Page 46: CMPT 225

70

27 36

76

48 37

94

42 58

Heapify Example

October 2004 John Edgar 46

89

2329

13

0

1 2

3 54 6

7 8 9 10

11

12

bubbleDown(5)bubbleDown(4)bubbleDown(3)bubbleDown(2)

94

13

23

2358

n = 12, (n-1)/2 = 5

Page 47: CMPT 225

58

42

94

13

23

70

27 36

76

48 37

Heapify Example

October 2004 John Edgar 47

89

29

13

0

1 2

3 54 6

7 8 9 10

11

12

bubbleDown(5)bubbleDown(4)bubbleDown(3)bubbleDown(2)

29

76

bubbleDown(1)

2948

n = 12, (n-1)/2 = 5

Page 48: CMPT 225

3729

76

48 58

42

94

13

23

70

27 36

Heapify Example

October 2004 John Edgar 48

89

13

0

1 2

3 54 6

7 8 9 10

11

12

bubbleDown(5)bubbleDown(4)bubbleDown(3)bubbleDown(2)bubbleDown(1)bubbleDown(0)

94

89

n = 12, (n-1)/2 = 5

Page 49: CMPT 225

Cost to Heapify an Array bubbleDown is called on half the array

The cost for bubbleDown is O(height) It would appear that heapify cost is O(n*logn)

In fact the cost is O(n) The analysis is complex but

bubbleDown is only called on n/2 nodes and mostly on sub-trees

October 2004 John Edgar 49

Page 50: CMPT 225

HeapSort Algorithm Sketch Heapify the array Repeatedly remove the root

After each removal swap the root with the last element in the tree

The array is divided into a heap part and a sorted part

At the end of the sort the array will be sorted in reverse order

October 2004 John Edgar 50

Page 51: CMPT 225

HeapSort Notes The algorithm runs in O(n*logn) time

Considerably more efficient than selection sort and insertion sort

The same average case complexity as MergeSort and QuickSort

The sort can be carried out in-place That is, it does not require that a copy of

the array to be made

October 2004 John Edgar 51

Page 52: CMPT 225

Summary

September 2004 John Edgar 52

Page 53: CMPT 225

Objectives Define the ADT priority queue Define the partially ordered property Define a heap Implement a heap using an array Implement the heapSort algorithm

October 2004 John Edgar 53

Page 54: CMPT 225

Readings

Java Ch. 12 C++ Ch. 11

October 2004 John Edgar 54