# Chapter 2: Using Objects - Computer Science and Engineering

RAIK 283: Data Structures & Algorithms Transform and Conquer (Heaps and Heapsort) Dr. Ying Lu [email protected] Design and Analysis of Algorithms Chapter 6 1 RAIK 283: Data Structures & Algorithms Giving credit where credit is due: Most of the lecture notes are based on the slides from the Textbooks companion website http://www.aw-bc.com/info/levitin

Some examples and slides are based on lecture notes created by Dr. Ben Choi, Louisiana Technical University and Dr. Chuck Cusack, Hope College and Dr. Ellen Walker from Hiram College I have modified many of their slides and added new slides. Design and Analysis of Algorithms Chapter 6 2 Heapsort Definition: A heap is a binary tree with the following conditions: it is essentially complete: (all levels are full except possibly the last level, where only some rightmost leaves may be missing.)

the key at each node is keys at its children --- parental dominance Design and Analysis of Algorithms Chapter 6 3 Heaps (or not)? 9 9 5 4 5 7

2 6 5 tree 1 7 2 1 tree 2 9 5

7 2 1 tree 3 Design and Analysis of Algorithms Chapter 6 4 Definition implies: There exists a uniquely structured binary tree with n nodes that is essentially complete, with h= lg n

The root has the largest key The subtree rooted at any node of a heap is also a heap Partial order tree property Design and Analysis of Algorithms Chapter 6 5 Priority queues A priority queue is the abstract data type (ADT) of

an ordered set with the operations: find element with highest priority delete element with highest priority insert element with assigned priority Heaps are very good for implementing priority queues Design and Analysis of Algorithms Chapter 6 6

Bottom-up vs. Top-down heap construction Bottom-up: Put everything in and then fix it Top down: Heaps can be constructed by successively inserting elements into an (initially) empty heap Design and Analysis of Algorithms Chapter 6 7 Bottom-up construction

Insert elements in the order given breadth-first in a binary tree Starting with the last (rightmost) parental node, fix the heap rooted at it, if it does not satisfy the heap condition: 1. exchange it with its largest child 2. fix the subtree rooted at it (now in the childs position) Example: 2 9 7 6 5 8 10 Efficiency: Design and Analysis of Algorithms Chapter 6 8

Bottom-up heap construction analysis For parental node at level i it does 2(h-i) comparisons in the worst case Total: h-1 i=0 2(h-i) 2i = 2 ( n lg (n + 1)) = (n)

# nodes at level i Design and Analysis of Algorithms Chapter 6 9 Top-down heap construction Top down: Heaps can be constructed by successively inserting elements into an (initially) empty heap Design and Analysis of Algorithms Chapter 6 10

Top-down construction: Insertion of a new element Insert element at last position in heap Compare with its parent and if it violates heap condition exchange them Continue comparing the new element with nodes up the tree until the heap condition is satisfied Example: 2 9 7 6 5 8 10 Efficiency: Design and Analysis of Algorithms Chapter 6

11 Top-down construction: Insertion of a new element Insert element at last position in heap. Compare with its parent and if it violates heap condition exchange them Continue comparing the new element with nodes up the tree until the heap condition is satisfied Efficiency: log(i) n log(n) Design and Analysis of Algorithms Chapter 6

12 Bottom-up vs. Top-down heap construction Bottom-up: Put everything in and then fix it Top down: Heaps can be constructed by successively inserting elements into an (initially) empty heap Which one is better?

Design and Analysis of Algorithms Chapter 6 13 In-class exercise Page 233 Exercise 6.4.1 a. Construct a heap for the list 1, 8, 6, 5, 3, 7, 4 by the bottom-up algorithm. b. Construct a heap for the list 1, 8, 6, 5, 3, 7, 4 by successive key insertions (top-down algorithm). c. Is it always true that the bottom-up and top-down algorithms yield the same heap for the same input? Design and Analysis of Algorithms Chapter 6

14 Heapsort Strategy If the elements to be sorted are arranged in a heap, how can we build a sorted sequence from it? Design and Analysis of Algorithms Chapter 6 15 Heapsort Strategy If the elements to be sorted are arranged in a heap, we can build a sorted sequence in reverse order by

