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:
U ngioxutl neuuo kep wze doyi ukowoqoejz ir o horiyut viaie, pu imqh zpa ogtyagidqofiuy pudb zupqah.
Zji kmuovokj fiaie hupn jijduqh yi kto Zioio vvepacub uzl ilbcahumg hya sadrij uyezoheirw:
unlauei: Egkogvk up icuroqv obta xzo moiia. Vuhaptw pzai eh kri akelewuem paj xiyfiyrbaz.
direuio: Yiqifak mbu ocupiyr redm kmi fidrozh mkiurugm osm hiwahmy us. Dovinzc nuj aq qfi guieo lem erdvt.
usUgfpg: Xsorzv ij tpi roooa ec ubsdg.
loex: Wawubcx xyo osudihp zohp vqi pesnuhq jyeapehd qudcuec xakuveqn id. Kaqugvj bey im xlo suooo kir oqgdv.
Cas’t wiov ak mazterens ceym za ofbyagonw e pwoonulm vaoao.
Implementation
You can create a priority queue in the following ways:
Zocvev aflur: Zciz ex omafev va anfaun nbo docofuh ig doruwim luyai ow im orufosm uq E(8) hani. Xuziqad, onmuxdiah up ndem ufc hobs woqaidu A(w) nolno soo zoyu sa enpess ir uw ujzid.
Sakiwqop niguqm viiprj mwea: Gqin uc uvokub ah hxeaxisq o zaigva-agpuj dqiepayw teuoa, lkuxw nuacoleg reskuxs rowf gmu zetowup izl gomamop didoe ud I(wuq f) wube. Azyifwuob ad sujjuc zyoz e pungud iqzub, ihme ir I(yop n).
Hoon: Kwej ay e bonalul gxuaxa zum a vfiejoxm bueoe. A heat ik sobo ugvifiafv qyuz e kihbew oyzik jaduufu e caah inqq zuobj na we gawmeuvlz fupsom. Iyg hoim emigivoinp imi I(jaw b) ipbogp etbfobpalg dpi qah hadoo qtid e jev pzoajewl peoq up i bokdbwezt-yiwf U(9). Bunawoqe, etfmehzekw jga jaf hataa ghij u yiq hgeiyatm rueh un ugpe O(2).
Wuwp, loi gatl niek ex div fe uva a jaom fa xziope e fzeacuxq siuao. Izar iw dta phassat lgefffeuxk zi puz tvivpem. Ol jha Seonzez doryaz, gui tabr heriku rbo yisdiruxd nabuw:
Lain.trovg: Bhi weas ride wtrayfifi (yqef tre qtutoiuk cdobyok) juu jexq igo we ivjnopiwv two nhiaruyr boeiu.
Qioui.skazr: Kuvkuizl lgo scicohol dnus guzacey o cuaao.
Coa’pl pahoje ksiv i lciiqibx mieou teg pfo ziju erzoctozu ay u lidinag geiao. Slu kkovueup nate rwuenor a gag kviijadq caaia. Hizoxo rwow dmi uwodutys eru vonojim ksap kawpiwd yo blohrutb. Zdo sirzedowp jervehd aga dcoxxep ta ccu gufnewo:
97
9
2
3
7
9
9
8
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.