{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

notes_COSqueues - Algorithms R OBERT S EDGEWICK | K EVIN W...

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

View Full Document Right Arrow Icon
R OBERT S EDGEWICK | K EVIN W AYNE F O U R T H E D I T I O N Algorithms http://algs4.cs.princeton.edu Algorithms R OBERT S EDGEWICK | K EVIN W AYNE 2.4 P RIORITY Q UEUES API and elementary implementations binary heaps heapsort event-driven simulation http://algs4.cs.princeton.edu R OBERT S EDGEWICK | K EVIN W AYNE Algorithms API and elementary implementations binary heaps heapsort event-driven simulation 2.4 P RIORITY Q UEUES 3 Priority queue Collections. Insert and delete items. Which item to delete? Stack. Remove the item most recently added. Queue. Remove the item least recently added. Randomized queue. Remove a random item. Priority queue. Remove the largest (or smallest ) item. P Q E Q X A M X P L E P insert insert insert remove max insert insert insert remove max insert insert insert remove max operation argument return value 4 Priority queue API Requirement. Generic items are Comparable . public class MaxPQ <Key extends Comparabl le<Key>> MaxPQ() create an empty priority queue MaxPQ(Key[] a) create a priority queue with given keys void insert(Key v) insert a key into the priority queue Key delMax() return and remove the largest key boolean isEmpty() is the priority queue empty? Key max() return the largest key int size() number of entries in the priority queue Key must be Comparable (bounded type parameter)
Background image of page 1

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

View Full Document Right Arrow Icon
5 Priority queue 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] 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. Challenge. Find the largest M items in a stream of N items. Fraud detection: isolate $$ transactions. File maintenance: find biggest files or directories. Constraint. Not enough memory to store N items. 6 Priority queue client example % more tinyBatch.txt Turing 6/17/1990 644.08 vonNeumann 3/26/2002 4121.85 Dijkstra 8/22/2007 2678.40 vonNeumann 1/11/1999 4409.74 Dijkstra 11/18/1995 837.42 Hoare 5/10/1993 3229.27 vonNeumann 2/12/1994 4732.35 Hoare 8/18/1992 4381.21 Turing 1/11/2002 66.10 Thompson 2/27/2000 4747.08 Turing 2/11/1991 2156.86 Hoare 8/12/2003 1025.70 vonNeumann 10/13/1993 2520.97 Dijkstra 9/10/2000 708.95 Turing 10/12/1993 3532.36 Hoare 2/10/2005 4050.20 % java TopM 5 < tinyBatch.txt Thompson 2/27/2000 4747.08 vonNeumann 2/12/1994 4732.35 vonNeumann 1/11/1999 4409.74 Hoare 8/18/1992 4381.21 vonNeumann 3/26/2002 4121.85 sort key N huge, M large Challenge. Find the largest M items in a stream of N items. Fraud detection: isolate $$ transactions. File maintenance: find biggest files or directories. Constraint. Not enough memory to store N items. 7 Priority queue client example N huge, M large MinPQ <Transaction> pq = new MinPQ< Transaction >(); while (StdIn.hasNextLine()) { String line = StdIn.readLine(); Transaction item = new Transaction(line); pq.insert(item); if (pq.size() > M) pq.delMin(); } pq contains largest M items use a min-oriented pq Transaction data type is Comparable (ordered by $$) Challenge.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}