Priority queues Hash tables chaining Priority queue ADT
Priority queues Hash tables – chaining Priority queue ADT Binary heap March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1
Separate chaining • March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2
Separate chaining example • 0 1 2 3 4 5 6 29 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 32 58 60 21 81 March 13, 2020 Cinda Heeren / Andy Roth 35 / Geoffrey Tien 3
Hash table discussion • March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 4
Priority queue ADT Binary heap March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 5
Priority queues? • Suppose Geoff has made a to-do list (with priority values): 6 – HW 2 regrades 2 – Vacuum home 8 – Renew car insurance 1 – Sleep 9 – Extinguish computer on fire 2 – Eat 8 – Lecture prep 1 – Bathe • We are interested in quickly finding the task with the highest priority March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 6
Priority queue ADT • A collection organised so as to allow fast access to and removal of the largest (or smallest) element – Prioritisation is a weaker condition than ordering – Order of insertion is irrelevant – Element with the highest priority (whatever that means) is the first element to be removed • Not really a queue: not FIFO! March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 7
Priority queue ADT • Priority queue operations – – – create destroy insert remove. Min (or remove. Max) is. Empty insert G(9) E(5) F(7) D(100) G(9) A(4) March 13, 2020 remove. Max C(3) Cinda Heeren / Andy Roth / Geoffrey Tien D(100) B(6) 8
Priority queue properties • A priority queue is an ADT that maintains a multiset of items – vs set: a multiset allows duplicate entries • Two or more distinct items in a priority queue may have the same priority • If all items have the same priority, will it behave FIFO like a queue? – not necessarily! Due to implementation details March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 9
Priority queue applications • Hold jobs for a printer in order of size • Manage limited resources such as bandwidth on a transmission line from a network router • Sort numbers • Anything greedy: an algorithm that makes the "locally best choice" at each step March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 10
Data structures for priority queues • Worst case complexities Structure Unordered array Ordered array Unordered list insert remove. Min Ordered list Binary search tree AVL tree Binary heap Heap has asymptotically same performance as AVL tree, but MUCH simpler to implement March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 11
Binary heap A complete, partially-ordered binary tree • A heap is binary tree with two properties • Heaps are complete – All levels, except the bottom, must be completely filled in – The leaves on the bottom level are as far to the left as possible • Heaps are partially ordered – For a max heap – the value of a node is at least as large as its children’s values – For a min heap – the value of a node is no greater than its children’s values March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 12
Review: complete binary trees incomplete binary trees March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 13
Partially ordered tree • max heap example 98 86 41 13 9 65 10 44 32 23 21 29 32 Heaps are not fully ordered – an in-order traversal would result in: 9, 13, 10, 86, 44, 65, 23, 98, 21, 32, 41, 29 March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 14
Duplicate priority values Min heap examples • It is important to realise that two binary heaps can contain the same data, but items may appear in different positions in the structure 2 2 5 5 March 13, 2020 7 7 8 5 7 Cinda Heeren / Andy Roth / Geoffrey Tien 5 7 8 15
Heap implementation Using an array • Heaps can be implemented using arrays • There is a natural method of indexing tree nodes 1 – 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 array (except index 0) 0 March 13, 2020 2 4 1 2 Cinda Heeren / Andy Roth / Geoffrey Tien 3 3 5 4 5 6 6 7 . . . 16
Referencing nodes • March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 17
Array heap example 98 1 86 2 Heap 13 4 9 8 41 3 65 5 10 9 44 10 32 6 23 11 21 12 29 7 32 13 Underlying array value 98 86 41 13 65 32 29 9 10 44 index 1 2 3 4 5 6 7 8 9 10 11 March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 23 21 32 12 13 18
Heap implementation class Min. Heap { private: int size; // number of stored elements int capacity; // maximum capacity of array int* arr; // array in dynamic memory public: . . . }; Min. Heap: : Min. Heap(int initcapacity) { size = 0; capacity = initcapacity; arr = new int[capacity+1]; } March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 19
Heap insertion • On insertion the heap properties have to be maintained – A heap is a complete binary tree and – A partially ordered binary tree • The insertion algorithm first ensures that the tree is complete – new item is 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 heapify up, percolate up, or trickle up, bubble up, etc. March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 20
Heap insertion example • max heap Insert 81 98 86 41 13 9 65 10 44 32 23 21 29 32 value 98 86 41 13 65 32 29 9 10 44 index 1 2 3 4 5 6 7 8 9 10 11 March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 23 21 32 12 13 14 21
Heap insertion example • max heap Insert 81 98 86 41 13 9 65 10 44 32 23 21 81 29 32 81 29 value 98 86 41 13 65 32 29 81 9 10 44 index 1 2 3 4 5 6 7 8 9 10 11 March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 23 21 32 81 29 12 13 14 22
Heap insertion example • max heap Insert 81 81 is less than 98 so finished 98 86 41 81 13 9 65 10 44 32 23 21 81 41 32 29 value 98 86 41 81 13 65 32 81 41 9 10 44 index 1 2 3 4 5 6 7 8 9 10 11 March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 23 21 32 29 12 13 14 23
Heap insertion complexity • March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 24
Building a heap (an heap? ) • March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 25
Readings for this lesson • Carrano & Henry – Chapter 13. 3 (ADT priority queue) – Chapter 17. 1 – 17. 3 (Heap) • Next class: – Carrano & Henry, Chapter 17. 4 (Heapsort) March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 26
- Slides: 26