394 chapter 15 priorit y queues chapter ov erview a
play

394 Chapter 15 Priorit y Queues Chapter Ov erview A - PDF document

394 Chapter 15 Priorit y Queues Chapter Ov erview A priority queue is a data structure useful in problems where it is imp ortan t to b e able to rapidly and rep eatedly nd and remo v e the largest elemen


  1. 394

  2. Chapter 15 Priorit y Queues Chapter Ov erview A priority queue is a data structure useful in problems where it is imp ortan t to b e able to rapidly and rep eatedly �nd and remo v e the largest elemen t from a collection of v alues. In this c hapter w e will presen t t w o di�eren t implemen tations of priorit y queues. The �rst tec hnique uses an abstraction called a he ap , and is constructed as an adaptor built on top another form of con tainer, t ypically a v ector or deque. The heap data structure is then used to demonstrate y et another approac h to sorting a collection of v alues. The second implemen tation strategy is the skew he ap . The sk ew heap is notable in that it do es not pro vide guaran teed p erformance b ounds for an y single op eration, but it can b e sho wn that if a n um b er of op erations are p erformed o v er time the a v erage execution time of op erations will b e small. As a demonstration one of the more common uses of heap, the c hapter concludes with a discussion of discrete ev en t driv en sim ulation. This topic is approac hed b y �rst dev eloping a general fr amework for sim ulations, then sp ecializing the framew ork using inheritanc e . � The priorit y queue data abstraction � Heaps and heap sort � Sk ew heaps � A framew ork for sim ulation � Discrete Ev en t-driv en sim ulation 395

  3. 396 CHAPTER 15. PRIORITY QUEUES 15.1 The Priorit y Queue Data Abstraction An ev eryda y example of a priorit y queue is the \to do" list of tasks w aiting to b e p erformed that most of us main tain to k eep ourselv es organized. Some jobs, suc h as \clean desktop", are not imp erativ e and can b e p ostp oned arbitrarily . Other tasks, suc h as \�nish rep ort b y Monda y" or \buy �o w ers for anniv ersary", are time crucial and m ust b e addressed more rapidly . Therefore, w e sort the tasks w aiting to b e accomplished in order of their imp ortance (or p erhaps based on a com bination of their critical imp ortance, their long term b ene�t, and ho w m uc h fun they are to do) and c ho ose the most pressing. F or a more computer-related example, an op erating system migh t use a priorit y queue to main tain a collection of curren tly activ e pro cesses, where the v alue asso ciated with eac h elemen t in the queue represen ts the urgency of the task. It ma y b e necessary to resp ond rapidly to a k ey pressed at a w orkstation, for example, b efore the data is lost when the next k ey is pressed. Other tasks can b e temp orarily p ostp oned in order to handle those that are time critical. F or this reason a priorit y queue is used so that the most urgen t task can b e quic kly determined and pro cessed. Another example migh t b e �les w aiting to b e output on a computer prin ter. It w ould b e a reasonable p olicy to prin t sev eral one page listings b efore a one h undred-page job, ev en if the larger task w as submitted earlier than the smaller ones. F or this reason a queue w ould main tain �les to b e prin ted in order of size, or a com bination of size and other factors, and not simply on time of submission. A sim ulation, suc h as the one w e will describ e in Section 15.4, can use a priorit y queue of \future ev en ts", where eac h ev en t is mark ed with a time at whic h the ev en t is to tak e place. The elemen t in this collection with the closest follo wing time is the next ev en t that will b e sim ulated. As eac h ev en t is executed, it ma y spa wn new ev en ts to b e added to the queue. These are only a few instances of the t yp es of problems for whic h a priorit y queue is a useful to ol. In terms of abstract op erations, a priorit y queue is a data structure that tak es elemen ts of t yp e value type and implemen ts the follo wing �v e op erations: void push(value t yp e) add a new v alue to the collection value t yp e & top() return a reference to the largest elemen t in collection void p op() delete the largest elemen t from the collection int size() return the n um b er of elemen ts in the collection b o ol empt y() return true if the collection is empt y Although the de�nition sp eaks of remo ving the largest v alue, in man y problems the v alue of in terest is the smallest item. As w e will see in some of the later examples, suc h uses can b e pro vided b y in v erting the comparison test b et w een elemen ts. Note that the name priorit y is a misnomer in that the data structure is not a queue queue, in the sense w e used the term in Chapter 10, since it do es not return elemen ts in a strict �rst-in �rst-out sequence. Nev ertheless, the name is no w �rmly asso ciated with this particular data t yp e.

Recommend


More recommend