Top Banner
15-211 Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps
24

[PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

May 18, 2018

Download

Documents

vominh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

15-211Fundamental Structures of Computer Science

March 02, 2006Ananda Guna

Binomial Heaps

Page 2: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

In this Lecture

Binomial Trees Definition properties

Binomial Heaps efficient merging

Implementation Operations About Midterm

Page 3: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Binary Heaps

Binary heap is a data structure that allows insert in O(log n) deleteMin in O(log n) findMin in O(1)

How about merging two heaps complexity is O(n)

So we discuss a data structure that allows merge in O(log n)

Page 4: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Applications of Heaps

Binary Heaps efficient findMin, deleteMin many applications

Binomial Heaps Efficient merge of two heaps Merging two heap based data

structures Binomial Heap is build using a

structure called Binomial Trees

Page 5: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Binomial Trees

A Binomial Tree Bk of order k is defined as follows B0 is a tree with one node Bk is a pair of Bk-1 trees, where root of

one Bk-1 becomes the left most child of the other (for all k ≥ 1)

B0 B

1B

2B

3

Page 6: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Merging two binomial trees

Merging two equal binomial trees of order j

+=

New tree has order j + 1

Page 7: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Properties of Binomial trees

The following properties hold for a binomial tree of order k Bk has 2k nodes The height of Bk is k Bk has kCi nodes at level i for i = 0,1,…k The root of Bk has k-children B0, B1, …Bk-1 (in

that order) where the ith child is a binomial tree of order i.

If binomial tree of order k has n nodes, then k ≤ log n

Page 8: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

ProofsLemma 1: BK has 2k nodesProof: (by induction). True for k=0, assume true for k=r.

Consider Br+1

Br+1 has 2r + 2r = 2r+1 nodes

Lemma 2: Bk has height kProof: homework

Lemma 3: Bk has kCi nodes at level i for i = 0,1,…kProof: Let T(k,i) be the number of nodes at depth i. Then T(k,i)

= T(k-1,i) + T(k-1,i-1) = k-1Ci + k-1Ci-1 = kCi

Page 9: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Binomial Heap

Binomial Heap is a collection of binomial trees that satisfies the following properties No two binomial trees in the collection

have the same size Each node in the collection has a key Each binomial tree in the collection

satisfies the heap order property Roots of the binomial trees are

connected and are in increasing order

Page 10: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Example A binomial heap of n=15 nodes

containing B0, B1, B2 and B3 binomial trees

What is the connection between n and the binomial trees in the heap?

Page 11: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Lemma

Given any integer n, there exists a binomial heap that contain n nodes

Proof:

Page 12: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

implementation

Page 13: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Implementation – Binomial Tree Node

Fields in a binomial tree node Key number of children (or degree) Left most child Right most sibling A pointer P to parent

Page 14: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Implementation – Binomial Heap

head

root1 root2 root3 root4

Page 15: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Operations

Page 16: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Operations on Binomial Heaps

Merge is the key operation on binomial heaps merge() insert() findMin()

find the min of all children O(log n) deleteRoot() deleteNode() decreaseKey()

Page 17: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Merging two binomial heaps Suppose H1 and H2 are two binomial

heaps Merge H1 and H2 into a new heap H Algorithm:

Let A and B be pointers to H1 and H2 for all orders i

If there is one order i tree, merge it to H If there are two order i trees, merge them into a new

tree of order i+1 and store them in a temp tree T If there are three order i trees in H1,H2 and T, merge

two of them, store as T and add the remainder to H

Page 18: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Example

Page 19: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Binary Heap Operations

Insert make a new heap H0 with the new node Merge(H0, H)

FindMin min is one of the children connected to

the root cost is O(log n)

Page 20: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Binary Heap Operations

DeleteRoot() Find the tree with the given root Split the heap into two heaps H1 and H2

Page 21: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Binary Heap Operations

DeleteRoot() ctd.. Rearrange binomial trees in heap H2 Merge the two heaps

vv

Page 22: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Example

Page 23: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

DeleteNode()

To delete a node, decrease its key to -∞, percolate up to root, then delete the root

DecreaseKey(): Decrease the key and percolate up until heap order property is satisfied

Page 24: [PPT]15-211 Fundamental Structures of Computer Science · Web view15-211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna In this Lecture Binomial

Summary Two heaps

Binary Heaps deleteMin()

Binomial Heaps mergeHeaps()

Next Week Midterm on Tuesday Strings and Tries on thursday