1 / 29

CS 221

CS 221. Analysis of Algorithms Data Structures Priority Queues and Heaps. Portions of the following slides are from Goodrich and Tamassia, Algorithm Design: Foundations, Analysis and Internet Examples, 2002 and

odetta
Download Presentation

CS 221

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 221 Analysis of Algorithms Data Structures Priority Queues and Heaps

  2. Portions of the following slides are from • Goodrich and Tamassia, Algorithm Design: Foundations, Analysis and Internet Examples, 2002 • and • Material provided by the publisher’s John Wiley & Son, Inc.) companion website for this book

  3. Priority queues • Remember queues and stacks • in both the removal of an element was based on the order it was placed on the structure… • … but what if removal was based on other criteria

  4. Priority queues • Priority queue • removal based on priority value rather than… • the order it was placed on the structure, or… • even the value of the element (maybe)

  5. Priority queues • Key • each element to be place in a priority queue is a two part element • the element value • key – a value associated with the element value • Think of a take-a-number ticket for counter service (like at DMV) (really a FIFO queue, right?) • You are the element value • Your ticket number is your key

  6. Priority queues • Key element Key ev Key ev Key ev Key ev Key ev Key ev Key ev

  7. A priority queue stores a collection of items An item is a pair(key, element) Main methods of the Priority Queue ADT insertItem(k, o)inserts an item with key k and element o removeMin()removes the item with smallest key and returns its element Priority queue ADT • Additional methods • minKey()returns, but does not remove, the smallest key of an item • minElement()returns, but does not remove, the element of an item with smallest key • size(), isEmpty() • Applications: • Standby flyers • Auctions • Stock market

  8. Priority queues • Need two more things • Total Order Relation • Comparator • to realize the Total Order Relation

  9. Keys in a priority queue can be arbitrary objects on which an order is defined Two distinct items in a priority queue can have the same key Total Order Relation • Mathematical concept of total order relation  • Reflexive property:x  x • Antisymmetric property:x  yy  x  x = y • Transitive property:x  yy  z  x  z

  10. Comparator • A comparator encapsulates the action of comparing two objects according to a given total order relation • A generic priority queue uses an auxiliary comparator • The comparator is external to the keys being compared • When the priority queue needs to compare two keys, it uses its comparator • Methods of the Comparator ADT, all with Boolean return type • isLessThan(x, y) • isLessThanOrEqualTo(x,y) • isEqualTo(x,y) • isGreaterThan(x, y) • isGreaterThanOrEqualTo(x,y) • isComparable(x)

  11. Priority Queue with an unordered sequence • How to implement? • vector? • linked list?

  12. Priority Queue with an unordered sequence • Selection-Sort • Input: unordered sequence • Output: ordered sequence • Insert element at end of queue (first phase) • O(?) • remove elements in key order (second phase) • Run-time? • In other words- • How many steps does it take to execute removeMin(P) to remove the smallest element value? • How many times will you run removeMin(P)?

  13. Priority Queue with an unordered sequence • Selection-Sort • First phase O(1) • Second phase • O(n+(n-1)+(n-2)+…2+1) or • O(Σi, i=1,n) • or O(n(n+1)/2 • so, • O(n2)

  14. Priority Queue with an ordered sequence • Insertion-Sort • Input: unordered sequence • Output: ordered sequence • Insert element in key order position • O(?) • remove elements in key order (second phase) • Run-time? • In other words- • How many steps does it take to execute removeMin(P) to remove the smallest element value? • How many times will you run removeMin(P)?

  15. Priority Queue with an unordered sequence • Selection-Sort • First phase O(1) • O(n+(n-1)+(n-2)+…2+1) or • O(Σi, i=1,n) • or O(n(n+1)/2 • so, • O(n2) • Second phase • O(1)

  16. Heaps • Remember Trees • especially binary tree • we like trees, right? • Why?

  17. Heaps • Imagine a Priority queue but • rather than linear vector or list • implemented as a tree • that is called a heap

  18. Heaps • We still need • Total Order Relation • Comparators • We need a few more things • Heap-Order Property • for every node v (except root) the key of v k(v) is greater than or equal to the key of its parent node • Complete Binary Tree – binary tree of height h where all levels (0…h-1) have the maximum number of nodes

  19. Heaps • Complete Binary Tree – what? • leaf (external nodes) hold no elements • just placeholders • all element values are in internal nodes • Now we need to keep track of the lastnode() • The last node of a heap is the rightmost internal node of depth h- 1

  20. Heaps 2 5 6 9 7 last node

  21. Heaps • So, let’s w=lastnode() • z = insertion point in heap • first external node to the right of w • this is where we will always add new elements (k,e) 2 5 6 9 7 w z

  22. Heaps • Heap depth and number of keys depth keys 0 1 1 2 h-2 2h-2 h-1 1

  23. Heaps and Priority Queues • We can use a heap to implement a priority queue • We store a (key, element) item at each internal node • We keep track of the position of the last node • For simplicity, we show only the keys in the pictures (2, Sue) (5, Pat) (6, Mark) (9, Jeff) (7, Anna)

  24. Method insertItem of the priority queue ADT corresponds to the insertion of a key k to the heap The insertion algorithm consists of three steps Find the insertion node z (the new last node) Store k at z and expand z into an internal node Restore the heap-order property (discussed next) 2 5 6 9 7 Insertion into a Heap (§2.4.3) z insertion node 2 5 6 z 9 7 1

  25. Insertion - Upheap Bubbling 2 5 6 z 9 7 1 2 1 5 1 5 2 z z 9 7 6 9 7 6

  26. removeMin() from heap

  27. 2 5 6 9 7 Removal from a Heap (§2.4.3) • Method removeMin of the priority queue ADT corresponds to the removal of the root key from the heap • The removal algorithm consists of three steps • Replace the root key with the key of the last node w • Compress w and its children into a leaf • Restore the heap-order property (discussed next) w last node 7 5 6 w 9

  28. 2 5 6 9 7 5 7 6 w 9 Downheap Bubbling 7 5 6 w 9 7 5 6 w 9

More Related