§8 Binomial Queues Haven’t we had enough about queues? What is a binomial queue for? Well, what is the average time for insertions with leftist or skew.

Slides:



Advertisements
Similar presentations
 Have to copy one array into another (N)
Advertisements

§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
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)
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
Priority Queues  MakeQueuecreate new empty queue  Insert(Q,k,p)insert key k with priority p  Delete(Q,k)delete key k (given a pointer)  DeleteMin(Q)delete.
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.
Advanced Data structure
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Rank-Pairing Heaps Bernhard Haeupler, Siddhartha Sen, and Robert Tarjan, ESA
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Nick Harvey & Kevin Zatloukal
CSE 373 Data Structures Lecture 12
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.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
5.9 Heaps of optimal complexity
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.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
ANALYSIS OF SOFT HEAP Varun Mishra April 16,2009.
1 Binomial heaps, Fibonacci heaps, and applications.
Chapter 9 Heap Structures
Binomial Queues Text Read Weiss, §6.8 Binomial Queue Definition of binomial queue Definition of binary addition Building a Binomial Queue Sequence of inserts.
Chapter 21 Binary Heap.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Cpt S 223 – Advanced Data Structures Priority Queues
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Priority Queues (Heaps)
Change Keys in heaps Fibonacci heap Zhao Xiaobin.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
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.
Heaps Priority Queues. 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist heaps.
CMSC 341 Binomial Queues and Fibonacci Heaps. Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) deleteMinO(lgN)
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Fibonacci heaps: idea List of multiway trees which are all heap-ordered. Definition: A tree is called heap-ordered if the key of each node is greater.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
CS 201 Data Structures and Algorithms
Source: Muangsin / Weiss
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
CMSC 341 Lecture 14 Priority Queues & Heaps
CE 221 Data Structures and Algorithms
Heaps Priority Queues.
Binary Trees, Binary Search Trees
CS 6310 Advanced Data Structure Wei-Shian Wang
Fundamental Structures of Computer Science II
CSE 373 Data Structures Lecture 12
Binary Trees, Binary Search Trees
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Presentation transcript:

§8 Binomial Queues Haven’t we had enough about queues? What is a binomial queue for? Well, what is the average time for insertions with leftist or skew heaps? O(log N) – What’s wrong? What is the total time for inserting N keys into an empty binary heap? According to Theorem 5.1 on p.156, the total time should be O(N)… Then what is the average time? Constant! So O(log N) for an insertion is NOT good enough!  Structure: A binomial queue is not a heap-ordered tree, but rather a collection of heap-ordered trees, known as a forest. Each heap-ordered tree is a binomial tree. A binomial tree of height 0 is a one-node tree. A binomial tree, B k, of height k is formed by attaching a binomial tree, B k – 1, to the root of another binomial tree, B k – 1. B0B0 B1B1 B2B2 B3B3 1/12 Discussion 13: B k consists of a root with children, which are. B k has exactly nodes. The number of nodes at depth d is.

§8 Binomial Queues 【 Example 】 Represent a priority queue of size 13 by a collection of binomial trees. B k structure + heap order + one binomial tree for each height A priority queue of any size can be uniquely represented by a collection of binomial trees. Solution: 13 =  = B0B0 B1B1 B2B2 B3B /12

§8 Binomial Queues  Operations:  FindMin: The minimum key is in one of the roots. There are at most roots, hence T p = O( ). log N Note: We can remember the minimum and update whenever it is changed. Then this operation will take O(1).  Merge: H1H1 H2H H1H = = c c T p = O( )log N Must keep the trees in the binomial queue sorted by height. 3/12

§8 Binomial Queues  Insert: a special case for merging. 【 Example 】 Insert 1, 2, 3, 4, 5, 6, 7 into an initially empty queue /12 Discussion 14: What is the average time complexity of performing N insertions on an initially empty binomial queue? Prove your conclusion.

§8 Binomial Queues  DeleteMin ( H ): H H’H’ Step 1: FindMin in B k Step 2: Remove B k from H Step 3: Remove root from B k H”H” Step 4: Merge (H’, H”) H /* O(log N) */ /* O(1) */ /* O(log N) */ 5/12

§8 Binomial Queues  Implementation: Binomial queue = array of binomial trees DeleteMin Merge Find all the subtrees quickly The children are ordered by their sizes OperationPropertySolution 6/12 Discussion 15: How can we implement the trees so that all the subtrees can be accessed quickly? Discussion 16: In which order must we link the subtrees?

§8 Binomial Queues typedef struct BinNode *Position; typedef struct Collection *BinQueue; typedef struct BinNode *BinTree; /* missing from p.176 */ struct BinNode { ElementType Element; Position LeftChild; Position NextSibling; } ; struct Collection { int CurrentSize; /* total number of nodes */ BinTreeTheTrees[ MaxTrees ]; } ; 7/12

