Binomial Heap.

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.
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.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
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.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
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.
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.
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”
Lecture X Fibonacci Heaps
CS 473Lecture X1 CS473-Algorithms Lecture BINOMIAL HEAPS.
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
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.
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
Fibonacci Heaps.
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?
Balanced Search Trees Modified from authors’ slides.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Analysis of Algorithms
CS 583 Analysis of Algorithms
Data Structures – Week #8
Lecture 7 Algorithm Analysis
Binomial Heaps Chapter 19.
Binomial Tree Adapted from: Kevin Wayne Bk-1 B0 Bk
Red-Black Trees.
CMSC 341 (Data Structures)
Lecture 29 Heaps Chapter 12 of textbook Concept of heaps Binary heaps
Chapter 20 Binomial Heaps
ICS 353: Design and Analysis of Algorithms
ערמות בינומיות ופיבונצ'י
Ch. 12: Binary Search Trees
CS 583 Analysis of Algorithms
Fundamental Structures of Computer Science
Binary Search Trees (13.1/12.1)
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VII
Chapter 12: Binary Search Trees
ICS 353: Design and Analysis of Algorithms
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
Design and Analysis of Algorithms
Data Structures – Week #8
Binomial heaps, Fibonacci heaps, and applications
Priority Queues Supports the following operations. Insert element x.
Analysis of Algorithms CS 477/677
Fibonacci Heaps & Doubled-Ended Heap Structures
Binary Search Trees Comp 122, Spring 2004.
Presentation transcript:

Binomial Heap

Binomial Heap History Binomial heap was introduced in 1978 by Jean Vuillemin Jean Vuillemin is a professor in mathematics and computer science.

Binomial Tree A binomial heap is a collection of binomial trees. Binomial tree Bk is an ordered tree defined recursively. The binomial tree B0 has one node. The binomial tree Bk consists of two binomial trees Bk-1 and they are connected such that the root of one tree is the leftmost child of the other. Binomial tree properties: Bk has 2^k nodes Bk has height k There are exactly ( ik ) nodes at depth i for i=0, 1, 2,…,k. The root has degree k which is greater than other node in the tree. Each of the root’s child is the root of a subtree Bi.

Binomial Tree Example B3 B0 B1 B2 B4 6 7 10 15 4 6 7 5 8 15 23 11 20 25 B4 21 23 32 14 4 7 5 9 3 6 33 11 31 34 22

Binomial Heap Properties Each binomial tree in H obeys the min heap property: key of a node is greater or equal to the key of its parent. The root has the smallest key in the tree. There is at most one binomial tree whose root has a given degree. The binomial trees in the binomial heap are arranged in increasing order of degree Example: head[H] 5 1 2 7 10 12 10 13 3 15 15 10 12 16

Binomial Heap Implementation Each node has the following fields: p: parent child: leftmost child sibling Degree Key Roots of the trees are connected using linked list.

Binomial Heap Implementation c) key degree NIL NIL child sibling 2 1 2 head[H] NIL NIL b) 10 12 head[H] 2 1 1 NIL NIL 10 12 15 15 NIL NIL

Binomial Heap Operations Create heap Find minimum key Union two binomial heap Insert a node Extract minimum node Decrease a key Delete a node

Create A New Binomial Heap The operation simply creates a new pointer and sets it to NIL. Pseudocode: Binomial-Heap-Create() 1 head[H] <- NIL 2 return head[H] Run time is θ(1).

Find Minimum Key Since the binomial heap is a min-heap-order, the minimum key of each binomial tree must be at the root. This operation checks all the roots to find the minimum key. Pseudocode: this implementation assumes that there are no keys with value ∞ Binomial-Heap-Minimum(H) 1 y <- NIL 2 x <- head[H] 3 min <- ∞ 4 while x is not NIL 5 do if key[x] < min then 6 min <- key[x] 7 y <- x 8 x <- sibling[x] 9 return y Run time: The run time is in the order of O(log n) since the most number of roots in binomial heap is |_(log n)_| +1

Find Minimum Key Example b) head[H] 2 5 1 head[H] 2 5 1 7 10 12 7 10 12 15 15 c) d) head[H] 2 5 1 head[H] 2 5 1 7 10 12 7 10 12 15 15

Union Two Binomial Heaps This operation consists of the following steps Merge two binomial heaps. The resulting heap has the roots in increasing order of degree For each tree in the binomial heap H, if it has the same order with another tree, link the two trees together such that the resulting tree obeys min-heap-order.

Case1: degree[x] ≠ degree[next-x]. The pointer move one position further down to the list. Case2: degree[x]=degree[next-x]=degree[sibling[next-x]]. Again pointer move one position further down to the list, and next iteration executes either case 3 or case 4. Case3: degree[x]=degree[next-x]≠degree[sibling[next-x]] and key[x]<=key[next-x]. We remove next-x from the root list and link to the x. Case4: degree[x]=degree[next-x]≠degree[sibling[next-x]] and key[x]>=key[next-x]. We remove x from the root list and link it to next-x. 12/6/2018

Union Two Binomial Heaps head[H1] 2 11 1 a) head[H2] 3 4 20 10 12 9 15 b) head[H1] 2 3 11 4 1 20 9 10 12 15

