40
Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu,

Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Embed Size (px)

Citation preview

Page 1: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Computer AlgorithmsLecture 9

Heapsort

Ch. 6, App. B.5

Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Page 2: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Heapsort

• Combines the better attributes of merge sort and insertion sort.– Like merge sort, but unlike insertion sort, running time is O(n lg n).– Like insertion sort, but unlike merge sort, sorts in place.

• Introduces an algorithm design technique – Create data structure (heap) to manage information during the

execution of an algorithm.

• The heap has other applications beside sorting.– Priority Queues

Page 3: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Background on Trees

• Def: Binary tree = structure composed of a finite set of nodes that either:– Contains no nodes, or– Is composed of three disjoint sets of nodes: a root node, a left

subtree and a right subtree

2

14 8

1

16

4

3

9 10

root

Right subtreeLeft subtree

Page 4: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Special Types of Trees

• Def: Full binary tree = a binary tree in which each node is either a leaf or has degree (number of children) exactly 2.

• Def: Complete binary tree = a binary tree in which all leaves have the same depth and all internal nodes have degree 2.

Full binary tree

2

14 8

1

16

7

4

3

9 10

12

Complete binary tree

2

1

16

4

3

9 10

Page 5: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Definitions

• Height of a node = the number of edges on a longest simple path from the node down to a leaf

• Depth of a node = the length of a path from the root to the node• Height of tree = height of root node

= lgn, for a heap of n elements

214 8

1

16

4

3

9 10Height = 3

Depth of (10)= 2

Height of root = 3

Page 6: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Complete k-ary tree• all leaves have the same depth• all internal nodes have degree k• degree k=2 – binary tree• How many leaves does the k-ary tree of height h have?

– The root has • k children

– Each of the root’s children has • k children at depth 2

– Number of leaves/nodes at depth h is • kh

– The height of a complete k-ary tree with n leaves is • logkn

– The number of internal nodes of complete binary tree of height h

• k=2

1

0

132

1

1...1

h

i

hih

k

kkkkkk

Page 7: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Data Structure Binary Heap

• Def: A heap is a nearly complete binary tree with the following two properties:– Structural property: all levels are full, except possibly the last one,

which is filled from left to right– Order (max-heap) property: for any node x

Parent(x) ≥ x

Heap

5

7

8

4 It doesn’t matter that 4 in level 1 is smaller than 5 in level 2

2

Page 8: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Data Structure Binary Heap (2)

– Root – A[ 1 ]– Left[i] – A[ 2i ]

• How can you do this operation using one instruction?

– Right[i] – A[ 2i+1 ]• How can you do this operation?

– Parent[i] – A[ i/2 ]• How can you do this operation?

• length[A] – number of elements in array A.• heap-size[A] – number of elements in heap stored in A.

– heap-size[A] length[A]

8 7 4 5 2

1 2 3 4 5

• Array viewed as a nearly complete binary tree.– Physically – linear array.– Logically – binary tree,

filled on all levels (except lowest.)

• Map from array elements to tree nodes and vice versa

5

7

8

4

2

Page 9: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Max-heap: example

26 24 20 18 17 19 13 12 14 11

1 2 3 4 5 6 7 8 9 10

26

24 20

18 17 19 13

12 14 11

Max-heap as anarray.

Last row filled from left to right.

Max-heap as a binary tree.

Page 10: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Heap Property (Max and Min)

• Max-Heap– For every node excluding the root,

value is at most that of its parent: A[parent[i]] A[i]• Largest element is stored at the root.• In any subtree, no values are larger than the value

stored at subtree root.

• Min-Heap– For every node excluding the root,

value is at least that of its parent: A[parent[i]] A[i] • Smallest element is stored at the root.• In any subtree, no values are smaller than the value

stored at subtree root

Page 11: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Operations on Heaps• Maintain the max-heap property

– MAX-HEAPIFY• Create a max-heap from an unordered array

– BUILD-MAX-HEAP• Sort an array in place

– HEAPSORT• Priority queue operations

Page 12: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Height

• Height of a node in a tree: the number of edges on the longest simple downward path from the node to a leaf.

• Height of a tree: the height of the root.

• Height of a heap: lg n

– Basic operations on a heap run in O(lg n) time

• No. of leaves = n/2

