SLIDE 1
Heaps and Priority Queues Heaps Complete Binary Tree Complete - - PowerPoint PPT Presentation
Heaps and Priority Queues Heaps Complete Binary Tree Complete - - PowerPoint PPT Presentation
Heaps and Priority Queues Heaps Complete Binary Tree Complete binary tree All layers (except lowest) are full. Lowest layer is filled from left to right. 3 / 28 Complete Binary Tree Array Representation 1-Based Array 0-Based Array
SLIDE 2
SLIDE 3
Complete Binary Tree
Complete binary tree
◮ All layers (except lowest) are full. ◮ Lowest layer is filled from left to right.
3 / 28
SLIDE 4
Complete Binary Tree – Array Representation
1-Based Array 0-Based Array Parent
i 2
- i − 1
2
- Left child
2i 2i + 1
Right child
2i + 1 2i + 2
1 2 3 4 5 6 7 8 9
4 / 28
SLIDE 5
(Min) Heap Property
Min Heap Property A complete binary tree satisfies min heap property if, for each node i which is not the root,
key(parent(i)) ≤ key(i).
In case of an array A: A[parent(i)] ≤ A[i].
1 3 2 3 2 1
5 / 28
SLIDE 6
Heap
Heap A heap is an array based representation of a complete binary tree satisfy- ing the heap property.
6 / 28
SLIDE 7
Building a Heap
SLIDE 8
Building a Heap
We have given an unsorted array. How do we transform it into a heap?
9 8 7 6 5 4 3 2 1
8 / 28
SLIDE 9
Building a Heap
Observation
◮ Leafs are valid heaps.
9 / 28
SLIDE 10
Building a Heap
Idea
◮ Assume left and right subtrees are valid heaps.
10 / 28
SLIDE 11
Building a Heap
Idea
◮ Assume left and right subtrees are valid heaps. ◮ If necessary, exchange root with root of left or right subtree.
10 / 28
SLIDE 12
Building a Heap
Idea
◮ Assume left and right subtrees are valid heaps. ◮ If necessary, exchange root with root of left or right subtree. ◮ Recursively repair subtree.
10 / 28
SLIDE 13
Building a Heap
Idea
◮ Assume left and right subtrees are valid heaps. ◮ If necessary, exchange root with root of left or right subtree. ◮ Recursively repair subtree.
10 / 28
SLIDE 14
Building a Heap
Idea
◮ Assume left and right subtrees are valid heaps. ◮ If necessary, exchange root with root of left or right subtree. ◮ Recursively repair subtree.
10 / 28
SLIDE 15
Building a Heap
Idea
◮ Assume left and right subtrees are valid heaps. ◮ If necessary, exchange root with root of left or right subtree. ◮ Recursively repair subtree.
10 / 28
SLIDE 16
Building a Heap — Algorithm
1 Procedure Min-Heapify (A, i) 2
Set l := left(i) and r := right(i).
3
If l ≥ |A| Then Return
4
If r < |A| and A[r ] < A[l] Then smallest := r
5
Else smallest := l
6
If A[smallest] < A[i] Then
7
Exchange A[smallest] and A[i].
8
Min-Heapify(A, smallest)
The algorithm assumes 0-based arrays.
11 / 28
SLIDE 17
Building a Heap — Algorithm
1 Procedure Build-Min-Heap (A) 2
For i := ⌊|A|/2⌋ − 1 DownTo 0
3
Min-Heapify(A, i)
The algorithm assumes 0-based arrays.
Theorem The algorithm Build-Min-Heap runs in linear time.
12 / 28
SLIDE 18
Example
9 8 7 6 5 4 3 2 1
13 / 28
SLIDE 19
Example
9 8 7 6 5 4 3 2 1
13 / 28
SLIDE 20
Example
9 8 7 6 4 3 2 1 5
13 / 28
SLIDE 21
Example
9 8 7 1 4 3 2 6 5
13 / 28
SLIDE 22
Example
9 8 3 1 4 7 2 6 5
13 / 28
SLIDE 23
Example
9 3 1 5 4 7 2 6 8
13 / 28
SLIDE 24
Example
1 3 2 5 4 7 9 6 8
13 / 28
SLIDE 25
Example
1 3 2 5 4 7 9 6 8
13 / 28
SLIDE 26
Priority Queues
SLIDE 27
Queues (“FIFO")
Enqueue
O(1)
◮ Adds an element to the queue.
Dequeue
O(1)
◮ Removes the oldest element in the queue.
Front
O(1)
◮ Return the oldest element in the queue without removing it. 5 2 4 1 3 Enqueue Dequeue Front
15 / 28
SLIDE 28
Priority Queues
Enqueue
◮ Adds an element to the queue.
Dequeue
◮ Removes the smallest element in the queue.
Min
◮ Return the smallest element in the queue without removing it. 5 2 4 1 3 Enqueue Dequeue Min
We can implement a priority queue using a heap.
16 / 28
SLIDE 29
Using a Heap – Min
Finding the minimum
◮ The root of the heap ◮ O(1) time
Min
1 3 2 5 4 7 9 6 8
17 / 28
SLIDE 30
Using a Heap – Dequeue
Dequeue
◮ “Remove” the root: Replace it by last element. 8 8 1 3 2 5 4 7 9 6
18 / 28
SLIDE 31
Using a Heap – Dequeue
Dequeue
◮ “Remove” the root: Replace it by last element. ◮ Restore heap property: Call Min-Heapify(A, 0) 8 1 3 2 5 4 7 9 6
18 / 28
SLIDE 32
Using a Heap – Dequeue
Dequeue
◮ “Remove” the root: Replace it by last element. ◮ Restore heap property: Call Min-Heapify(A, 0) ◮ O(log n) time 1 2 3 6 5 4 7 9 8
18 / 28
SLIDE 33
Using a Heap – Enqueue
Enqueue
◮ Add new element at the end.
Enqueue
1 2 3 5 8 4 7 9 6 1
19 / 28
SLIDE 34
Using a Heap – Enqueue
Enqueue
◮ Add new element at the end. ◮ Restore heap property: Exchange with parent until parent is smaller
- r equal.
1 2 3 5 8 4 7 9 6 1
19 / 28
SLIDE 35
Using a Heap – Enqueue
Enqueue
◮ Add new element at the end. ◮ Restore heap property: Exchange with parent until parent is smaller
- r equal.
◮ O(log n) time 1 1 3 5 2 4 7 9 6 8
19 / 28
SLIDE 36
Heapsort
SLIDE 37
Heapsort
Idea
◮ Make array A to a heap and use it as priority queue. ◮ Order of removing is order in sorted array.
21 / 28
SLIDE 38
Heapsort
Algorithm
◮ Make array A to a max heap. Max-Heap Sorted
22 / 28
SLIDE 39
Heapsort
Algorithm
◮ Make array A to a max heap. ◮ Exchange A[0] (root of the heap) with A[heapSize − 1]
22 / 28
SLIDE 40
Heapsort
Algorithm
◮ Make array A to a max heap. ◮ Exchange A[0] (root of the heap) with A[heapSize − 1] ◮ Decrease heapSize by 1
22 / 28
SLIDE 41
Heapsort
Algorithm
◮ Make array A to a max heap. ◮ Exchange A[0] (root of the heap) with A[heapSize − 1] ◮ Decrease heapSize by 1 ◮ Call Max-Heapify(A, 0).
22 / 28
SLIDE 42
Heapsort
Algorithm
◮ Make array A to a max heap. ◮ Exchange A[0] (root of the heap) with A[heapSize − 1] ◮ Decrease heapSize by 1 ◮ Call Max-Heapify(A, 0). Max-Heap Sorted
22 / 28
SLIDE 43
Heapsort
Properties
◮ Runtime: O(n log n) ◮ Memory: O(1) (if implemented correctly) ◮ Not stable
23 / 28
SLIDE 44
Exercises
SLIDE 45
Exercises
You wish to store a set of n numbers in either a max-heap or a sorted array. For each application below, state which data structure is better, or if it does not matter. Explain your answers. (a) Want to find the maximum element quickly. (b) Want to be able to delete an element quickly. (c) Want to be able to form the structure quickly. (d) Want to find the minimum element quickly.
25 / 28
SLIDE 46
Exercises
Give an O(n log k) time algorithm to merge k sorted lists into one sorted list, where n is the total number of elements in all the input lists.
26 / 28
SLIDE 47
Exercises
Design a data structure that supports the following two operations:
◮ addNum(i)
Adds an integer to the data structure.
◮ findMedian()
Returns the median of all elements so far.
27 / 28
SLIDE 48
Exercises
You are given a max-heap with n elements. Give an algorithm to find the k largest elements. You are allowed to destroy the heap. How fast is your algorithm?
28 / 28