Heaps CSE 331 Section 2 James Daly
Dec 17, 2015
Heaps
CSE 331Section 2James 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
1 2 3 4
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
Structure Insertion Find Min
Unordered Array O(1) O(n)
Sorted Array O(n) O(1)
AVL Tree O(log n) O(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
1
2 3
1
3 2
Storage Representation
• Can represent the tree with an array• Children of i are (2i) and (2i+1)
201076
118
54
2
9
1
2 3
4 5 6 7
8 9 10
2 4 5 6 7 10 20 8 11 91 2 3 4 5 6 7 8 9 10
Basic Operation
• FindMin: O(1)• It’s right at the top!
201076
118
54
2
9
1
2 3
4 5 6 7
8 9 10
2 4 5 6 7 10 20 8 11 91 2 3 4 5 6 7 8 9 10
Basic Operation
• Insert: O(log n)• Displace bigger items towards the next empty
spot
201076
118
54
2
9
1
2 3
4 5 6 7
8 9 10
1
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
2010
7
6
118
5
4
2
9
1
2 3
4 5 6 7
8 9 10
1
11
Heap-Extract(H, key)
If (Size(H) ≤ 0) Raise “Heap underflow”
min ← H[1]H[1] = Heap[Size(H)] // Move last to rootSize(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 = lElse
smallest = iIf (r <= Size(H) and H(r) < H(smallest))
smallest = rIf (smallest != i)
Swap(H[i], H[smallest])Heapify(H, smallest)
2 7 5 6 4 10 20 8 11 91 2 3 4 5 6 7 8 9 10
Heapify
1 2 5 6 4 10 20 8 11 9 71 2 3 4 5 6 7 8 9 10 11
7 2 5 6 4 10 20 8 11 91 2 3 4 5 6 7 8 9 10
2 4 5 6 7 10 20 8 11 91 2 3 4 5 6 7 8 9 10
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)
For down to 1Heapify(H, i)
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
• Worst case for each Heapify is moving i to the bottom (Height i)
• Cost = • Assume max tree of height h
• 1 node at height h• 2 nodes at height h-1• 4 nodes at height h-2• 2i nodes at height h-i
• Cost =
Running Time
• Cost = • Cost = • 2Cost = • 2Cost – Cost = • Cost = • Since the cost must be O(n)
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(n2) worst case