404_HeapLecture_F11

404_HeapLecture_F11 - UMass Lowell Computer Science 91.404...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, Fall 2011 Heap Lecture Chapter 6 Ch t Use NOTES feature to see explanation accompanying some slides. PseudoPseudo-code is from Cormen et al. textbook. Heap Basics Structure: 16 14 10 Nearly complete binary tree y p y Convenient array representation Parent's label not less than 2 Parent s that of each child 16 14 10 8 7 9 HEAP Property: (for MAX HEAP *) 8 4 3 2 7 1 4 1 9 3 1 2 3 4 5 6 7 8 9 10 * analogous property and operations for MIN HEAP Operations on a Heap assuming array representation MAX-HEAPIFY: MAX-HEAPIFY: for a given node that is the root of a subtree, if both subtrees subtree, of that node are already HEAPs, MAX-HEAPIFY enforces y , MAXthe maximum HEAP PROPERTY via "downward swaps" so that the node together with its subtrees form a MAX HEAP 16 14 8 7 10 9 3 2 4 1 16 14 10 8 1 2 3 4 7 5 9 6 3 2 7 8 4 9 1 10 Operations on a Heap assuming array representation BUILD-MAX-HEAP: BUILD-MAX-HEAP: builds a MAX HEAP from scratch using MAX-HEAPIFY MAX16 14 8 7 10 9 3 2 4 1 16 14 10 8 1 2 3 4 7 5 9 6 3 2 7 8 4 9 1 10 Building a Max Heap using MAX-HEAPIFY vs. MAX-HEAP-INSERT MAX HEAPIFY vs MAX HEAP INSERT MAXMAX-HEAPIFY 16 14 8 7 10 9 3 number of height levels MAX-HEAPMAX-HEAP-INSERT swaps down compares parent with both children before each swap lg n swaps up compares parent with one child before each swap maximum number of swaps = length of path f from this level up to root number of height levels bound on number of nodes in this level n 2h+1 O(h) h =0 h = O n h h =0 2 lg n 2 4 1 maximum number of swaps = length of path from this level down to leaf n 0 2 h +1 ( lg n - h ) bound on number of h = nodes in this level lg n lg n 1 = O ( n lg n ) h h=0 2 lg n lg n n - h +1 h h=0 2 h h = O n h = O ( n 2) = O ( n ) h =0 2 h=0 1 = h 2 lg n 1 0 2 h= h 1 0 2 = 2 O(n lg n) h= O(n) as in HEAPIFY Asymptotic worst-case running time of BUILD-MAX-HEAP using MAX-HEAPIFY is in worstBUILD-MAXMAXO(n). However, using MAX-HEAP-INSERT the time would only be in O(n lg n). MAX-HEAP- Operations on a Heap assuming array representation HEAPSORT: sorts an array by first using BUILD-MAXBUILD-MAXHEAP th repeatedly swapping out root then t dl i t t and calling MAX-HEAPIFY 16 MAX14 8 7 10 9 3 2 4 1 16 14 10 8 1 2 3 4 7 5 9 6 3 2 7 8 4 9 1 10 Operations on a Max Heap assuming array representation MAX-HEAPIFY: MAX-HEAPIFY: Asymptotic worst-case worstrunning time is in O(lg n). O(lg For a node at height h, time is in O(h). T(n) T(2n/3) + (1) T(2n/3) is in O(lgn) using O(lgn) Master Theorem BUILD-MAX-HEAP: BUILD-MAX-HEAP: lg n Asymptotic worstworstcase running time is lg n h in O(n lg n). = O n h However, this is a h =0 2 loose bound! Time is also in O(n). = O n h = O(n2) = O(n) h n 2h+1 O(h) h =0 h =0 2 HEAPSORT: HEAPSORT: Asymptotic worst-case worstrunning time is in O(n lg n). nO(lgn) O(n) + O(lgn) Operations on a Max Heap assuming array representation PRIORITY QUEUE SUPPORT: MAX-HEAPMAX-HEAP-INSERT adds new leaf to the tree and then "swaps up" to restore p p MAX HEAP PROPERTY MAX HEAP PROPERTY guarantees that maximum is at the root of a MAX HEAP removes the maximum value from the root by swapping it out restores MAX HEAP PROPERTY using MAX-HEAPIFY MAX- HEAPHEAP- MAXIMUM HEAP- EXTRACTHEAP- EXTRACT-MAX Applications: Applications: Job Scheduling, Event Scheduling Operations on a Max Heap assuming array representation PRIORITY QUEUE SUPPORT: MAX-HEAP-INSERT MAX-HEAPAsymptotic worst-case worstrunning time is in O(lg n). O(lg For a node at height h time is in O(h) h, O(h). HEAPHEAP- MAXIMUM Asymptotic worst-case y p worstrunning time is in O(1). HEAP- EXTRACTHEAP- EXTRACTMAX O(1) + Asymptotic worst-case worstO(lgn) O(lgn) running time is in O(lg n). O(lg For a node at height h, time is in O(h). ...
View Full Document

This note was uploaded on 02/13/2012 for the course CS 91.404 taught by Professor Dr.karendaniels during the Fall '09 term at UMass Lowell.

Ask a homework question - tutors are online