Lecture 29 Heaps Chapter 12 of textbook Concept of heaps Binary 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.
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
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.
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.
Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures in C" and some supplement.
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.
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.
Binomial Heaps Melalite Ayenew Dec 14, 2000.
Binomial Heaps Referred to “MIT Press: Introduction to Algorithms 2 nd Edition”
CS 473Lecture X1 CS473-Algorithms Lecture BINOMIAL HEAPS.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
+ 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.
Binary Search Trees (BST)
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.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Fibonacci Heaps. Fibonacci Binary insert O(1) O(log(n)) find O(1) N/A union O(1) N/A minimum O(1) O(1) decrease key O(1) O(log(n)) delete O(log(n) O(log(n))
Analysis of Algorithms
Binary Search Trees What is a binary search tree?
Lecture 23 Red Black Tree Chapter 10 of textbook
Lecture Efficient Binary Trees Chapter 10 of textbook
BCA-II Data Structure Using C
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Heap Sort Example Qamar Abbas.
CMSC 341 Lecture 13 Leftist Heaps
Insert in amortized cost O(1), Merge in O(lgn) DeleteMax in O(lgn)
Lecture 7 Algorithm Analysis
Binomial Heaps Chapter 19.
Binomial Tree Adapted from: Kevin Wayne Bk-1 B0 Bk
Wednesday, April 18, 2018 Announcements… For Today…
Part-D1 Priority Queues
Ch 6: Heapsort Ming-Te Chi
Tree Representation Heap.
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Binomial Heap.
Chapter 20 Binomial Heaps
Fibonacci Heap.
ערמות בינומיות ופיבונצ'י
CS 583 Analysis of Algorithms
Fundamental Structures of Computer Science
Lecture 7 Algorithm Analysis
CS 583 Analysis of Algorithms
CS6045: Advanced Algorithms
Lecture 7 Algorithm Analysis
21장. Fibonacci Heaps 숭실대학교 인공지능 연구실 석사 2학기 김완섭.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Topic 6: Binary Search Tree Data structure Operations
Binary and Binomial Heaps
Binomial heaps, Fibonacci heaps, and applications
HEAPS.
Fibonacci Heaps.
Binomial heaps, Fibonacci heaps, and applications
Priority Queues Supports the following operations. Insert element x.
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Lecture 29 Heaps Chapter 12 of textbook Concept of heaps Binary heaps Binomial heaps Fibonacci heaps Heap summary

2. Binomial Heap Binomial Heap is defined by Binomial tree. Binomial tree is an ordered tree that can be recursively defined as: A binomial tree of order 0 has a single node. A binomial tree of order i has a root node whose children are root nodes of binomial trees of order i-1, i-2, …, 2, 1, 0. Example B0=0 B1 = B0+1 B2 = B1+B0+1, Bi = Bi-1 + … + B1 +B0 + 1 Bi-1 = Bi-2 + B1 +B0 + 1 Bi-Bi-1 = Bi-1, Bi= 2Bi-1

Properties of Binomial Tree A binomial tree order i, denoted Bi , has 2i nodes The height of a binomial tree Bi is i The root has i children A binomial tree Bi consists of two binomial trees Bi-1 linked together such that root of one is the leftmost child of the root of the other There are C(i, h) node at level h B0=1 B1 = B0+1 = 2 B2 = B1+B0+1, Bi = Bi-1 + … + B1 +B0 + 1 Bi-1 = Bi-2 + B1 +B0 + 1 Bi-Bi-1 = Bi-1, Bi= 2Bi-1 Bi = 2 * Bi-1 = 2* 2* Bi-2 = 2 * 2 *…* 2 = 2^I there are exactly k i  nodes at depth i for i = 0, 1, . . . , k, and 4. the root has degree k, which is greater than that of any other node; moreover if the children of the root are numbered from left to right by k − 1, k − 2, . . . , 0, child i is the root of a subtree Bi . H(B1) = 1 h(Bi) = I h(Bi+1) = h(Bi) + 1 = i+1, true. By mathematical induction, h(Bi) = 1.

Binomial Heap A Binomial Heap is a set of Binomial Trees with two properties: each binomial tree follows min-heap property there can be at-most one binomial tree of any degree. 7 27 11 16 9 36 14 10 18 12 21 19

Binomial Heap Derived properties: root of each binomial tree contains the smallest key in the tree a binomial heap H of N nodes contains at most log N + 1 binomial trees The second property implies that an n-node binomial heap H consists of at most ⌊lg n⌋ + 1 binomial trees. To see why, observe that the binary representation of n has ⌊lg n⌋ + 1 bits, say hb⌊lg n⌋, b⌊lg n⌋−1, . . . , b0i, so that n = sum⌊lg n⌋ i=0 bi 2^i . By property 1 of Lemma 19.1, therefore, binomial tree Bi appears in H if and only if bit bi = 1. Thus, binomial heap H contains at most ⌊lg n⌋ + 1 binomial trees. Proof 2. assume i0< i1 < i2 < …. < ik-1 j <= ij Bj < Bij B0+ … + Bk-1 = Bk 2^k <= N, k < log2 N

Linked node representation of Binomial Heaps Each node N has following fields a field that stores its key a field d stores the degree of N pointer P[N] points to the parent of N pointer Child [N] points to the leftmost child Pointer Sibling[N] that point to the right sibling of N If N is the root node, then P[N] = NULL. If N has no children, then Child[N] = NULL, and if N is the rightmost child of its parent, then sibling[N] = NIL. The root nodes of the binomial trees are organized in a linked list with increasing order of degree, called root list.

Operations on Binomial Heaps find-min delete-min Union Insert Dec-key Binary Heap < Binomial Heap < Fibonacci Heap This is only with respect to performance. From Wiki, +------------+---------------+----------+-----------+ | Operation | Binary | Binomial | Fibonacci | | Find-min | Θ(1) | Θ(1) | Θ(1) | | delete-min | Θ(log n) | Θ(log n) | O(log n) | | insert | Θ(log n) | Θ(1) | Θ(1) | | dec-key | Θ(log n) | Θ(log n) | Θ(1) | | merge | Θ(m log(n+m)) | O(log n) | Θ(1) |

Operations on Binomial Heaps Find-min The procedure returns a pointer to the node having minimum value in the binomial heap H. A binomial heap is heap-ordered, the node with minimum value in a particular binomial tree is at the root node of the binomial tree. The procedure checks all roots. Since there are at most log n + 1 roots to check, the running time of this procedure is O(log n). Binary Heap < Binomial Heap < Fibonacci Heap This is only with respect to performance. From Wiki, +------------+---------------+----------+-----------+ | Operation | Binary | Binomial | Fibonacci | | Find-min | Θ(1) | Θ(1) | Θ(1) | | delete-min | Θ(log n) | Θ(log n) | O(log n) | | insert | Θ(log n) | Θ(1) | Θ(1) | | dec-key | Θ(log n) | Θ(log n) | Θ(1) | | merge | Θ(m log(n+m)) | O(log n) | Θ(1) |

Operations on Binomial Heaps Example: find-min 7 27 11 16 9 36 14 10 18 12 21 19 Head [H] 7 27 11 16 9 36 14 10 18 12 21 19 Head [H] X Min = 12

Operations on Binomial Heaps 7 27 11 16 9 36 14 10 18 12 21 19 Head [H] X Y Min = 7 7 27 11 16 9 36 14 10 18 12 21 19 Head [H] X Y Min = 7

Union two Binomial Heaps Union -- the procedure of uniting two binomial heaps into one binomial heap Algorithm: given binomial heaps H1 and H2 Step 1. merge H1 and H2, i.e. link the roots of H1 and H2 in non- decreasing order. Step 2. restoring binomial heap by linking binomial trees of the same degree together: traverse the linked list, keep track of three pointers, prev, ptr and next. Case 1: degrees of ptr and next are not same, move ahead. Case 2: If degree of next->next is also same, move ahead. Case 3: If key of x is smaller than or equal to key of next, make next as a child of ptr by linking it with ptr. Case 4: If key of ptr is greater, then make ptr as child of next.

Uniting two Binomial Heaps Union_Binomial-Heap(H1, H2) Step 1: SET H = Create_Binomial-Heap() Step 2: SET Head[H] = Merge_Binomial-Heap() Step 3: Free the memory occupied by H1 and H2 Step 4: If Head[H] = NULL, then RETURN Step 5: SET PREV = NULL, PTR = Head[H] and NEXT = Sibling[PTR] Step 6: Repeat Step 7 while NEXT ≠ NULL Step 7: IF Degree[PTR]≠Degree[NEXT] OR (Sibling[NEXT]≠NULL AND Degree[Sibling[NEXT] = Degree[PTR]), then SET PREV = PTR, PTR = NEXT ELSE IF Val[PTR] ≤ Val[NEXT], then SET Sibling[PTR] = Sibling[NEXT] Link_Binomial-Tree(NEXT, PTR) Else IF PREV = NULL, then Head[H] = NEXT ELSE Sibling[PREV] = NEXT Link_Binomial-Tree(PTR, NEXT) SET PTR = NEXT SET NEXT = Sibling[PTR] Step 8: RETURN H

Uniting two Binomial Heaps Time complexity of union operation If H1 contain n1 nodes and H2 contain n2 nodes, then H1 contains at most 1og n1 + 1 roots and H2 contains at most 1og n2 + 1 roots, H contains at most 1og n2 + 1og n1 + 2 ≤ 2 1og n + 2 = O(1og n) Since, n = n1 + n2, the merge takes O(log n) time. Each iteration (the step 2) takes O(1) time. There are at most 1og n1 + 1og n2 + 2 iterations, the total time is thus O(log n).

Example of Union operation 7 27 9 14 10 12 Head [H1] 4 8 36 14 10 18 12 21 19 Head [H2] 7 24 29 31 39 19

Example of union operation Head [H] 18 12 7 27 4 19 9 14 10 8 36 21 24 29 31 39 PTR NEXT Head [H] 7 27 12 18 4 19 9 14 10 8 36 21 24 29 31 39 PTR NEXT 21

Example of union operation Head [H] 7 27 12 18 4 19 9 14 10 8 36 21 17 24 29 31 39 PTR NEXT PREV Head [H] 12 4 19 7 27 9 14 10 8 36 18 21 17 24 29 31 39 PTR NEXT PREV 18

Unite the Binomial Heaps given below Head [H] 12 18 9 14 10 4 19 7 27 36 21 17 24 29 31 39 PTR NEXT PREV 8

Inserting a new node Insert --- insert a node x into binomial heap H. Algorithm Step 1. create a new binomial heap H1 of one node of value x Step 2. Unite H1 and H. Insert_Binomial_Heap() Step 1: SET H’ = Create_Binomial-Heap() Step 2: SET Parent[x] = NULL, Child[x] = NULL and sibling[x] = NULL, Degree[x] = NULL Step 3: SET Head[H’] = x Step 4: SET Head[H] = Union_Binomial-Heap(H, H’) Step 5: END Time complexity: ? Space complexity: ?

extracting-min / delete-min Delete-min – delete node with minimum key from binomial heap H. Algorithm Step 1. call find-min to find min node x of H Step 2. remove x from root list of H Step 3. create a new binomial heap H1 by delete x and reverse its children Step 4. call union(H, H1) Time complexity: ? Space complexity: ?

Extracting the node with minimum key Min-Extract_Binomial Heap (H) Step 1: Find the root R having minimum value in the root list of H Step 2: Remove R from the root list of H Step 3: SET H' = Create_Binomial-Heap() Step 4: Reverse the order of R's children thereby forming a linked list Step 5: Set head[H'] to point to the head of the resulting list Step 6: SET H = Union_Binomial-Heap(H, H’) Step 7: Return R

Extracting the node with minimum key Example: Extract the node with minimum value from the binary heap given below. 8 19 4 36 14 10 18 12 21 Head [H] 5 24 29 31 39 16 4 36 14 10 18 12 21 19 5 24 29 31 39 R 8 Head [H] 16

Extracting the node with minimum key Head [H’] 36 14 10 18 12 21 19 5 24 29 31 39 8 Head [H] 16 36 Head [H] 14 21 12 5 18 29 31 24 39 8 16 10 19

Decreasing a value of a node The algorithm to decrease the value of a node x in a binomial heap H is given below. In the algorithm the value of the node is overwritten with a new value k, less than the current value of the node. The Binomial-Heap_Decrease_Val procedure takes O(lg n) time as the maximum depth of node x is lg n, so the while loop will iterate at most lg n times. Binomial-Heap_Decrease_Val(H, x, k) Step 1: IF Val{x] < k, then Print “ ERROR” Step 2: SET Val[x] = k Step 3: SET PTR = x and PAR = Parent[PTR] Step 4: Repeat while PAR ≠ NULL and Val[PTR] < Val[PAR] Step 5: SWAP ( Val[PTR}, Val[PAR] ) Step 6:

Deleting a node Once we have understood the Binomial-Heap_Decrease_Val procedure, it becomes very easy to delete a node x's value from binomial heap H in O(lg n) time. To start with the algorithm we set the value of x to - ∞. Assuming that there is no node in the heap that has a value less than -∞. The algorithm to delete a node from a binomial heap can be given as below. Binomial-Heap_Delete-Node(H, x) Step 1: Binomial-Heap_Decrease_Val(H, x, -∞) Step 2: Min-Extract_Binomial-Heap(H) Step 3: END