CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.

Slides:



Advertisements
Similar presentations
BY Lecturer: Aisha Dawood. Heapsort  O(n log n) worst case like merge sort.  Sorts in place like insertion sort.  Combines the best of both algorithms.
Advertisements

Analysis of Algorithms
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2009 Heap Lecture Chapter 6 Use NOTES feature to see explanation.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Analysis of Algorithms CS 477/677
Comp 122, Spring 2004 Heapsort. heapsort - 2 Lin / Devi Comp 122 Heapsort  Combines the better attributes of merge sort and insertion sort. »Like merge.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 7 Heapsort and priority queues Motivation Heaps Building and maintaining heaps.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
1 Priority Queues A priority queue is an ADT where: –Each element has an associated priority –Efficient extraction of the highest-priority element is supported.
3-Sorting-Intro-Heapsort1 Sorting Dan Barrish-Flood.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2007 Heap Lecture Chapter 6 Use NOTES feature to see explanation.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Heap Lecture 2 Chapter 7 Wed. 10/10/01 Use NOTES feature to.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
Sorting Algorithms (Part II) Slightly modified definition of the sorting problem: input: A collection of n data items where data item a i has a key, k.
2IL50 Data Structures Spring 2015 Lecture 3: Heaps.
Ch. 6: Heapsort n nodes. Heap -- Nearly binary tree of these n nodes (not just leaves) Heap property If max-heap, the max-heap property is that for every.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
Binary Heap.
2IL50 Data Structures Fall 2015 Lecture 3: Heaps.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
David Luebke 1 6/3/2016 CS 332: Algorithms Heapsort Priority Queues Quicksort.
Prepared by- Jatinder Paul, Shraddha Rumade
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Data Structure & Algorithm Lecture 5 Heap Sort & Binary Tree JJCAO.
Introduction to Algorithms
1 Analysis of Algorithms Chapter - 03 Sorting Algorithms.
1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Priority Queues and Binary Heaps Algorithms.
CPSC 311 Section 502 Analysis of Algorithm Fall 2002 Department of Computer Science Texas A&M University.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΡΧΕΙΩΝ
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Chapter 6: Heapsort Combines the good qualities of insertion sort (sort in place) and merge sort (speed) Based on a data structure called a “binary heap”
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
CSE 5392 Fall 2005 Week 4 Devendra Patel Subhesh Pradhan.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
Internal and External Sorting External Searching
David Luebke 1 2/5/2016 CS 332: Algorithms Introduction to heapsort.
Analysis of Algorithms CS 477/677 Lecture 8 Instructor: Monica Nicolescu.
COSC 3101A - Design and Analysis of Algorithms 3 Recurrences Master’s Method Heapsort and Priority Queue Many of the slides are taken from Monica Nicolescu’s.
Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.
ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.
Sept Heapsort What is a heap? Max-heap? Min-heap? Maintenance of Max-heaps -MaxHeapify -BuildMaxHeap Heapsort -Heapsort -Analysis Priority queues.
Heapsort Lecture 4 Asst. Prof. Dr. İlker Kocabaş.
6.Heapsort. Computer Theory Lab. Chapter 6P.2 Why sorting 1. Sometimes the need to sort information is inherent in a application. 2. Algorithms often.
1 Algorithms CSCI 235, Fall 2015 Lecture 13 Heap Sort.
"Teachers open the door, but you must enter by yourself. "
Heaps, Heapsort, and Priority Queues
Heaps, Heap Sort and Priority Queues
Heapsort Chapter 6 Lee, Hsiu-Hui
Heap Sort Example Qamar Abbas.
Introduction to Algorithms
Heaps, Heapsort, and Priority Queues
CS200: Algorithm Analysis
Ch 6: Heapsort Ming-Te Chi
Heapsort.
"Teachers open the door, but you must enter by yourself. "
Design and Analysis of Algorithms
Topic 5: Heap data structure heap sort Priority queue
HEAPS.
Heapsort Sorting in place
Solving Recurrences Continued The Master Theorem
Binary Heaps and Heapsort
Algorithms CSCI 235, Spring 2019 Lecture 15 Analysis of Heap Sort
Presentation transcript:

CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis

2 Special Types of Trees Def: Full binary tree a binary tree in which each node is either a leaf or has degree exactly 2. Def: Complete binary tree a binary tree in which all leaves are on the same level and all internal nodes have degree 2. Full binary tree Complete binary tree

Definitions Height of a node = the number of edges on the longest simple path from the node down to a leaf Level of a node = the length of a path from the root to the node Height of tree = height of root node Height of root = 3 Height of (2)= 1 Level of (10)= 2