• No. of nodes of height h n/2h+1

Page 13: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Maintaining the Heap Property

• Assume:– A node i is smaller than a child– Left and Right subtrees of i are max-heaps– The heap condition is violated only at that

node

• To eliminate the violation:– Exchange with larger child– Move down the tree– Recursively fix the children until all of them

satisfy the max-heap property.

Page 14: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

MaxHeapify – Example

26

14 20

24 17 19 13

12 18 11

14

14

2424

14

14

1818

14

MaxHeapify(A, 2)

Page 15: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Procedure MaxHeapify

MaxHeapify(A, i)

1. l left(i)

2. r right(i)

3. if l heap-size[A] and A[l] > A[i]

4. then largest l5. else largest i6. if r heap-size[A] and A[r] > A[largest]

7. then largest r8. if largest i9. then exchange A[i] A[largest]

10. MaxHeapify(A, largest)

MaxHeapify(A, i)

1. l left(i)

2. r right(i)

3. if l heap-size[A] and A[l] > A[i]

4. then largest l5. else largest i6. if r heap-size[A] and A[r] > A[largest]

7. then largest r8. if largest i9. then exchange A[i] A[largest]

10. MaxHeapify(A, largest)

Assumption:Left(i) and Right(i) are max-heaps.

Page 16: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

MAX-HEAPIFY Running Time

• Intuitively:– A heap is an almost complete binary tree must process O(lgn)

levels, with constant work at each level• Running time of MAX-HEAPIFY is O(lgn) • Can be written in terms of the height of the heap, as being O(h)

– Since the height of the heap is lgn

Page 17: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Running Time for MaxHeapify

MaxHeapify(A, i)

1. l left(i)

2. r right(i)

3. if l heap-size[A] and A[l] > A[i]

4. then largest l5. else largest i6. if r heap-size[A] and A[r] > A[largest]

7. then largest r8. if largest i9. then exchange A[i] A[largest]

10. MaxHeapify(A, largest)

MaxHeapify(A, i)

1. l left(i)

2. r right(i)

3. if l heap-size[A] and A[l] > A[i]

4. then largest l5. else largest i6. if r heap-size[A] and A[r] > A[largest]

7. then largest r8. if largest i9. then exchange A[i] A[largest]

10. MaxHeapify(A, largest)

Time to fix node i and its children = (1)

Time to fix the subtree rooted at one of i’s children = T(size of subree at largest)

PLUS

Page 18: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Building a heap• Use MaxHeapify to convert an array A into a max-heap.• How?• Call MaxHeapify on each element in a bottom-up manner.

BuildMaxHeap(A)

1. heap-size[A] length[A]

2. for i length[A]/2 downto 1

3. do MaxHeapify(A, i)

BuildMaxHeap(A)

1. heap-size[A] length[A]

2. for i length[A]/2 downto 1

3. do MaxHeapify(A, i)

Why length[A]/2

Page 19: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

BuildMaxHeap – Example

24 21 23 22 36 29 30 34 28 27

Input Array:

24

21 23

22 36 29 30

34 28 27

Initial Tree:(not max-heap)

Page 20: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

BuildMaxHeap – Example

24

21 23

22 36 29 30

34 28 27

MaxHeapify(10/2 = 5)

3636

MaxHeapify(4)

2234

22

MaxHeapify(3)

2330

23

MaxHeapify(2)2136

21

MaxHeapify(1)

2436

2434

2428

24

21

21

27

Page 21: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Correctness of BuildMaxHeap• Loop Invariant:

– At the start of each iteration of the for loop, each node i+1, i+2, …, n is the root of a max-heap.

• Initialization: – Before first iteration i = n/2– Nodes n/2+1, n/2+2, …, n are leaves and hence roots of max-

heaps.

• Maintenance:– By LI, subtrees at children of node i are max heaps.– Hence, MaxHeapify(i) renders node i a max heap root (while

preserving the max heap root property of higher-numbered nodes).– Decrementing i reestablishes the loop invariant for the next iteration.

Page 22: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Running Time of BuildMaxHeap

• Loose upper bound:– Cost of a MaxHeapify call No. of calls to MaxHeapify– O(lg n) O(n) = O(nlg n)

• Heapify costs O(n lg n)• There are n such calls

