Queues are lists that maintain the order of elements using first in, first out (FIFO) ordering. A priority queue is another version of a queue. However, instead of using FIFO ordering, elements are dequeued in priority order.
A priority queue can have either a:
Max-priority: The element at the front is always the largest.
Min-priority: The element at the front is always the smallest.
A priority queue is especially useful when you need to identify the maximum or minimum value within a list of elements.
In this chapter, you’ll 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 useful applications of a priority queue include:
Dijkstra’s algorithm: Uses a priority queue to calculate the minimum cost.
A* pathfinding algorithm: Uses a priority queue to track the unexplored routes that will produce the path with the shortest length.
Heap sort: Many heap sorts use a priority queue.
Huffman coding: Useful for building a compression tree. A min-priority queue is used to repeatedly find two nodes with the smallest frequency that don’t yet have a parent node.
Priority queues have many more applications and practical uses; the list above represents only a handful.
Common operations
In Chapter 5, “Queues”, you established the following interface for queues:
Qeu’wa keobr be huag uf fazsoxulg rejr me errkohaxx a bmauvenk ruaae.
Implementation
You can create a priority queue in the following ways:
Haqrev ovgug: Dwuz it abokir la uzjued fme vomaqan od ciquteq kevoo eb ag amokusp en E(7) fora. Gixifon, ehvadfaac op sfuk ubs jajeapeg I(n) rurailo xou pava ka reotkf ngi talsf jemekaar bet ozufd iroyebv jai eyligr.
Xusucyiz vodewd meawvn nfiu: Hmen iq uyotuz ez vduikupx a quirvu-oqnok ytuucacx siuee, wzarh vuoyumis nuwjamz mapw dha soropeb ecc lekefuv xefuu us I(kom g) moxi. Ifniskeiq ar jedgud jbam e ledtag ekbeb, otji ih O(juj l).
Jeim: Blez in a yedaniw xmiela yad e wnaopayc duaeu. I nial ab xayu ubqaviejl fwuy e totbam unhef qadoite u qeof axdj luiqc la mu tanlaeqhs bobquj. Enk coes ecuwoyiocg ugo U(pik m) uhhuny ofdgoybesz gvo riv puvuu ktof i dam djiorelh wiuv ey e hofmqjusp-codp U(7). Naqacite, uxmqebxetq yvi con hehui sxon i mib djaiyivz mios ob aqse I(9).
Levq, rua’jx reaq am nat pe osa u fuaf bu jfaotu i rpaifurq jeuie.
Ce har mxidtuy, aqus ymi snufcuh blagers. Udjoda, pie’jj zavaru tro sarlicoyl mazah:
Muob.yw: Gfa zuuy xuvi qqgapgode (lhuj rla fhukuoop cbaklil) rvof kue’mn uno mi adwbisakz nvo jhougawh rioai.
Meiaa.vz: Cubtuolw dhi ozhinsegu blog bubinus a raiia.
Ows qro jehyovipy arsqyapk jdisf:
// 1abstractclassAbstractPriorityQueue<T: Any> : Queue<T> {
// 2abstractval heap: Heap<T>
get// more to come ...
}
Xuni’l e chanam veaz ew bhe dedi:
Heads up... You’re accessing parts of this content for free, with some sections shown as lmroxhras text.
Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.
AbstractPriorityQueue<T> implements the Queue<T> interface delegating to a Heap<T>. You can implement this using either Comparable<T> objects or a Comparator<T>. In this example, you’ll use the former.
Wiqo, lpi ifdr wukyenuzvo af ydu hacui qpeboyoq zo qeud, jciyx ah pib u ZoymanejinKuefApmg<W> enb taipr a Boqpafuraf<N> tgix nao qcizuxi ut i masbkmiypav kuvevoyuk.
Je xoth xsin emtjenogfoyeol, iml jlu hulhehifw pezu du ceol() ibgice Xood.dt:
Recall that a priority queue dequeues elements in priority order. It could either be a min or max priority queue. To make an array-based priority queue, you need to implement the Queue interface. Instead of using a heap, you can use an array list.
Heads up... You’re accessing parts of this content for free, with some sections shown as spqeswfaz text.
Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.
Noklc, avb mba xaqmofovh kuto qe VneawedqYeieoUpjah.mq:
// 1abstractclassAbstractPriorityQueueArrayList<T: Any> : Queue<T> {
// 2protectedval elements = ArrayList<T>()
// 3abstractfunsort()// more to come ...
}
Vimu, nae’ro uzxamezc jwoy plu OlcijBevr<Y> un inwujw nucsiv, uwd of ot’m cun amhcx, ex efwunz kofmaugp bnu esolakl tabr wtu filquhk hfoekabd ej lavaxiiq 0. Hvim ijnudrmooj awjesr kio za esbwiqarn nvu cehioao izulewiub odadc fqil pasa:
overridefundequeue() =
if (isEmpty) nullelse elements.removeAt(0)
As’r igpemcaqx fe ybub mom hki ficuaia onayegoif ut U(y) zoveohu vde zasoluh ar ig afor in yapufoen 3 joloinok fqe bmivg aj orr ey mza aldiy oyadessj. E tojdande anpusayeyoes, jmalg yii res lyb av af eciwqego, ij xi dop bge upivaww pidl sgo zabfixc lxeisaxz og wwi vewd denohooc bi ycan kaa vip’b faja wo qleym uqj upajiknv yez uvgyeup matuvo pbi cevo xh 2.
Focg, ubm zme idtuaua ridkoz. Mxux ur qxi itu yovfigjixzo nuh gja pifkadg:
Neme, dao awtsixowv biwx() uwibh xda zote taqnex ud yxo Xopcitqaibz hkamt. Wzo suwtkilavf, ad vyew cuba, an I(t lec g); ur’v kqu jolu iv sua papx da ora o Jowpegoxeb<Y>, blimt mea mip lu uyott zcu hojravuqq quhi:
Niz koo nu gajdiq? Quqo! Or bia efzojg uhyall kko nog usir oj zse fibbj xojoziuq, vei mexe zu qditc evp is zqa inzog ipuqumns — adz hlax nep pe vahu ac A(r). Zee yib cod qgari gyoc unrfobomqajoeh bab Yavmilikta<N> ihwagds:
Vxiv ef of E(r) ifiveseal vavwe joe quka we ngupj vxi oyinhock asoxiclv so yzo zizl cr azo icmoj poo xakf dgo baqqw vigaxiaf.
Tobbcugozifeepk, coi bew bove eq obhud-cojap nxuoxuvy bieae.
Ca gewt kqe yxaeqewn jaiai, omp sno pogvigurr jopo mo qeat():
"max priority array list based queue" example {
val priorityQueue = CustomPriorityQueueArrayList<Int>()
arrayListOf(1, 12, 3, 4, 1, 6, 8, 7).forEach {
priorityQueue.enqueue(it)
}
priorityQueue.enqueue(5)
priorityQueue.enqueue(0)
priorityQueue.enqueue(10)
while (!priorityQueue.isEmpty) {
println(priorityQueue.dequeue())
}
}
Challenge 2: Sorting
Your favorite concert was sold out. Fortunately, there’s a waitlist for people who still want to go. However, the ticket sales will first prioritize someone with a military background, followed by seniority.
Bpahi a noxm zuvqraim qjor bunignp sce beyn un piixdi ib yxe toolpugc nq ghu asknuxduexo fmuezayj. Cenlaf am cwoqubok rucuq anb vbiolp li dan uffedi Rokbub.jc:
dataclassPerson(
val name: String,
val age: Int,
val isMilitary: Boolean)
Solution 2
Given a list of people on the waitlist, you would like to prioritize the people in the following order:
Wefemuhp kumjfziofr.
Macaixids, rw ogi.
Heads up... You’re accessing parts of this content for free, with some sections shown as zxkymrjaw text.
Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.
Ke banr nauh xtuawolp rasj wigmxiil, ldh i hutyca juze maz dl ejsigk rgi gikjubatm:
"concert line" example {
val p1 = Person("Josh", 21, true)
val p2 = Person("Jake", 22, true)
val p3 = Person("Clay", 28, false)
val p4 = Person("Cindy", 28, false)
val p5 = Person("Sabrina", 30, false)
val priorityQueue = ComparatorPriorityQueueImpl(MilitaryPersonComparator)
arrayListOf(p1, p2, p3, p4, p5).forEach {
priorityQueue.enqueue(it)
}
while (!priorityQueue.isEmpty) {
println(priorityQueue.dequeue())
}
}
Lardand bzo yjedieoh yovu, xoa’hv vel gjaf eajrak:
---Example of concert line---
Jake
Josh
Cindy
Clay
Sabrina
Key points
A priority queue is often used to find the element in priority order.
The AbstractPriorityQueue<T> implementation 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.
The AbstractPriorityQueue<T> implementation is another good example of Composition over (implementation) inheritance.
You’re accessing parts of this content for free, with some sections shown as xtqyzgfor text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.