Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Design and Analysis of Algorithms – Chapter 61 Transform and Conquer (Heaps and Heapsort) Dr. Ying Lu RAIK 283: Data Structures & Algorithms."— Presentation transcript:

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

2 Design and Analysis of Algorithms – Chapter 62 b Giving credit where credit is due: Most of the lecture notes are based on the slides from the Textbooks companion websiteMost of the lecture notes are based on the slides from the Textbooks 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 CollegeSome 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.I have modified many of their slides and added new slides. RAIK 283: Data Structures & Algorithms

3 Design and Analysis of Algorithms – Chapter 63 Heapsort Definition: A heap is a binary tree with the following conditions: b it is essentially complete: (all levels are full except possibly the last level, where only some rightmost leaves may be missing.) b the key at each node is keys at its children --- parental dominance

4 Design and Analysis of Algorithms – Chapter 64 Heaps (or not)? tree 1tree 2 tree 3

5 Design and Analysis of Algorithms – Chapter 65 Definition implies: b There exists a uniquely structured binary tree with n nodes that is essentially complete, with h= lg n b The root has the largest key b The subtree rooted at any node of a heap is also a heap b Partial order tree property

6 Design and Analysis of Algorithms – Chapter 66 Priority queues b A priority queue is the abstract data type (ADT) of an ordered set with the operations: find element with highest priorityfind element with highest priority delete element with highest prioritydelete element with highest priority insert element with assigned priorityinsert element with assigned priority b Heaps are very good for implementing priority queues

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

8 Design and Analysis of Algorithms – Chapter 68 Bottom-up construction b Insert elements in the order given breadth-first in a binary tree b 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: Efficiency:

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

10 Design and Analysis of Algorithms – Chapter 610 Top-down heap construction b Top down: Heaps can be constructed by successively inserting elements into an (initially) empty heap

11 Design and Analysis of Algorithms – Chapter 611 Top-down construction: Insertion of a new element b Insert element at last position in heap b Compare with its parent and if it violates heap condition exchange them b Continue comparing the new element with nodes up the tree until the heap condition is satisfied Example: Efficiency:

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

13 Design and Analysis of Algorithms – Chapter 613 Bottom-up vs. Top-down heap construction b Bottom-up: Put everything in and then fix it b Top down: Heaps can be constructed by successively inserting elements into an (initially) empty heap b Which one is better?

14 Design and Analysis of Algorithms – Chapter 614 In-class exercise b Page 233 Exercise a. Construct a heap for the list 1, 8, 6, 5, 3, 7, 4 by the bottom-up algorithm.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).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?c. Is it always true that the bottom-up and top-down algorithms yield the same heap for the same input?

15 Design and Analysis of Algorithms – Chapter 615 Heapsort Strategy b If the elements to be sorted are arranged in a heap, how can we build a sorted sequence from it?

16 Design and Analysis of Algorithms – Chapter 616 Heapsort Strategy b 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,repeatedly removing the element from the root, rearranging the remaining elements to reestablish the partial order tree property,rearranging the remaining elements to reestablish the partial order tree property, and so on.and so on.

17 Design and Analysis of Algorithms – Chapter 617 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.

18 Design and Analysis of Algorithms – Chapter 618 Root deletion and Heap fix up The root of a heap can be deleted and the heap fixed up as follows: b exchange the root with the last leaf b 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. b continue the comparison/exchange with its current children until it reaches a level of the tree where it is larger than both its children

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

20 Design and Analysis of Algorithms – Chapter 620 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.

21 Design and Analysis of Algorithms – Chapter 621 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. Θ(n)Θ(n) Θ(log k) k=n – 1, n-2, … 1 Total: Total: Θ(n) + Θ( n log n) = Θ(n log n) Note: Note: this is the worst case. Average case also Θ(n log n).

22 Design and Analysis of Algorithms – Chapter 622 Analysis of Heapsort (continued) b Is HeapSort an in-place sorting algorithm? b How to store/represent a Heap in an array?

23 Design and Analysis of Algorithms – Chapter 623 Representation b Use an array to store breadth-first traversal of heap tree: b Example: b Left child of node j is at 2j b Right child of node j is at 2j+1 b Parent of node j is at j/2 b Parental nodes are represented in the first n/2 locations b Using array: simpler and efficient implementation of heaps

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

25 Design and Analysis of Algorithms – Chapter 625 Heapsort example b Sorting the array

26 Design and Analysis of Algorithms – Chapter 626 In-class exercises (I) b Exercise (a) (c) Sort the following lists by heapsort by using the array representation of heaps: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) b Design a heap construction algorithm by applying divide and conquer strategy


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

Similar presentations


Ads by Google