Heaps CSE 331 Section 2 James Daly
Reminder Project 2 is out Covers tree sets Due Friday at midnight Exam 1 will be Thursday, February 26 Review next Tuesday 1 sheet of notes Office hours tomorrow cancelled Rose still has hers Extra slot Friday, 10-Noon, Bone Lab
Review : Queue Data structure that keeps things in the order inserted Two operations Enqueue: Append to the end of the list Dequeue: Remove from the front of the list First In, First Out Oldest item is the first removed 1234
Other Orders Sometimes want items in other orders Job scheduling Each job has a set priority Higher priority jobs should be processed first Add new job (insert) Extract highest priority jobs (deleteMin) Scheduler New JobsHighest Priority Job
Efficiency StructureInsertionFind Min Unordered ArrayO(1)O(n) Sorted ArrayO(n)O(1) AVL TreeO(log n)
Priority Queue Abstract data type supporting these operations Insert FindMin (no deletion) ExtractMin (deletion) Alternately FindMax and ExtractMax (but not both min and max)
(Binary) Heaps Not to be confused with the heap (where you get memory for new objects from) Common priority queue implementation Operations Insert: O(log n) FindMin: O(1) ExtractMin: O(log n)
Heaps Structure: a complete binary tree Completely filled except bottom level Fills left to right
Heaps Order items by priority Parents are smaller then their children (in a min-heap) K(Parent(i)) <= K(i) Root is the min node
Storage Representation Can represent the tree with an array Children of i are (2i) and (2i+1)
Basic Operation FindMin: O(1) It’s right at the top!
Basic Operation Insert: O(log n) Displace bigger items towards the next empty spot
Heap-Insert(H, key) Size(H)++ i ← Size(H) While (i > 1 and H[Parent(i)] < key): H[i] ← H[Parent(i)] i ← Parent(i) H[i] ← key
Basic Operation ExtractMin: O(log n) FindMin and remove it Fix the heap
Heap-Extract(H, key) If (Size(H) ≤ 0) Raise “Heap underflow” min ← H[1] H[1] = Heap[Size(H)] // Move last to root Size(H)-- Heapify(H, 1) Return min
Heapify Given an array with left(i) and right(i) both being heaps, make i a heap
Heapify(H, i) l <- left(i) r = right(i) If (l <= Size(H) and H(l) < H(i)) smallest = l Else smallest = i If (r <= Size(H) and H(r) < H(smallest)) smallest = r If (smallest != i) Swap(H[i], H[smallest]) Heapify(H, smallest)
Heapify
Bulkloading Want to create the heap all at once Given a bunch of jobs, create the priority queue Should have O(n) running time
BuildHeap(H)
Running Time Seems like it should be O(n log n) n/2 Calls to Heapify Each call could take O(log n) time Total is O(n log n) Bound is very loose
Running Time
Selection Problem Given a list of items, we want to find the kth smallest item Possible solutions? Sort the list, then return item at index k O(n log n) running time (merge sort) Keep the top k items, insert the next, then leave the biggest O(k n) running time Bad for k = n/2 (find median)
Selection Problem – Alternate Solution Build a heap from the input Call ExtractMin k times The last item is the solution Running time: O(n + k log n) Goes to O(n log n) for k = n/2 (find median)
Selection Problem – Alternate Solution 2 Build a max-heap from the first k items Insert the next item, then pop the biggest The max item in the heap at the end is the solution Running time: O(k + n log k) Still O(n log n) for the median item
Selection Problem – Alternate Solution 3 Do quicksort, but only look at one partition O(n) time on average O(n 2 ) worst case