290 likes | 453 Views
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
E N D
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 • Material provided by the publisher’s John Wiley & Son, Inc.) companion website for this book
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
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)
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
Priority queues • Key element Key ev Key ev Key ev Key ev Key ev Key ev Key ev
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
Priority queues • Need two more things • Total Order Relation • Comparator • to realize the Total Order Relation
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 yy x x = y • Transitive property:x yy z x z
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)
Priority Queue with an unordered sequence • How to implement? • vector? • linked list?
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)?
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)
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)?
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)
Heaps • Remember Trees • especially binary tree • we like trees, right? • Why?
Heaps • Imagine a Priority queue but • rather than linear vector or list • implemented as a tree • that is called a heap
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
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
Heaps 2 5 6 9 7 last node
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
Heaps • Heap depth and number of keys depth keys 0 1 1 2 h-2 2h-2 h-1 1
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)
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
Insertion - Upheap Bubbling 2 5 6 z 9 7 1 2 1 5 1 5 2 z z 9 7 6 9 7 6
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
2 5 6 9 7 5 7 6 w 9 Downheap Bubbling 7 5 6 w 9 7 5 6 w 9