week 10: heap and priority queue. any feature here?

31
Week 10: Heap and Priority queue

Upload: maximillian-leonard

Post on 21-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Week 10: Heap and Priority queue. Any feature here?

Week 10: Heap and Priority queue

Page 2: Week 10: Heap and Priority queue. Any feature here?

Any feature here?

Page 3: Week 10: Heap and Priority queue. Any feature here?
Page 4: Week 10: Heap and Priority queue. Any feature here?

Heap

• No particular relationship among nodes on any given level, even among the siblings

• When a heap is a complete binary tree, it has a smallest possible height—a heap with N nodes always has O(log N) height.

• A heap is a useful data structure when you need to remove the object with the highest (or lowest) priority.

Page 5: Week 10: Heap and Priority queue. Any feature here?

Heaps• For a maximum heap, the value of a parent is greater than or

equal to the value of each of its children. • For a minimum heap, the value of the parent is less than or

equal to the value of each of its children.• We assume that a heap is a maximum heap.

Page 6: Week 10: Heap and Priority queue. Any feature here?

Vectors and complete binary trees• In practice, heaps are usually implemented as arrays

• A complete binary tree of depth d contains all possible nodes through level d - 1 and that nodes at level d occupy the leftmost positions in the tree.

Page 7: Week 10: Heap and Priority queue. Any feature here?

How to figure out parent-child from indices?

Page 8: Week 10: Heap and Priority queue. Any feature here?

Heap = Array-based trees

• With some simple rules, we can view a direct-access container, such as an array or vector, as a binary tree.

• These trees are referred to as array-based trees and form the basis for a new container type, called a heap

• Heaps are designed to provide efficient access to the maximum element or the minimum element in the collection.

• In this way, a heap acts like a priority queue. The highest (or lowest) priority element is always stored at the root, hence the name heap.

Page 9: Week 10: Heap and Priority queue. Any feature here?

Heapify

• Heapify() maintain the heap property

• Given: a node i in the heap with children l and r (two subtrees rooted at l and r )

• Problem: The subtree rooted at i violate the heap property

• Action: let the value of the parent node recursively “float down” so subtree rooted a i satisfy the heap property

Page 10: Week 10: Heap and Priority queue. Any feature here?
Page 11: Week 10: Heap and Priority queue. Any feature here?
Page 12: Week 10: Heap and Priority queue. Any feature here?
Page 13: Week 10: Heap and Priority queue. Any feature here?
Page 14: Week 10: Heap and Priority queue. Any feature here?
Page 15: Week 10: Heap and Priority queue. Any feature here?
Page 16: Week 10: Heap and Priority queue. Any feature here?
Page 17: Week 10: Heap and Priority queue. Any feature here?
Page 18: Week 10: Heap and Priority queue. Any feature here?
Page 19: Week 10: Heap and Priority queue. Any feature here?
Page 20: Week 10: Heap and Priority queue. Any feature here?

Analyzing Heapify()

• When Heapify is called, the running time depends on how far an element might move down in tree. In other words it depends on the height h (depth d) of node. In the worst case the element might go down all the way to the leaf level, which is O(logN).

Page 21: Week 10: Heap and Priority queue. Any feature here?

Heapify all subtrees in a tree

• Wiki – Building a heap:

Page 22: Week 10: Heap and Priority queue. Any feature here?

Insertion

• A heap is an array-based tree with a vector as the underlying storage structure.

• What makes the tree a heap is the ordering of the elements.

1. push_back -- O(1)2. Re-heapify -- O(logN)

Page 23: Week 10: Heap and Priority queue. Any feature here?

Deletion• Deletion from a heap is restricted to the root only. Hence,

the operation specifically removes the largest element.

1. Exchange -- O(1)2. pop_back -- O(1)3. Re-heapify -- O(logN)

Page 24: Week 10: Heap and Priority queue. Any feature here?

Time complexity - Heap

• Find max/min: O(1)

• Delete max/min: O(logN)

• Insert max/min: O(logN)

A heap is a useful data structure when you need to remove the object with the highest (or lowest) priority.

Page 25: Week 10: Heap and Priority queue. Any feature here?

Still remember the Priority Queue?

• We discussed in week 5 on “Queues”

Page 26: Week 10: Heap and Priority queue. Any feature here?

Main IndexMain Index ContentsContents26

Priority Queue

J o b # 3C lerk

J o b # 4Su p erv is o r

J o b # 2P res id en t

J o b # 1M an ager

A Special form of queue from which items are removed according to their designated priority and not the order in which they entered.

Items entered the queue in sequential order but will be removed in the order #2, #1, #4, #3.

Page 27: Week 10: Heap and Priority queue. Any feature here?

Heaps and priority queues

• Heap delete() operation removes the optimal element from the heap, much like Priority-queue pop() operation remove the highest priority element from priority queue.

• Heap insertion() simply adds an element, much like the Priority-queue push() operation.

• Both the heap insertion and deletion operations update the underlying tree storage structure in such a way that it remains a heap; that is, they maintain the integrity of the storage structure.

Page 28: Week 10: Heap and Priority queue. Any feature here?

Heaps and priority queues

• The standard container adaptor priority_queue calls make_heap, push_heap and pop_heap automatically to maintain heap properties for a container.

Page 29: Week 10: Heap and Priority queue. Any feature here?

29

Heap

Priority queue

The storage structure should be selected so thatthe container operations can be implementedefficiently

Page 30: Week 10: Heap and Priority queue. Any feature here?

Reading

• Chapter 6

Page 31: Week 10: Heap and Priority queue. Any feature here?

Reminder:

HW4 Due Wednesday 11/5/2014

Submission before Thursday 10/30/2014 will receive 30 extra

points