priority queue
play

Priority queue Binary heap March 06, 2019 Cinda Heeren / Will - PowerPoint PPT Presentation

Priority queue Binary heap March 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1 REMINDER Section 201 midterm is in WOOD 2 19:00 21:00 March 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 2 Priority queues?


  1. Priority queue Binary heap March 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1

  2. REMINDER • Section 201 – midterm is in WOOD 2 – 19:00 – 21:00 March 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 2

  3. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 3

  4. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 4

  5. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 5

  6. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 6

  7. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 7

  8. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 8

  9. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 9

  10. Review: complete binary trees complete binary trees incomplete binary trees March 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 10

  11. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 11

  12. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 12

  13. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 13

  14. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 14

  15. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 15

  16. 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]; } March 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 16

  17. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 17

  18. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 18

  19. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 19

  20. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 20

  21. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 21

  22. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 22

  23. 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 06, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 23

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend