Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority...

37
Heap, HeapSort and Priority Queue May 28, 2014 Heap, HeapSort and Priority Queue

Transcript of Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority...

Page 1: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Heap, HeapSort and Priority Queue

May 28, 2014

Heap, HeapSort and Priority Queue

Page 2: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Heap

A heap (data structure) is a linear array that stores a nearlycomplete tree.Only talking about binary heaps that store binary trees.nearly complete trees:

all levels except possibly the lowest one are filledthe bottom level is filled from left to right up to some point

Want to store trees like that to facilitate search in the tree.

Heap, HeapSort and Priority Queue

Page 3: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Suppose that array A stores (or represents) a binary heapTwo major attributes:

length(A) is number of elements in array Aheap-size(A) is number of elements in heap stored withinarray A

Although A[1], . . . ,A[length(A)] can contain valid numbers, onlyelements A[1], . . . ,A[heap-size(A)] actually store elements of theheap, heap-size(A) ≤ length(A).

Heap, HeapSort and Priority Queue

Page 4: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Assignment of tree vertices to array elements:Very easy:

root is A[1]

given index i of some node, we have

Parent(i) = bi/2cLeft(i) = 2iRight(i) = 2i + 1

Implementation straightforward:

i → 2ileft-shift by one of bit string representing i

i → 2i + 1left-shift by one plus adding a 1 to the last bit

i → bi/2cright-shift by one

Heap, HeapSort and Priority Queue

Page 5: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

1

2 3

4 5 6 7

8 9 10 11 12

k

b

h

g f j d e

a c

`

i

k b ` h i a c g f j d e

1 2 3 4 5 6 7 8 9 10 11 12

Heap, HeapSort and Priority Queue

Page 6: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

This particular vertex numbering isn’t the only requirement for thething to be a proper heapTwo kinds: min-heaps and max-heapsBoth cases, values in nodes satisfy heap property

max-heap with max-heap property:for every node i (other than root)

A[Parent(i)] ≥ A[i ]

meaning: value of node is at most value of parent, largestvalue is stored at root

min-heap with min-heap property:for every node i (other than root)

A[Parent(i)] ≤ A[i ]

meaning: value of node is at least value of parent, smallestvalue is stored at root

Heap, HeapSort and Priority Queue

Page 7: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Suppose given values 1,2,4,6,6,7,8,12,14,15,19,21

1

2 3

4 5 6 7

8 9 10 11 12

21

19 12

15 6 2 8

14 7 6 4 1 Max-Heap

1

2 3

4 5 6 7

8 9 10 11 12

1

2 12

4 7 15 14

6 6 8 19 21 Min-Heap

Heap, HeapSort and Priority Queue

Page 8: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Note: given fixed set of values, there are many possible propermin-heaps and max-heaps (except for what’s at root)

If viewing heap as “ordinary” tree, define height of vertex as # ofedges on longest simple downward path from vertex to some leaf

Heap, HeapSort and Priority Queue

Page 9: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

3

2 2

1 1 1 1

0 0 0 0 0

The height of a heap is the height of its root.A heap of n elements is based on a complete binary tree, thereforeits height is Θ(log n)

Heap, HeapSort and Priority Queue

Page 10: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

What are the minimum and maximum numbers of elements in aheap of height h? Prove that an n-element heap has heightblog2 nc.

Heap, HeapSort and Priority Queue

Page 11: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Important basic procedures for max-heaps

1 Max-Heapify, runs in time O(log n) time, a key procedurethat maintains the max-heap property (if we change a value inthe root of a heap, this procedure will correct the heap, sothat it’s again a heap)

2 Build-Max-Heap, runs in time O(n), produces a max-heapfrom unsorted data

Also Max-Heap-Insert, Heap-Extract-Max, Heap-Increase-Key(run in time O(log n)), Heap-Maximum, (run in time O(1)), usedwhen the heap is used to implement a priority queue

Heap, HeapSort and Priority Queue

Page 12: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Maintains the max-heap property

