Presentation is loading. Please wait.

Presentation is loading. Please wait.

Priority Queues (Chapter 6):

Similar presentations


Presentation on theme: "Priority Queues (Chapter 6):"— Presentation transcript:

1 Priority Queues (Chapter 6):
Abstract model of Priority Queues Priority Queue Get the one with the highest priority insert the minimum one (or equivalently, maximum) a black box 5/5/2019 IT 279

2 Let’s cleaning up some confusion.
Is Binary Search Tree a priority queue? Binary Search Tree Get the one with the highest priority insert No! It is not, but nothing wrong to use it in the black box. the minimum one (or equivalently, maximum) 5/5/2019 IT 279

3 Items with higher priority will be treated first.
Stacks (FILO), queues (FIFO) are Priority Queues Arrays are not, since we can randomly access the items in an array The priority of the item may be fixed or changed over the time. 5/5/2019 IT 279

4 Using a Binary Search Tree is overkilled. Why?
17 Get the one with the highest priority insert 14 19 12 15 18 11 13 the minimum one (or equivalently, maximum) Items are totally ordered, but we don’t really this information (nothing comes free) 5/5/2019 IT 279

5 Our goals: 1. Can get the minimum one efficiently
2. and low maintenance 11 9 16 12 11 12 Get the minimum one 9 13 insert 13 12 14 17 15 19 18 13 14 17 15 19 18 16 14 17 15 19 18 Binary Heap 5/5/2019 IT 279

6 Binary Heap Using (abstractly) binary tree structure
For every node in the tree, its key is smaller than (or equal to) the keys of its two children. Therefore, for every node in the tree, its data is the smallest one among data stored in the sub-trees (the two children). Different from BST, not every key in the left-sub-tree is less than every key in the right-sub-tree. Loosing this requirement, we gain some benefits. 5/5/2019 IT 279

7 A Binary Heap but we can organize it in a better way
without too much extra cost. 1 4 6 5 9 23 10 25 8 14 11 25 12 29 28 12 17 20 15 22 29 30 19 36 13 29 23 30 37 5/5/2019 IT 279

8 A Binary Heap in complete binary tree
10 12 13 15 14 16 17 23 22 21 18 19 24 25 26 27 32 29 30 25 26 37 pack 5/5/2019 IT 279

9 A Binary Heap implemented in an array
10 1 12 13 2 3 15 14 16 17 4 5 6 7 23 22 21 28 39 24 25 42 10 11 15 8 9 12 13 14 27 32 29 40 35 36 37 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1st child (left-child) of i is *i 2nd child (right-child) of i is 2*i+1 The parent of i is i / 2 double the size of the array if the space ran out 5/5/2019 IT 279

10 Heap operation: insert an item
11 10 12 13 15 14 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 37 11 5/5/2019 IT 279

11 Heap operation: insert an item
11 10 12 13 15 14 16 17 23 22 21 39 24 25 42 11 27 32 29 40 35 36 37 28 5/5/2019 IT 279

12 Heap operation: insert an item
11 10 12 13 15 11 16 17 23 22 21 39 24 25 42 14 27 32 29 40 35 36 37 28 5/5/2019 IT 279

13 Heap operation: insert an item
11 10 11 13 15 12 16 17 23 22 21 39 24 25 42 14 27 32 29 40 35 36 37 28 5/5/2019 IT 279

14 Heap operation: delete the minimum
10 12 13 15 14 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 37 5/5/2019 IT 279

15 Heap operation: delete the minimum
10 12 13 15 14 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 37 5/5/2019 IT 279

16 Heap operation: delete the minimum
10 37 12 13 15 14 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 5/5/2019 IT 279

17 Heap operation: delete the minimum
10 12 37 13 15 14 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 5/5/2019 IT 279

18 Heap operation: delete the minimum
10 12 14 13 15 37 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 5/5/2019 IT 279

19 Heap operation: delete the minimum
10 12 14 13 15 16 21 17 23 22 37 28 39 24 25 42 27 32 29 40 35 36 5/5/2019 IT 279

20 Heap operation: delete the minimum
10 12 14 13 15 16 21 17 23 22 35 28 39 24 25 42 27 32 29 40 37 36 5/5/2019 IT 279

21 Time complexity for insertion
best case: O(1) worst case: O(log n) 14 log n 15 21 23 22 37 25 27 32 29 40 35 36 30 27 average case: 5/5/2019 IT 279

22 Time complexity of Building a heap
Insert them one by one into the empty heap What is the cost? best case: O(n) average case: O(n) worst case: O(n log n) Can we improve the worst case? 34 15 30 2 23 9 25 6 32 5 40 11 25 21 7 5/5/2019 IT 279

23 Percolating a non-heap
Worst case: O(n) 34 2 15 2 5 30 7 7 2 15 6 5 23 9 25 7 30 21 6 23 9 21 15 6 32 5 23 40 11 31 30 21 7 25 15 32 34 40 11 31 30 25 Binary Heap 5/5/2019 IT 279

24 Percolating a non-heap
Worst case: O(S) S = Sum of heights of all nodes h O(S) = O(n) 5/5/2019 IT 279


Download ppt "Priority Queues (Chapter 6):"

Similar presentations


Ads by Google