Priority Heapsmitchell/cmpt-225/2020...Heap Extract-MinTo remove the litem withthe) smallest key...
Transcript of Priority Heapsmitchell/cmpt-225/2020...Heap Extract-MinTo remove the litem withthe) smallest key...
priorityQueue-ADTLPO.liStores a collection of pairs ( item , priority)
- Priorities are from some ordered set
( For simplicity , we use prioritiesfrom 0
, 1,2, . . . with 0"
highest priority" )
- Main operations :- insert ( item , priority)adds item with priority priority
. extract-
mindremoves 4.returns) item with least priority
- update ( item , priority)changes priority of item to priority
RootedBmaryTreeter- proof = every node has 0 or
2 children .
- perfect = proper BT. where everyt.nl has the same death .
• We want a data structure to implementefficient PQS
.
Eeg . 040g n) time for all operations .
. We (again) will use a particular kind of tree
. .
.
. . .ofordered-binarytrees.iris its each node of the tree once
• visits every node at depth i before anynode at depth it1 *
• visits every depth -d descendent of left G)before any depth
-d descendent of right G) .
F- - Isg. Is# III.• → → → sA1→AH#↳ t.to#.-shg-7-z
> → →
* in some texts,it is bottom - up ,
not top - down.
RootedBmaryTreeter- proof = every node has 0 or
2 children .
- perfect = proper BT. where every1on f. has the same death .
* has 2 ** in theComplete Binary Tree children level - orderF- traversal
A complete binary tree of height h is1
;a binary tree of height hi
2 with 2d nodes at depth d, for every 0 ← 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,53F. IF, µ
✓
#bt.sk#osITNjho7Ibbh.JbbA µ sinkG) × 4) * ✓ ✓
Binary Heap Data Structure÷ "
- a complete binary tree]shape invariant
- with vertices labelled by keys fromsome ordered 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 we implement the operations so that
the invariants are maintained ?
- Consider Insertion:If we want to insert14 to the heap ,where should it go ?
-Notice: there no choice about how the shade changes :
T 1ha I✓ to do d. do To do1 AM. hobnob
HeapInTo insert an item with key K :1. add a new leaf v with keyG) = K , so as to
maintain the shape invariant
2. re - establish the order invariant byexecuting percolate-upG) .
percolate-upG) {while ( r is not root and keyLv ) a key
.
(parent lol) ) I
swap positions of v and parentG) in the tree
} ②} ¥ ←
48×
/ ③4 \€↳←8h10 }i.%# 2
Heap Extract - Min .
-
Consider : ssmo%¥
⇒ §yjthese
heap
we must replace the root with the smaller of its children :
f-① 8%8 ?10 12
0k Not ok !
Heap Extract - Min .-
To remove the litem with the) smallest keyfrom the heap :
1. remove the root
2. replace the root with the"
last leaf ",
so as to maintain the shape invariant .
4. restore the order invariant by callingpercolate-down(root)
Percolate-down is more work than percolate-up ,
because it must look at both children
to see what to do ( and the childrenmay or may not exist)
Heapreml- remove the root- fix the shape invariant by moving
the
" bottom right"or" last " nude to the root .
X.tl #.
percolate -down 4){while ( r has a child c with keyG) a key as){
c ← child of r with the smallest keyamong the children
of r.
swap r and c in the tree
}I
Notice that :- V may have 0,1 , or 2 children
. if v has 2 children , we care about
the one with the smallest key .
Complexity of Heap Insert ! Extract -min-Claim : Insert to Extract - min take time 0( log a)
for heaps of size n.
Recall: A perfect binary tree of height h has2h"-I nodes
.
II. By induction on h ( or"the structure of the tree.
B¥s : If h=o then we have It'- I = 1 nodes . V
III. : Consider some H70and assume the perfect
binarytree of height h has 2h"- I ii.des .
+ ,
II ! show the p .b.t .of height htt has 21h") - I nodes s .
The tree is:}A DI¥"
so it has 2h"- it 2h11 + I = 2. 2h11 = 21""I 1 modesty
sizeboundsoncompletebinarytrees.IEvery complete binary tree with height hand in nodes
satisfies : 2h ± in ⇐ zhtly
Smallest : Left:
I p.IT . of height h- I p.b.tv of height h .
# nodes = 21h-htt. It , = 2h
So,we have : 2h In
log.2h I loginh E loginh= Ollogn)
⇒ Heap insert 4 extract min take time 0( log a)-
Array-BasedBinarytteapImplemeutatimllse.cothis embedding of a complete binary treeof site n in a size- n array :
⑥
←↳ ith node in
③/h④ ⑤ To level- order
④¥ ④ ④ ⑤ traversal
4 Itith array element
• Children of node i are nodes 2it I & 2 it 2
• Parent of node i is node KE14
1AX0*14*18%11www.
Array-BasedBinarytteapImplemeutatimllse.cothis embedding of a complete binary treeof site n in a size- n array :
⑥
←↳ ith node in
③/h④ ⑤ To level- order
④¥ ④ ④ ⑤ ⑤ traversal
4 Itith 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 embedding
partiaky-fttedArrayImpkmentationofBinarytteap.LI②¥←↳ =
'
④←
Insert④¥, ¥57
⑧%¥i%① 6 I ¥ 6
2
① 1-← to Id'
%
Insert for Array- based Heap-+
. Variables : ar# ,size
- 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'
9itV 11
⑥ l④ it # 6 It
←¥8¥ toa#⑥ € 5¥ "
①
0¥ ¥11 to
Extraot.minforArray-basedtte.ptextract - milktemp ← A Lo] A record value to returnsite ← size - I
AC03 ← A Lsieej Amove old last leaf to rooti← 0while ( ait t a site ) while i not a a leafchild← Zit 1 11 the left child of i
percolate if ( 2 it 2 a site AND AGit 23 L AL2 itD) {11 use the right child if it exists
down } *xieo! ÷÷::?.swap A Lehild] and Ali]
[ ← child
} else {
[ , 3return temp
return temp .}
A small space - for - time trade- off_inExtraot-m- Extract -min does many comparisons, eg this size) tocheck if i is a leaf .
- Suppose we ensure the array has site7 2. size
a¥ there is a big value, denoted a , that can bestored in the array but will never
be a key .and every array entry
that is not a key is a .
-Then,we can skip the explicit checks for
being a leaf .
Extract-mmrariantextract-m.NL i has a child
temp ← Alo] A record value to return that is out of
site ← size - Iorder
move # last leaf to
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 childis 2it 2}
" {eturw temp}
makingatteapfromase.toSuppose you have n keys and want to make a heap with them .
.Clearly can be done in time 04 log n) , with n inserts .• Claim : the following alg . does it in time 0(a) .
make-heap ( T) SXT is a complete bit . with n keys .for ( i = tht -1 down to a){call percolate- down on node i
}3
Howdoesmake-heapwork.7.hn/2J-lis the last internal node
- the algorithm does a percolate- down at eachinternal node
, working bottom - up .
( percolate- down makes a tree into a heapif the only node violating the orderproperty is the root )
0•
I✓\]N4 5TG
15 16 " '8 "9 "
Lnfhj - I = Leith - I = I
tceheapExam, ④ .
- .-#④✓ 4 ④
2
¥1 ⑤ 708z 4
* ¥ #aD= 10 ; Ln I2J- I
= 4
Notice: The exact order of visiting nodes does notmatter - as long as we visit childrenbefore parents .
[It follows that it is easy to do a recursive make- 'heap)
Make-heaplomplexitf.CIearly Oln log n) : n percolate- down calls , each 040gn) .
. How can we see it is actually 0(n) ?Intuition : mark a distinct edge forererypossibleswo.plTime taken is bounded by max . # of swaps possible)
⑧
←I \
*
Timelomplexityoftlakeheapletsln) be the max number of swaps carriedout by make- heap on a set of size n.
We can bound Scn) by :
541 I 2dL - d)d-- o b [The max # of swaps forA / a call to percolate- down on
percolate - down ) t a node at depth dis called
,at most
,there are
is h-don each node at 2d nodeseach depth d at depth dfrom 0 to ht
.
she 2dL-d) aA-0 11
=I
= 21h - o) t2Yh- 1) t . . . t ['
( h -th-D) t 2h" (h- Chi))set i=h-d
,so D= h - i and while d ranges over
0,1 , - . . , h- I , i will range over he;o , h- I , . . . h- (in - 1)"h 1
Now
she iii.ili) = Etta.ie?IEiilbenYI)=nIIi.snhEoIit2n-( £o = It fit f. + It . . . = It It IttIIy )
Lomplexityotmakeheapblorkdone by make - heap is bounded by a
constant times the number of swaps
so is 0(hogn)T 0 (n) .
forrated)
Updatingprioritie.siSuppose a heap contains an item with priority K ,and we execute update-priority ( item , j ) .
- We replace K with j in the heap , and thenrestore the order invariant :
if ja k , do percolate-up from themodified node
if kaj , do percolate -down fromthe modified node .
X. 13>I µ^ ANAD ADMD
Th takes ollogn) time - bit howdowe-t.nlis the right node to change ??Restoring-
-To do this,we need an auxiliary data structure .
Correctness of swapping in percolate down-
ob . Suppose we are percolating down cA.c the:c:D!: III.µ Toe . If a > e and Ed ,[ A D
we swap c. e
Now : . we know bt.es c
and be. es dob
a#e . so order is OK , except
µ dfhq.ge possibly below a - which[ A D we still have to look at .
Correctness of swapping in percolate - up-
" suppose we are percolating up a°b
we know CED ,ese , because weHad previously swapped c with dore
µ Doe - we know that bea
[ 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 order is ok, except
T3 possibly with ancestors ofC
,which we still must check.