COSC160: Data Structures Heaps. Jeremy Bolton, PhD Assistant Teaching Professor

Similar documents
COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor

CS350: Data Structures Heaps and Priority Queues

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

CMSC 341 Lecture 14: Priority Queues, Heaps

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

CS301 - Data Structures Glossary By

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

Course Review. Cpt S 223 Fall 2009

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Multiway Search Trees. Multiway-Search Trees (cont d)

Priority Queues and Heaps. Heaps of fun, for everyone!

Course Review for Finals. Cpt S 223 Fall 2008

Priority Queues Heaps Heapsort

9. Heap : Priority Queue

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Algorithms. AVL Tree

Programming II (CS300)

Heaps, Heap Sort, and Priority Queues.

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Data Structures Lesson 9

8. Binary Search Tree

Module 2: Classical Algorithm Design Techniques

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Data Structures and Algorithms

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

CS165: Priority Queues, Heaps

Heap Model. specialized queue required heap (priority queue) provides at least

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

CSCI2100B Data Structures Heaps

Data Structures Question Bank Multiple Choice

Summer Final Exam Review Session August 5, 2009

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Cosc 241 Programming and Problem Solving Lecture 21 (15/5/2017) Heaps

Algorithms and Data Structures

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Chapter 2: Basic Data Structures

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

Course Review. Cpt S 223 Fall 2010

Stacks, Queues, and Priority Queues. Inf 2B: Heaps and Priority Queues. The PriorityQueue ADT

Algorithms. Deleting from Red-Black Trees B-Trees

Binary heaps (chapters ) Leftist heaps

Binary Trees, Binary Search Trees

l Heaps very popular abstract data structure, where each object has a key value (the priority), and the operations are:

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Heaps. 2/13/2006 Heaps 1

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Stores a collection of elements each with an associated key value

Programming II (CS300)

Data Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions.

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

l So unlike the search trees, there are neither arbitrary find operations nor arbitrary delete operations possible.

CH 8. HEAPS AND PRIORITY QUEUES

Design and Analysis of Algorithms - Chapter 6 6

Binary Trees and Binary Search Trees

CH. 8 PRIORITY QUEUES AND HEAPS

Self-Balancing Search Trees. Chapter 11

Balanced Binary Search Trees. Victor Gao

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

CSE 5311 Notes 4a: Priority Queues

Operations. Priority Queues & Heaps. Prio-Q as Array. Niave Solution: Prio-Q As Array. Prio-Q as Sorted Array. Prio-Q as Sorted Array

Priority Queues, Binary Heaps, and Heapsort

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

A set of nodes (or vertices) with a single starting point

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

Dictionaries. Priority Queues

Priority Queues and Binary Heaps

Figure 1: A complete binary tree.

Topic: Heaps and priority queues

Priority Queues (Heaps)

Lecture 6: Analysis of Algorithms (CS )

CS102 Binary Search Trees

Trees & Tree-Based Data Structures. Part 4: Heaps. Definition. Example. Properties. Example Min-Heap. Definition

Binary Heaps. CSE 373 Data Structures Lecture 11

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

Search Trees - 1 Venkatanatha Sarma Y

Basic Data Structures (Version 7) Name:

Priority Queues. T. M. Murali. January 23, T. M. Murali January 23, 2008 Priority Queues

Multi-Way Search Trees

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Binary Search Trees Treesort

Sorting and Searching

Priority Queues. T. M. Murali. January 29, 2009

Advanced Algorithms. Class Notes for Thursday, September 18, 2014 Bernard Moret

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Priority Queues (Heaps)

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

UNIT III BALANCED SEARCH TREES AND INDEXING

Data Structures and Algorithms for Engineers

COMP Analysis of Algorithms & Data Structures

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Sorting and Searching

Trees. (Trees) Data Structures and Programming Spring / 28

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Multi-Way Search Trees

Transcription:

COSC160: Data Structures Heaps Jeremy Bolton, PhD Assistant Teaching Professor

Outline I. Priority Queue II. Heaps I. Binary Heaps II. Skew Heaps

Balancing a Tree Binary trees lack a depth constraint. As a result the worst case insertion, removal and retrieval times are O(n).

Balanced Trees: Time Complexity Implications If a tree is balanced in some sense. The time complexity of insertions, removals and retrievals may have a logarithmic bound.

Heaps Another tree structure well suited for a priority queue is a Heap. Similar to other BSTs, BUT key order is determined by parent-child relationship alone, not necessarily which child (left and right child have no order constraint). A binary heap of size n, is a complete, binary tree where key order is determined by the heap-order property. A complete binary tree of size n has a height of log n and the tree is completely filled up to depth log n 1. A complete tree, has all nodes at the maximum depth in leftmost position. If the tree was implemented as an array and ordered by a BFS, all nodes would be contiguous (no vacant spaces until BFS is complete). (min) Heap-order: a parents key is less than its children s key min-heap or max-heap