Useful Properties Height of root = 3 Height of (2)= 1 Level of (10)= 2 There are at most 2 l nodes at level (or depth) l of a binary tree A binary tree of height h has at most (2 h+1 – 1) nodes **here A binary tree with n nodes has height at least  lgn . (see Ex 6.1-1&2)

5 The Heap Data Structure Def: A heap is a nearly complete binary tree with the following two properties: ◦ Structural property: all levels are full, except possibly the last one, which is filled from left to right ◦ Order (heap) property: for any node x, Parent(x) ≥ x Heap From the heap property, it follows that: The root is the maximum element of the heap!

Array Representation of Heaps A heap can be stored as an array A. ◦ Root of tree is A[1] ◦ Left child of A[i] = A[2i] ◦ Right child of A[i] = A[2i + 1] ◦ Parent of A[i] = A[  i/2  ] ◦ Heapsize[A] ≤ length[A] The elements in the subarray A[(  n/2  +1).. n] are leaves

7 Heap Types Max-heaps (largest element at root), have the max-heap property: ◦ for all nodes i, excluding the root: A[PARENT(i)] ≥ A[i] Min-heaps (smallest element at root), have the min-heap property: ◦ for all nodes i, excluding the root: A[PARENT(i)] ≤ A[i]

8 Operations on Heaps Maintain/Restore the max-heap property ◦ MAX-HEAPIFY Create a max-heap from an unordered array ◦ BUILD-MAX-HEAP Sort an array in place ◦ HEAPSORT Priority queues

9 Maintaining the Heap Property MAX-HEAPIFY Suppose a node i is smaller than a child and Left and Right subtrees of i are max-heaps How do you fix it? To eliminate the violation:  Exchange node i with the larger child  Move down the tree  Continue until node is not smaller than children ◦ MAX-HEAPIFY

Example MAX-HEAPIFY(A, 2, 10) A[2] violates the heap property A[2]  A[4] A[4] violates the heap property A[4]  A[9] Heap property restored

Maintaining the Heap Property Assumptions: ◦ Left and Right subtrees of i are max-heaps ◦ A[i] may be smaller than its children Alg: MAX-HEAPIFY( A, i, n ) 1.l ← LEFT( i ) 2.r ← RIGHT( i ) 3.if l ≤ n and A[l] > A[i] 4. then largest ← l 5. else largest ← i 6.if r ≤ n and A[r] > A[largest] 7. then largest ← r 8.if largest  i 9. then exchange A[i] ↔ A[largest] 10. MAX-HEAPIFY( A, largest, n )

MAX-HEAPIFY Running Time It traces a path from the root to a leaf (longest path length h) At each level, it makes two comparisons Total number of comparisons = 2h Running Time of MAX-HEAPIFY = O(2h) = O(lgn)

13 Building a Heap Alg: BUILD-MAX-HEAP (A) 1. n = length[A] 2. for i ←  n/2  downto 1 3. do MAX-HEAPIFY (A, i, n) Convert an array A[1 … n] into a max-heap ( n = length[A] ) The elements in the subarray A[(  n/2  +1).. n] are leaves Apply MAX-HEAPIFY on elements between 1 and  n/2  (bottom up) A:

Example: A i = 5 i = 4 i = 3 i = 2 i = 1

15 Running Time of BUILD MAX HEAP  Running time: O(nlgn) This is not an asymptotically tight upper bound! Why? Alg: BUILD-MAX-HEAP (A) 1.n = length[A] 2. for i ←  n/2  downto 1 3. do MAX-HEAPIFY (A, i, n)  O(lgn) O(n)

Running Time of BUILD MAX HEAP MAX-HEAPIFY takes O(h)  the cost of MAX-HEAPIFY on a node i proportional to the height of the node i in the tree Height Level h 0 = 3 (  lgn  ) h 1 = 2 h 2 = 1 h 3 = 0 i = 0 i = 1 i = 2 i = 3 (  lgn  ) No. of nodes h i = h – i height of the heap rooted at level i n i = 2 i number of nodes at level i  see the next slide

