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 operations : - insert ( item , priority ) adds item with priority priority - mind . extract item with least priority removes 4. returns ) - update ( item , priority ) item to priority changes priority of
RootedBmaryTreeter or 2 children . 0 = every node has - proof . where every = proper BT - perfect same death . has the t.nl structure to implement a data We want • efficient PQS . n ) time for all operations . Eeg . 040g . We ( again ) will a particular kind of tree use
. . . ofordered-binarytrees.ir . . . is its each node of the tree once node at depth i before any • visits every depth it 1 * node at - d descendent of left G) • visits depth every - d descendent of right G) . before any depth Is Is F- g. # III. - → sA1 → AH# ↳ t.to#.-shg-7-z • → → > → → - down . , not top bottom - up , it is * in some texts
RootedBmaryTreeter or 2 children . 0 = every node has - proof . where every = proper BT - perfect same death . has the on f. 1 Complete Binary Tree * * in the * has 2 level - order children traversal F- A complete binary tree of height h is a binary tree of height hi 1 ; 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 proper 4 except perhaps the last , which may have just a left child . . :* , F. 157,53 ✓ F. IF , t.sk#osITNjho7Ibbh.Jbb µ #b A µ sink ✓ 4) * G) × ✓
Binary Heap Data Structure ÷ " shape invariant - a complete binary tree ] - with vertices labelled by keys from ftp.iorities-st.keyldtkeiylpareutlvD-YIIIaut ordered set some , " for every node V ← . f- Eg ① / - ⑥ ② ③ ⑤ ' I ④ £1.0 { to ④ ② to ✓ ⑤ ⑨ × - This is the basic D. S . for implementing PQS ( Binary min - cheap ) . .
- How do so that we implement the operations the invariants are maintained ? - Consider Insertion : we want to insert If to the heap , 14 go ? where should it - how the shade changes there choice about Notice : : no I 1ha T o do T d. do do ✓ to hobnob AM 1 .
HeapIn o insert an item with key T K : 1. add a new leaf key G) = K v with so as to , maintain the shape invariant re - establish the order invariant by 2. percolate - up G) . executing - up G) { percolate . ( parent lol ) ) I key Lv ) a key while ( r is not root and swap positions of v parent G) in the tree and } ② ← 48 ¥ } × ③ / \ € ↳ ← 8h10 4 } i. %# 2
Insert-2.thenI.thenzinto.io ① ④ TO ¥1 T o ¥ ④ ¥¥ I ① , ① I ← ↳ 70s ② ⇐ ⑤ D ④ ¥ A ' ⑧ ⑤ ④ ⑥ To i ④
Heap Extract - Min . - ssmo%¥ Consider : ⇒ §yj these heap we must replace the root with the smaller of its children : ① 8%8 ? f- 10 12 ok ! Not 0k
Heap Extract - Min . - To remove the litem with the ) smallest key from the heap : remove the root 1. " last leaf " 2. replace the root with the , as to maintain the shape invariant . so restore the order invariant by calling 4. - down ( root ) percolate Percolate - down more work than percolate - up , is both at because it must look children see what to do ( and the children to or may not exist ) may
He apreml - remove the root - fix the shape invariant by moving the " last " nude to the root . " bottom right " or X.tl # . - down 4) { percolate c with key G) a key as ){ while ( r has a child r with the smallest key c ← child of among the children of r . c in the tree r and swap } I Notice that : or 2 children may have 0,1 , V - if 2 children , has care about v we . with the smallest key the one .
⇒ extract - min 3 times : Do AT - i ④ ¥h * # ④ ⑤ ' ④ ⑤
⇒ ⇒ Doextraot-m . I ⑦ ' ① ④
Heap Insert ! Extract - min Complexity of - 0( log a) to Extract - min take time Claim : Insert of size for heaps n . Recall : A perfect binary tree of height h has 2h " - I nodes . " the structure of the tree on h ( or II. By induction . ' we have It : If h=o then . V = 1 - I B¥s nodes assume the perfect : Consider some H70 and III. 2h " ii. des . - I binary tree of height h has + , 21h " ) s has nodes . b. t . of height htt - I . II show the ! p The tree is :} A DI¥ " = 21 " " I 1 modesty - it 2h11 + I so it has 2h " = 2. 2h11
sizeboundsoncompletebinarytrees.IE very complete binary tree with height hand in nodes satisfies : ⇐ zhtly 2h ± in Smallest : Left : I p.IT . of height h - I p.b.tv of height h . 21h - htt = 2h # nodes . It , = 2h In , we have : So log . 2h login I E login h h= Ollogn ) ⇒ Heap insert 4 extract min take time 0( log a) -
hinkedImplemewtatimr.at " last " µ D 1¥ , I X. * 1 € last " ¥t¥ N¥ E
Array-BasedBinarytteapImplemeutatimllse.co this embedding of binary tree complete a of size - n array site in a n : ⑥ ith node in ← ↳ ⑤ T o ③ /h ④ level - order traversal ⑤ ④ ¥ ④ ④ It 4 ith array element & 2 it 2 nodes 2it I • Children of node i are is node KE14 • Parent of node i 1AX 0*14*18%11 www.
Array-BasedBinarytteapImplemeutatimllse.co this embedding of binary tree complete a of size - n array site in a n : ⑥ ith node in ← ↳ ⑤ T o ③ /h ④ level - order traversal ⑤ ⑤ ④ ¥ ④ ④ It 4 ith array element ¢ 2 it 2 nodes 2it I • Children of node i are is node kindly • Parent of node i I was × Growing 4 Shrinking the tree in the array embedding is easy
↳ partiaky-fttedArrayImpkmentationofBinarytteap.LI ¥ ← ② ' = ← ④ Insert ④ ¥ , ¥57 ⑧ %¥i% I ¥ 6 ① 6 2 ① 1- ' ← to Id %
- based Heap Insert for Array + - ar # , size . Variables : - I . Afsite - Heap elements A fo ) are in . insert 4) I ÷÷÷÷ :* :* :* :] :::± . AG3 < Alps ){ while ( v > o and and A Cp ] AG3 Swap ✓ ← p p ← 4- 1) 12J } site ← site t t ; }
- fitted Array Implementation of Binary Heap . : Extract - min Partially I - ' ¥%HHXN ← ¥ = 9 it V 11 ⑥ l ④ # 6 It it ← ¥ 8¥ to a # ⑥ € 5¥ " ① 0¥ ¥ 11 to
Extraot.minforArray-basedtte.pt extract - milk value to return A record A Lo ] temp ← - I site ← size ← A Lsieej A move old last leaf to root AC03 down } i ← 0 not a a leaf a site ) while i while ( ait t left child of i 11 the child ← Zit 1 L AL2 it D) { A Git 23 if ( AND 2 it 2 a site percolate 11 use the right child if it exists * xieo ! ÷÷::? . A Lehild ] and Ali ] swap [ ← child } else { [ , return temp 3 return temp . }
- time trade - off _inExtraot-m - for A small space - min does many comparisons , eg this size ) to - Extract leaf . check if i is a we ensure the array - Suppose 7 2. size has site a big value , , that be denoted a a¥ there is can but will never be a key stored in the array . every array entry that is not a key is a . and we can skip the explicit checks for - Then , a leaf . being
Extract-mmrariantextract-m.NL a child i has is out of that value to return temp ← Alo ] A record order site ← size - I move # last leaf to peggwlatefg.tl?!II?nII.Isi:IIci i ← 0 ¥ Akita ] < ALII ){ while ( AL2 it is a ACD , } it is the left chid AL2 it is and Acis } is the right child Setseswap it is 2 it 2 } " { eturw temp }
makingatteapfromase.to n keys and want to make a heap with them . Suppose you have can be done in time 04 log n ) , with n inserts . . Clearly in time 0 (a) . • Claim : the following alg does it . make - heap ( T ) S n keys XT is a complete bit . with . a) { = tht -1 down to for ( i call percolate on node i - down } 3
Howdoesmake-heapwork.7.hn/2J-l last internal node is the - the algorithm does a percolate - down at each bottom - up node , working internal . ( percolate a tree into heap - down makes a the only node violating the order if root ) is the property 0 • I ✓ \ ] N4 5TG " " 9 ' 8 16 " = I 15 - I = Leith Lnfhj - I
;% D 'D
tceheapExam , ④ . - . - # ④ ✓ 4 ④ ⑤ 708 2 ¥1 * ¥ # a 4 z Ln I2J - I = 4 D= 10 ; exact order of visiting Notice : The nodes does not - as long matter visit children we as before parents . [ heap ) It follows that it is easy to do a recursive make - '
Make-heaplomplexitf.CI each 040g n ) . n percolate Oln log n ) - down calls early : , see it is actually 0 ( n ) ? . How can we ererypossibleswo.pl distinct edge for Intuition : mark a bounded by possible ) Time taken is . # of swaps max ⑧ ← I \ *
Timelomplexityoftlakeheaplet sln ) number of swaps be the max carried make - heap out of by set size n . on a ) by can bound We Scn : 2dL - d) 541 I [ The max # of swaps for b d- / - o call to percolate - down on A a - down ) percolate node at depth d t a there are is called , at most h - d , is 2d nodes node at on each at depth d each depth d from 0 to ht .
Recommend
More recommend