CS 583 Analysis of Algorithms

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

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.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
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.
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
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.
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.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
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.
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”
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
Christopher Moh 2005 Application of Data Structures.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Lecture X Fibonacci Heaps
CS 473Lecture X1 CS473-Algorithms Lecture BINOMIAL HEAPS.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
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.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
Fibonacci Heaps.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Analysis of Algorithms
Binary Search Trees What is a binary search tree?
Heaps, Heap Sort and Priority Queues
Chapter 11: Multiway Search Trees
Lecture 22 Binary Search Trees Chapter 10 of textbook
Heapsort.
CMSC 341 Lecture 13 Leftist Heaps
CS 583 Analysis of Algorithms
Data Structures – Week #8
Lecture 7 Algorithm Analysis
Binomial Heaps Chapter 19.
Binary Trees, Binary Search Trees
CS 583 Analysis of Algorithms
Multi-Way Search Trees
Lecture 29 Heaps Chapter 12 of textbook Concept of heaps Binary heaps
Binomial Heap.
Chapter 20 Binomial Heaps
ערמות בינומיות ופיבונצ'י
CS 583 Analysis of Algorithms
Fundamental Structures of Computer Science
Lecture 7 Algorithm Analysis
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
21장. Fibonacci Heaps 숭실대학교 인공지능 연구실 석사 2학기 김완섭.
Topic 6: Binary Search Tree Data structure Operations
Binary and Binomial Heaps
Topic 5: Heap data structure heap sort Priority queue
Binomial heaps, Fibonacci heaps, and applications
HEAPS.
Heapsort Sorting in place
Design and Analysis of Algorithms
Priority Queues Supports the following operations. Insert element x.
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
Presentation transcript:

CS 583 Analysis of Algorithms Binomial Heaps CS 583 Analysis of Algorithms 1/17/2019 CS583 Fall'06: Binomial Heaps

CS583 Fall'06: Binomial Heaps Outline Definitions Binomial trees Binomial heaps Operations on Binomial Heaps Creating a new heap Finding the minimum key Union Self-testing 19.1-1, 19.2-1 1/17/2019 CS583 Fall'06: Binomial Heaps

CS583 Fall'06: Binomial Heaps Introduction Heap is a data structure that has enormous utility. We saw how a binary heap was used to design an efficient heapsort algorithm. Heaps are used to implement priority queues. Priority queue maintains elements according to their keys. Min-priority queue supports the following operations: Insert(Q,x) – insert an element x Minimum(Q) – returns the element with the smallest key Extract-Min(Q) – removes and return the min element Decrease-Key(Q,x,k) – decrease the value of the key of x to k. For example, priority queues are used to schedule jobs on shared computer resources. 1/17/2019 CS583 Fall'06: Binomial Heaps

CS583 Fall'06: Binomial Heaps Introduction (cont.) We study data structures known as mergeable heaps, which support the following operations: Make-Heap() creates and returns an empty heap. Insert(H,x) inserts a node x with a key into a heap H. Minimum(H) returns a pointer to the node whose key is minimum. Extract-Min(H) deletes the minimum node and returns its pointer. Union(H1, H2) creates and returns a new heap that contains all nodes from H1 and H2. Binary heaps work well if we don’t need the Union operation that takes (n) time. The Union on binomial heaps takes O(lg n) time. 1/17/2019 CS583 Fall'06: Binomial Heaps

CS583 Fall'06: Binomial Heaps Binomial Trees The ordered tree is a rooted tree, where the order of children nodes matters. The binomial tree Bk is an ordered tree defined recursively: B0 consists of a single node. Bk consists of two binomial trees Bk-1 that are linked together: The root of one tree is the leftmost child of the root of another tree. 1/17/2019 CS583 Fall'06: Binomial Heaps

Binomial Trees: Properties Lemma 19.1. For the binomial tree Bk, there are 2k nodes, the height of the tree is k, there are exactly (ki) nodes at depth i for i=0,1,...,k the root has degree k, which is greater than any other node; moreover if children of the root are numbered left to right by k-1,k-2,...,0, the child i is the root of subtree Bi. Proof. The proof is by induction. Verifying all properties for B0 is trivial. Assume the lemma holds for Bk-1. 1. Binomial tree Bk consists of two copies of Bk-1, hence Bk has 2k-1+2k-1 = 2k nodes. 1/17/2019 CS583 Fall'06: Binomial Heaps

Binomial Trees: Properties (cont.) 2. The way two copies of Bk-1 are linked together, the height of Bk is increased by one compared to Bk-1. Hence its maximum depth is 1 + (k-1) = k. 3. Let D(k,i) be the number of nodes at depth i of Bk. Bk is composed of two Bk-1 with one tree a one level below another one. Hence the number of nodes at level i of Bk is all nodes at level i of the upper Bk-1 and all nodes at level (i-1) of the lower Bk-1: D(k,i) = D(k-1, i) + D(k-1, i-1) = (by inductive hypothesis) (k-1i) + (k-1i-1) = (see Exercise C.1-7 & HW2) (ki). 1/17/2019 CS583 Fall'06: Binomial Heaps

