# RAIK 283: Data Structures & Algorithms

## Presentation on theme: "RAIK 283: Data Structures & Algorithms"— Presentation transcript:

RAIK 283: Data Structures & Algorithms
Transform and Conquer (Heaps and Heapsort) Dr. Ying Lu Design and Analysis of Algorithms – Chapter 6

RAIK 283: Data Structures & Algorithms
Giving credit where credit is due: Most of the lecture notes are based on the slides from the Textbook’s companion website 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

Design and Analysis of Algorithms – Chapter 6
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

Design and Analysis of Algorithms – Chapter 6
Heaps (or not)? 7 2 6 5 9 1 7 2 4 5 9 tree 1 tree 2 7 2 5 9 1 tree 3 Design and Analysis of Algorithms – Chapter 6

Design and Analysis of Algorithms – Chapter 6
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

Design and Analysis of Algorithms – Chapter 6
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

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

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: exchange it with its largest child fix the subtree rooted at it (now in the child’s position) Example: Efficiency: Design and Analysis of Algorithms – Chapter 6

Bottom-up heap construction analysis
For parental node at level i it does 2(h-i) comparisons in the worst case Total: Σ 2(h-i) 2i = 2 ( n – lg (n + 1)) = Θ(n) i=0 h-1 # nodes at level i Design and Analysis of Algorithms – Chapter 6

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

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: Efficiency: Design and Analysis of Algorithms – Chapter 6

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

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

Design and Analysis of Algorithms – Chapter 6
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

Design and Analysis of Algorithms – Chapter 6
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

Design and Analysis of Algorithms – Chapter 6
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

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

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

Design and Analysis of Algorithms – Chapter 6
deleteMax() in action Design and Analysis of Algorithms – Chapter 6

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

Analysis of Heapsort (continued)
Recall algorithm: Build heap Remove root –exchange with last (rightmost) leaf Fix up heap (excluding last leaf) Repeat 2, 3 until heap contains just one node. Θ(n) Θ(log k) 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

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

Design and Analysis of Algorithms – Chapter 6
Representation Use an array to store breadth-first traversal of heap tree: Example: 9 1 5 3 4 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

Bottom-up heap construction algorithm
Design and Analysis of Algorithms – Chapter 6

Design and Analysis of Algorithms – Chapter 6
Heapsort example Sorting the array Design and Analysis of Algorithms – Chapter 6

In-class exercises (I)
Exercise (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