CMSC 341 Binomial Queues and Fibonacci Heaps. Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) deleteMinO(lgN)

Slides:



Advertisements
Similar presentations
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.
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
COL 106 Shweta Agrawal and Amit Kumar
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.
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.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
1 Pertemuan 20 Binomial Heap Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
1 COSC 2P03 Lecture #5 – Trees Part III, Heaps. 2 Today Take up the quiz Assignment Questions Red-Black Trees Binary Heaps Heap sort D-Heaps, Leftist.
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.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 11 Tuesday, 12/4/01 Advanced Data Structures Chapters.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
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.
§3 Binary Heap 1. Structure Property: 【 Definition 】 A binary tree with n nodes and height h is complete iff its nodes correspond to the nodes numbered.
ANALYSIS OF SOFT HEAP Varun Mishra April 16,2009.
1 Binomial heaps, Fibonacci heaps, and applications.
Algorithms 2005 Ramesh Hariharan. Amortization in Dynamic Algorithms A single insertion/deletion might take say O(log n) time Does a sequence of n insertions.
Binomial heaps, Fibonacci heaps, and applications
The Power of Incorrectness A Brief Introduction to Soft Heaps.
§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.
Binomial Queues Text Read Weiss, §6.8 Binomial Queue Definition of binomial queue Definition of binary addition Building a Binomial Queue Sequence of inserts.
CS 473Lecture X1 CS473-Algorithms Lecture BINOMIAL HEAPS.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Cpt S 223 – Advanced Data Structures Priority Queues
Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Change Keys in heaps Fibonacci heap Zhao Xiaobin.
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
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.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
1 Fat heaps (K & Tarjan 96). 2 Goal Want to achieve the performance of Fibonnaci heaps but on the worst case. Why ? Theoretical curiosity and some applications.
Two-tier relaxed heaps Presented by Claus Jensen Joint work with Amr Elmasry and Jyrki Katajainen Slides available at
Priority Queues CS 110: Data Structures and Algorithms First Semester,
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.
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Fibonacci Heap Fibonacci heapshave better asymptotic time bounds than binary heaps for the INSERT, UNION, and DECREASE-KEY operations, and they.
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
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))
CS 201 Data Structures and Algorithms
Data Structures Binomial Heaps Fibonacci Heaps Haim Kaplan & Uri Zwick
Binomial heaps, Fibonacci heaps, and applications
Heaps Binomial Heaps Lazy Binomial Heaps 1.
Source: Muangsin / Weiss
CMSC 341 Lecture 13 Leftist Heaps
Insert in amortized cost O(1), Merge in O(lgn) DeleteMax in O(lgn)
Priority Queues MakeQueue create new empty queue
ערמות בינומיות ופיבונצ'י
Fundamental Structures of Computer Science
CE 221 Data Structures and Algorithms
Binomial heaps, Fibonacci heaps, and applications
Fibonacci Heaps.
Binomial heaps, Fibonacci heaps, and applications
Priority Queues Supports the following operations. Insert element x.
CS 6310 Advanced Data Structure Wei-Shian Wang
Fibonacci Heaps & Doubled-Ended Heap Structures
Fundamental Structures of Computer Science II
CSE 373 Data Structures Lecture 12
Priority Queues Binary Heaps
Presentation transcript:

CMSC 341 Binomial Queues and Fibonacci Heaps

Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) deleteMinO(lgN) decrease Key O(lgN) mergeO(N)O(lgN) findMinO(1)

Amortized Time Binomial Queues and Fibonacci Heaps have better performance in an amortized sense Cost per operation vs. cost for sequence of operations RB trees are O(lgN) per operation Splay trees are O(M lgN) for M operations –Individual ops can be more/less expensive that O(lgN) –If one is more expensive, another is guaranteed to be less –On average, cost per op is O(lgN)

Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) O(1) amortized deleteMinO(lgN) decrease Key O(lgN) mergeO(N)O(lgN) findMinO(1)

Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) O(1) amortized deleteMinO(lgN) O(lgN) amortized decrease Key O(lgN) O(1) amortized mergeO(N)O(lgN) O(1) amortized findMinO(1) O(1) amortized

Binomial Tree Has heap order property B k = binomial tree of height k B 0 = tree with one node B k formed by adding a B k-1 as child of root of another B k-1 B k has exactly 2 K nodes (why?)

Binomial Trees B0B0 B1B1 B2B2 B3B3

