CS2_31_Heaps - CS2 Module 31 Category: CS Concepts Topic:...

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

View Full Document Right Arrow Icon
CS2 Module 31 Category: CS Concepts Topic: Heaps Objectives Heaps Priority Queues Heapsort Revised to use Vector 10/27/2001
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS 2 Introduction to Object Oriented Programming Module 31 CS Concepts Heaps, Priority Queues, Heapsort
Background image of page 2
Priority Queues In a number of applications a data structure which can produce the smallest item in a collection is useful. Obviously any sorted list can do the job. However, we may wish to have a data structure with the ability to add items at any time and remove the smallest item at any time In the interest of efficiency a novel data structure can be utilized to produce good results. But first, let's be clear on exactly what is our target application
Background image of page 3

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

View Full DocumentRight Arrow Icon
Imagine a bakery It’s the kind where when you enter you take a number. Only they don’t have one of those “Now Serving” gadgets. And they don’t mind if you leave and come back. The rule is simple: Lowest number (in the bakery) gets served next. How do you determine who has the smallest number?
Background image of page 4
007 Or take, for example, a queue of Qs: (That’s Q from the James Bond movies)
Background image of page 5

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

View Full DocumentRight Arrow Icon
008 Perhaps we need to find the smallest in this queue of Qs.
Background image of page 6
Alternatives We might search for the smallest one: Or we might keep them in order:
Background image of page 7

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

View Full DocumentRight Arrow Icon
Priority Queues A better example is a queue of documents waiting to be printed. Perhaps we wish to find the smallest document, and print it first. This is known as the  Shortest Job First  scheduling algorithm
Background image of page 8
Priority Queues Take for example a printer queue. The small jobs take less time to print. So it’s a better policy to print them first 90 mins. 20 mins. 5 mins. 5 secs. Simply printing ‘in order’ can cause bottle necks – maybe we need to beef up the throughput!
Background image of page 9

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

View Full DocumentRight Arrow Icon
Sound Unlikely? So, accept for the moment that we must create a data structure that has the following property: Finding or removing the Finding or removing the smallest element is fast smallest element is fast Now, exactly how might we implement this?
Background image of page 10
Poor Technique #1 Plain Linked List We might start with a simple linked list, and keep the list unsorted. 1 1 1 2 2 2 Insertion is at the head of the list, ( O (1) ). Deletion* requires us to search for the smallest, ( O (N) ). We could also adopt some rules to make the list fast: So insertion is fast, and deletion is slow * Deletion is what we do when we remove the smallest element
Background image of page 11

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

View Full DocumentRight Arrow Icon
Poor Technique #2 We can tinker with our previous solution to try and make it faster. Let’s sort the elements on insertion. 1 1 2 2 2 Insertion is in order ( O (N) ) Deletion simple removes the smallest, ( O (1) ) So deletion is fast, and insertion is slow There’s a tradeoff between insertion and deletion speed
Background image of page 12
Better Technique #3 A linked list either required O(N) for either insertion of deletion. Could we instead use a BST to get some log(N) behavior for either of the activities?
Background image of page 13

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

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/23/2012 for the course CS 1371 taught by Professor Stallworth during the Fall '08 term at Georgia Institute of Technology.

Page1 / 143

CS2_31_Heaps - CS2 Module 31 Category: CS Concepts Topic:...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online