CS 473Lecture X1 CS473-Algorithms Lecture BINOMIAL HEAPS.

Slides:



Advertisements
Similar presentations
Binomial Heaps. Heap Under most circumstances you would use a “normal” binary heap Except some algorithms that may use heaps might require a “Union” operation.
Advertisements

Fibonacci Heaps Especially desirable when the number of calls to Extract-Min & Delete is small (note that all other operations run in O(1) This arises.
COL 106 Shweta Agrawal and Amit Kumar
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Chapter 4: Trees Part II - AVL Tree
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
Binary Search Trees Comp 550.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
Dynamic Sets and Data Structures Over the course of an algorithm’s execution, an algorithm may maintain a dynamic set of objects The algorithm will perform.
Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key Priority Queues make-heap Operation insert find-min delete-min union decrease-key.
Fibonacci Heap.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 11 Tuesday, 12/4/01 Advanced Data Structures Chapters.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps.
1 Binomial heaps, Fibonacci heaps, and applications.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Binomial Heaps Melalite Ayenew Dec 14, 2000.
Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap.
Binomial Heaps Referred to “MIT Press: Introduction to Algorithms 2 nd Edition”
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Lecture X Fibonacci Heaps
CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Chapter 19: Fibonacci Heap Many of the slides are from Prof. Leong Hon Wai’s resources at National University of Singapore.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
Binomial Tree B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 Adapted from: Kevin Wayne B k : a binomial tree B k-1 with the addition of a left child with another.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
The Linked List (LL) Each node x in a linked list contains: Key(x) head key[x]- The value stored at x. next[x]- Pointer to left child of x. The while list.
CMSC 341 Binomial Queues and Fibonacci Heaps. Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) deleteMinO(lgN)
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
Fibonacci Heaps.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Data structures Binomial Heaps - Binomial Trees B0B0 BkBk B k-1.
Fibonacci Heap. p2. Procedure Binary heap (worst-case) Fibonacci heap (amortized) MAKE-HEAP  (1) INSERT  (lg n)  (1) MINIMUM  (1) EXTRACT-MIN  (lg.
Analysis of Algorithms
Binary Search Trees What is a binary search tree?
Lecture 22 Binary Search Trees Chapter 10 of textbook
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
Binomial Heaps Chapter 19.
Red-Black Trees.
Lecture 9 Algorithm Analysis
Lecture 29 Heaps Chapter 12 of textbook Concept of heaps Binary heaps
Binomial Heap.
Lecture 9 Algorithm Analysis
Chapter 20 Binomial Heaps
Lecture 9 Algorithm Analysis
CS 583 Analysis of Algorithms
Fundamental Structures of Computer Science
Lecture 7 Algorithm Analysis
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
21장. Fibonacci Heaps 숭실대학교 인공지능 연구실 석사 2학기 김완섭.
Analysis of Algorithms CS 477/677
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Chapter 12&13: Binary Search Trees (BSTs)
Red-Black Trees CS302 Data Structures
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

CS 473Lecture X1 CS473-Algorithms Lecture BINOMIAL HEAPS

CS 473Lecture X2 Binomial Heaps DATA STRUCTURES: MERGEABLE HEAPS MAKE-HEAP ( ) –Creates & returns a new heap with no elements. INSERT (H,x) –Inserts a node x whose key field has already been filled into heap H. MINIMUM (H) –Returns a pointer to the node in heap H whose key is minimum.

CS 473Lecture X3 Mergeable Heaps EXTRACT-MIN (H) –Deletes the node from heap H whose key is minimum. Returns a pointer to the node. DECREASE-KEY (H, x, k) –Assigns to node x within heap H the new value k where k is smaller than its current key value.

CS 473Lecture X4 DELETE (H, x) –Deletes node x from heap H. UNION (H 1, H 2 ) –Creates and returns a new heap that contains all nodes of heaps H 1 & H 2. –Heaps H 1 & H 2 are destroyed by this operation Mergeable Heaps

CS 473Lecture X5 Binomial Trees A binomial heap is a collection of binomial trees. The binomial tree B k is an ordered tree defined recursively B o Consists of a single node. B k Consists of two binominal trees B k-1 linked together. Root of one is the leftmost child of the root of the other.

CS 473Lecture X6 Binomial Trees B k-1 B k

CS 473Lecture X7 Binomial Trees B0B0 B1B1 B2B2 B3B3 B1B1 B1B1 B2B2 B1B1 B4B4 B0B0 B3B3 B2B2 B1B1 B0B0

CS 473Lecture X8 Binomial Trees B k-2 B k-1 B2B2 B1B1 Bo BkBk

CS 473Lecture X9 Properties of Binomial Trees LEMMA: For the binomial tree B k ; 1.There are 2 k nodes, 2.The height of tree is k, 3.There are exactly nodes at depth i for i = 0,1,..,k and 4.The root has degree k > degree of any other node if the children of the root are numbered from left to right as k-1, k-2,...,0; child i is the root of a subtree B i. k i

CS 473Lecture X10 Properties of Binomial Trees PROOF: By induction on k Each property holds for the basis B 0 INDUCTIVE STEP: assume that Lemma holds for B k-1 1.B k consists of two copies of B k-1 | B k | = | B k-1 | + | B k-1 | = 2 k-1 +2 k-1 = 2 k 2. h k-1 = Height (B k-1 ) = k-1 by induction h k =h k-1 +1 = k-1 +1 = k

CS 473Lecture X11 Properties of Binomial Trees 3. Let D(k,i) denote the number of nodes at depth i of a B k ; true by induction B k-1 d=1 d=i d=i-1 d=i D(k-1,i -1) D(k-1, i) D(k,i)=D(k-1,i -1) + D(k-1,i) = k-1 i -1 + k-1 i = k i

CS 473Lecture X12 Properties of Binomial Trees(Cont.) 4.Only node with greater degree in B k than those in B k-1 is the root, The root of B k has one more child than the root of B k-1, Degree of root B k =Degree of B k-1 +1=(k-1)+1=k

CS 473Lecture X13 Properties of Binomial Trees (Cont.) B k-1 B k-3 B k-2 B2B2 B1B1 B0B0 B k-3 B k-2 B2B2 B1B1 B0B0

CS 473Lecture X14 Properties of Binomial Trees (Cont.) COROLLARY: The maximum degree of any node in an n-node binomial tree is lg(n) The term BINOMIAL TREE comes from the 3rd property. i.e. There are nodes at depth i of a B k terms are the binomial coefficients. k i k i

CS 473Lecture X15 Binomial Heaps A BINOMIAL HEAP H is a set of BINOMIAL TREES that satisfies the following “Binomial Heap Properties” 1.Each binomial tree in H is HEAP-ORDERED the key of a node is ≥ the key of the parent Root of each binomial tree in H contains the smallest key in that tree.

CS 473Lecture X16 Binomial Heaps 2. There is at most one binomial tree in H whose root has a given degree, –n-node binomial heap H consists of at most [lgn] + 1 binomial trees. –Binary represantation of n has lg(n) + 1 bits, n ≤ b lgn, b lgn -1,....b 1, b 0 > = Σ lgn i=0 b i 2 i By property 1 of the lemma (B i contains 2 i nodes) B i appears in H iff bit b i =1

CS 473Lecture X17 Binomial Heaps Example: A binomial heap with n = 13 nodes = 2 Consists of B 0, B 2, B 3 head[H] B0B0 B2B2 B3B3

CS 473Lecture X18 Representation of Binomial Heaps Each binomial tree within a binomial heap is stored in the left-child, right-sibling representation Each node X contains POINTERS –p[x] to its parent –child[x] to its leftmost child –sibling[x] to its immediately right sibling Each node X also contains the field degree[x] which denotes the number of children of X.

CS 473Lecture X19 Representation of Binomial Heaps HEAD [H] 10 parent degree key child sibling ROOT LIST (LINKED LIST)

CS 473Lecture X20 Representation of Binomial Heaps Let x be a node with sibling[x] ≠ NIL –Degree [sibling [x]]=degree[x]-1 if x is NOT A ROOT –Degree [sibling [x]] > degree[x] if x is a root

CS 473Lecture X21 Operations on Binomial Heaps CREATING A NEW BINOMIAL HEAP MAKE-BINOMIAL-HEAP ( ) allocate H head [ H ]  NIL return H end RUNNING-TIME= Θ(1)

CS 473Lecture X22 BINOMIAL-HEAP-MINIMUM (H) x  Head [H] min  key [x] x  sibling [x] while x ≠ NIL do if key [x] < min then min  key [x] y  x endif x  sibling [x] endwhile return y end Operations on Binomial Heaps

CS 473Lecture X23 Since binomial heap is HEAP-ORDERED The minimum key must reside in a ROOT NODE Above procedure checks all roots NUMBER OF ROOTS ≤ lgn + 1 RUNNING–TIME = O (lgn) Operations on Binomial Heaps

CS 473Lecture X24 Uniting Two Binomial Heaps BINOMIAL-HEAP-UNION Procedure repeatedly link binomial trees whose roots have the same degree BINOMIAL-LINK Procedure links the B k-1 tree rooted at node y to the B k-1 tree rooted at node z it makes z the parent of y i.e. Node z becomes the root of a B k tree

CS 473Lecture X25 BINOMIAL - LINK (y,z) p [y]  z sibling [y]  child [z] child [z]  y degree [z]  degree [z] + 1 end Uniting Two Binomial Heaps

CS 473Lecture X26 NIL sibling [y] child[z] p[y] Uniting Two Binomial Heaps z +1 NIL

CS 473Lecture X27 Uniting Two Binomial Heaps: Cases We maintain 3 pointers into the root list x = points to the root currently being examined prev-x = points to the root PRECEDING x on the root list sibling [prev-x] = x next-x = points to the root FOLLOWING x on the root list sibling [x] = next-x

CS 473Lecture X28 Initially, there are at most two roots of the same degree Binomial-heap-merge guarantees that if two roots in h have the same degree they are adjacent in the root list During the execution of union, there may be three roots of the same degree appearing on the root list at some time Uniting Two Binomial Heaps

CS 473Lecture X29 CASE 1: Occurs when degree [x] ≠ degree [next-x] prev-x x next-x sibling { next-x} prev-x x next-x abcd abcd B k B l l >k B k B l Uniting Two Binomial Heaps

CS 473Lecture X30 prev-x x next-x sibling [next-x] prev-x x next-x abcd ab c d B K B K B K Uniting Two Binomial Heaps: Cases CASE 2: Occurs when x is the first of 3 roots of equal degree degree [x] = degree [next-x] = degree [sibling[next-x]]

CS 473Lecture X31 CASE 3 & 4: Occur when x is the first of 2 roots of equal degree degree [x] = degree [next-x] ≠ degree [sibling [next-x]] Occur on the next iteration after any case Always occur immediately following CASE 2 Two cases are distinguished by whether x or next-x has the smaller key The root with the smaller key becomes the root of the linked tree Uniting Two Binomial Heaps: Cases

CS 473Lecture X32 prev-x x next-x sibling [next-x] prev-x x next-x B k B k B l l > k CASE 3 key [b] ≤ key [c] CASE 4 key [c] ≤ key [b] prev-x x next-x abcd a c bd a b cd Uniting Two Binomial Heaps: Cases CASE 3 & 4 CONTINUED

CS 473Lecture X33 The running time of binomial-heap-union operation is O (lgn) Let H 1 & H 2 contain n 1 & n 2 nodes respectively where n= n 1 +n 2 Then, H 1 contains at most lgn 1 +1 roots H 2 contains at most lgn 2 +1 roots Uniting Two Binomial Heaps: Cases

CS 473Lecture X34 So H contains at most lgn 1 + lgn 2 +2 ≤ 2 lgn +2= O (lgn) roots immediately after BINOMIAL-HEAP-MERGE Therefore, BINOMIAL-HEAP-MERGE runs in O(lgn) time and BINOMIAL-HEAP-UNION runs in O (lgn) time Uniting Two Binomial Heaps: Cases

CS 473Lecture X35 Binomial-Heap-Union Procedure BINOMIAL-HEAP-MERGE PROCEDURE -Merges the root lists of H 1 & H 2 into a single linked- list - Sorted by degree into monotonically increasing order

CS 473Lecture X36 BINOMIAL-HEAP-UNION (H 1,H 2 ) H  MAKE-BINOMIAL-HEAP ( ) head [ H ]  BINOMIAL-HEAP-MERGE (H 1,H 2 ) free the objects H 1 & H 2 but not the lists they point to prev-x  NIL x  HEAD [H] next-x  sibling [x] while next-x ≠ NIL do if ( degree [x] ≠ degree [next-x] OR (sibling [next-x] ≠ NIL and degree[sibling [next-x]] = degree [x]) then prev-x  x CASE 1 and 2 x  next-x CASE 1 and 2 elseif key [x] ≤ key [next-x] then sibling [x]  sibling [next -x] CASE 3 Binomial-Heap-Union Procedure

CS 473Lecture X37 BINOMIAL- LINK (next-x, x) CASE 3 else if prev-x = NIL then head [H]  next-x CASE 4 else CASE 4 sibling [prev-x]  next-x CASE 4 endif BINOMIAL-LINK(x, next-x) CASE 4 x  next-x CASE 4 endif next-x  sibling [x] endwhile return H end Binomial-Heap-Union Procedure (Cont.)

CS 473Lecture X38 Uniting Two Binomial Heaps vs Adding Two Binary Numbers H 1 with n 1 NODES : H 1 = H 2 with n 2 NODES : H 2 = ex: n 1 = 39 : H 1 = = { B 0, B 1, B 2, B 5 } n 2 = 54 : H 2 = = { B 1, B 2, B 4, B 5 }

CS 473Lecture X39 B5B5 B5B5 B4B4 B2B2 B2B2 B1B1 B1B1 B0B0 MERGE H B5B5 B5B5 B4B4 B2B2 B2B2 B1B1 B1B1 B0B0 B5B5 B5B5 B4B4 B2B2 B2B2 B1B1 B0B0 B 1 B5B5 B5B5 B4B4 B2B2 B2B2 B2B2 B0B0 next-x x x x x B2B2 CASE1 MARCH Cin=0 1+0=1 CASE3 or 4 LINK Cin=0 1+1=10 CASE2 MARCH then CASE3 and CASE4 LINK Cin=1 1+1=11

CS 473Lecture X40 B5B5 B5B5 B4B4 B2B2 B2B2 B0B0 B5B5 B5B5 B4B4 B3B3 B2B2 B0B0 B5B5 B5B5 B4B4 B3B3 B2B2 B0B0 B5B5 B5B5 B2B2 B 3 CASE1 MARCH Cin=1 0+0=1 x B4B4 B3B3 B2B2 B0B0 next-x x x B 6 x CASE1 MARCH Cin=0 0+1=1 CASE3 OR 4 LINK Cin=0 1+0=10

CS 473Lecture X41 Inserting a Node BINOMIAL-HEAP-INSERT (H,x) H'  MAKE-BINOMIAL-HEAP (H, x) P [x]  NIL child [x]  NIL sibling [x]  NIL degree [x]  O head [H’]  x H  BINOMIAL-HEAP-UNION (H, H’) end RUNNING-TIME= O(lg n)

CS 473Lecture X42 B5B5 B4B4 B1B1 B0B0 B5B5 B4B4 B1B1 B0B0 B5B5 B4B4 B1B1 B0B0 B5B5 B4B4 B1B1 Relationship Between Insertion & Incrementing a Binary Number B0B0 B0B0 B1B1 H : n 1 =51 H = = { B 0, B 1,B 4, B 5 } H MERGE ( H,H’) LINK B 2 B 4 B 5 x next-x

CS 473Lecture X43 -More effıcient -Case 2 never occurs -While loop should terminate whenever case 1 is encountered A Direct Implementation that does not Call Binomial-Heap-Union

CS 473Lecture X44 Extracting the Node with the Minimum Key BINOMIAL-HEAP-EXTRACT-MIN (H) (1) find the root x with the minimum key in the root list of H and remove x from the root list of H (2) H’  MAKE-BINOMIAL-HEAP ( ) (3) reverse the order of the linked list of x’ children and set head [H’]  head of the resulting list (4) H  BINOMIAL-HEAP-UNION (H, H’) return x end

CS 473Lecture X45 head [H] B0B0 B1B1 x Extracting the Node with the Minimum Key Consider H with n = 27, H = = {B 0, B 1, B 3, B 4 } assume that x = root of B 3 is the root with minimum key B2B2 B1B1 B0B0 B4B4

CS 473Lecture X46 head [H’] Extracting the Node with the Minimum Key head [H] B0B0 B1B1 x B2B2 B1B1 B0B0 B4B4

CS 473Lecture X47 Unite binomial heaps H= {B 0,B 1,B 4 } and H’ = {B 0,B 1,B 2 } Running time if H has n nodes Each of lines 1-4 takes O(lgn) time it is O(lgn). Extracting the Node with the Minimum Key

CS 473Lecture X48 BINOMIAL-HEAP-DECREASE-KEY (H, x, k) key [x]  k y  x z  p[y] while z ≠ NIL and key [y] < key [z] do exchange key [y]  key [z] exchange satellite fields of y and z y  z z  p [y] endwhile end Decreasing a Key

CS 473Lecture X49 Similar to DECREASE-KEY in BINARY HEAP BUBBLE-UP the key in the binomial tree it resides in RUNNING TIME: O(lgn) Decreasing a Key

CS 473Lecture X50 Deleting a Key BINOMIAL- HEAP- DELETE (H,x) y ← x z ← p [y] while z ≠ NIL do key [y] ← key [z] satellite field of y ← satellite field of z y ← z ; z ← p [y] endwhile H’← MAKE-BINOMIAL-HEAP remove root z from the root list of H reverse the order of the linked list of z’s children and set head [H’] ← head of the resulting list H ← BINOMIAL-HEAP-UNION (H, H’) RUNNING-TIME= O(lg n)

CS 473Lecture X51 H’ ← MAKE-BINOMIAL-HEAP remove root z from the root list of H reverse the order of the linked list of z’s children set head [H’] ← head of the resulting list H ← BINOMIAL-HEAP-UNION (H, H’) end Deleting a Key (Cont.)