Union Two Binomial Heaps c) head[H1] 2 4 1 3 11 9 10 12 20 15 d) head[H1] 2 1 3 4 10 12 11 9 15 20

Union Two Binomial Heaps Binomial-Heap-Union(H1,H2) H <- Make-Binomial-Heap() Head[H] <- Binomial-Merge(H1,H2) Free the objects H1 and H2 but not the lists they point to If head[H] = NIL then return H Prev-x <-NIL X <- head[H] Next-x <- sibling[x] while next-x not NIL do if(degree[x] not degree[next-x]) or (sibling[next-x not NIL and degree[sibling[next-x]]=degree[x]) then prev-x <-x x <- next-x else if key[x] <= key[next-x] then sibling[x] <- sibling[next-x] Binomial-Link(next-x,x) else if prev-x = NIL then head[H] <-next-x else sibling[prev-x] <- next-x Binomial-Link(x,next-x) next-x <- sibling[x] return H

Union Two Binomial Heaps Pseudocode: Binomial-Link(y,z) 1 p[y] <- z 2 sibling[y] <- child[z] 3 child[z] <- y degree[z] <- degree[z] + 1 Example: link node 5 to node 1 5 1 1 7 12 5 12 child 7 sibling parent

Union Two Binomial Heaps Binomial-Heap-Merge(H1,H2) P  Head[H]; P1  Head[H1]; P2  Head[H2] while P1 ≠ NIL OR P2 ≠ NIL do if degree[P1] < degree[P2] then sibling [P] P1; P1  sibling[P1] P<-sibling[p] else sibling[P]  P2; P2  sibling[P2] Run time: The running time is O (log n) The total number of combined roots is at most |_logH1_| + |_logH2_| +2. Binomial-Heap-Merge is O (log n) + the while loop is O (log n). Thus, the total time is O(log n).

Insert New Node Create a new heap H’ and set head[H’] to the new node. Union the new heap H’ with the existing heap H. Pseudocode: Binomial-Heap-Insert(H,x) H’ <- Make-Binomial-Heap() p[x] <- NIL child[x] <- NIL sibling[x] <- NIL degree[x] <- 0 head[H’] <- x H <- Binomial-Heap-Union(H,H’) Run time: O(log n)

Insert New Node Example 5 head[H’] 5 head[H] 1 10 12 15 head[H] 5 1 10 12 15

Extract Node With Minimum Key This operation is started by finding and removing the node x with minimum key from the binomial heap H. Create a new binomial heap H’ and set to the list of x’s children in the reverse order. Unite H and H’ to get the resulting binomial heap. Pseudocode 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’s children, and set head[H’] to point to the head of the resulting list. 4 H <- Binomial-Heap-Union(H,H’) 5 Return x Run time: O(log n)

Extract Minimum Key Example head[H] 5 1 2 7 10 12 10 12 3 15 15 10 12 15 head[H] 5 1 2 7 10 12 10 12 3 15 15 10 12 15

Extract Minimum Key Example head[H] 5 2 head[H’] 12 10 7 10 12 15 2 15 10 12 15 head[H] 12 5 2 10 7 10 12 2 15 15 10 12 15

Decreasing a key The current key is replaced with a new key. To maintain the min-heap property, it is then compared to the key of the parent. If its parent’s key is greater then the key and data will be exchanged. This process continues until the new key is greater than the parent’s key or the new key is in the root. Pseudocode: Binomial-Heap-Decrease-Key(H,x,k) 1 if k > key[x] 2 then error “new key is greater than current key” 3 key[x] <-k 4 y <-x 5 z <-p[y] 6 while z not NIL and key[y] < key[z] 7 do exchange key[y] <-> key[z] 8 if y and z have satellite fields, exchange them, too. 9 y <- z 10 z <- p[y]

Decreasing a key Execution time: This procedure takes O(log n) since the maximum depth of x is |_log n_|. Example: head[H] 5 2 head[H] 5 2 10 12 10 12 15 1 head[H] 5 2 head[H] 5 1 1 12 2 12 10 10

Delete a Node With assumption that there is no node in H has a key of -∞. The key of deleting node is first decreased to -∞. This node is then deleted using extracting min procedure. Pseudocode: (from book) Binomial-Heap-Delete(H,x) 1 Binomial-Heap-Decrease-Key(H,x,-∞) 2 Binomial-Heap-Extract-Min(H) Run time: O(log n) since the run time of both Binomial-Heap-Decrease-Key and Binomial-Heap-Extract-Min procedures are in order of O(log n).

Delete a Node Example a) b) c) d) -∞ 5 2 12 10 15 head[H] 5 2 12 -∞ 15

Delete a Node Example e) f) g) head[H] 5 12 2 head[H] 5 2 15 12 15

Compare With Binary Heap Procedure Binomial Heap Binary Heap Make-Heap O (1) Insert O (log n) Minimum Extract-Min Union O (n) Decrease-Key Delete

References Thomas H. Cormen, Charles E. Leiserson, Ronald L. Revest, and Clifford Stein, Introduction To Algorithms, McGraw-Hill Higher Education, second edition, 2001