Inputs are an array A and an index iAssumption: sub-trees rooted in Left(i) and Right(i) are propermax-heaps, but A[i ] may be smaller than its children

Task of Max-Heapify is to let A[i ] float down in the max-heapbelow it so that heap rooted in i becomes proper max-heap

Heap, HeapSort and Priority Queue

Page 13: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Max-Heapify (A, i)

1. ` := Left(i)2. r := Right(i)

3. if ` ≤ heap-size(A) and A[`] > A[i ]4. largest := `

5. else6. largest := i

7. if r ≤ heap-size(A) and A[r ] > A[largest]8. largest := r

9. if largest 6= i10. exchange A[i ]↔ A[largest]11. Max-Heapify(A, largest)

Heap, HeapSort and Priority Queue

Page 14: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Idea:Lines 1–2 are just for convenienceLines 3–8 find the largest of elements A[i ], A[`], and A[r ]Lines 9–11

1 first check if there’s anything to be done at all,2 and if yes,

1 move the “misplaced element” one level down,2 and make a recursive call one level deeper on this element

We know that after the exchange we have the largest of A[i ], A[`],and A[r ] in position i , so among these three, everything is OK.However, further down may still be problems.Also note that we check ` ≤ heap-size(A) and r ≤ heap-size(A), sothat we don’t go checking outside of the heap – the recursion willend if these tests fail

Heap, HeapSort and Priority Queue

Page 15: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

1

8 9 10 11 12

5 6 7

32

4

1

8 9 10 11 12

54 6 7

32

1

8 9 10 11 12

54 6 7

32

1

2

46

6

7

8

12

14

21

15

12

1

2

46

6

7

8

12

21

15

12

14

1

2

46

6

7

8

12

15

21

12

14

Heap, HeapSort and Priority Queue

Page 16: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Running time of Max-Heapify on node of height h (counted frombottom!) is O(h) and h = O(log n)

Heap, HeapSort and Priority Queue

Page 17: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Building a Heap

Easy using Max-HeapifySuppose we have given an unordered arrayA[1], . . . ,A[n] with n = length(A)One can show that the elements

A[bn/2c+ 1], A[bn/2c+ 2], . . . , A[n]

are the leaves of a heap..Thus, it’s OK to initially consider them as 1-element heaps, andrun Max-Heapify “on top” of them, once for each non-leafelement (1-element heaps are always proper heaps!).Build-Max-Heap(A)1. heap-size(A) := length(A)2. for i := blength(A)/2c downto 13. Max-Heapify(A, i)

Heap, HeapSort and Priority Queue

Page 18: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

1

32

4 5 6 7

9 108

1

32

4 5 6 7

9 108

1

32

4 5 6 7

9 108

32

4 5 6 7

9 108

1

32

4 5 6 7

9 108

1

32

4 5 6 7

9 108

1

Result

4 8 710 Input array A

Binary tree representing A

141 3 2 916

4

1 10

14 16 9 3

2 8 7

16

14 10

8 7 9 3

2 4 1

4

16 10

14 7 9 3

2 8 1

4

1 3

14 16 9 10

2 8 7

4

1 3

2 16 9 10

14 8 7

4

1 3

2 16 9 10

14 8 7

Heap, HeapSort and Priority Queue

Page 19: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Build-Max-Heap Running time

Simple bound: calls to Max-Heapify cost O(log n), there are O(n)of them, thus O(n log n).

Heap, HeapSort and Priority Queue

Page 20: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

HeapSort

It is easy to write down the algorithm for Heapsort.Idea as follows:

1 Given unsorted array A, build heap on A, usingBuild-Max-Heap

2 Extract largest element (is in A[1]), and move it to the end ofarray (swap A[1] and A[n])

3 Decrease the size of the heap by 14 The root might not satisfy the heap property (that’s where

the element formerly in A[n] now is), hence, usingMax-Heapify, correct the heap

5 Extract the 2nd-largest element (again, in A[1]), and so on. . .

Heap, HeapSort and Priority Queue

Page 21: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Heapsort(A)1. Build-Max-Heap(A)2. for i := length(A) downto 2

3. exchange A[1]↔ A[i ]

4. heap-size(A) := heap-size(A)− 1

5. Max-Heapify(A, 1)

Running time: O(n log n) (Build-Max takes O(n), and then O(n)rounds with O(log n) each).

Heap, HeapSort and Priority Queue

Page 22: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Heapsort(A)1. Build-Max-Heap(A)2. for i := length(A) downto 2

3. exchange A[1]↔ A[i ]

4. heap-size(A) := heap-size(A)− 1

5. Max-Heapify(A, 1)

Running time: O(n log n) (Build-Max takes O(n), and then O(n)rounds with O(log n) each).

Heap, HeapSort and Priority Queue

Page 23: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Priority queues

They are different: there’s no “real” FIFO rule anymore.A priority queue maintains set S of elements, each with a key(priority).Two kinds: max-priority queues and min-priority queues,usually implemented by max-heaps and min-heaps.Max-priority queueOperations:

Insert(S , x) inserts element x into set SMaximum(S) returns element of S with largest keyExtract-Max(S) removes and returns element of S withlargest keyIncrease-Key(S , x , k) increases x ’s key to new value k ,assuming k is at least as large as x ’s old key

Min-priority queue is used via operations: Insert, Minimum,Extract-Min, and Decrease-Key.

Heap, HeapSort and Priority Queue

Page 24: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Max-Heap Operations, Return Max

using max-heaps, we know that the largest element is in A[1]:we have O(1) access to largest element

removing/inserting elements and increasing keys means thatwe (basically) can call Max-Heapify or a similar procedure(fixing the heap from bottom up) at the right place (relativelyefficient operation O(log n))

Min-priority queues analogous.

Implementation

Heap-Maximum(A)1. return A[1]

implements Maximum operation in O(1) time.

Heap, HeapSort and Priority Queue

Page 25: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Max-Heap Operations, Return Max

using max-heaps, we know that the largest element is in A[1]:we have O(1) access to largest element

removing/inserting elements and increasing keys means thatwe (basically) can call Max-Heapify or a similar procedure(fixing the heap from bottom up) at the right place (relativelyefficient operation O(log n))

Min-priority queues analogous.

Implementation

Heap-Maximum(A)1. return A[1]

implements Maximum operation in O(1) time.

Heap, HeapSort and Priority Queue

Page 26: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Max-Heap Operations, Remove

How to we remove the largest element from the queue/heap sothat we will still have a proper max-heap?

Heap-Extract-Max(A)1. if heap-size(A) < 1 error “heap underflow”

2. max := A[1]

3. A[1] := A[heap-size(A)]

4. heap-size(A) := heap-size(A)− 1

5. Max-Heapify(A, 1)

6. return max

Running time is O(log n)

Heap, HeapSort and Priority Queue

Page 27: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Max-Heap Operations, Increase key

Suppose that the element which key is to be increased is identifiedby index i

We first update the key A[i ]

Clearly, this can destroy the max-heap property, thus we need tofind a new place for this element

The idea is to move the updated element as far up toward the rootas necessary

Heap, HeapSort and Priority Queue

Page 28: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Max-Heap Operations, Increase key

Heap-Increase-Key(A, i , key)

1. if key < A[i ]

2. error “new key is smaller than current key”

3. A[i ] := key

4. while i > 1 and A[Parent(i)] < A[i ]

5. exchange A[i ]↔ A[Parent(i)]

6. i := Parent(i)

Running time is O(log n) (height of tree)

Heap, HeapSort and Priority Queue

Page 29: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Max-Heap Operations, Insert

Inserting is now easy:

1 add a new element to the end of heap2 set its key to desired value

Heap-Insert(A, key)1. heap-size(A) := heap-size(A) + 1

2. A[heap-size(A)] := −∞

3. Heap-Increase-Key(A, heap-size(A), key)

Running time is O(log n)

Conclusion: all considered operations can be implemented to runin time O(log n), Maximum() even in O(1)

Heap, HeapSort and Priority Queue

Page 30: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Max-Heap Operations, Insert

Inserting is now easy:

1 add a new element to the end of heap2 set its key to desired value

Heap-Insert(A, key)1. heap-size(A) := heap-size(A) + 1

2. A[heap-size(A)] := −∞

3. Heap-Increase-Key(A, heap-size(A), key)

Running time is O(log n)

Conclusion: all considered operations can be implemented to runin time O(log n), Maximum() even in O(1)

Heap, HeapSort and Priority Queue

Page 31: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

Max-Heap Operations, Insert

Inserting is now easy:

1 add a new element to the end of heap2 set its key to desired value

Heap-Insert(A, key)1. heap-size(A) := heap-size(A) + 1

2. A[heap-size(A)] := −∞

3. Heap-Increase-Key(A, heap-size(A), key)

Running time is O(log n)

Conclusion: all considered operations can be implemented to runin time O(log n), Maximum() even in O(1)

Heap, HeapSort and Priority Queue

Page 32: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

The lower bound for sorting an arbitrary array of values of size n isθ(n log n).

WHY ?

For a given set of n numbers there are at most n! permutation orordering of these numbers. If we consider a binary tree with theleaves be one of the permutation then any path from root to aleave would be a potential sorting.

A binary tree with n! has height log2(n!).

Heap, HeapSort and Priority Queue

Page 33: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

The lower bound for sorting an arbitrary array of values of size n isθ(n log n).

WHY ?

For a given set of n numbers there are at most n! permutation orordering of these numbers. If we consider a binary tree with theleaves be one of the permutation then any path from root to aleave would be a potential sorting.

A binary tree with n! has height log2(n!).

Heap, HeapSort and Priority Queue

Page 34: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

input sequence 〈a1, a2, . . . , an〉for ai and aj we can perform any of the testsai < aj?, ai ≤ aj?, ai > aj?, ai ≥ aj?to determine their relative orderwe are not interested in actual values of the elementsfor simplicity let’s us assume that ai 6= aj for all i 6= j , and hencewe can assume that all comparisons have the form ai ≤ aj?Decision tree

a full binary tree (every node has either zero or two children)represents comparisons between elements performed byparticular algorithm run on all possible inputsonly comparisons are relevant, everything else is ignored

Heap, HeapSort and Priority Queue

Page 35: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

<

<

<

<< <<

> >

> >

>

> >

1:2

1:3

2:3 1:2

1,2,3 1,3,2 3,1,2 2,1,3 3,2,12,3,1

2:3

1:3 1:2

* *

<

<

<

<<<

> >

> >

>

>

1:2

1:3

2:3 1:2

1,2,3 1,3,2 3,1,2 2,1,3 3,2,12,3,1

2:3

1:3 1:2

Heap, HeapSort and Priority Queue

Page 36: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

<

<

<

<< <<

> >

> >

>

> >

1:2

1:3

2:3 1:2

1,2,3 1,3,2 3,1,2 2,1,3 3,2,12,3,1

2:3

1:3 1:2

* *

<

<

<

<<<

> >

> >

>

>

1:2

1:3

2:3 1:2

1,2,3 1,3,2 3,1,2 2,1,3 3,2,12,3,1

2:3

1:3 1:2

Heap, HeapSort and Priority Queue

Page 37: Heap, HeapSort and Priority Queue - SFU.ca - …arashr/lecture10.pdfHeap, HeapSort and Priority Queue Suppose that array A stores (or represents) a binary heap Two major attributes:

internal nodes are labelled i : j for 1 ≤ i , j ≤ n, meaningelements ai and aj are compared

edges are labelled “≤” or >, depending on the outcome ofthe comparisons

leaves are labelled with a permutation

π(1), π(2), . . . , π(n) – representing the output of the

algorithm, i.e., at this pointaπ(1) ≤ aπ(2) ≤ · · · ≤ aπ(n)

a branch from the root to a leaf describes a sequence ofcomparisons (nodes and edges) resulting in a permutation (aleaf)

a reachable node – a node into which we can get on aninput; especially we are interested in reachable leaves

Heap, HeapSort and Priority Queue