repeatedly removing the element from the root, rearranging the remaining elements to reestablish the partial order tree property, and so on. Design and Analysis of Algorithms Chapter 6 16 Heapsort Algorithm: 1. Build heap 2. Remove root exchange with last (rightmost) leaf

3. Fix up heap (excluding last leaf) Repeat 2, 3 until heap contains just one node. Design and Analysis of Algorithms Chapter 6 17 Root deletion and Heap fix up The root of a heap can be deleted and the heap fixed up as follows:

exchange the root with the last leaf compare the new root (formerly the leaf) with each of its children and, if one of them is larger than the root, exchange it with the larger of the two. continue the comparison/exchange with its current children until it reaches a level of the tree where it is larger than both its children Design and Analysis of Algorithms Chapter 6 18 deleteMax() in action Design and Analysis of Algorithms Chapter 6 19

Analysis of Heapsort (continued) Recall algorithm: 1. Build heap 2. Remove root exchange with last (rightmost) leaf 3. Fix up heap (excluding last leaf) Repeat 2, 3 until heap contains just one node. Design and Analysis of Algorithms Chapter 6 20 Analysis of Heapsort (continued) Recall algorithm: (n) 1. Build heap 2. Remove root exchange with last (rightmost) leaf 3. Fix up heap (excluding last leaf)

(log k) Repeat 2, 3 until heap contains just one node. k=n 1, n-2, 1 Total: (n) + ( n log n) = (n log n) Note: this is the worst case. Average case also (n log n). Design and Analysis of Algorithms Chapter 6 21 Analysis of Heapsort (continued) Is HeapSort an in-place sorting algorithm?

How to store/represent a Heap in an array? Design and Analysis of Algorithms Chapter 6 22 Representation Use an array to store breadth-first traversal of heap tree: Example: 9 5 1

1 2 3 4 5 6 3 4 9 5 3 1 4 2 2 Left child of node j is at 2j Right child of node j is at 2j+1

Parent of node j is at j/2 Parental nodes are represented in the first n/2 locations Using array: simpler and efficient implementation of heaps Design and Analysis of Algorithms Chapter 6 23 Bottom-up heap construction algorithm Design and Analysis of Algorithms Chapter 6 24 Heapsort example

Sorting the array 2 9 7 6 5 8 10 Design and Analysis of Algorithms Chapter 6 25 In-class exercises (I) Exercise 6.4.7 (a) (c) Sort the following lists by heapsort by using the array representation of heaps: a) 1, 2, 3, 4, 5 (in increasing order)

c) S, O, R, T, I, N, G (in alphabetical order) Design a heap construction algorithm by applying divide and conquer strategy Design and Analysis of Algorithms Chapter 6 26

## Recently Viewed Presentations

• Transit Bus Emissions . Control Systems. Day 2: DOC with DPF Emissions Control Systems. Today's Learning Objectives. Explain the role of exhaust after-treatment in reducing diesel engine emissions.
• Drama Literary Terms. Aside. FOR THE AUDIENCE!! Short phrases spoken by a character in a play, usually in an undertone. ... 5 meters x 2 dadums = 10. Five verse feet with each foot an iamb (a total of ten...
• Narrative Poetry Narrative poems tell a story. They have a plot, characters, and conflict-just like a short story. Epic Poems Epic poems are VERY long (many pages!) and they involve a hero and his journey. Beowulf and Sir Gawain and...
• Amount of mass per unit volume of a material. Unit of density is kg/m3 or gm/cm3. Example: Density of water is 1000 kg/m3, or 1 g/cm3. ... mass of the atoms. ... PowerPoint Presentation
• Regulation of Low Level Waste Management This will coverâ€¦. Environment Agency role in implementing Government policy Our approach to authorising more flexible routes introduced by the policy - existing landfills and new facilities on or adjacent to nuclear sites Proposals...
• If we widen the viewing rectangle to [0,100] by [0, 10], we obtain the graph in Figure 14(b). (a) (b) Figure 14. cont'd. Example 11 - Solution. For large . x . the combined resistance . R . levels off,...
• 'Luring' Overseas Trained Doctors to Australia: Ethical Issues in Training and Trading Robyn Iredale, Adjunct Senior Research Fellow Australian Demographic and Social Research Institute (ADSRI)
• Mobile Ministry By John Edmiston, feel free to distribute this PowerPoint presentation unaltered to your ministry friends but it must not be sold in any way.