This preview shows pages 1–3. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Robert Sedgewick and Kevin Wayne • Copyright © 2005 • http://www.Princeton.EDU/~cos226 Priority Queues Reference: Chapter 6, Algorithms in Java, 3 rd Edition, Robert Sedgewick. 2 Priority Queues Data. Items that can be compared. Basic operations. ! Insert. ! Remove largest. ! Copy. ! Create. ! Destroy. ! Test if empty. defining ops generic ops 3 Priority Queue Applications Applications. ! Event-driven simulation. [customers in a line, colliding particles] ! Numerical computation. [reducing roundoff error] ! Data compression. [Huffman codes] ! Graph searching. [Dijkstra's algorithm, Prim's algorithm] ! Computational number theory. [sum of powers] ! Artificial intelligence. [A* search] ! Statistics. [maintain largest M values in a sequence] ! Operating systems. [load balancing, interrupt handling] ! Discrete optimization. [bin packing, scheduling] ! Spam filtering. [Bayesian spam filter] Generalizes: stack, queue, randomized queue. 4 Priority Queue Client Example Problem: Find the largest M of a stream of N elements. ! Fraud detection: isolate \$\$ transactions. ! File maintenance: find biggest files or directories. Constraint. Not enough memory to store N elements. Solution. Use a priority queue. MinPQ < String > pq = new MinPQ < String >(); while (! StdIn . isEmpty ()) { String s = StdIn . readString (); pq . insert ( s ); if ( pq . size () > M ) pq . delMin (); } while (! pq . isEmpty ()) System . out . println ( pq . delMin ()); sort Operation elementary PQ binary heap best in theory N space M M M N lg N time M N N lg M N 5 Priority Queue: Elementary Implementations Two elementary implementations. Challenge. Implement both operations efficiently. unordered array Implementation ordered array N Del Max 1 1 Insert N worst-case asymptotic costs for PQ with N items 6 Priority Queue: Unordered Array Implementation public class UnorderedPQ < Item extends Comparable > { private Item pq ; // pq[i] = ith element on PQ private int N ; // number of elements on PQ public UnorderedPQ ( int maxN ) { pq = ( Item ) new Comparable [ maxN ]; } public boolean isEmpty () { return N == ; } public void insert ( Item x ) { pq [ N ++] = x ; } public Item delMax () { int max = ; for ( int i = 1 ; i < N ; i ++) if ( less ( max , i )) max = i ; exch ( max , N- 1 ); return pq [-- N ]; } } no generic array creation 9 Binary Heap Heap: Array representation of a heap-ordered complete binary tree. Binary tree. ! Empty or ! Node with links to left and right trees. Heap-ordered binary tree. ! Keys in nodes. ! No smaller than children’s keys. Array representation. ! Take nodes in level order. ! No explicit links needed since tree is complete. 12 Binary Heap Properties Property A. Largest key is at root....
View Full Document

{[ snackBarMessage ]}