Download presentation
Presentation is loading. Please wait.
Published byLaura Floyd Modified over 9 years ago
1
Heaps CSE 331 Section 2 James Daly
2
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
3
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
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
5
Efficiency StructureInsertionFind Min Unordered ArrayO(1)O(n) Sorted ArrayO(n)O(1) AVL TreeO(log n)
6
Priority Queue Abstract data type supporting these operations Insert FindMin (no deletion) ExtractMin (deletion) Alternately FindMax and ExtractMax (but not both min and max)
7
(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)
8
Heaps Structure: a complete binary tree Completely filled except bottom level Fills left to right
9
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 23 1 32
10
Storage Representation Can represent the tree with an array Children of i are (2i) and (2i+1) 201076 118 5 4 2 9 1 23 4567 8910 2 4 5 6 7 10 20 8 11 9 1 2 3 4 5 6 7 89 10
11
Basic Operation FindMin: O(1) It’s right at the top! 201076 118 5 4 2 9 1 23 4567 8910 2 4 5 6 7 10 20 8 11 9 1 2 3 4 5 6 7 89 10
12
Basic Operation Insert: O(log n) Displace bigger items towards the next empty spot 201076 118 5 4 2 9 1 23 4567 8910 1
13
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
14
Basic Operation ExtractMin: O(log n) FindMin and remove it Fix the heap 2010 7 6 118 5 4 2 9 1 23 4567 8910 1 11
15
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
16
Heapify Given an array with left(i) and right(i) both being heaps, make i a heap
17
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)
18
2 7 5 6 4 10 20 8 11 9 1 2 3 4 567 8910 Heapify 1 2 5 6 4 10 20 8 11 9 7 1 2 3 4 567 8910 11 7 2 5 6 4 10 20 8 11 9 1 2 3 4 567 8910 2 4 5 6 7 10 20 8 11 9 1 2 3 4 567 8910
19
Bulkloading Want to create the heap all at once Given a bunch of jobs, create the priority queue Should have O(n) running time
20
BuildHeap(H)
21
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
22
Running Time
24
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)
25
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)
26
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
27
Selection Problem – Alternate Solution 3 Do quicksort, but only look at one partition O(n) time on average O(n 2 ) worst case
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.