Nick Harvey & Kevin Zatloukal

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

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.
Advanced Data structure
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2009 Heap Lecture Chapter 6 Use NOTES feature to see explanation.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Analysis of Algorithms CS 477/677
A Binary Tree root leaf. A Binary Tree root leaf descendent of root parent of leaf.
Rank-Pairing Heaps Bernhard Haeupler, Siddhartha Sen, and Robert Tarjan, ESA
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
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.
1 Binary heaps binary tree that satisfy two properties –structural property (is a complete tree) –heap-ordering property (minimum item on top) Can have.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Tirgul 7 Heaps & Priority Queues Reminder Examples Hash Tables Reminder Examples.
CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Tree Traversals Pre-order traversal –process root.
Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
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.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
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.
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
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.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Chapter 21 Binary Heap.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
What is a heap? Always keep the thing we are most interested in close to the top (and fast to access). Like a binary search tree, but less structured.
Change Keys in heaps Fibonacci heap Zhao Xiaobin.
+ 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,
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.
1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
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.
8 January Heap Sort CSE 2011 Winter Heap Sort Consider a priority queue with n items implemented by means of a heap  the space used is.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
CSE 5392 Fall 2005 Week 4 Devendra Patel Subhesh Pradhan.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
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.
CSC 2300 Data Structures & Algorithms March 13, 2007 Chapter 6. Priority Queues.
Fibonacci Heap Fibonacci heapshave better asymptotic time bounds than binary heaps for the INSERT, UNION, and DECREASE-KEY operations, and they.
Nov 2, 2001CSE 373, Autumn Hash Table example marking deleted items + choice of table size.
Source: Muangsin / Weiss
Priority Queue & Heap CSCI 3110 Nan Chen.
Heaps and the Heapsort Heaps and priority queues
Heap Sort CSE 2011 Winter January 2019.
Binary and Binomial Heaps
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heaps & Multi-way Search Trees
Presentation transcript:

Nick Harvey & Kevin Zatloukal The Post-Order Heap Nick Harvey & Kevin Zatloukal

Binary Heap Review … Simple priority queue keys 1 3 5 12 9 11 7 … 15 13 18 20 12 23 Simple priority queue “Heap-ordered”: parent key < children keys Insert, DeleteMin require O(log n) time

Binary Heap Review … … “Implicit”: stored in array with no pointers array indices 1 Implicit tree structure: 2 3 4 5 6 7 … 8 9 10 11 12 13 … Array: 1 2 3 4 5 6 7 8 9 10 11 12 13 “Implicit”: stored in array with no pointers Array index given by level-order traversal

The Heapify Operation +   Combine 2 trees of height h + new root fix order +   Combine 2 trees of height h + new root Fix up heap-ordering Produces tree of height h+1 Time: O(h)

The BuildHeap Operation    Batch insert of n elements Initially n/2 trees of height 0 Repeatedly Heapify to get: n/4 trees of height 1 n/8 trees of height 2 … Total time:

The FUN begins… BuildHeap: O(n) batch insert of n elements. Cannot FindMin efficiently until done. Is O(1) Insert possible? Yes: Fibonacci Heaps Implicit Binomial Queues (Carlsson et al. ’88) Is there a simple variant of binary heap with O(1) Insert? Yes: The Post-Order Heap

FindMin during BuildHeap During BuildHeap, a FindMin is slow Many small trees  must search for min But BuildHeap can heapify in other orders “Children before parents” sufficient Idea: Change order to reduce # trees!

A Better Ordering BuildHeap: new node either parent or leaf O(log n) trees 7 3 6 10 9 8 12 11 1 2 4 5 BuildHeap: new node either parent or leaf Parent is good  reduces # trees by 1 Idea: add parent whenever possible This is a Post-Order insertion order

Insert Insert: Run BuildHeap incrementally Insertion order = post-order

FindMin & DeleteMin FindMin DeleteMin: like binary heap Enumerate the log n roots O(log n) time (assuming enumeration is easy) DeleteMin: like binary heap Find min, swap it with last element Heapify to fix up heap order O(log n) time

Insert Analysis Potential function  = Sum of tree heights Insert leaf 0 comparisons,  unchanged Insert parent at height h h comparisons for heapify Decrease in  = 2(h-1) - h = h - 2  Amortized cost = 2 Total time: O(1) amortized

No Don, Potential Functions! Fun with Sums Write BuildHeap sum as How can we evaluate this exactly? Expand and Contract! No Don, Potential Functions!

Fun with Sums How can we evaluate exactly? Consider BuildHeap with n = 2k+1 - 1 The 2k leafs pay €0 The 2k - 1 internal nodes pay €2 Potential at end is k (height of final heap) Consider BuildHeap with n = 2k+1 - 1 The 2k leafs pay $0 The 2k - 1 internal nodes pay $2 Total = 2k+1 - 2 - k

Back to Post-Order Heaps Tree: Array: Problem: Array sparse  not implicit Why? Tree-array map = level-order Insertion order = post-order Solution: use post-order for tree-array map

Navigating with Post-Order where are the children? x ? ? 7 3 6 10 9 8 12 11 1 2 4 5 For node x at height h Right child = x - 1 Left child = x - 1 - (size of right subtree) = x - 2h Must know height to navigate!

Height of New Nodes Where is node x+1? height h x+1 x Where is node x+1? 1) x is left child  x+1 is leaf  height 0 2) x is right child  x+1 is parent  height h+1 Must know ancestry to update height!

Ancestry String … For node x at height h: Bits below h are 0 ith bit describes x’s ancestor at height i 0 = left child, 1 = right child … z h zero bits 1 x = right child y = left child … y x height h

Updating Ancestry String One ancestry string, for last node in heap Must update after Insert no ancestors x = left child … x’s ancestry string: x+1 y x height h all left children y = right child 1 … x+1’s ancestry string:

Updating Ancestry String One ancestry string, for last node in heap Must update after Insert no ancestors x = right child 1 … x’s ancestry string: height h x+1 x no ancestors … x+1’s ancestry string:

Updating Ancestry String One ancestry string, for last node in heap Must update after Insert O(1) time Must update after DeleteMin Easy to do in O(log n) time

Recap: Problems & Solutions Main idea: Do Insert by incremental BuildHeap Problem Too many trees Solution Post-order insertion order Not implicit Post-order tree-array map Need height to navigate Maintain height for last node Updating height Maintain ancestry string

height & ancestry bookkeeping Pseudocode enumerate roots find children height & ancestry bookkeeping

Experiments 1 million Inserts (300 times): Post-Order Heap Ordering Comparisons Time Increasing 1.00 2.31 2.00 7.59 Random 2.38 5.66 1.87 7.53 Decreasing 17.32 27.09 5.00 Binary Heap Post-Order Heap Post-Order Heap improves worst-case reduces # comparisons larger constant factor due to bookkeeping

Summary Potential function analysis of BuildHeap Post-Order Heaps: Implicit: O(1) extra space Insert: O(1) amortized time DeleteMin: O(log n) time Simple, practical and FUN!