Heap Example: Heap Order (min) Heap-order: a parents key is less than its children s key Structural constraints A complete binary tree of size n has a height of log n and the tree is completely filled up to depth log n 1. A complete tree, has all nodes at the maximum depth in leftmost position. If the tree was implemented as an array and ordered by a BFS, all nodes would be contiguous (no vacant spaces until BFS is complete). h = log 15 = 3 mindepth = log 11 1 = 2 h = log 11 = 3 has all nodes at the maximum depth in leftmost position.

Not Binary Heaps Violations of Min filled depth Heap order Leftmost position

Implementation Given that the heap is complete in leftmost position, it is reasonable to implement as an array. In the simple case, no need for left and right child pointers Con: static maximum size (use dynamic array) Attributes: Array (for keys / priorities) Some use sentinel at 0 th position currentsize maxsize inorder Chaining implementation Binary Tree Node

Traversing an array implementation of a tree Assume a (BF) level-ordering of keys in the array. Review and Examples of array implementation of trees: Implement a method to traverse and print keys in BF order Implement a method to traverse and print keys in a DF order Observations Doubling parentindex: leftchild index Double parentindex + 1: rightchild index childindex/2 : parentindex» Assume floor when odd Try this at home!

Insertion Conceptually 1. Insert the key k in the leftmost available position at height log n. 2. If heap order is not violated by k, then done. 3. Else, swap k with parent key 4. Repeat at step 2. Implementation Inserting and swapping occurs in array. Must be able to determine parents index.

Heap Insertion // Assumes array size sufficient // insert hole into next leftmost location // swaps hole up to maintain heap order // inserts val in hole function insertheap heap, val hole := heap.currentsize := heap.currentsize + 1 while val < heap.array[hole/2] // swap keys/priorities down heap.array[hole] := heap.array[hole/2] hole := hole/2 heap. array[hole] := val // inserts val at stopped hole location

Insertion Example 1. Insert hole in leftmost position 2. Repeatedly swap up until insertion does not violate heap order 3. Then insert

Removal Find min The min value (in a minheap) is simply the root of the tree (if heap is inorder) Remove Min The item to remove from the priority queue is the item with highest priority (always the root in a heap!) The last item is detached (for future insertion at hole) since the number of elements is decremented Result will be a tree with a hole for a root Fix: propagate hole down and insert the last item into the hole when possible last refers to order in a BF scan.

Heap Removal Example 1. Remove root (always root if heap is inorder) 2. Detach last item from tree to insert into new location Last in BF scan Right most item at max depth 3. Swap hole down until we can insert the last item

Heap Removal Example (cont) 1. Remove root (always root if heap is inorder) 2. Detach last item from tree to insert into new location Last in BF scan Right most item at max depth 3. Swap hole down until we can insert the last item

Heap Removal // Removes item with highest priority // detaches lastitem // Swaps hole down and reinserts lastitem so heap is inorder function Remove heap returnval := heap.array[1] temp := heap.array[heap.currentsize] heap.currentsize := heap.currentsize 1 hole := 1 while hole <= heap.currentsize // while there exists a child, traverse down child := hole * 2 // check left child if child!= heap.currentsize AND heap.array[child+1] < heap.array[child] // swap down to lesser of two children child := child + 1 if heap.array[child] < temp // not ready to insert continue swapping down heap.array[hole] := heap.array[child] else break // ready to insert heap.array[hole] := toinsert return returnval

RE-write removal using helper function function swapdown heap, hole temp := heap.array[hole] // assumes last item was copied to hole location while hole <= heap.currentsize // while there exists a child, traverse down child := hole * 2 // check left child if child!= heap.currentsize AND heap.array[child+1] < heap.array[child] // swap down to lesser of two children child := child + 1 if heap.array[child] < temp // not ready to insert continue swapping down heap.array[hole] := heap.array[child] hole := child else break // ready to insert hole := child // update and continue traversal heap.array[hole] := temp // Removes item with highest priority // detaches lastitem // Swaps hole down and reinserts lastitem so heap is inorder function Remove heap returnval := heap.array[1] heap.array[1] := heap.array[heap.currentsize] // copy last item to hold location heap.currentsize := heap.currentsize 1 swapdown(heap, 1) return returnval

Analysis of Insert and Remove Insert 1. Add hole to next leftmost position 2. Swap up hole until insertion does not violate heap order Worst case: O(log n) Remove 1. Replace root with hole 2. Detach rightmost item at max depth 3. Swap hole down until insertion of detached item does not violate heap order Worst case: O(log n)

Heap uses Observe we have sacrificed order for balance When can we make use of a heap? Only partial order matters Min or max