§8 Binomial Queues BinTree CombineTrees( BinTree T1, BinTree T2 ) { /* merge equal-sized T1 and T2 */ if ( T1->Element > T2->Element ) /* attach the larger one to the smaller one */ return CombineTrees( T2, T1 ); /* insert T2 to the front of the children list of T1 */ T2->NextSibling = T1->LeftChild; T1->LeftChild = T2; return T1; } T p = O( 1 ) T1 T2 8/12

§8 Binomial Queues BinQueue Merge( BinQueue H1, BinQueue H2 ) {BinTree T1, T2, Carry = NULL; int i, j; if ( H1->CurrentSize + H2-> CurrentSize > Capacity ) ErrorMessage(); H1->CurrentSize += H2-> CurrentSize; for ( i=0, j=1; j CurrentSize; i++, j*=2 ) { T1 = H1->TheTrees[i]; T2 = H2->TheTrees[i]; /*current trees */ switch( 4*!!Carry + 2*!!T2 + !!T1 ) { case 0: /* 000 */ case 1: /* 001 */ break; case 2: /* 010 */ H1->TheTrees[i] = T2; H2->TheTrees[i] = NULL; break; case 4: /* 100 */ H1->TheTrees[i] = Carry; Carry = NULL; break; case 3: /* 011 */ Carry = CombineTrees( T1, T2 ); H1->TheTrees[i] = H2->TheTrees[i] = NULL; break; case 5: /* 101 */ Carry = CombineTrees( T1, Carry ); H1->TheTrees[i] = NULL; break; case 6: /* 110 */ Carry = CombineTrees( T2, Carry ); H2->TheTrees[i] = NULL; break; case 7: /* 111 */ H1->TheTrees[i] = Carry; Carry = CombineTrees( T1, T2 ); H2->TheTrees[i] = NULL; break; } /* end switch */ } /* end for-loop */ return H1; } 9/12 Discussion 17: What does each case number mean?

§8 Binomial Queues ElementType DeleteMin( BinQueue H ) {BinQueue DeletedQueue; Position DeletedTree, OldRoot; ElementType MinItem = Infinity; /* the minimum item to be returned */ int i, j, MinTree; /* MinTree is the index of the tree with the minimum item */ if ( IsEmpty( H ) ) { PrintErrorMessage(); return –Infinity; } for ( i = 0; i < MaxTrees; i++) { /* Step 1: find the minimum item */ if( H->TheTrees[i] && H->TheTrees[i]->Element < MinItem ) { MinItem = H->TheTrees[i]->Element; MinTree = i; } /* end if */ } /* end for-i-loop */ DeletedTree = H->TheTrees[ MinTree ]; H->TheTrees[ MinTree ] = NULL; /* Step 2: remove the MinTree from H => H’ */ OldRoot = DeletedTree; /* Step 3.1: remove the root */ DeletedTree = DeletedTree->LeftChild; free(OldRoot); DeletedQueue = Initialize(); /* Step 3.2: create H” */ DeletedQueue->CurrentSize = ( 1<<MinTree ) – 1; /* 2 MinTree – 1 */ for ( j = MinTree – 1; j >= 0; j – – ) { DeletedQueue->TheTrees[j] = DeletedTree; DeletedTree = DeletedTree->NextSibling; DeletedQueue->TheTrees[j]->NextSibling = NULL; } /* end for-j-loop */ H->CurrentSize – = DeletedQueue->CurrentSize + 1; H = Merge( H, DeletedQueue ); /* Step 4: merge H’ and H” */ return MinItem; } 10/12 This can be replaced by the actual number of roots Home work: p Merge two given binomial queues p Insert without calling Merge

Research Project 7 Amortized Analysis (23) Amortized analysis considers the worst-case running time for any sequence of M operations. This contrasts with the more typical analysis in which a worst-case bound is given for any single operation. This project requires you to analyze the binomial queue operation Merge. You must introduce the potential function for Merge, illustrate the amortized running time bound, and prove your conclusions. Detailed requirements can be downloaded from 11/12

Research Project 8 Fibonacci Heaps (23) A Fibonacci heap is a heap data structure consisting of a forest of trees. It has a better amortized running time than a binomial heap. Fibonacci heaps were developed by Michael L. Fredman and Robert E. Tarjan in 1984 and first published in a scientific journal in The name of Fibonacci heap comes from Fibonacci numbers which are used in the running time analysis. This project requires you to introduce the Fibonacci heap and compare it with binomial heaps. Detailed requirements can be downloaded from 12/12