nalysis of lgorithms Universidad Nacional de Colombia Tomado
nalysis of lgorithms Universidad Nacional de Colombia Tomado del libro de Cormen
OUTLINE v Heaps v Heap Sort v Priority Queues
Heap Sort
INTRODUCTION In this chapter we introduce another sorting algorithm which combines the better attributes of insertion sort and merge sort algorithms. Heap sort also introduces another algorithm desingn thechnique: the use of a data structure called heap, to manage information during the execution of the algorithm.
HEAPS The (binary) heap data structure is an array object that can be viewed as a nearly complete binary tree. Each node of the tree corresponds to an element of the array that stores the value in the node.
BINARY TREE 1 16 2 14 4 8 8 2 9 4 5 7 6 9 3 10 7 3 10 1 2 3 4 5 6 7 8 9 10 16 14 10 8 7 9 3 2 4 1 1
HEAPS An array A that represents a heap is an object with two attributes: • length[A]: the number of elements in he array. • Heap-size[A]: the number of elements in the heap stored within array A
HEAPS The root of the tree is A[1], and given the index i of a node, the indices of its parent Parent(i), left child Left(i), and right child Right(i) can be computed simply: • Parent(i): return • Left(i): return 2 i. • Right(i): return 2 i +1.
HEAPS There are two kinds of binary heaps: Max-heaps and min-heaps. In both kinds, the values in the nodes satisfy a heap property, the specifics of which depend on the kind of heap.
HEAPS In a max-heap, the max- heap property is that for every node i other than the Root, A[Parent(i)] A [ i] That is, the value of a node is at most the value of its parent. Thus the largest element in a max-heap is stored at the root, and the Subtree rooted at a node contains values no larger than the contained at the node itself.
HEAPS A min-heap is organized in the opposite way; The min-heap property is that for every node i other than the root, A[Parent(i)] A [ i] The smallest element in a min-heap is at the root. For the heapsort algorithm, we use max-heaps.
HEAP PROPERTIES Viewing a heap as a tree, we define the height of a node in a heap to be the number of edges on the longest simple downward path from the node to a leaf, and we define the height of the heap to be the height of its root. Since a heap of n elements is based on a complete binary tree , its height is (lg n).
HEIGHT Height 1 3 16 4 8 8 2 2 14 5 7 9 10 4 1 6 9 3 10 7 3 2 1 0
BASIC PROCEDURES Here we present five basic procedures and shows how they are used in a sorting Algorithm and a priority-queue data structure • Max-Heapify. • Build-Max-Heap. • HEAPSORT. • Max-Heap-Insert, Heap-Extract-Max, Heap-increase-key, and Heap-Maximum.
MAINTAINING THE HEAP PROPERTY
MAX-HEAPIFY Max-Heapify is an important subroutine for Manipulating max-heaps. Its inputs are an array A and an index i into the array. When Max-Heapify is called, it is assumed that the binary trees rooted at LEFT(i) and RIGHT(i) are max-heaps, but that A[i] may be smaller than its children, thus violating the max-heap property. The function of Max-Heapify is to let the value at A[i] “float down” in the max-heap so that the subtree rooted at index i becomes a max-heap.
MAX-HEAPIFY Max-Heapify(A, i) 1 l LEFT(A, i) 2 r RIGHT(i) 3 If l heap-size[A] and A[l] A[i] 4 then largest l 5 else largest i 6 If r heap-size[A] and A[r] > A[largest] 7 then largest r 8 If largest i 9 then exchange A[i] A[largest] 10 Max-Heapify(A, largest)
MAX-HEAPIFY(A, 2) 11 16 16 44 14 48 4 88 22 22 14 4 14 99 84 8 10 10 11 55 77 66 99 33 10 10 77 33
MAX-HEAPIFY RUNNING TIME The running time of Max-Heapify on a Subtree of size n rooted at given node i is the (1) time to fix up the relationships among the elements A[i], A[LEFT(i)], and A[RIGHT(i)], plus the time to run Max-Heapify on a subtree rooted at one of the children of node i.
MAX-HEAPIFY RUNNING TIME The children´s subtrees each have size at most 2 n/3 –-the worst case occurs when the last row of the tree is exactly half full--- and the running time of Max-Heapify can be described by the recurrence: T(n) T(2 n /3) + (1). 2 n/3 The solution of this recurrence, by case 2 of the master theorem is T(n) = O(lg n).
BUILDING A HEAP
BUILD-MAX-HEAP We can use the procedure Max-Heapify in a bottom-up manner to convert an array A[1. . n], where n = length[A], into a max-heap. The elements in the subarray A[( +1). . n] Are all leaves of the tree, and so each is a 1 -element heap to begin with. The procedure Build-Max-Heap goes through the remaining nodes of the tree and runs Max-Heapify on each one.
BUILD-MAX-HEAP Build-Max-Heap(A) 1 Heap-size[A] 2 for i 3 length[A] downto 1 do Max-Heapify(A, i)
BUILD-MAX-HEAP 11 16 44 44 14 14 28 2 88 14 14 22 22 16 14 11 55 16 16 7 1 99 10 10 88 4 71 7 66 99 33 10 33 77 10 10 3
BUILD-MAX-HEAP LOOP INVARIANT To show why Build-Max Heap works correctly, we use the following loop 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. ”
BUILD-MAX-HEAP LOOP INVARIANT Initialization: Prior to the first iteration of the loop, i = Each node +1, + 2. . n is a leaf and is thus the root of a trivial Max-heap. Maintenance: By the loop invariant the children of node i 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. Decrementing i in the for loop update reestablishes the loop invariant for the next iteration.
BUILD-MAX-HEAP LOOP INVARIANT 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.
BUILD-MAX-HEAP RUNNING TIME Our analysis relies on the properties that an n-element heap has height and at most nodes of any height h. So we can express the total cost of Build. Max-Heap as: =
BUILD-MAX-HEAP RUNNING TIME Where, Thus, the running time of Build-max-Heap can be bounded as
THE HEAPSORT ALGORITHM
THE HEAPSORT ALGORITHM The heapsort algorithm starts by using Build-Max. Heap to build a max-heap on the input array A[1. . n], where n = length[A]. Since the maximum element of the array is stored at the root A[1], it can be put into its correct final position by exchanging it with A[n].
THE HEAPSORT ALGORITHM If we now “discard” node n from the heap ( by decrementing heap-size[A] ), we observe that A[1. . (n-1)] can easily be made into max-heap. The children of the root remain max-heaps, but the new root element may violate the maxheap property. All that is needed to restore the max-heap property, however, is one call to Max-Heapify(A, 1), which leaves a maxheap in A[1. . (n-1)].
THE HEAPSORT ALGORITHM The heapsort algorithm then repeats this process for the max-heap of size n-1 down to a heap of size 2. 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] – 1 5 Max-Heapify(A, 1)
HEAPSORT RUNNING TIME The heapsort procedure takes time (n lgn), since the call to Build-Max. Heap takes time (n) and each of the n – 1 calls to Max-Heapify takes time (lg n).
Heapsort Applet Taken from http: //www 2. hawaii. edu/~copley/665/
PRIORITY QUEUES
PRIORITY QUEUES Now we present one of the most popular applications of a heap : its use as an efficient priority queue. As with heaps, there are two kinds of priority queues: max-priority queues and minpriority queues. We will focus here on how to implement max-priority queues, which are in turn based on max-heaps.
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 S. • 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.
HEAP MAXIMUM The procedure Heap-Maximum implements the Maximum operation in (1) time. Heap-Maximum(A) Return A[1];
HEAP-EXTRACT-MAX The procedure Heap-Extract-Max implements the extract-Max operation. 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 6 7 Heap-size[A] heap-size[A] -1 Max-Heapify(A, 1) return max The running time of Heap-Extract-Max is (lg n), since it performs a constant amount of work on top of the O(lg n) time for Max-Heapify.
HEAP INCREASE KEY Heap-increase-key(A, i, key) 1 If key < A[i] 2 then error “new key is smaller than current key” 3 A[i] key 4 While i > 1 and A[PARENT(i)] < A[i] 5 do exchange A[i] A[PARENT(i)] 6 i PARENT(i) The running time of Heap-Increase-Key on an n-element heap is (lg n), since the path traced from the node updated in line 3 to the root has length (lg n).
MAX-HEAP-INSERT Max-Heap-Insert(A, key) 1 2 3 heap-size[A] + 1 A[heap-size[A]] Heap-Increase-Key(A, heáp-size[A], key) The running time of Max-Heap-Insert on an n-element heap is (lg n).
THE END
- Slides: 43