29
Heaps Chapter 21

Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

Embed Size (px)

Citation preview

Page 1: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

Heaps

Chapter 21

Page 2: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

What is a heap used for?

• Sorting– HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory

• Priority Queues– Heaps allow inserting an element and extracting

the smallest element in a set, both in O(log N) time

Page 3: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

What is a heap?

• A heap is a binary tree with two properties– Order: the key at any node is less than or equal

to the keys in the node’s children• the least element is at the root of the tree

• there is no relative order of left and right children

– Shape: all leaves are on at most two levels, with those on the bottom level asfar left as possible, and there are no “holes”

Page 4: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

Here is a heap of 12 integers

Page 5: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

Implementing a heap

• There are many possible representations of binary trees

• The shape property allows us to use an array with the tree information only implicit in the representation

Page 6: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

A 12-element tree as an array

Page 7: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

Operation on a tree with Shape

Various functions on the tree are defined as follows:

Root = 1Key(i) = A[i]LeftChild(i) = 2*iRightChild(i) = 2*i+1Parent(i) = i / 2Null(i) = (i < 1) || (i > N)

Page 8: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

Representation

12 20 15 29 23 17 22 35 40 26 51 191 12

Page 9: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

Heap property

• Since the Shape property is guaranteed by the representation, we will use the name Heap to mean that the key in any node is greater than or equal to the key in its parentHeap: 2iN A[i/2] A[i]

• More generally, A[L..U] has the Heap property if Heap(L,U): 2LiU A[i/2] A[i]

Page 10: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

Two Critical Routines

• These two routines are used to fix an array whose Heap property is broken at one end or the other– Siftup - used when A[1..N-1] is a heap, but

A[1..N] is not– Siftdown - used then A[2..N] is a heap, but

A[1..N] is not

Page 11: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

SiftUp illustrated (1)

out of order

Page 12: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

SiftUp illustrated (2)

Swapped

Page 13: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

SiftUp illustrated (3)

Swapped

Page 14: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

Siftup

• This process continues until the circled node is greater than or equal to its parent, or until it is the root of the tree.

Page 15: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

Siftup routine

procedure SiftUp(int N)//pre: Heap(1,N-1), N > 0//post: Heap(1,N)int i = N;loop

if (i == 1) break; // i is the rootint p = i /2; // p is the parentif (A[p] <= A[i]) break; //in orderswap(A[p], A[i]);i = p;

end loop;

Page 16: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

Performance of SiftUp

• Since SiftUp does constant work at each level of the heap (binary tree), it takes time proportional to log N

Page 17: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

SiftDown

• Assigning a new value to A[1] when A[1..N] is a heap leaves Heap(2,N).

• Routine SiftDown restores Heap(1,N).

• A[1] is sifted down until it has no children or is less than or equal to the children it does have

Page 18: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

SiftDown Illustrated

out of order

Page 19: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

SiftDown Illustrated (2)

Swapped

Page 20: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

SiftDown Illustrated (3)

Swapped

Page 21: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

SiftDown routineprocedure SiftDown(int N)

//pre: Heap(2,N), N >= 0//post: Heap(1,N)int i = 1;loop

int c = 2*i;if (c > N) break; //no childrenif (c+1 <= N)

if (A[c+1] < A[c])c = c+1; // c -- least child

if (A[i] <= A[c]) break;swap(A[c], A[i]);i = c;

end loop;

Page 22: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

Performance of SiftDown

• Since SiftDown does constant work at each level of the heap (binary tree), it takes time proportional to log N

Page 23: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

Priority Queue Performance

1 insert 1 ExtractMin N of eachSorted sequence O(N) O(1) O(N 2̂)Heaps O(log N) O(log N) O(N log N)Unsorted Sequence O(1) O(N) O(N 2̂)

Run TimesData Structure

Page 24: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

Priority Queue

• Implementation of Insertprocedure Insert(t)

if (N >= MaxSize) throw OverflowException

N++;A[N] = t;

// Heap(1, N-1)SiftUp(N);

// Heap(1,N)

Page 25: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

Priority Queue

• Implementation of ExtractMinprocedure ExtractMin()

if (N < 1) throw UnderflowExceptiont = A[1]; A[1] = A[N];N--;

// Heap(2, N)SiftDown(N);

// Heap(1,N)return t;

Page 26: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

A Sorting Algorithm

• A very simple sorting algorithm based on priority queues:

//sort array B[1..N]for (i=1; i<=N, i++)

Insert(B[i]);for (i=1; i<=N, i++)

B[i] = ExtractMin();

Page 27: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

Sorting Algorithm

• This sorting algorithm has a worst-case cost of O(N log N)

• The array used for the heap requires additional N memory locations

• HeapSort improves on this by sorting the array in place

Page 28: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

HeapSort routine

for(i = 2; i<=N; i++)SiftUp(i)

for(i = N; i > 1; i--)

{Swap(A[1],A[i]);SiftDown(i-1);

}

Page 29: Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues

HeapSort Performance

• HeapSort does N-1 SiftUps and N-1 SiftDowns

• Each SiftUp and SiftDown is O(log N)

• Thus, HeapSort is O(N log N)