Queue FIFO and LIFO Removal from queue is based on order of entry As a result, access can be restricted to head or tail (base or top) only Priority Queue Removal is based on priority ranking Tracking priority rankings is necessary to determine removal order

Priority Queue Using a list implementation Array or linked list Option 1 unordered: Insert: O(1) Remove: O(n) Option 2 ordered: Insert: O(n) Remove: O(1) Using a BST Insert, remove, search: Average case, O(log n) AVL: worst case O(log n) ** (later!)

Lazy Insertion: Heap O(log n) insertion time is not bad. Can we do better? Sometimes. Lazy insertion. Simply place the item in the heap, without consideration for heap order. Time: O(1) Why? If we plan to perform many insertions, before attempting any removals, then why waste time maintaining hash order. Instead delay ordering until the next removal. Before a removal must fixheap aka heapify update heap such that heap order is maintained.

Lazy insert or tossin Lazy Insert: 12, 33, 31 // insert vals into next leftmost location, no check for heap order function tossin heap, val heap.currentsize := heap.currentsize + 1 heap. array[currentsize] := val if(val < heap.array[currentsize/2]) heap.inorder := false // set flag

Binary Heap Example: fixheap aka heapify // for use with lazy insert // repeatedly check with swap down function fixheap heap i := heap.currentsize / 2 while i > 0 swapdown(heap, i) i := i - 1 heap.inorder = true

Binary Heap Example: fixheap aka heapify // for use with lazy insert // repeatedly check with swap down function fixheap heap i := heap.currentsize / 2 while i > 0 swapdown(heap, i) i := i - 1 heap.inorder = true Try at home: create a worst case unordered heap. How many swaps are needed?

Binary Heap Example: fixheap aka heapify Another heap order violation. Swap down is applied.

Binary Heap Example: fixheap aka heapify Swap Down may need to perform repeated swaps to ensure heap order is preserved Observations: The initial swap of 12 and 35 is progress, however, there is a violation still with 35 and 33. The swap down method will repeatedly swap down, thus assuring heap property order. Result: Swap down in the worst case will result in O(log n); swapping down the height of the tree.

Analysis of tossin and fixheap (heapify) tossin all cases time (assuming size is sufficient): O(1) fixheap Fix heap performs a reverse BFS O(n) and potentially swaps down at each step of the traversal O(log n), for a total upperbound of O(n log n) Swap down has a worst case of O(log n), BUT is this worst case possible in every iteration of our reverse BFS in fixheap NO. In fact we can limit the total number of swaps performed by swap down when used in conjunction with this reverse BFS. The Result: O(n)

Bounding fixheap How can we more tightly bound fixheap by O(n)? Proof idea: Observe: Each node can be swapped down, at maximum the distance to its furthest leaf node (the height of that specific node) Thus we need only compute the sum of the heights of all nodes to compute an upper bound (teaser alert! Its O(n)). The maximum number of swaps 25 could potentially have is 2 (the distance to 89), this is its height

Proof idea: fixheap is O(n) Show the sum of the heights of all nodes in a BST is linear. 1 node (the root node), will have the maximum height log 2 n, its children (2 nodes) will have heights log 2 n - 1, continuing on in this fashion we have the following sum of heights: Sum of heights = log 2 n i=0 2 i h log 2 n i = i=0 2 i h i = 2 h 0 + 2 h 1 1 + +2 0 h = 2 h [ 0 + 2 1 1 + +2 h h ] = h =n i=0 i 2 i 2n Note that when h approaches inf, this sum converges to 2, thus we can upperbound it. Side note: this proof shows that we can upper bound the number of total swaps linearly! In fact we can more strictly bound this expression: Sum of heights = n h. Try a proof by induction. See our example on previous slide: Sum of heights = 9 n h = 12-3

Repeated inserts vs. toss and heapify Which is more efficient? Assume we start with an empty heap and perform n inserts. tossin and fixheap O(n) + O(n) = O(n) Repeated Inserts O(n log n)

Priority Queues (with changing priorities) Array implementation permits efficient implementation Previous implementation priorities were fixed Permitting priority updates Concerns 1. Identifying item to be updated 2. Updating item and updating heap order

updatepriority Updating a nodes priority Assume location of priority to be updated is known. **If location is not assumed, must search heap before update // updates priority value at node index with newval function updatep heap, index, newval if heap.array[index] < newval // lowering priority heap.array[index] := newval swapdown(heap, index) else // increasing priority heap.array[index] := newval swapup(heap, index)

Side Note: Applications Priority Queues Sorting Data structures are used to store data Data structures are also used to help facilitate computational tasks efficiently Heaps are useful for sorting Takes advantage of efficiency of fixheap (aka heapify) First weakly orders elements, then removes min

