Presentation is loading. Please wait.

Presentation is loading. Please wait.

UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2007 Heap Lecture Chapter 6 Use NOTES feature to see explanation.

Similar presentations


Presentation on theme: "UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2007 Heap Lecture Chapter 6 Use NOTES feature to see explanation."— Presentation transcript:

1 UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2007 Heap Lecture Chapter 6 Use NOTES feature to see explanation accompanying some slides.

2 Heap Basics ä Structure: ä Nearly complete binary tree ä Convenient array representation ä HEAP Property: (for MAX HEAP) ä Parent’s label not less than that of each child 16 1410 8793 241 1614108793241 1 2 3 4 5 6 7 8 9 10

3 Operations on a Heap ä HEAPIFY: ä for a given node that is the root of a subtree, if both subtrees of that node are already HEAPs, HEAPIFY enforces the HEAP PROPERTY via “downward swaps” so that the node together with its subtrees form a HEAP assuming array representation 16 1410 8793 241 1614108793241 1 2 3 4 5 6 7 8 9 10

4 Operations on a Heap ä BUILD-HEAP: ä builds a HEAP from scratch using HEAPIFY assuming array representation 16 1410 8793 241 1614108793241 1 2 3 4 5 6 7 8 9 10

5 Operations on a Heap ä HEAPSORT: ä sorts an array by first using BUILD- HEAP then repeatedly swapping out root and calling HEAPIFY assuming array representation 16 1410 8793 241 1614108793241 1 2 3 4 5 6 7 8 9 10

6 Operations on a Heap ä HEAPIFY: ä BUILD-HEAP: ä HEAPSORT: assuming array representation Asymptotic worst-case running time is in O(lg n). Asymptotic worst- case running time is in O(n lg n). However this is a loose bound! Time is also in O(n). Asymptotic worst-case running time is in O(n lg n). For a node at height h, time is in O(h). O(n) + O(lgn) nO(lgn) T(n) = T(2n/3) +  (1) is in O( lgn ) using Master Theorem

7 Operations on a Heap ä PRIORITY QUEUE SUPPORT: ä HEAP-INSERT ä adds new leaf to the tree and then “swaps up” to restore HEAP PROPERTY ä HEAP- MAXIMUM ä HEAP PROPERTY guarantees that maximum is at the root of a MAX HEAP ä HEAP- EXTRACT-MAX ä removes the maximum value from the root by swapping it out ä restores HEAP PROPERTY using HEAPIFY assuming array representation Applications: Job Scheduling, Event Scheduling

8 Operations on a Heap ä PRIORITY QUEUE SUPPORT: ä HEAP-INSERT ä HEAP- MAXIMUM ä HEAP- EXTRACT-MAX assuming array representation Asymptotic worst-case running time is in O(lg n). For a node at height h, time is in O(h). Asymptotic worst-case running time is in O(1). Asymptotic worst-case running time is in O(lg n). For a node at height h, time is in O(h). O(1) + O(lgn)

9 Building a Heap using HEAPIFY vs. HEAP-INSERT ä HEAPIFY ä swaps down ä compares parent with both children before each swap 16 1410 8793 241 Asymptotic worst-case running time of BUILD-HEAP using HEAPIFY is in O(n). However, using HEAP-INSERT the time would only be in O(n lg n). ä HEAP-INSERT ä swaps up ä compares parent with one child before each swap number of levels number of nodes in this level maximum number of swaps = length of path from this level down to leaf number of levels number of nodes in this level maximum number of swaps = length of path from this level up to root O(n) as in HEAPIFY


Download ppt "UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2007 Heap Lecture Chapter 6 Use NOTES feature to see explanation."

Similar presentations


Ads by Google