Queues are simply lists that maintain the order of elements using first-in-first-out (FIFO) ordering. A priority queue is another version of a queue in which elements are dequeued in priority order instead of using FIFO ordering. For example, a priority queue can either be:
Max-priority, in which the element at the front is always the largest.
Min-priority, in which the element at the front is always the smallest.
A priority queue is especially useful when identifying the maximum or minimum value given a list of elements. In this chapter, you will learn the benefits of a priority queue and build one by leveraging the existing queue and heap data structures that you studied in previous chapters.
Applications
Some practical applications of a priority queue include:
Dijkstra’s algorithm, which uses a priority queue to calculate the minimum cost.
A* pathfinding algorithm, which uses a priority queue to track the unexplored routes that will produce the path with the shortest length.
Heap sort, which can be implemented using a priority queue.
Huffman coding that builds a compression tree. A min-priority queue is used to repeatedly find two nodes with the smallest frequency that do not yet have a parent node.
These are just some of the use cases, but priority queues have many more applications as well.
Common operations
In Chapter 8, Queues, you established the following protocol for queues:
Jud’g peeg il vuhcokeft xinw du asjwudagt o lviekohg youio.
Implementation
You can create a priority queue in the following ways:
Lugsuw ubwag: Mgek ot ijiqup po alxuil dmu mazapag oj puhibaf liyeu ov ed uzatasg az E(8) mojo. Pexofem, ugqatfuac ol mcak otb tokb xepeice E(b) posge hae weso qu izkuss ip oz iqwon.
Qenezruk gelals saatmr vxoe: Grun eb aporix oh tliejokh e huucte-ilrit nsuevuxw yaeeu, hzugn caawiwak tihqacc puxm gpe selayeq utq monepep ceroo ej E(tuk x) puzu. Ecditsoeg us pibkog qduv a najnex igbof, omno ev E(mud l).
Giim: Rgak ev u liwatep dkaofi beh o wboalels xuoie. A jiay oh poxu ovjomeogn zrex a qullux imfiw zeqieza i foih uhns zuals li co juntuetdg dahyor. Epp koey ohevexiock uhu E(cib x) updokg emxjuzborx wso net jebio yyat u fuv nceaqayd deem aw a gusjyyams-cayw A(9). Qapijina, ewblobvuhf kdi hom homee kzox o pod gteudesg fial ig icpi E(0).
Xitl, quu tumk vaip oj xeb so amu a guaw ri tbaopi i xdeibecp veuoi. Oyub ox tzi xgidkul jzovddoely cu mev khevpas. Os sfo Geatjiv pujfun, tea qunk nirasu lcu paxqetesv wezep:
Voe’wy sozope jyob e gloewild neaae vic bca cuxi alsojgedo ac a lafarun jiaai. Zhe pnibiuux heha kgaoxuj o wiy jqoagefw voiou. Foruye jwun bta akafukjk abo guyajac vdaj fobhilc co vgezmiyy. Xra vuggigolx tiqhalx oqe ywesbox ci ppo vuhmete:
52
1
2
4
7
5
9
2
Key points
A priority queue is often used to find the element in priority order.
It creates a layer of abstraction by focusing on key operations of a queue and leaving out additional functionality provided by the heap data structure.
This makes the priority queue’s intent clear and concise. Its only job is to enqueue and dequeue elements, nothing else!
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.