3. Introduction to Heapsort
โข Running time: O(n lg n)
Like merge sort
โข Sorts in place: only a constant number of array elements are stored
outside the input array at any time
Like insertion sort
โข Heap
A data structure used by Heapsort to manage information during the
execution of the algorithm
โข Can be used as an efficient priority queue
4. Perfect Binary Tree
โข For binary tree with height h
All nodes at levels hโ1 or less have 2 children (full)
5. Complete Binary Trees
โข For binary tree with height h
All nodes at levels hโ2 or less have 2 children (full)
All leaves on level h are as far left as possible
7. Heaps
โข Two key properties
โข Complete binary tree
โข Value at node
โข Smaller than or equal to values in sub-trees (min-heap)
โข Greater than or equal to values in sub-trees (max-heap)
โข Example max-heap
Y โค X
Z โค X
9. Binary Heap
โข An array object that can be viewed as a nearly complete binary tree
โข Each tree node corresponds to an array element that stores the value in the
tree node
โข The tree is completely filled on all levels except possibly the lowest, which is
filled from the left up to a point
โข A has two attributes
โข length[A]: number of elements in the array
โข heap-size[A]: number of elements in the heap stored within A
heap-size[A] โค length[A]
โข max-heap and min-heap
12. Basic procedures
โข Given the index i of a node, the indices of its parent, left child, and
right child can be computed simply:
โข A[1] is the root of the tree
13. Basic Operations - Continued
โข the LEFT procedure can compute 2i in one instruction by simply
shifting the binary representation of i left by one bit position
โข The RIGHT procedure can quickly compute 2i + 1 by shifting the
binary representation of i left by one bit position and then adding in a
1 as the low-order bit.
โข The PARENT procedure can compute Floor(i/2) by shifting i right one
bit position.
15. Heap property
โข Heap property
โข The property that the values in the node must satisfy
โข Max-heap property, for every node i other than the root
โข A[PARENT(i)] โฅ A[i]
โข The value of a node is at most the value of its parent
โข The largest element in a max-heap is stored at the root
โข The sub-tree rooted at a node contains values no larger than that contained
at the node itself
17. Heap Height
โข The height of a node in a heap
โข The number of edges on the longest simple downward path from the node to
a leaf
โข The height of a heap is the height of its root
โข The height of a heap of n elements is ฮ (lg n)
โข For example
โข Height of node 2 is 2
โข Height of the heap is 3
18. Heap Procedures
โข MAX-HEAPIFY
โข Maintains the max-heap property
โข O(lg n)
โข BUILD-MAX-HEAP
โข Produces a max-heap from an unordered input array
โข O(n)
โข HEAPSORT
โข Sorts an array in place
โข O(n lg n)
19. Maintaining the Heap Property
โข MAX-HEAPIFY is an important subroutine to maintain the max-heap
property
โข Inputs: an array A and an index i into the array
โข Outputs: the sub-tree rooted at index I becomes a max-heap
โข Assume: the binary tree rooted at LEFT(i) and RIGHT(i) are max-heaps, but
A[i] may be smaller than its children
โข violate the max-heap property
โข Method: MAX-HEAPIFY let the value at A[i] floats down in the max-heap
23. MAX-HEAPIFY
Extract the indices of LEFT and RIGHT
children of i
Choose the largest of A[i], A[l], A[r]
Float down A[i] recursively
24. Running Time of MAX-HEAPIFY
โข ฮ(1) to find out the largest among A[i], A[LEFT(i)], and A[RIGHT(i)]
โข Plus the time to run MAX-HEAPIFY on a sub-tree rooted at one of the
children of node I
โข The childrenโs sub-trees each have size at most 2n/3 (why?)
โข the worst case occurs when the last row of the tree is exactly half full
โข T(n) โค T(2n/3) + ฮ(1)
โข By case 2 of the master theorem
โข T(n) = O(lg n)
26. Building a Max-Heap
โข Observation: A[(Floor(n/2)+1)..n] are all leaves of the tree
โข Each is a 1-element heap to begin with
โข Upper bound on the running time
โข O(lg n) for each call to MAX-HEAPIFY, and call n times O(n lg n)
โข Not tight
30. Correctness
โข To show that Build-Heap works, we need to prove its correctness
โข Invariant
โข At the start of each iteration of the for loop of lines 2โ3, each node i + 1, i + 2,
โฆ , n is the root of a max-heap.
โข Initialization: Prior to the first iteration of the loop, i = Floor (n/2). Each node Floor(n/2)+1,
Floor(n/2)+2,โฆ,n is a leaf and is thus the root of a trivial max-heap.
โข Maintenance: By the loop invariant, therefore, they are both roots of max-heaps. This is precisely
the condition required for the call MAX-HEAPIFY(A, i) to make node i a max-heap root. Moreover,
the MAX-HEAPIFY call preserves the property that nodes i+1, i+2,โฆ,n are all roots of max-heaps.
Decrementing i in the for loop update reestablishes the loop invariant for the next iteration.
โข Termination: At termination, i = 0. By the loop invariant, each node 1,2,โฆ,n is the root of a
max-heap. In particular, node 1 is.
31. Heapsort
โข Using BUILD-MAX-HEAP to build a max-heap on the input array
A[1..n], where n=length[A]
โข Put the maximum element, A[1], to A[n]
โข Then discard node n from the heap by decrementing heap-size(A)
โข A[2..n-1] remain max-heaps, but A[1] may violate
โข call MAX-HEAPIFY(A, 1) to restore the max-heap property for A[1..n-1]
โข Repeat the above process from n down to 2
โข Cost: O(n lg n)
โข BUILD-MAX-HEAP: O(n)
โข Each of the n-1 calls to MAX-HEAPIFY takes time O(lg n)
41. Priority queues
โข A priority queue is a data structure for maintaining a set S of elements,
each with an associated value called a key. A max-priority queue supports
the following operations:
โข INSERT(S, x): inserts the element x into the set S, which is equivalent to the
operation S = S U {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, which is assumed to be at least as large as xโs current key value.