Binomial Queue A collection (list) of Binomial Trees –A forest No more than one B k in queue for each k Queue with N nodes contains no more than  lgN  trees (why?)

findMin Scan trees and return smallest root –O(lgN) because there are O(lgN) trees Keep track of tree with smallest root –Update due to other operations (e.g. insert, deleteMin) –O(1)

merge Merge Q1 and Q2, creating Q3 Q3 can contain only one B k for each k If only one of Q1 and Q2 contain a B k, add it to Q3 What if Q1 and Q2 both contain a B k ? Merge them and add a B k+1 to Q3 Now what if Q1, Q2, or both contain a B k+1 ? Merge until there are zero or one of them

merge Think of Q1 and Q2 as binary integers Bit k=1 iff queue contains a B k Compute Q3 = Q1 + Q2 To compute value of bit k for Q3 –Add bit k from Q1 and Q2 and carry from position k-1 –Adding bits corresponds to merging trees –May generate carry bit (tree) to position k+1

merge Complexity is O(lgN) There are O(lgN) trees in Q1 and Q2 Merging trees takes O(1)

merge example Q2: Q1:

Q2: Q1: Q3:

Q2: Q1: Q3: 13

Q2: Q1: Q3:

Q2: Q1:

Q2: Q1:

insert Insert value X into queue Q Create binomial queue Q’ with B 0 containing X Merge Q with Q’ Worst case O(lgN) because Q can contain lgN trees Suppose B i is smallest tree not in Q, then time is O(i)

insert Suppose probability that Q contains B k for any k is 1/2 Probability that B i is smallest tree not in Q is 1/2 i (why?) The expected value of i is then: –∑i*(1/2i) = 2 On average, insertion will require a single merge and is therefore O(1) amortized

deleteMin Find tree with minimum root and remove root Treat sub-trees of root as a new binomial queue Merge this new queue with the original one O(lgN)

Fibonacci Heap All heap operations take O(1) amortized time! Except deleteMin, which takes O(lgN) amortized time Implemented using Binomial Queue Two new ideas –Lazy merging –New implementation of decreaseKey

Lazy Merging To merge Q1 and Q2, just concatenate lists of Binomial Trees This takes O(1) time Result may contain multiple B k for any given k (we’ll deal with this in a minute) Insertion is now O(1) (why?)

deleteMin Scan list of trees for one with smallest root –No longer guaranteed to be O(lgN) because of duplicate B k from lazy merging Remove root and lazily merge sub-trees with binomial queue Reinstate binomial queue by merging trees to ensure at most one B k for any k

Reinstating a Binomial Queue R = rank of tree, number of children of root L R = set of all trees of rank R in queue T = number of trees in queue Code below is O(T + lgN) (why?) for (R = 0; R <= lgN; R++) while {|L R | >= 2} remove two trees from L R merge them into a new tree add the new tree to L R+1

deleteMin Example

deleteMin Example Remove this node

deleteMin Example More than one B 0 tree, merge

deleteMin Example More than one B 1 tree, merge

deleteMin Example Still more than one B 1 tree, merge

deleteMin Example More than one B 2 tree, merge

deleteMin Example

Complexity of deleteMin Theorem: The amortized running time of deleteMin is O(lgN) Proof: It’s a bit tricky! But it’s in the text for those with burning curiosity.

decreaseKey Standard approach is to change value (decrease it) and percolate up Not O(1), which is goal, unless height of tree is O(1) Instead, decrease value and then cut link between node and parent yielding two trees

Cut Example Decrease key 15 to 1 Cut

Cascading Cuts When cutting, do the following –Mark a (non-root) node the first time that it loses a child due to a cut –If a marked node loses another child, then cut it from its parent. This node becomes the root of a separate tree that is no longer marked. This is called a cascading cut because several could occur due to a single decreaseKey.

Cascading Cuts Example * 33* Parts of tree not shown Nodes with * marked Decrease 39 to 12

Cascading Cuts Example * 33* Decrease value Cut from parent

Cascading Cuts Example * Marked node (33) lost second child Cut from parent and unmark

Cascading Cuts Example Marked node (10) lost second child Cut from parent and unmark

Cascading Cuts Example 3 5* Unmarked node (5) loses first child Mark it

Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) O(1) amortized deleteMinO(lgN) O(lgN) amortized decrease Key O(lgN) O(1) amortized mergeO(N)O(lgN) O(1) amortized findMinO(1) O(1) amortized