heaps and priority queuespeople.cs.pitt.edu/~aus/cs1501/priorityqueue.pdf · binary heap binary...

20
Heaps and Priority Queues

Upload: others

Post on 11-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Heaps and Priority Queues

Page 2: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Binary HeapBinary heap is a binary tree that is

● Complete:

● Satisfies the heap property:

○ Every level except possibly the bottom one is completely filled and the leaves in the

bottom level are as far left as possible

○ The key stored in every node is greater than or equal to the keys stored in

its children (max-heap).

Page 3: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Are these binary heaps?

No. Heap property not satisfied. No. It is not complete.

Page 4: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Observe the IndexSince the structure of a heap is so regular (i.e., always a complete binary tree) they can easily be stored in arrays:

Given index i of a node,● the index of its parent is:

○ ?● the indices of its children are:

○ ?

Unused

i / 2

2i and 2i+1

Page 5: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Observe the Index

Page 6: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Benefits of using an array vs references?

● Each node would have 3 references, so:○ [Memory Overhead] Each reference occupies 64 bits of memory (on 64 bit system).○ [Time overhead] All references would need to be updated on every swap.

● Cache locality using an array is better since all values are stored close to each other, and not scattered across memory like in reference case.

Page 7: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Binary HeapsIs every array a heap?

No. Array elements might be completely out of order and not satisfy heap properties.

Is every sorted array a heap?

Yes. Max-heap if descending, Min-heap if ascending.

Where in a Max-heap is the smallest element?

It is always a leaf but we cannot know exactly where it is located.

Is every heap a sorted array?

No. Array elements might still satisfy the heap property while not being sorted.

Page 8: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Inconsistent Heap Order

Can be caused by insertions or deletions.

Need to restore the order with reheapify operations (Swim and Sink).

Page 9: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Swim operationIf in a max-heap one of the nodes is greater than its parent, it switches places with the parent, and repeats this until it is not greater than its parent.

Page 10: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Sink operationIf in a max-heap one of the nodes is smaller than one or both of its children, it switches places with the larger one of the two children, and repeats this until all its children are smaller than itself.

Page 11: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Priority QueuePriority queue is an abstract data type where each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority.

Example scenarios?

Heap can be used as a priority queue.

Various applications with scheduling (scheduling of processes on the CPU, patients at the hospital scheduling, etc.)

Page 12: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Main operations of priority queue

Insert

Remove the maximum (minimum)

Page 13: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Insertion to a heap

Page 14: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Remove the maximum

Page 15: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Examplehttps://www.cs.usfca.edu/~galles/visualization/Heap.html

Insert: 50, 60, 70, 30, 90, 20, 25, 55, 10, 35, 15

Remove Smallest 11 times

Page 16: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Complexities

Page 17: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

What if we want to update/delete nodes in the middle?

We need additional index structures.

Page 18: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Index Priority Queue

Textbook code example:

http://algs4.cs.princeton.edu/24pq/IndexMinPQ.java.html

Page 19: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Index Priority Queue Produced by the Textbook Code

Keys and their sequential numbers Array representation of the heap Index structure

Page 20: Heaps and Priority Queuespeople.cs.pitt.edu/~aus/cs1501/PriorityQueue.pdf · Binary Heap Binary heap is a binary tree that is Complete: Satisfies the heap property: Every level except

Index Priority Queue Produced by the Textbook CodeKeys and their sequential

numbersArray representation

of the heap Index structure

● Draw the graphical diagram of this heap● Insert new value: #10 “Mouse”● Replace word #5 “Times” with “Cat”● Replace word #0 “It” with “Zoo”● Remove word #4 “Of”● What is stored in pq and qp after these

operations?

Exercise