Page 1 Today's Goals • Mergeable Heaps – Review Binary Heaps
Total Page:16
File Type:pdf, Size:1020Kb
ToDay’s&Goals& • MerGeable&Heaps& – Review&Binary&Heaps& – LinkeD2lists& – Binomial&Trees& – Binomial&Heaps& • ReD2Black& h<p://recursivelyrecursive.wordpress.com/& – Prove&balance& Heaps&O’&Fun& – Look&at&insert&/&Delete& CS&140&HMC&2&Jim&Boerkoel& Binary&heap&representaPons& ExtractMax& 16&&&14&&&10&&&8&&&&&7&&&&&&9&&&&&3&&&&&2&&&&&4&&&&&1& Return&anD&remove&the&larGest&element&in&the&set& 1&&&2&&&&3&&&&4&&&&5&&&&6&&&&7&&&&8&&&&9&&&&10& 16& 16& 14& 10& 14& 10& 8& 7& 9& 3& 8& 7& 9& 3& 2& 4& 1& 2& 4& 1& ExtractMax& ExtractMax& Return&anD&remove&the&larGest&element&in&the&set& Return&anD&remove&the&larGest&element&in&the&set& ?& 1& Heapify- 14& 10& 14& 10& 8& 7& 9& 3& 8& 7& 9& 3& 2& 4& 1& 2& 4& ExtractMax& ExtractMax& Return&anD&remove&the&larGest&element&in&the&set& Return&anD&remove&the&larGest&element&in&the&set& 14& Heapify- 8& 10& 4& 7& 9& 3& 2& 1& MerGeable&Heap& Binary&heaps& A&merGeable&heap&is&any&Data&structure&that&supports&the& followinG&five&operaPons,&in&which&each&element&has&a&key:& • MakeHeap()&2&creates&anD&returns&a&new&heap&containinG&no&elements.& • Insert(H,x)&2&inserts&element&x,&whose&key&has&already&been&filleD&in,&into& heap&H.& • Minimum(H)&2&returns&a&pointer&to&the&element&in&heap&H&whose&key&is& minimum.& • Extract:Min(H)&2&Deletes&the&element&from&heap&H&whose&key&is&minimum,& returninG&a&pointer&to&the&element.& • Union(H1,-H2)-2&creates&anD&returns&a&new&heap&that&contains&all&the& elements&of&heaps&H1&anD&H2.&Heaps&H1&anD&H2&are&“DestroyeD”&by&this& operaPon.& • ALSO:&Increase2Element,&Delete& MerGeable&heaps& Union&for&binary&heaps& 2 MerGeable&heaps&support& the&union&operaPon& 2 Allows&us&to&combine&two& concatenate the arrays and heaps&to&Get&a&sinGle&heap& then call Build-Heap 2 Union&runPme&for&binary& heaps?& & Adapted from: Worksheet:& Kevin Wayne Binomial&Tree& (Doubly)&linkeD2list&heap& B0- Bk- Bk: a binomial tree Bk-1 with the Linked-list addition of a left child with Bk:1- another binomial tree B k-1 Bk:1- B0- B1- B2- B3- B4- Binomial&Tree& Binomial&Tree& Bk- Bk- Number of nodes with Height? B B 1- B0- 1- B0- respect to k? B2- B2- Bk:1- Bk:1- B0- B1- B2- B3- B4- B0- B1- B2- B3- B4- Binomial&Tree& Binomial&Tree& Bk- Bk- Degree of root node? What are the children of B B 1- B0- 1- B0- B2- the root? B2- Bk:1- Bk:1- B0- B1- B2- B3- B4- B0- B1- B2- B3- B4- Binomial&Heap& Binomial&Tree& •Binomial&heap&& Vuillemin,&1978.& Sequence&of&binomial&trees&that&saPsfy&binomial&heap&property:& Why is it called a binomial tree? • each&tree&is&min2heap&orDereD& • top&level:&full&or&empty&binomial&tree&of&orDer&k& • which&are&empty&or&full&is&baseD&on&the&number&of&elements& 6- 3 18 depth-0- depth-1- 8 29- 10- 44- 37- depth-2- 30- 23- 22- 48- 31- 17- depth-3 - 45- 32- 24- 50- depth-4 - B4- 55- B4- B1- B0- Binomial&Heap& Binomial&Heap:&&ProperPes& RepresenteD&as&a&series&of&arrays&(each&represenPnG&a&min2heap)& How many heaps? A0: [18] A1: [3, 7] A2: empty A3: empty A3: [6, 8, 29, 10, 44, 30, 23, 22, 48, 31, 17, 45, 32, 24, 55] 6- 3 18 6- 3 18 8 29- 10- 44- 37- 8 29- 10- 44- 37- 30- 23- 22- 48- 31- 17- N-=-19- 30- 23- 22- 48- 31- 17- N-=-19- #-trees-=-3- #-trees-=-3- height-=-4- height-=-4- 45- 32- 24- 50- 45- 32- 24- 50- binary-=-10011 binary-=-10011 55- 55- B4- B1- B0- B4- B1- B0- Binomial&Heap:&&ProperPes& Binomial&Heap:&&ProperPes& Where is the max/min? Runtime of max/min? 6- 3 18 6- 3 18 8 29- 10- 44- 37- 8 29- 10- 44- 37- 30- 23- 22- 48- 31- 17- N-=-19- 30- 23- 22- 48- 31- 17- N-=-19- #-trees-=-3- #-trees-=-3- height-=-4- height-=-4- 45- 32- 24- 50- 45- 32- 24- 50- binary-=-10011 binary-=-10011 55- 55- B4- B1- B0- B4- B1- B0- Binomial&Heap:&&ProperPes& Binomial&Heap:&&Union& Height? •How&can&we&merge&two&binomial&tree&heaps&of&the&same&size&(2 k)?& • connect&roots&of&H'&and&H''& • choose&smaller&key&to&be&root&of&H&& - largest tree = Blog n Runtime? - height of that tree is log n 6- 3 18 6- 8 29- 10- 44- 37- 8 29- 10- 44- 30- 23- 22- 48- 31- 17- N-=-19- 30- 23- 22- 48- 31- 17- #-trees-=-3- height-=-4- 45- 32- 24- 50- 45- 32- 24- 50- binary-=-10011 55- 55- B4- B1- B0- H'- H''- Binomial&Heap:&&Union& Binomial&Heap:&&Union& Go through each tree size starting at 0 and merge as we go 6- 3 18 6- 3 18 8 29- 10- 44- 37- 8 29- 10- 44- 37- 30- 23- 22- 48- 31- 17- 30- 23- 22- 48- 31- 17- 15- 7 12 15- 7 12 45- 32- 24- 50- 45- 32- 24- 50- 28- 33- 25- 28- 33- 25- 55- 55- +- 41- How can we combine/merge binomial 1- 1- 1- 1 0 0 1 1 heaps (i.e. a combination of binomial - - - + 0 0- 1- 1- 1 19-+-7-=-26- tree heaps)? 1 1- 0- 1- 0 3- 3- 12 15- 7- 37- 7- 37- 18 Binomial&Heap:&&Union& 28- 33- 25- 25- Analogous&to&binary&addiPon& 41- 6- 3 18 8 29- 10- 44- 37- •Running&Pme?& 30- 23- 22- 48- 31- 17- – 15- 7 12 ProporPonal&to&number&of&trees&in&root&lists&2&O(log2&N)& 45- 32- 24- 50- – 28- 33- 25- 55- +- 41- 6- 3- 12- 1- 1- 1- 8 29- 10- 44- 15- 7- 37- 18- 1 0- 0- 1- 1 30- 23- 22- 48- 31- 17- 28- 33- 25- + 0 0- 1- 1- 1 19-+-7-=-26- 45- 32- 24- 50- 41- 1 1- 0- 1- 0 55- Binomial&Heap:&&Delete&Min/Max& Binomial&Heap:&&Delete&Min& We can find the min/max in O(log n). •Delete&noDe&with&minimum&key&in&binomial&heap&H.& How can we extract it? – FinD&root&x&with&min&key&in&root&list&of&H,&anD&Delete& – H'&←&&broken&binomial&trees& Hint: Bk consists of binomial trees: – H&&←&&Union(H',&H)& Bk-1, Bk-2, …, B0 3- 6 18 3- 6 18 8 29- 10- 44- 37- 8 29- 10- 44- 37- 30- 23- 22- 48- 31- 17- 30- 23- 22- 48- 31- 17- H- H- 45- 32- 24- 50- 45- 32- 24- 50- 55- 55- Binomial&Heap:&&Delete&Min& Binomial&Heap:&&Decrease&Key& •Delete&node&with&minimum&key&in&binomial&heap&H.& – Find&root&x&with&min&key&in&root&list&of&H,&and&delete& •Just&call&Decrease2Key/Increase2Key&of&Heap& – H'&←&&broken&binomial&trees& – Suppose&x&is&in&binomial&tree&Bk& – H&&←&&Union(H',&H)& – Bubble&node&x&up&the&tree&if&x&is&too&small& •Running&Pme:&& •Running&Pme?&& & 3- 6 18 – ProporPonal&to&depth&of&node&x& 3- 6 18 depth-=-3 8 29- 10- 44- 37- 8 29- 10- 44- 37- 30- 23- 22- 48- 31- 17- 30- 23- 22- 48- 31- 17- H- H- 45- 32- 24- 50- x- 32- 24- 50- 55- 55- Binomial&Heap:&&Delete& Binomial&Heap:&&Insert& •Delete&node&x&in&binomial&heap&H& •Insert&a&new&node&x&into&binomial&heap&H& – Decrease&key&of&x&to&2∞& – H'&←&&MakeHeap(x)& – H&&←&&Union(H',&H)& – Delete&min& •Running&Pme.&& 3- 6 18 x •Running&Pme:&& 8 29- 10- 44- 37- 30- 23- 22- 48- 31- 17- H- H'- 45- 32- 24- 50- 55- Build2Heap& BuilD2Heap& •Call&insert&n&Pmes& •Call&insert&n&Pmes& RunQme?- Consider-inserQng-n-numbers- times cost Can-we-get-a-Qghter-bound?- how-many-Qmes-will-B0-be-empty? -- how-many-Qmes-will-we-need-to-merge-with-B0?- how-many-Qmes-will-we-need-to-merge-with-B1?- how-many-Qmes-will-we-need-to-merge-with-B2?- …- how-many-Qmes-will-we-need-to-merge-with-Blog-n?- Heaps& BST&refresher& • Data&structures&that&support&many&Dynamic2set&operaPons& • Can&be&useD&as&a&both&a&DicPonary&anD&as&a&priority&queue& • Basic&operaPons&take&Pme&proporPonal&to&the&heiGht&of&the& tree& – Complete&binary&tree&with&n&noDes:&worst&case&Θ(log&n)& – For&linear&chain&of&n&noDes:&worst&case&Θ(n)& • ReD2black&trees& – VariaPon&of&binary&search&tree& – BalanceD:&heiGht&is&O(log&n)& – OperaPons&take&O(log&n)&in&the&worst&case& ReD2Black&Tree& ReD2black&tree& • A&variaPon&of&binary&search&trees& • A&red:black&tree&is&a&binary&search&tree&+1&bit&per& • Balanced:&heiGht&is&O(log&n)& noDe:&a&color&a<ribute,&which&is&either&reD&or&black& • OperaPons&will&take&O(log&n)&in&the&worst&case& • All&leaves&are&empty&anD&coloreD&black& – Use&sinGle&black&noDe,&T.nil,&for&root&anD&all&leaves&of&tree& & • Inherits&all&other&properPes&of&a&binary&search&tree& ReD2Black&Tree&ProperPes& ReD2Black&Tree&Example& 1. Every&noDe&is&either&reD&or&black.& 26- 2. The&root&is&black.& 3. Every&(null)&leaf&is&black.& 17- 4. If&a&noDe&is&reD,&then&both&its&chilDren&are&black.& 30- 47- 5. For&each&noDe,&all&paths&from&the&noDe&to& DescenDant&leaves&contain&the&same&number&of& black&noDes.& 38- 50- NIL- HeiGht&of&a&ReD2Black&Tree& ReD2Black&Tree&Example& h=4,bh=2, • Height-of-a-node:-is&the&number&of&eDGes&in&a& 26- lonGest&path&to&a&leaf& h=1,bh=1, h=3,bh=2, • Black:height-of-a-node-x:&bh(x)-is&the&number& 17- of&black&noDes&(incluDinG&NIL)&on&the&path& h=2,bh=1, h=2,bh=1, from&x&to&a&leaf,¬&incluDinG&x.& 30- 47- – Well2DefineD&Due&to&property&5& h=1,bh=1, h=1,bh=1, 38- 50- NIL- Worksheet:&Prove& Worksheet:&Prove& • Claim-1:&Any&noDe&with&heiGht&h&has&black2 • Claim-2:&The&subtree&rooteD&at&noDe&x& heiGht&≥&h/2.& contains&≥&2bh(x)&21& & The&key&to&success& OperaPons&on&Red2black&trees& • Lemma:&A&reD&black&tree&with&n&internal&noDes& • Non2modifying&operaPons&run&as&before& has&heiGht&≤&2loG(n+1).& – Minimum,&Maximum,&Successor,&Predecessor,&Search& – O(height)&!&O(log&n), h=4,bh=2, • Proof:&Let&h,anD&b&be&the&heiGht&anD&black2 26- heiGht&of&the&root.&Then,& h=1,b h=1, h=3,bh=2, 17- n ≥ 2b −1≥ 2h/2 −1 h=2,bh=1, h=2,bh=1, → log(n +1) ≥ h / 2 30- 47- h=1,bh=1, h=1,bh=1, → h ≤ 2log(n +1).