Heap Sort Input: n orderable items Heap sort algorithm 1. Build heap 1. tossin all n items, O(n) 2. fixheap, O(n) 2. Iteratively disassemble heap and build ordered list Repeatedly Remove O(n log n) 1. remove root (min thus far) and add to tail of growing list 2. swapdown(heap,1)

Analysis of some sorting algorithms Insertion Sort Time Worst Case: O(n 2 ) Best Case: O(n) Space: In-place: YES Heap Sort Time Worst Case:? Best Case:? Space: In-place: YES Bubble Sort Time Worst Case: O(n 2 ) Best Case: O(n) Space: In-place: YES Selection Time Worst Case: O(n 2 ) Best Case: O(n) Space: In-place: YES

Merging Heaps In some applications, merging or combining priority queues is fundamental Eg a set of queues is formed for set of resources and a resource is eliminated. Implementation Implications Array Idea: copy items from array into appropriate locations into other array (likely with some extra rearrangements) At best, linear time given contiguous allocation (must at least copy over one of the hashes) One simple scheme 1. newheap := concatenate(heap1,heap2) // repeated lazy insertions 2. fixheap(newheap)

Merging Examples One simple scheme (assuming array implementation) 1. newheap := concatenate(heap1,heap2) // repeated lazy insertions 2. fixheap(newheap)

Merging Heaps Array vs. Chaining Simple merging for array implementation required O(sizeHeap1 + sizeheap2) Chaining If the priority queue requires a merging operation, then a chaining implementation may be more efficient. Merging can be done with O(log n) with the following design changes Chaining with nodes (not array) Relax height constraint (really?!?) The skew heap!

Skew heaps A skew heap is a binary tree with heap order (and no balance constraint) and a skew merging scheme Result: may have linear depth in worst case (though as we have shown, with BST, the average depth is logarithmic) All operations will have logarithmic time for the average case. Implementation (structurally) Chaining with pointers: leftchild and rightchild Node similar to node used in BST

Merging Skew Heaps Scheme to merge heap 1 and heap 2, with root nodes r 1 and r 2 Repeatedly merge 1. Base Case: if one tree is empty, return the other 2. Recursive Case: 1. temp := r 1.rightChild 2. r 1.rightChild := r 1.leftChild 3. r 1.leftChild := merge(temp, r 2 ) 4. return r 1 - Remove right child of left tree - Make left child of left tree, the right child - Make new left child of left tree, the result of merging the right tree with the old right child.

Merge Skew Heap // assume r 1 and r 2 are the roots of heap 1 and heap 2 initially // recursively merges the heaps using skew scheme function mergeskewheap r 1, r 2 if r 1 is NULL, return r 2 if r 2 is NULL, return r 1 if r 1. priority < r 2. priority temp := r 1.rChild r 1.rChild := r 1.lChild r 1.lChild := mergeskewheap(r 2, temp) return r 1 else return merge(r 2, r 1 )

Merge Skew Heaps Example // assume r 1 and r 2 are the roots of heap 1 and heap 2 initially // recursively merges the heaps using skew scheme 1 2 4 5 function mergeskewheap r 1, r 2 if r 1 is NULL, return r 2 if r 2 is NULL, return r 1 if r 1. priority < r 2. priority temp := r 1.rChild r 1.rChild := r 1.lChild r 1.lChild := mergeskewheap(r 2, temp) return r 1 else return mergeskewheap(r 2, r 1 ) 3 6

7 Merge Skew Heaps Example // assume r 1 and r 2 are the roots of heap 1 and heap 2 initially // recursively merges the heaps using skew scheme 8 function mergeskewheap r 1, r 2 if r 1 is NULL, return r 2 if r 2 is NULL, return r 1 if r 1. priority < r 2. priority temp := r 1.rChild r 1.rChild := r 1.lChild r 1.lChild := mergeskewheap(r 2, temp) return r 1 else return mergeskewheap(r 2, r 1 ) 9

Notes Other operations on skew heaps Since Skew Heaps are not array implementations, nor are they balanced, the insertion operation and update priority operations are somewhat different as compared to the Binary Heap Interestingly, these operations can be managed with using merges Insert(val): merge(heaproot, newnode(val)) Remove: 1. newroot : =merge(root.lchild, root.rchild) 2. return oldroot updatepriority(node, newval): 1. node.priority := newval 2. detach node from parent // this requires pointer to parent 3. newroot := merge(root, node)

Summary Binary Heaps Relaxed order compared to BSTs Strict balance and leftmost structure Provides for log time worst case for all operations except merge Array implementation provides for efficient space and time Not bad for sorting Skew Heaps Chaining implementation Binary Tree with Heap order (but no balance constraint) Provides for average logarithmic time for all operations, including merge