Priority Queue ! Heaps
priorityQueue-ADTLPO.li Stores a collection of pairs ( item , - - PowerPoint PPT Presentation
priorityQueue-ADTLPO.li Stores a collection of pairs ( item , - - PowerPoint PPT Presentation
Priority Queue ! Heaps priorityQueue-ADTLPO.li Stores a collection of pairs ( item , priority ) are from - Priorities ordered set some ( For simplicity , we use priorities " ) " highest priority from with 0 , 1,2 , 0 . . . - Main
priorityQueue-ADTLPO.li
Stores a collection of pairs ( item , priority)
- Priorities
are from
some
- rdered set
( For simplicity , we
use prioritiesfrom
0, 1,2 ,
. . .with
"highest priority
" )- Main operations
- insert ( item , priority)
adds item with priority priority
. extract- mind
removes 4.returns)
item with least priority
- update( item , priority)
changes priority of
item to priority
RootedBmaryTreeter
- proof
node has
- r 2 children .
- perfect
t.nl
has the
same death .
- We want
a data
structure to implement
efficient
PQS
.Eeg
. 040g n) time for all operations . . We (again) willuse
a particular
kind of tree
- fordered-binarytrees.ir
isits each node of the tree
- nce
- visits every
node at depth i before any node at
depth it1 *
- visits
every
depth
- d descendent of
left G)
before any depth
- d descendent of right G) .
F-
- Is
g.
Is
# III.
- →
→
→ sA1→AH#
↳ t.to#.-shg-7-z
> → → * in some texts , it isbottom - up
, not top- down.
RootedBmaryTreeter
- proof
node has
- r 2 children .
- perfect
- n f.
has the
same death .
* has 2 ** in theComplete Binary Tree
children
level - order
F-
traversal
A complete binary tree of height
h
is
1;
a binary tree of height hi 2 with 2d nodes at depth d
, for every0 ← dah
- 3. level order traversal visits every internal node before any leaf
**
- 4. every internal
node is proper4 except perhaps the last
,which may have just a left child .
. :*, F.157,53
- F. IF,
µ
✓#b
t.sk#osITNjho7Ibbh.Jbb
A
µ sink
G) × 4) *
✓
✓
Binary Heap Data Structure
÷
"- a complete binary tree]
shape invariant
- with vertices labelled by keys from
some
- rdered set
ftp.iorities-st.keyldtkeiylpareutlvD-YIIIaut
"for every
node
V
←Eg
. f-①
③ ②
/ -⑥
{ to
⑤ '
④
④
I
✓ ② to
£1.0
⑨
⑤
×
- This is the basic D.S. for implementing
PQS
(Binary
min - cheap) . .- How do
- perations
the invariants are maintained ?
- Consider Insertion:
If
we want to insert14
to the heap ,
where should it
go ?
- Notice:
there
nochoice about how the shade changes
:T
1ha
I
✓ to
do
d.
do
T
- do
1
AM
.hobnob
HeapIn
T
- insert an item with key
K
:- 1. add a new leaf
v with
key G) = K
, so as tomaintain the shape invariant
2.
re - establish the order invariant by
executing
percolate -upG) .
percolate
- up G) {
while ( r is not root and
keyLv ) a key
. ( parent lol) ) I swap positions of v andparent G)
in the tree}
②
}
¥
←48×
/
③
4\
€ ↳ ← 8h10
}
i.%#
2
Insert-2.thenI.thenzinto.io
①
¥1 T
- ④ TO
④ ¥¥
¥
I
①
,①I
← ↳
②
70s
A
④ ¥
⇐ ⑤ D
'
⑧
④
⑤ ④ ⑥To
i
Heap Extract
- Min .
- Consider :
ssmo%¥
⇒ §yj
these
heap
we must replace the root with the smaller of its children :
f-
① 8%8 ?
10 120k
Not
- k !
Heap Extract
- Min .
- To remove the litem with the) smallest key
from the heap:
1.
remove the root
- 2. replace the root with the
so as to maintain the shape invariant .
4.
restore the order invariant by calling
percolate
- down(root)
Percolate
- down
percolate
- up ,
because it must
look
at
both
children
to
see what to do ( and the childrenmay
- r may
not
exist)
Heapreml
- remove the root
- fix the shape invariant by moving the
" bottom right
"- r
" last "
nude to the root .
X.tl #
.percolate
- down 4){
while ( r has
a childc with keyG) a key as){
c ← child of
r with the smallest key
among the children of r .
swap r and
c in the tree
}
I
Notice that :
- V
may have
0,1 ,
- r 2 children
if
vhas
2 children ,
we
care aboutthe
- ne
with the smallest
key
.Do
extract - min
3 times:
- AT
*
⇒
i④ ¥h
#
④
⑤ '
④
⑤
Doextraot-m
⇒
⇒
.
I
⑦'
①
④
Complexity of
Heap Insert ! Extract - min
- Claim : Insert
to Extract - min take time
0( log a)
for
heaps
- f size
Recall: A perfect binary tree of height h has 2h"
- I nodes
- II. By induction
- n h ( or
B¥s
: If h=o then we have It '- I
III.
: Consider some H70and
assume the perfectbinarytree of height h
has2h"
- I
- ii. des .
II
!show the
p . b.t . of height htthas
21h
")- I
The tree
is:}A DI¥
"
so it has 2h
"- it 2h11 +I
sizeboundsoncompletebinarytrees.IE
very complete binary tree with height hand
in nodessatisfies :
2h
± in ⇐ zhtlySmallest :
Left
:I
p.IT . of height h-I
p.b.tv of height h .
# nodes
=21h
- htt
So
, we have :2h In
log.2h I
login
h
E loginh= Ollogn)
⇒ Heap insert 4 extract
min take time 0( log a)
hinkedImplemewtatimr.at
µ
,
D
" last "I
1¥
X.
*
1€
last
N¥
" ¥t¥E
Array-BasedBinarytteapImplemeutatimllse.co
this embedding of
acomplete
binary tree
- f
site
n in
asize - n array
:⑥
← ↳
ith node in
③/h④
⑤ T
- level - order
④¥ ④ ④
⑤ traversal
4
It
ith array element
- Children of
node i
are nodes 2it I
& 2 it 2
- Parent of
node i
is node KE14
1AX
0*14*18%11
www.
Array-BasedBinarytteapImplemeutatimllse.co
this embedding of
acomplete
binary tree
- f
site
n in asize - n array
:⑥
← ↳
ith node in
③/h④
⑤ T
- level - order
④¥ ④ ④
⑤ ⑤ traversal
4
It
ith array element
- Children of
node i
are nodes 2it I
¢
2 it 2
- Parent of
node i
is node kindly
I
was
× Growing 4 Shrinking the tree is easy in the array embeddingpartiaky-fttedArrayImpkmentationofBinarytteap.LI
②
¥←
↳
=
'④
←
Insert
④¥ ,
¥57
⑧%¥i%
① 6
I ¥
6 2
①
1-
← to Id
'%
Insert for Array
- based Heap
- +
- Heap elements
are in
A fo)
. . Afsite- I
insert4)I
÷÷÷÷:*:*:*:]
:::±.
while ( v > o
and
AG3 < Alps){
Swap
AG3
and A Cp]
✓ ← p p ← 4- 1) 12J}
}
site ← site t t ;
Partially
- fitted Array Implementation of Binary Heap. : Extract- min
- I
← ¥
=¥%HHXN
' 9it V 11⑥ l④
it#
6It
←¥
8¥ to
a # ⑥ €
5¥
"
①
0¥ ¥
11 toExtraot.minforArray-basedtte.pt
extract - milk
temp ←
A Lo]A record
value to return
site ← size- I
i ← 0
while ( ait t
a site )while i
not a a leafchild ←
Zit 1
11 theleft child of i
percolate
if (
2 it 2 a site AND A Git 23 L AL2itD){ 11 use the right child if it existsdown}
* xieo! ÷÷::?.
swap A Lehild] and Ali][ ← child
} else {
[ ,
3
return temp return temp
.}
A small
space
- for
- time trade - off
_inExtraot-m
- Extract
- min does many comparisons, eg this size) to
check if
i is a
leaf .
- Suppose
we ensure the array
has site
7 2. size
a¥ there is
a big value,
denoted a
, that canbe
stored in the array
but will never be a key
.and
every array entry that is not a key is a .
- Then ,
being
a leaf .
Extract-mmrariantextract-m.NL
i has
a childtemp ← Alo]
A record
value to return
that
is out of
site ← size - I- rder
i ← 0
while ( AL2it is a ACD
¥ Akita] < ALII){
peggwlatefg.tl?!II?nII.Isi:IIci
,} it
is the left chidSetseswap
AL2 it is and Acis}
it
is the right child
is 2it 2
}
" {eturw temp
}
makingatteapfromase.to
Suppose you have
n keys and want to make a heap with them . .Clearlycan be done in time 04 log n) , with
n inserts .- Claim : the following alg
does it
in time 0(a) . make -heap ( T) S
XT is a complete bit . with
n keys .for ( i
= tht -1 down toa){
call percolate
- down
- n node i
}
3
Howdoesmake-heapwork.7.hn/2J-l
is the
last internal node
- the algorithm does a
percolate
- down at
each internal
node , working
bottom - up
.( percolate
- down makes
a tree into
aheap
if
the only node violating the order
property
is the
root )
0 • I ✓\] N4
5TG
15 16 " ' 8 "9"
Lnfhj
- I
- I
;%
D'D
tceheapExam
, ④
.- .-
# ④ ✓
4 ④
2¥1
⑤ 708
z 4
* ¥ #a
D= 10
;
Ln I2J - I
= 4Notice: The exact order of visiting
nodes does not
matter
- as long
we
visit children
before parents
.[
It follows that it is easy to do
a recursive make - 'heap)
Make-heaplomplexitf.CI
early
Oln log n)
:n percolate
- down calls
each 040g n) .
. How can we see it is actually 0(n) ?Intuition :
mark
a
distinct edge for
ererypossibleswo.pl
Time taken is
bounded by
max
. # of swapspossible)
⑧←
I
\
*
Timelomplexityoftlakeheaplet
sln)
be the max
number of swaps
carried
- ut
by
make- heap
- n a
set
- f
size
n .
We
can boundScn
) by
:541
I
2dL - d)
d-
- o
b
[The max # of swaps
for
A/
a
call to percolate
- down
- n
percolate
- down)
t
a node at depth d
is called
, at most ,there are
ish-d
- n each
node at
2d nodes
each depth d
at depth d
from
0 to ht
.she
2dL
- d)
a
A- 0
11 = I = 21h - o) t2Yh-1) t . . .t [' ( h -th
- D)
set
i=h-d
, so D= h - iand while d ranges
- ver
0,1 ,
- .
- ver
he;o , h- I ,
. . . h- (in- 1)
"
h
1
Now
she iii.ili)
= Etta.ie?IEiilbenYI)=nIIi.snhEoIit2n-
(£o
= It fit f. + It . . . =It ItIttIIy )Lomplexityotmakeheapblork
done by
make - heap is
bounded by
a
constant times the
number of swaps
so
is 0(hogn)T
0 (n) .
forrated
)
Updatingprioritie.si
Suppose a heap contains an item with priority K , and we executeupdate
- priority( item , j ) .
- We replace
- rder invariant :
if ja k
, dopercolate
- up from the
modified
nodeif
kaj
,do
percolate - down from
the modified
node .X.
13>
I
µ
^ AN
AD
ADMD
Th takes ollogn) time
- bit howdowe-t.nl
the right
node to change ??
Restoring -
- T
- do this, we
need an auxiliary data structure .
End
=
Correctness of
swapping
in
percolate down
- b
A.c
the:c:D!:
III.
µ T
- e
a > e
and
Ed ,
[
A
D
we
swap
- c. e
Now :
. weknow
bt.es c
and
be
. es d- b
a#e
. so orderis
OK , except
µ
dfhq.ge
possibly
below
a
- which
[
A
D
we still
have to
look at
.Correctness
- f swapping in
percolate
- up
- " suppose
up
a°b
we know CED , ese , because we
Had
previously swapped
c with doreµ
Doe
- we know that be a
[
A D. if cab ,
we
swap e.b
Now :
.we now know that
- a
cab se
{↳
and
cc bed
µ
dfh.ge
and
a
< bsa(
D
D
' so- rder
is ok , except
T3
possibly
with ancestors of
C
, which we still must check.