Heaps and Priority Queues Heaps Complete Binary Tree Complete - - PowerPoint PPT Presentation

heaps and priority queues heaps complete binary tree
SMART_READER_LITE
LIVE PREVIEW

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-1
SLIDE 1

Heaps and Priority Queues

slide-2
SLIDE 2

Heaps

slide-3
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
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
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
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
SLIDE 7

Building a Heap

slide-8
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
SLIDE 9

Building a Heap

Observation

◮ Leafs are valid heaps.

9 / 28

slide-10
SLIDE 10

Building a Heap

Idea

◮ Assume left and right subtrees are valid heaps.

10 / 28

slide-11
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
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
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
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
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
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
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
SLIDE 18

Example

9 8 7 6 5 4 3 2 1

13 / 28

slide-19
SLIDE 19

Example

9 8 7 6 5 4 3 2 1

13 / 28

slide-20
SLIDE 20

Example

9 8 7 6 4 3 2 1 5

13 / 28

slide-21
SLIDE 21

Example

9 8 7 1 4 3 2 6 5

13 / 28

slide-22
SLIDE 22

Example

9 8 3 1 4 7 2 6 5

13 / 28

slide-23
SLIDE 23

Example

9 3 1 5 4 7 2 6 8

13 / 28

slide-24
SLIDE 24

Example

1 3 2 5 4 7 9 6 8

13 / 28

slide-25
SLIDE 25

Example

1 3 2 5 4 7 9 6 8

13 / 28

slide-26
SLIDE 26

Priority Queues

slide-27
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
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
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
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
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
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
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
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
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
SLIDE 36

Heapsort

slide-37
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
SLIDE 38

Heapsort

Algorithm

◮ Make array A to a max heap. Max-Heap Sorted

22 / 28

slide-39
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
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
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
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
SLIDE 43

Heapsort

Properties

◮ Runtime: O(n log n) ◮ Memory: O(1) (if implemented correctly) ◮ Not stable

23 / 28

slide-44
SLIDE 44

Exercises

slide-45
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
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
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
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