priority queues and heaps cs16: introduction to data structures & algorithms tuesday, february...

24
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

Upload: alexander-chapman

Post on 05-Jan-2016

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

1

PRIORITY QUEUES AND HEAPS

CS16: Introduction to Data Structures & Algorithms

Tuesday, February 24, 2015

Page 2: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

2

Outline

• Priority Queues• Motivation• Abstract Data Type• Implementations

• Heaps• insert and upheap• removeMin and downheap

Tuesday, February 24, 2015

Page 3: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

3

Priority Queue Motivation

• Many problems are solved by maintaining a collection of items and assigning each a priority

• Examples:• Plane departures

• Different flights require the runway• Some flights are higher priority than others

• Bandwidth management• Highest priority data (real-time traffic like Skype, for

example) is transmitted first

Tuesday, February 24, 2015

Page 4: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

4

Priority Queue Abstract Data Type

• A priority queue stores a collection of items• An item is a pair: (key, element)

• The key defines the element’s position in the queue.

• Main methods:• insert(key, element)

inserts an item with the specified key and element• removeMin()

removes the item with the smallest key and returns the associated element

Tuesday, February 24, 2015

Page 5: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

5

Priority Queue Implementations

Tuesday, February 24, 2015

Implementation add removeMin

Unsorted Array O(1) O(n)Sorted Array O(n) O(1)Unsorted Linked List O(1) O(n)Sorted Linked List O(n) O(1)Hash Table O(1) O(n)Heap O(log n) O(log n)

Page 6: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

6

Heaps

• Data structure used to implement the priority queue ADT• insert(key, element)• removeMin()

• Can be implemented with a tree(link-based) or with an array

• Let’s look at tree first

Tuesday, February 24, 2015

2

65

79

Page 7: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

7

Heap Properties

• Binary• Each node has at most two children

• Key (or “priority”) at each node• Heap order

• for min-heap: n.key ≥ n.parent.key• for max-heap: n.key ≤ n.parent.key

• Left-complete• Height O(log n)

Tuesday, February 24, 2015

2

65

79

Page 8: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

8

Heaps and Priority Queues

• We can use a heap to implement a priority queue• We store a (key, element) item at each node• For simplicity, we will show only the keys in the following pictures

Tuesday, February 24, 2015

(2, Nick)

(6, David)(5, Marley)

(9, Pat) (7, Sarah)

Page 9: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

9

insert()

• We must keep track of the next spot where we will insert to keep the tree left-complete: the “insertion node”

Tuesday, February 24, 2015

2

65

79

insertion node

Page 10: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

10

2

65

79 1

insert() (2)

• Example: insert(1)• Put the new node where the insertion node was

Tuesday, February 24, 2015

Page 11: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

11

insert() (3)

• But now heap order is violated

Tuesday, February 24, 2015

2

65

79 1

Page 12: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

12

Upheap• Repair heap order by swapping the new item up the tree until all keys are properly sorted

• The first swap fixes everything underneath the new location• Note that since all keys beneath the 6’s old location must have been greater or equal to 6, they must be greater than or equal to 1

Tuesday, February 24, 2015

2

6

5

79

1

Page 13: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

13

Upheap (2)

• One more swap, because the 1 is less than the 2 on top

• Now both the completeness and the heap order properties are satisfied

Tuesday, February 24, 2015

25

79

1

6

Page 14: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

14

Upheap (3)

• After the insertion of a new key k, the heap order property may be violated

• The upheap algorithm restores the order by swapping k along an upward path from the insertion node

• Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k

• Since a heap has height O(log n), upheap runs in O(log n) time

Tuesday, February 24, 2015

Page 15: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

15

Upheap (4)

Tuesday, February 24, 2015

1

25

79 6

2

15

79 6

Page 16: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

16

removeMin()

• The minimum element of the heap is always the root due to heap order

• How do we remove the root of the heap without destroying the ordering?

Tuesday, February 24, 2015

2

65

79

65

79BAD

Page 17: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

17

removeMin() (2)

• Instead swap the root (which we want to remove) with the last item in the heap• Removing from the last position is easy

• But now heap order isn’t preserved…

Tuesday, February 24, 2015

2

65

79

GOOD 65

7

9

7

65

29

GOOD

Page 18: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

18

Downheap

• Swap the root down as necessary

• Now the heap is in order• What if 7 were a 20?

Tuesday, February 24, 2015

GOOD

65

7

9

6

5

7

9

Page 19: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

19

Downheap (2)

• Downheap restores the heap order property by swapping key k along a downward path from the root, swapping with the lesser of the two children

• Downheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k

• Since a heap has height O(log n), downheap runs in O(log n) time

Tuesday, February 24, 2015

Page 20: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

20

Heap Implementation Recap

•insert():• Insert item into the “insertion node,” the next available spot (which you must keep track of)

• Upheap from the bottom as necessary

•removeMin():• Swap root with last item in heap• Delete swapped root• Downheap from the root as necessary

Tuesday, February 24, 2015

Page 21: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

21

Finding the Insertion Node

• The insertion node can be found by traversing a path of O(log n) nodes• Start with the last added node• Go up until a left child or the root is reached• If a left child is reached, go to its sibling (the corresponding

right child)• Go down left until a leaf is reached

Tuesday, February 24, 2015

Page 22: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

22

Finding the Insertion Node (2)

• This is O(log n) but it can also be done in constant time using an additional data structure.

• Think about it – you’ll want to do it for your next project.

Tuesday, February 24, 2015

Page 23: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

23

Array-based Implementation• We can represent a heap with n

keys by means of an array of length n + 1

• Implementation• For the node at index i

• the left child is at index 2i• the right child is at index 2i + 1

• Leaves and edges (links between nodes) are not represented

• The cell at index 0 is not used

• Operations• insert corresponds to inserting at

index n + 1• removeMin corresponds to swap with

index n and remove

Tuesday, February 24, 2015

2

65

79

2 5 6 9 7

1 2 3 4 50

Page 24: PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1

24

Priority Queue Analysis Recap

Tuesday, February 24, 2015

Implementation add removeMin

Unsorted Array O(1) O(n)Sorted Array O(n) O(1)Unsorted Linked List O(1) O(n)Sorted Linked List O(n) O(1)Hash Table O(1) O(n)Heap O(log n) O(log n)

SUBLINEAR == AWESOME