Binomial Trees: Properties (cont.) 4. The only node with the greater degree in Bk than Bk-1 is the root, which has one more child. Hence the root of Bk has degree (k-1) + 1 = k. By inductive hypothesis children of Bk-1 are roots of Bk-2, Bk-3, ... , B0. Once Bk-1 is linked to Bk-1 from the left, the children of the resulting root are Bk-1, Bk-2, ... , B0.  Corollary 19.2 The maximum degree of any node in an n-node binomial tree is lg n. Proof. Follows from properties 1 and 4. The root has the maximum degree k, where k = lg n.  1/17/2019 CS583 Fall'06: Binomial Heaps

CS583 Fall'06: Binomial Heaps A binomial heap H is a set of binomial trees that satisfies the following properties: Each binomial tree in H obeys the min-heap property: the key of a node >= the key of its parent this ensures that the root of the min-heap-ordered tree contains the smallest key. For any k >= 0, there is at most one binomial tree in H whose root has degree k. this implies that an n-node binomial heap H consists of at most (lg n + 1) binomial trees. it can be observed by a binary representation of n as (lg n + 1) bits. A bit i = 1 only if a tree Bi is in the heap. 1/17/2019 CS583 Fall'06: Binomial Heaps

Binomial Heaps: Representation Each binomial tree within a binomial heap is stored in the left-child, right-sibling representation. Each node has the following fields: p[x] pointer to the parent. child[x] pointer to the leftmost child. sibling[x] pointer to the sibling immediately to the right. degree[x] – the number of children of x. The roots of the binomial trees in the heap are organized in a linked list, -- root list. The degrees of roots increase when traversing to the right. A heap H is accessed by the field head[H], which is the pointer to the first root in the root list. 1/17/2019 CS583 Fall'06: Binomial Heaps

Operations: Finding the Minimum This procedure returns a node with the minimum key in an n-node heap H. Note that in the min-heap-ordered heap, the minimum key must reside in a root node. Binomial-Heap-Minimum(H) 1 y = NIL 2 x = head[H] 3 min =  4 while x <> NIL 5 if key[x] < min 6 min = key[x] 7 y = x 8 x = sibling[x] // next root 9 return y Because there are at most (lg n + 1) roots to check, the running time of this algorithm is O(lg n). 1/17/2019 CS583 Fall'06: Binomial Heaps

CS583 Fall'06: Binomial Heaps Uniting Two Heaps The operation of uniting two heaps is used as a subroutine by most of other operations. The procedure is implemented by repeatedly linking binomial trees whose roots have the same degree. It uses an auxiliary Binomial-Link procedure to link two trees. It also uses an auxiliary Binomial-Heap-Merge procedure to merge two root lists into a single linked list sorted by degree. Uniting two heaps H1 and H2 returns a new heap and destroys H1 and H2 in the process. 1/17/2019 CS583 Fall'06: Binomial Heaps

CS583 Fall'06: Binomial Heaps Binomial Link This procedure links the Bk-1 tree rooted at node y to the Bk-1 tree rooted at node z. Node z becomes the root of a Bk tree. The procedure is straightforward because of the left-child, right-sibling representation of trees. Binomial-Link(y,z) 1 p[y] = z 2 sibling[y] = child[z] 3 child[z] = y 4 degree[z]++ The procedure simply updates pointers in constant time (1). 1/17/2019 CS583 Fall'06: Binomial Heaps

CS583 Fall'06: Binomial Heaps Heap Union Algorithm Binomial-Heap-Union(H1, H2) 1 H = Make-Binomial-Heap // create an empty heap // Merge the root lists of H1, H2 sorted by degree 2 head[H] = Binomial-Heap-Merge(H1,H2) 3 <free objects H1,H2, but not its lists> 4 if head[H] = NIL 5 return H 6 prev-x = NIL 7 x = head[H] // next tree in the root list next-x = sibling[x] In the first phase of the algorithm two root lists are merged in a single one. In the second phase, two trees in the root list are linked if they have the same degree. 1/17/2019 CS583 Fall'06: Binomial Heaps

Heap Union Algorithm (cont.) 9 while next-x <> NIL // traverse the root list 10 if (degree[x] <> degree[next-x]) or (sibling[next-x] <> NIL and degree[sibling[next-x]] = degree[x])) 11 prev-x = x // skip to the next node 12 x = next-x 13 else // retain min-heap property 14 if key[x] <= key[next-x] // link into x 15 sibling[x] = sibling[next-x] 16 Binomial-Link(next-x,x) 1/17/2019 CS583 Fall'06: Binomial Heaps

Heap Union Algorithm (cont.) 17 else // link into next-x 18 if prev-x = NIL 19 head[H] = next-x 20 else 21 sibling[prev-x] = next-x 22 Binomial-Link(x, next-x) 23 x = next-x 24 next-x = sibling[x] 25 return H 1/17/2019 CS583 Fall'06: Binomial Heaps

Heap Union: Performance The running time of Binomial-Heap-Union is O(lg n), where n is the total number of nodes in binomial heaps H1 and H2. Let H1 contain n1 nodes and H2 contain n2 nodes: n=n1+n2. After merging two heaps, H contains at most lg n1 + lg n2 + 2 <= 2 lg n + 2 = O(lg n) roots. Hence the time to perform Binomial-Heap-Merge is O(lg n). Each iteration of the while loop takes (1) time, and there are at most lg n1 + lg n2 + 2 = O(lg n) iterations. Each iteration either advances the pointers one position down the root list, or removes a root from the root list. 1/17/2019 CS583 Fall'06: Binomial Heaps