priority queues
play

Priority queues Hash tables chaining Priority queue ADT Binary - PowerPoint PPT Presentation

Priority queues Hash tables chaining Priority queue ADT Binary heap March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1 Separate chaining Separate chaining takes a different approach to collisions Each entry in the hash table


  1. Priority queues Hash tables – chaining Priority queue ADT Binary heap March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1

  2. Separate chaining • Separate chaining takes a different approach to collisions • Each entry in the hash table is a pointer to a linked list (or other dictionary-compatible data structure) – If a collision occurs the new item is added to the end of the list at the appropriate location • Performance degrades less rapidly using separate chaining – with uniform random distribution, separate chaining maintains good performance even at high load factors 𝜇 > 1 March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2

  3. Separate chaining example • ℎ 𝑦 = 𝑦 mod 23 • Insert 81, ℎ(𝑦) = 12 , add to back (or front) of list • Insert 35, ℎ(𝑦) = 12 • Insert 60, ℎ(𝑦) = 14 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 29 32 58 60 21 81 35 March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 3

  4. Hash table discussion • If 𝜇 is less than ½, open addressing and separate chaining give similar performance – As 𝜇 increases, separate chaining performs better than open addressing – However, separate chaining increases storage overhead for the linked list pointers • It is important to note that in the worst case hash table performance can be poor – That is, if the hash function does not evenly distribute data across the table March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 4

  5. Priority queue Priority queue ADT Binary heap March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 5

  6. Priority queues? • Suppose Geoff has made a to-do list (with priority values): 6 – HW2 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

  7. 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

  8. Priority queue ADT • Priority queue operations – create – destroy – insert – removeMin (or removeMax) E(5) F(7) – isEmpty removeMax insert C(3) D(100) G(9) D(100) G(9) B(6) A(4) • Priority queue property – For two elements 𝑦 and 𝑧 in the queue, if 𝑦 has a higher priority value than 𝑧 , 𝑦 will be removed before 𝑧 – Note that in most definitions, a single priority queue structure supports only removeMin , or only removeMax , and not both March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 8

  9. 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

  10. 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

  11. Data structures for priority queues • Worst case complexities Structure insert removeMin 𝑃 1 𝑃 𝑜 Unordered array 𝑃 𝑜 𝑃 𝑜 Ordered array 𝑃 1 𝑃 𝑜 Unordered list 𝑃 𝑜 𝑃 1 Ordered list 𝑃 𝑜 𝑃 𝑜 Binary search tree 𝑃 log 𝑜 𝑃 log 𝑜 AVL tree 𝑃 log 𝑜 𝑃 log 𝑜 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

  12. 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

  13. Review: complete binary trees complete binary trees incomplete binary trees March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 13

  14. Partially ordered tree • max heap example 98 86 41 13 65 32 29 9 10 44 23 21 32 Heaps are not fully ordered – an in-order traversal would result in: 9, 13, 10, 86, 44, 65, 23, 98, 21, 32, 32, 41, 29 March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 14

  15. 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 7 5 5 5 7 8 7 7 8 March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 15

  16. Heap implementation Using an array • Heaps can be implemented using arrays 1 • There is a natural method of indexing tree nodes – Index nodes from top to bottom 2 3 and left to right as shown on the right – Because heaps are complete binary 4 5 6 7 trees there can be no gaps in the array (except index 0) 1 3 5 .. 0 2 4 6 . March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 16

  17. Referencing nodes • To move around in the tree, 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 1 to 𝑜 • Each level’s nodes are indexed from: • 2 𝑚𝑓𝑤𝑓𝑚 to 2 𝑚𝑓𝑤𝑓𝑚+1 − 1 (where the root is level 0) – The children of a node 𝑗 , are the array elements indexed at 2𝑗 and 2𝑗 + 1 – The parent of a node 𝑗 , is the array element indexed at 𝑗 2 March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 17

  18. Array heap example 1 98 2 3 Heap 86 41 4 5 6 7 13 65 32 29 8 9 10 11 12 13 9 10 44 23 21 32 Underlying array value 98 86 41 13 65 32 29 9 10 44 23 21 32 2 4 5 6 8 10 12 index 1 3 7 9 11 13 March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 18

  19. Heap implementation class MinHeap { private: int size; // number of stored elements int capacity; // maximum capacity of array int* arr; // array in dynamic memory public: ... }; MinHeap::MinHeap(int initcapacity) { size = 0; capacity = initcapacity; arr = new int[capacity+1]; } March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 19

  20. 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

  21. Heap insertion example • max heap Insert 81 98 86 41 13 65 32 29 9 10 44 23 21 32 value 98 86 41 13 65 32 29 9 10 44 23 21 32 2 4 6 8 10 12 14 index 1 3 5 7 9 11 13 March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 21

  22. Heap insertion example • max heap Insert 81 98 86 41 13 65 32 81 29 9 10 44 23 21 32 81 29 14 2 = 7 parent: value 98 86 41 13 65 32 29 81 9 10 44 23 21 32 81 29 2 4 6 8 10 12 14 index 1 3 5 7 9 11 13 March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 22

  23. Heap insertion example • max heap 81 is less than 98 so finished Insert 81 98 86 41 81 13 65 32 41 81 9 10 44 23 21 32 29 parent: 7 2 = 3 value 98 86 81 41 13 65 32 41 81 9 10 44 23 21 32 29 2 4 6 8 10 12 14 index 1 3 5 7 9 11 13 March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 23

  24. Heap insertion complexity • Item is inserted at the bottom level in the first available space – this can be tracked using the heap size attribute – 𝑃 1 access using array index • Repeated heapify-up operations. How many? – each heapify-up operation moves the inserted value up one level in the tree – Upper limit on the number of levels in a complete tree? 𝑃 log 𝑜 • Heap insertion has worst-case performance of 𝑃 log 𝑜 March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 24

  25. Building a heap (an heap?) • A heap can be constructed by repeatedly inserting items into an empty heap. Complexity? – 𝑃 log 𝑜 per item, 𝑃 𝑜 items – 𝑃 𝑜 log 𝑜 total • More about this next class March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 25

Recommend


More recommend