• Tighter bound:– Cost of a call to MaxHeapify at a node depends on the height, h, of

the node – O(h).– Height of most nodes smaller than n.– Height of nodes h ranges from 0 to lg n.– No. of nodes of height h is n/2h+1

Page 23: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Running Time of BuildMaxHeap

n

hh

n

hh

hnO

hOn

lg

0

lg

01

2

)( 2

2

)2/11(

2/1

(A.8)in 2/1, 2

2

2

0

lg

0

xh

h

hh

n

hh

)(

22 0

lg

0

nO

hnO

hnO

hh

n

hh

Tighter Bound for T(BuildMaxHeap): • Heapify time at a node depends on the node’s height• Heights of many nodes are small• In n-element heap there are at most nodes of height h

T(BuildMaxHeap)

Can build a heap from an unordered array in linear time

2 1

h

n

Page 24: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Heaps in Sorting

• Use max-heaps for sorting.• The array representation of max-heap is not sorted.• Steps in sorting

– Convert the given array of size n to a max-heap (BuildMaxHeap)– Swap the first and last elements of the array.

• Now, the largest element is in the last position – where it belongs.

• That leaves n – 1 elements to be placed in their appropriate locations.

• However, the array of first n – 1 elements is no longer a max-heap.

• Float the element at the root down one of its subtrees so that the array remains a max-heap (MaxHeapify)

• Repeat step 2 until the array is sorted.

Page 25: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Intro 25

Heapsort• Goal:

– Sort an array using heap representations

• Idea:

– Build a max-heap from the array

– Swap the root (the maximum element) with the last element in

the array

– “Discard” this last node by decreasing the heap size

– Call MAX-HEAPIFY on the new root

– Repeat this process until only one node remains

Page 26: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

26

Alg: HEAPSORT(A)

HeapSort(A)

1. Build-Max-Heap(A)

2. for i length[A] downto 2

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

4. heap-size[A] heap-size[A] – 1

5. MaxHeapify(A, 1)

HeapSort(A)

1. Build-Max-Heap(A)

2. for i length[A] downto 2

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

4. heap-size[A] heap-size[A] – 1

5. MaxHeapify(A, 1)

O(n)

O(lg n)

n-1 times

Running time: ?? O(n lg n)

Page 27: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

27

Example:

MAX-HEAPIFY(A, 1, 4) MAX-HEAPIFY(A, 1, 3) MAX-HEAPIFY(A, 1, 2)

MAX-HEAPIFY(A, 1, 1)

7 4 3 1 2

input A (maxHeapyfied)

1 2 3 4 7

output A (sorted)

Page 28: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Heapsort

• Sort by maintaining the as yet unsorted elements as a max-heap.

• Start by building a max-heap on all elements in A.– Maximum element is in the root, A[1].

• Move the maximum element to its correct final position.– Exchange A[1] with A[n].

• Discard A[n] – it is now sorted. – Decrement heap-size[A].

• Restore the max-heap property on A[1..n–1].– Call MaxHeapify(A, 1).

• Repeat until heap-size[A] is reduced to 2.

Page 29: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Algorithm Analysis

• In-place

• Not Stable– A sorting algorithm is stable, if it leaves the order of equal elements

unchanged.

• Build-Max-Heap takes O(n) and each of the n-1 calls to Max-Heapify takes time O(lg n).

• Therefore, T(n) = O(n lg n)

HeapSort(A)

1. Build-Max-Heap(A)

2. for i length[A] downto 2

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

4. heap-size[A] heap-size[A] – 1

5. MaxHeapify(A, 1)

HeapSort(A)

1. Build-Max-Heap(A)

2. for i length[A] downto 2

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

4. heap-size[A] heap-size[A] – 1

5. MaxHeapify(A, 1)

Page 30: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Heap Procedures for Sorting

• MaxHeapify O(lg n)• BuildMaxHeap O(n)• HeapSort O(n lg n)

Page 31: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Priority Queue

Popular & important application of heaps.• Max and min priority queues.

– Maintains a dynamic set S of elements.– Each set element has a key – an associated value.– Goal is to support insertion and extraction efficiently.

Applications:• Ready list of processes in operating systems by their priorities – the

list is highly dynamic• In event-driven simulators to maintain the list of events to be simulated

in order of their time of occurrence.

Page 32: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Basic Operations

• Operations on a max-priority queue:

– Insert(S, x) - inserts the element x into the set S• S S {x}.

– Maximum(S) - returns the element of S with the largest key.– Extract-Max(S) - removes and returns the element of S with the largest

key.– Increase-Key(S, x, k) – increases the value of element x’s key to the

new value k.

• Min-priority queue supports Insert, Minimum, Extract-Min, and Decrease-Key.

• Heap gives a good compromise between fast insertion but slow extraction and vice versa.

Page 33: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Heap Property (Max and Min)

• Max-Heap– For every node excluding the root,

value is at most that of its parent: A[parent[i]] A[i]

• Largest element is stored at the root.

• In any subtree, no values are larger than the value stored at subtree root.

• Min-Heap– For every node excluding the root,

value is at least that of its parent: A[parent[i]] A[i]

• Smallest element is stored at the root.

• In any subtree, no values are smaller than the value stored at subtree root

Page 34: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

HEAP-MAXIMUM

Goal:

– Return the largest element of the heap

Alg: HEAP-MAXIMUM(A)

1. return A[1] Running time: O(1)

Heap A:

Heap-Maximum(A) returns 7

Page 35: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

HEAP-EXTRACT-MAX

Goal:

– Extract the largest element of the heap (i.e., return the max value and

also remove that element from the heap

Idea:

– Exchange the root element with the last

– Decrease the size of the heap by 1 element

– Call MAX-HEAPIFY on the new root, on a heap of size n-1

Heap A: Root is the largest element

Page 36: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

HEAP-EXTRACT-MAX

Alg: HEAP-EXTRACT-MAX(A, n)

1. if n < 1

2. then error “heap underflow”

3. max ← A[1]

4. A[1] ← A[n]

5. MAX-HEAPIFY(A, 1, n-1) remakes heap

6. return max

Running time: O(lg n)

Page 37: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Example: HEAP-EXTRACT-MAX

82 4

14

71

16

10

9 3max = 16

82 4

14

7

1

10

9 3

Heap size decreased with 1

42 1

8

7

14

10

9 3

Call MAX-HEAPIFY (A, 1, n-1)

Page 38: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Heap-Extract-Max(A)

Heap-Extract-Max(A)

1. if heap-size[A] < 1

2. then error “heap underflow”

3. max A[1]

4. A[1] A[heap-size[A]]

5. heap-size[A] heap-size[A] - 1

6. MaxHeapify(A, 1)

7. return max

Heap-Extract-Max(A)

1. if heap-size[A] < 1

2. then error “heap underflow”

3. max A[1]

4. A[1] A[heap-size[A]]

5. heap-size[A] heap-size[A] - 1

6. MaxHeapify(A, 1)

7. return max

Running time : Dominated by the running time of MaxHeapify = O(lg n)

Implements the Extract-Max operation.

Page 39: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

Heap-Insert(A, key)

Heap-Insert(A, key)

1. heap-size[A] heap-size[A] + 1

2. i heap-size[A]

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

5. do A[i] A[Parent(i)]

6. i Parent(i)

7. A[i] key

Heap-Insert(A, key)

1. heap-size[A] heap-size[A] + 1

2. i heap-size[A]

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

5. do A[i] A[Parent(i)]

6. i Parent(i)

7. A[i] key

Running time is O(lg n)The path traced from the new leaf to the root has length O(lg n)

Page 40: Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

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

Heap-Increase-Key(A, i, key)1 If key < A[i]2 then error “new key is smaller than the current key”

3 A[i] key4 while i > 1 and A[Parent[i]] < A[i]5 do exchange A[i] A[Parent[i]]6 i Parent[i]

Heap-Increase-Key(A, i, key)1 If key < A[i]2 then error “new key is smaller than the current key”

3 A[i] key4 while i > 1 and A[Parent[i]] < A[i]5 do exchange A[i] A[Parent[i]]6 i Parent[i]

Heap-Insert(A, key)1 heap-size[A] heap-size[A] + 12 A[heap-size[A]] –3 Heap-Increase-Key(A, heap-size[A], key)

Heap-Insert(A, key)1 heap-size[A] heap-size[A] + 12 A[heap-size[A]] –3 Heap-Increase-Key(A, heap-size[A], key)