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

Info icon This 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)
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.
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern