Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1."— Presentation transcript:

1 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 2

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 3

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 4

5 Priority Queue Implementations Tuesday, February 24, 2015 5 Implementation addremoveMin 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)

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 6 2 65 79

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 7 2 65 79

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 8 (2, Nick) (6, David)(5, Marley) (9, Pat)(7, Sarah)

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 9 2 65 79 insertion node

10 2 65 79 1 insert() (2) Example: insert(1) Put the new node where the insertion node was Tuesday, February 24, 2015 10

11 insert() (3) But now heap order is violated Tuesday, February 24, 2015 11 2 65 79 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 12 2 6 5 79 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 13 2 5 79 1 6

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 14

15 Upheap (4) Tuesday, February 24, 2015 15 1 2 5 79 6 2 1 5 79 6

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 16 2 65 79 65 7 9 BAD

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 17 2 65 79 GOOD 65 7 9 7 65 2 9

18 Downheap Swap the root down as necessary Now the heap is in order What if 7 were a 20? Tuesday, February 24, 2015 18 GOOD 65 7 9 6 5 7 9

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 19

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 20

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 21

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 22

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 23 2 65 79 25697 123450

24 Priority Queue Analysis Recap Tuesday, February 24, 2015 24 Implementation addremoveMin 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) SUBLINEAR == AWESOME


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

Similar presentations


Ads by Google