Cost of HEAPIFY at level i  (# of nodes at that level) Replace the values of n i and h i computed before Multiply by 2 h both at the nominator and denominator and write 2 i as (1/2 -i ) Change variables: k = h - i The sum above is smaller than the sum of all elements to  and h = lgn Running time of BUILD-MAX-HEAP T(n) = O(n) Running Time of BUILD MAX HEAP x=1/2 in the summation

Goal: ◦ Sort an array using heap representations Idea: ◦ Build a max-heap from the array ◦ Swap the root (the maximum element) with the last element in the array ◦ “Discard” this last node by decreasing the heap size ◦ Call MAX-HEAPIFY on the new root ◦ Repeat this process until only one node remains 18 Heapsort

19 Example: A=[7, 4, 3, 1, 2] MAX-HEAPIFY(A, 1, 4) MAX-HEAPIFY(A, 1, 3) MAX-HEAPIFY(A, 1, 2) MAX-HEAPIFY(A, 1, 1)

20 Alg: HEAPSORT(A) 1. BUILD-MAX-HEAP (A) 2. for i ← length[A] downto 2 3. do exchange A[1] ↔ A[i] 4. MAX-HEAPIFY (A, 1, i - 1) Running time: O(nlgn)  Can be shown to be Θ(nlgn)  O(n)  O(lgn) n-1 times

Priority Queues Each element is associated with a value (priority) The key with the highest (lowest) priority is extracted first Support the following operations: ◦ INSERT (S, x) : inserts element x into set S ◦ EXTRACT-MAX (S) : removes and returns element of S with largest key ◦ MAXIMUM (S) : returns element of S with largest key ◦ INCREASE-KEY (S, x, k) : increases value of element x ’s key to k (Assume k ≥ x ’s current key value) 145

22 HEAP-MAXIMUM Goal: ◦ Return the largest element of the heap Alg: HEAP-MAXIMUM (A) 1. return A[1]  Running time: O(1) Heap A: Heap-Maximum(A) returns 7

23 HEAP-EXTRACT-MAX Goal: ◦ Extract the largest element of the heap (i.e., return the max value and also remove that element from the heap Idea: ◦ Exchange the root element with the last ◦ Decrease the size of the heap by 1 element ◦ Call MAX-HEAPIFY on the new root, on a heap of size n-1 Heap A: Root is the largest element

Example: HEAP-EXTRACT-MAX max = Heap size decreased with Call MAX-HEAPIFY (A, 1, n-1)

25 HEAP-EXTRACT-MAX Alg: HEAP-EXTRACT-MAX (A, n) 1. if n < 1 2. then error “heap underflow” 3. max ← A[1] 4. A[1] ← A[n] 5. MAX-HEAPIFY (A, 1, n-1) % remakes heap 6. return max Running time: O(lgn)

HEAP-INCREASE-KEY Goal: ◦ Increases the key of an element i in the heap Idea: ◦ Increment the key of A[i] to its new value ◦ If the max-heap property does not hold anymore: traverse a path toward the root to find the proper place for the newly increased key i Key [i] ← 15

Example: HEAP-INCREASE-KEY i i Key [ i ] ← i i

Alg: HEAP-INCREASE-KEY (A, i, key) 1. if key < A[i] 2. then error “new key is smaller than current key” 3. A[i] ← key 4. while i > 1 and A[PARENT(i)] < A[i] 5. do exchange A[i] ↔ A[PARENT(i)] 6. i ← PARENT(i) Running time: O(lgn) i Key [i] ← 15

29 -- MAX-HEAP-INSERT Goal:  Inserts a new element into a max-heap Idea:  Expand the max-heap with a new element whose key is -   Calls HEAP-INCREASE-KEY to set the key of the new node to its correct value and maintain the max-heap property

30 Example: MAX-HEAP-INSERT -- Insert value 15: Start by inserting -  Increase the key to 15 Call HEAP-INCREASE-KEY on A[11] = The restored heap containing the newly added element

Alg: MAX-HEAP-INSERT (A, key, n) 1. heap-size[A] ← n A[n + 1] ← -  3. HEAP-INCREASE-KEY (A, n + 1, key) Running time: O(lgn) --

32 Summary We can perform the following operations on heaps: ◦ MAX-HEAPIFY O(lgn) ◦ BUILD-MAX-HEAP O(n) ◦ HEAP-SORT O(nlgn) ◦ MAX-HEAP-INSERT O(lgn) ◦ HEAP-EXTRACT-MAX O(lgn) ◦ HEAP-INCREASE-KEY O(lgn) ◦ HEAP-MAXIMUM O(1) Average: O(lgn)

33 Priority Queue Using Linked List Average: O(n) Increase key: O(n) Extract max key: O(1) 12 4

Problems a. What is the maximum number of nodes in a max heap of height h? b. What is the maximum number of leaves? c. What is the maximum number of internal nodes? d. Assuming the data in a max-heap are distinct, what are the possible locations of the second-largest element?

35 Demonstrate, step by step, the operation of Build-Heap on the array A = [5, 3, 17, 10, 84, 19, 6, 22, 9] Let A be a heap of size n. Give the most efficient algorithm for the following tasks: (a)Find the sum of all elements (b)Find the sum of the largest lgn elements Problems