Top Banner
Heaps CSE 331 Section 2 James Daly
27

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.

Dec 17, 2015

Download

Documents

Laura Floyd
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

Heaps

CSE 331Section 2James Daly

Page 2: 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.

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

Page 3: 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.

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

Page 4: 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.

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

Page 5: 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.

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)

Page 6: 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.

Priority Queue

• Abstract data type supporting these operations• Insert• FindMin (no deletion)• ExtractMin (deletion)

• Alternately FindMax and ExtractMax (but not both min and max)

Page 7: 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.

(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)

Page 8: 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.

Heaps

• Structure: a complete binary tree• Completely filled except bottom level• Fills left to right

Page 9: 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.

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

Page 10: 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.

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

Page 11: 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.

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

Page 12: 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.

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

Page 13: 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.

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

Page 14: 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.

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

Page 15: 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.

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

Page 16: 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.

Heapify

• Given an array with left(i) and right(i) both being heaps, make i a heap

Page 17: 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.

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)

Page 18: 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.

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

Page 19: 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.

Bulkloading

• Want to create the heap all at once• Given a bunch of jobs, create the priority

queue• Should have O(n) running time

Page 20: 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.

BuildHeap(H)

For down to 1Heapify(H, i)

Page 21: 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.

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

Page 22: 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.

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 =

Page 23: 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.

Running Time

• Cost = • Cost = • 2Cost = • 2Cost – Cost = • Cost = • Since the cost must be O(n)

Page 24: 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.

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)

Page 25: 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.

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)

Page 26: 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.

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

Page 27: 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.

Selection Problem – Alternate Solution 3

• Do quicksort, but only look at one partition• O(n) time on average• O(n2) worst case