Module 5: Priority Queues & Optimal BST
Module 5: Priority Queues & Optimal BST
MODULE 5:
PRIORITY QUEUES
A priority queue is a type of queue that arranges elements based on their priority values. Elements
with higher priority values are typically retrieved before elements with lower priority values.
A priority queue is an extension of a queue that contains the following characteristics:
• Every element in a priority queue has a priority value associated with it
• The element with the higher priority will be moved to the top and removed first
• If two elements in a priority queue have the same priority value, they’ll be arranged using
the FIFO principle
variants
1. Single Ended
2. Double ended
The operations supported by min priority The operations supported by min priority
queue are queue are
SP1: (peek)Return element with minimum priority SP1: (peek)Return element with maximum priority
Sp2:Insert element with an arbitrary priority Sp2:Insert element with an arbitrary priority
Sp3:delete element with minimum priority Sp3:delete element with maximum priority
1 / 22
Downloaded by Prince Allu (alluprince69@[Link])
lOMoARcPSD|44644628
Priority queue can be implemented using an array, a linked list, a heap data structure, or a binary
search tree. Among these data structures, heap data structure provides an efficient implementation
of priority queues.
Applications
1. To implement network buffer : Keep waiting packets in buffer. When link is available transmit
packet with highest priority (Delete max). Adding packet to buffer is done with Insert
operation. If buffer is full and packets have to be dropped delete one with min priority(delete
min).
2. External Quick Sort : In an external sort, we have more elements than can be held in the
memory of our computer. The elements to be sorted are initially on a disk and the sorted
sequence is to be left on the disk. When the internal quick sort method outlined above is
extended to an external quick sort, the middle group M is made as large as possible through
the use of a DEPQ. The external quick sort strategy is:
A. Read in as many elements as will fit into an internal DEPQ. The elements in the DEPQ will
eventually be the middle group of elements.
B. Read in the remaining elements. If the next element is <= the smallest element in the
DEPQ, output this next element as part of the left group. If the next element is >= the
largest element in the DEPQ, output this next element as part of the right group.
Otherwise, remove either the max or min element from the DEPQ (the choice may be
made randomly or alternately); if the max element is removed, output it as part of the
right group; otherwise, output the removed element as part of the left group; insert the
newly input element into the DEPQ.
C. Output the elements in the DEPQ, in sorted order, as the middle group.
D. Sort the left and right groups recursively.
2 / 22
Downloaded by Prince Allu (alluprince69@[Link])
lOMoARcPSD|44644628
A binary search tree is one of the most important data structures in computer science. One of its
principal applications is to implement a dictionary, a set of elements with the operations of searching,
insertion, and deletion.
If probabilities of searching for elements of a set are known e.g., from accumulated data about past
searches it is natural to pose a question about an optimal binary search tree for which the average
number of comparisons in a search is the smallest possible.
In many applications the cost of searching is very important. So, it is required that the overall cost of
searching should be as less as possible. And we know that search time of BST is more than
the Balanced Binary Search Tree, as Balanced Binary Search tree has less number of levels than the
BST. And there is one way which can further reduce the cost than the Balanced BST, which is Optimal
Binary Search Tree . Let us understand that by following example.
As there are 3 different keys, so we can have total 5 various BST by changing order of keys. The total
number of binary search trees with n keys is equal to
So following are the various possible Binary Search Trees of the above data. And also the overall cost
for searching for each BST. The cost is computed by multiplying each node’s frequency with the level
of tree( Here we are assuming that the tree starts from level 1 ) and then add them to compute the
overall cost of BST.
As it is shown in above figure that 2nd BST is balanced and the 4th BST is not balanced, though it’s
cost is less than the cost of Balanced BST and its cost is the least among all, so it is our Optimal Binary
Search Tree for the given data.
3 / 22
Downloaded by Prince Allu (alluprince69@[Link])
lOMoARcPSD|44644628
To derive a recurrence underlying a dynamic programming algorithm, we will consider all possible
ways to choose a root ak among the keys ai, . . . , aj . For such a binary search tree , the root contains
key ak, the left subtree Tik−1 contains keys ai, . . . , ak−1 optimally arranged, and the right subtree
Tjk+1contain s keys ak+1, . . . , aj also optimally arranged.
If we count tree levels starting with 1 to make the comparison numbers equal the keys’ levels, the
following recurrence relation is obtained:
The algorithm computes C(1, n)—the average number of comparisons for successful searches in the
optimal binary tree.
4 / 22
Downloaded by Prince Allu (alluprince69@[Link])
Downloaded by Prince Allu (alluprince69@[Link])
5 / 22
2. 2o4
Cot32
-i=1
ete Woy
C3,3] c 2.08=
C2,2 C 2|4
25Co= j-i:3
clo,oJ l4 -
Nate
224=3
19 Co2 -2-i
C24-B |312
2
12 Wo2
234-4 Coi
3 3
WB4
(
=0. 4 4 Cu
o C22
2 Woo
3 2
ustng weighfs all Shop
Roo
Cuy Coo
stepl:
Sol:
atchnsuccasnfel 9:
3 Keys
succeufol P: 10
25 20
Search
at a2 al
give ie foR
fhe
Optimal tnd Exarnple
data: BST
lOMoARcPSD|44644628
Downloaded by Prince Allu (alluprince69@[Link])
6 / 22
Root 3)9 Ot
C[2,8
+
[3,43= c
Root
|2,
C3,
8] e,
+ 2)
w,3]
+
4 = 12 T
+ J o, c Co,2I c
=
Step
2
3
Ca3
3 =23 W23=3
2 ho2
& Wo Co
3I=I+(+
2 =+3
2+3
lOMoARcPSD|44644628
lOMoARcPSD|44644628
qs,3] = c[o, o] +
(c o, 21 + ef2,2)J
Rsot
25
3
Rot.
T t 3
step
9 + 3
7 / 22
Downloaded by Prince Allu (alluprince69@[Link])
lOMoARcPSD|44644628
Constuchon ef Dptimal
&ST Fooo table
Root is Ros 2n
2nd key 15
Roy 16
Ro
(20 ) 3.
8 / 22
Downloaded by Prince Allu (alluprince69@[Link])
lOMoARcPSD|44644628
O
Hashena'
ehasacls
Hasina the
dr ansematon irg
nto uusually sA«e fdd lenyil vale i hay
on demand
4 siaiic Hashina
e haini
Hash tables
Hashin3 Oveflow
unelWo ns Honding Ppe Addresir
Diuigion lineas
paobing
mid Lquarl quadaaie
psobi
Foldng
L Sue alng Aehas hing
Aandom
LfolAing at the boundausProaig
Digit analyss
2. Dyamic Hashing
Coovexiina Kays tv
itegeas
ETo3,htT3. At To-1.
stot o elt Hash table eit
0acos atan
buckets an d
2
2 chaa Cer 2slots Pea buc kol
25 eacon
K- L L loeatton
co lision.
10 / 22
Downloaded by Prince Allu (alluprince69@[Link])
lOMoARcPSD|44644628
k . D
to D- ase generated
enerrte
Buckat addsss es o
t mi nedi22
Sually D is chosen axafly
o. stRout small dwisks)
colli sios. Cptir
emed- Squae method.
Home bucket a ka
Da onined by squorsig k
kat
bits
a Paaopiat.
mumber
o
and -then asing
h Sqase
fom Ra middle o
to 21 ao2s
Ex: k 3205
Keg CE) ,
ha
onust be used fer all kos. n
Sasne postns
tuaed
above example 4 35" lais eon ight aae
k is paioned
Ke same
lenaR. Kan
lengk. Ren
last being
peseb to obtain hash oddlaass k.
Rese as
aldel tvget
laad at patHon
Foling
tolding oct boundauas: Ka is
S
Ex 2323 2 4 2ao
RaveR
302 211
in
n
advance
advanca
knowon
known
ne table
table as
all ha
te kays
kaqs a Decima, Hexa
Todx
is iiea p3ated uaina
Each Ka distsi buon
keoed
Tha digits ase
examlned an s
eoraiiks
oher Tnbeas) keapi7
Ctails lomge addsess nte Tage
to gves auddaes
Smal enough
digits
Re hask table .
n + CCint) ka Ci <<8',
sL efte Su onuslg
Ovealow Handling
thoat
Suppose hen olocolzon we
a ke
ke, and
Collison
CA Open Addaassing-:
A) aaslue eollision , n thes
mekod e y end maxt
asailable locostien
acceRding to eaiteLa
HCk 4 2
1
eColksion Y Coliin xcolk
CoRsn
13 / 22
Downloaded by Prince Allu (alluprince69@[Link])
lOMoARcPSD|44644628
A B C D 22
+ + 2 +2 +2+3
asch
n o probes
ATSuCca4s
al se
4+ 3+ 2+I + 2+I1+8
O 7+ 6+5 + 3- 6
lo c ateons
tend to euste Cappeos
cusHea Cappeos
aReords
isadesantoge a ex tto
anohor)
Racord
incaeageg Seaa ch tma fR
olireas poobirg b
Can
mekods
tagolCorseng
do mini mie
clusleaig
sed Ra localiën
ocalibu
Re
Snsteod of Seaacding
&nodratic proking: localzon
eack ha enpdy
A, h+,
h+2 ete na asy
e addresa h
4+k, h+9, h+l6.
h, A+i,
hash unclo is
Second
Rehashing CDouble Hashing)
usd aesolve colli_ion.
*C)= h tRa
hask unclon
Siuppase st
HCR)= A [Link]
Second O
can be seaschel at
locathons
emp
A, Ah', A+2h, A+sk
Kandom Paobing: Arandom localion is ound teaa,
14 / 22 and ud to Aeaelve
a psedo Tandom qenerataL
Downloaded by Prince Allu (alluprince69@[Link]) colisim
lOMoARcPSD|44644628
Tecos&
Stappos e dalet
Seaadh o C. whom ou
Lart
NoO locoon
at ke . At 'o
Aas ou
at A Da
2
A is Resa, Next you seasck
NULL So it Tehurns a nett presert
D So
ase taken to
Some ofhea raasie3
ness
handle +Kis
A ust is naintaimad
ased
Moreaaue Hy
.
C choining
e each ka
L- acos ati||atoe
NOLL
Ex 2 A D
15 / 22
Downloaded by Prince Allu (alluprince69@[Link])
lOMoARcPSD|44644628
ul s
mi Hash4: ensue
Jo good P oT 7manCe
,
2
a
ohenuwe
has h table
LeRs8aly lo wn cazase dAa Si2e of a
Cessa taushakd.
oo adinga denay excaads a Psuspe cifed
al ading
has meheds
Y cderstandehg 2
popacla d 1amie
ACk
AO A
A oo A
CI O OO
2
Jha
Disectois
Dynaic Hashung Osing hCE)
TLnbe ef
biHfs af
diiectdy 'ddepends on -Re indxig
is done
ha dewcty. han
c2&.ed fo in dex
is 24.
2 bits size eduiecty Cnph-2)
bucket S &it is
al eady occapied
kecausse Re Epio= 2, e
bg Al B 16 / 22
Downloaded by Prince Allu (alluprince69@[Link])
lOMoARcPSD|44644628
Ao,Bo deph 3
Al,81 nexE hase h
so
e
a
O C
Ao, eo
*, BIs moved
ALCI| bseve
to too Ceda feR
6
s
tooot and las
Otlo
es e 4its loO| as
Compaand do el
c s sBooed in
to Second bucket
a Asra Doubing
ATTOH fs hea, u
sed
Io Oveao. Howev aahashina is done
on n etries hera buckats ovefos
(to)
isectiyless Dmamic Hashina C&inaan Dymanic Haskg)
n eaalea
Snsiead of daeoty o bucket poinles taaed
nethod, n aa buckets is sd
sed Lo Kanp toack ef
Two vaRiables and
tRe actie buc kefs
is h stat o a ehain of buckels
Each actuue bucke
uoi ll be actuwe.
buckels O -
2+-\
At omy
a om chain ae calad oveaoto
buckeis
The Telain mug
buckels
ex t asit A = 2 and
B4Ad A BS C2
lo
Insett too
6o0 Olo
() p A Bss C -
c3 - e4
e ache loucka
Henca ovelloo
Al and 8S in
a Ca ona
kat
Ovaoo is e handlA by aivalg
extra [Link]) Tucket 4ooD is actvat eA
achashal unga +1)= 3 bits. ? B
otKess ase
ooo bucke to loo bucke. cs
Rom
S adddas do ovesClow bucket
O0 ol
ca- (4 |Bs CS
Ce A-A AC e2 -
new acwe
18 / 22
nseat C Downloaded by Prince Allu (alluprince69@[Link])
lOMoARcPSD|44644628
Now he we fu tD add
inSetb)
ehick is alseadocupas
geis
we have es.
buckek
by Al BS and
ond in ov ea(low
). Rehash
So
So acva one 3eRe buckot lo
amdes sell
koeth coligiom BS
oucketfs
and Al & C o
ove to ne acbe buckds
occupy bucket ool Seed in
occP1
19 / 22
Downloaded by Prince Allu (alluprince69@[Link])
lOMoARcPSD|44644628
Kellist Jaees
&hetast(AigktchildC«)8SoRak
2
Ex: A
2
20 / 22
Downloaded by Prince Allu (alluprince69@[Link])
lOMoARcPSD|44644628
stanchue
s saet ypeda shud daplesl leltstTea
daftistTaee datehl,
3 elenet' elenent data
Teee Righteild
nt sheto
shelerl
Mi TAee
nodles TageuCmalle) han
shiel key
ehilde.
key 2
2
x:
2
12
dallist trea
Eranple
21 / 22
Downloaded by Prince Allu (alluprince69@[Link])
lOMoARcPSD|44644628
is extenal
iteenl
Ciot 4)+
Clepeekil ot (aglt chil))
Ex
2
Fig A
lze
Loeight biased laflasl
A
inlmal oda the value lat lild
at
chi ld
S value Bi ght
A
al c Cefe)
Fi8
X
22 / 22
Downloaded by Prince Allu (alluprince69@[Link])