1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Advanced Data Structures Chapter 16. Priority Queues Collection of elements each of which has a priority. Does not maintain a first-in, first-out discipline.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Analysis of Algorithms CS 477/677
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2007 Heap Lecture Chapter 6 Use NOTES feature to see explanation.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
Dr. Andrew Wallace PhD BEng(hons) EurIng
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
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.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 11 Tuesday, 12/4/01 Advanced Data Structures Chapters.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
Heapsort Based off slides by: David Matuszek
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 21 Binary Heap.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
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.
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.
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
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:
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Lecture: Priority Queue. Questions Is array a data structure? What is a data structure? What data structures are implemented by array? Priority queue.
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1 Last modified: 2/22/2016.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
Priority Queues A priority queue is an ADT where:
"Teachers open the door, but you must enter by yourself. "
School of Computing Clemson University Fall, 2012
Source: Muangsin / Weiss
Priority Queues.
CS200: Algorithm Analysis
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
Priority Queues.
"Teachers open the door, but you must enter by yourself. "
Heaps & Multi-way Search Trees
Presentation transcript:

1 Chapter 6 Heapsort

2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap property for all nodes Use Array to represent Heap Introduce Priority Queue

3 Heap A heap (or binary heap) is a binary tree that satisfies both: (1) Shape Property – All levels, except deepest, are fully filled – Deepest level is filled from left to right (2) Heap Property – Value of a node ≤ Value of its children

4 Satisfying Shape Property Example of a tree with shape property

5 Not Satisfying Shape Property This level (not deepest) is NOT fully filled

6 Not Satisfying Shape Property Deepest level NOT filled from left to right

7 Satisfying Heap Property

8 Not Satisfying Heap Property Help!

9 Min-Heap Q. Q. Given a heap, what is so special about the root’s value? A. A. … always the minimum Because of this, the previous heap is also called a min-heap

10 Heap Operations Find-Min : find the minimum value   (1) time Extract-Min : delete the minimum value   (log n) time (how??) Insert : insert a new value into heap   (log n) time (how??) n = # nodes in the heap

11 How to do Extract-Min? Heap before Extract-Min

12 Step 1: Restore Shape Property Copy value of last node to root. Next, remove last node

13 Next, highlight the root  Only this node may violate heap property Step 2: Restore Heap Property If violates, swap highlighted node with “smaller” child (if not, everything done)

14 Step 2: Restore Heap Property After swapping, only the highlighted node may violate heap property If violates, swap highlighted node with “smaller” child (if not, everything done)

15 Step 2: Restore Heap Property As soon as the highlighted node satisfies the heap property Everything done !!!

16 How to do Insert? Heap before Insert

17 Step 1: Restore Shape Property Create a new node with the new value. Next, add it to the heap at correct position 5

18 Highlight the new node  Only this node’s parent may violate heap property Step 2: Restore Heap Property If violates, swap highlighted node with parent (if not, everything done)

19 After swapping, only highlighted node’s parent may violate heap property Step 2: Restore Heap Property If violates, swap highlighted node with parent (if not, everything done)

20 As soon as highlighted node’s parent satisfies heap property Step 2: Restore Heap Property Everything done !!!

21 Running Time Let h = node-height of heap Both Extract-Min and Insert require  (h) time to perform Since h =  (log n) (why??)  Both require  (log n) time n = # nodes in the heap

22 Heapsort Q. Q. Given n numbers, can we use heap to sort them, say, in ascending order? A. A. Yes, and extremely easy !!! 1.Call Insert to insert n numbers into heap 2.Call Extract-Min n times  numbers are output in sorted order Runtime: n x  (log n) + n x  (log n) =  (n log n) This sorting algorithm is called heapsort

23 Challenge (Fixing heap property for all nodes) Suppose that we are given a binary tree which satisfies the shape property However, the heap property of the nodes may not be satisfied … Question: Can we make the tree into a heap in  (n) time? n = # nodes in the tree

24 How to make it a heap?

25 Observation u = root of a binary tree L = subtree rooted at u’s left child R = subtree rooted at u’s right child u R L Obs: If L and R satisfy heap property, we can make the tree rooted at u satisfy heap property in  ( max { height(L), height(R) } ) time. We denote the above operation by Heapify(u)

26 Heapify Then, for any tree T, we can make T satisfy the heap property as follows: Step 1. h = node _ height(T) ; Step 2. for k = h, h-1, …, 1 for each node u at level k Heapify(u) ; Why is the above algorithm correct?

27 Example Run First, heapify this tree

28 Example Run Next, heapify this tree

29 Example Run Next, heapify this tree

30 Example Run Next, heapify this tree

31 Example Run Next, heapify this tree

32 Example Run Next, heapify this tree

33 Example Run Next, heapify this tree

34 Example Run Next, heapify this tree

35 Example Run Finally, heapify the whole tree

36 Example Run Finally, heapify the whole tree

37 Example Run Everything Done !

38 Back to the Challenge (Fixing heap property for all nodes) Suppose that we are given a binary tree which satisfies the shape property However, the heap property of the nodes may not be satisfied … Question: Can we make the tree into a heap in  (n) time? n = # nodes in the tree

39 Back to the Challenge (Fixing heap property for all nodes) Let h = node-height of a tree So, 2 h-1 ≤ n ≤ 2 h - 1 (why??) For a tree with shape property, at most 2 h-1 nodes at level h, exactly 2 h-2 nodes at level h-1, exactly 2 h-3 nodes at level h-2, …

40 Back to the Challenge (Fixing heap property for all nodes) Using the previous algorithm to solve the challenge, the total time is at most 2 h-1 x h-2 x h-3 x 3+ … + 1 x h [why??] = 2 h ( 1 x ½ + 2 x (½) x (½) 3 + … + h x (½) h ) ≤ 2 h  k=1 to ∞ k x (½) k = 2 h x 2 ≤ 4n  Thus, total time is O(n)

41 Array Representation of Heap ?? Given a heap. Suppose we mark the position of root as 1, and mark other nodes in a way as shown in the right figure. (BFS order) Anything special about this marking?

42 Array Representation of Heap ?? Yes, something special: 1.If the heap has n nodes, the marks are from 1 to n 2.Children of x, if exist, are 2x and 2x+1 3.Parent of x is

43 The special properties of the marking allow us to use an array A[1..n] to store a heap of size n Array Representation of Heap Advantage: Avoid storing or using tree pointers !! Try this at home: Write codes for Insert and Extract-Min, assuming the heap is stored in an array

44 Max-Heap We can also define a max-heap, by changing the heap property to: Value of a node ≥ Value of its children Max heap supports the following operations: (1)Find Max, (2) Extract Max, (3) Insert Do you know how to do these operations?

45 Priority Queue Consider S = a set of items, each has a key Priority queue on S supports: Min(S): return item with min key Extract-Min(S): remove item with min key Insert(S,x,k): insert item x with key k Decrease-Key(S,x,k): decrease key of item x to k

46 Using Heap as Priority Queue 1. Store the items in an array 2. Use a heap to store keys of the items 3. Store links between an item and its key E.g., item 9  item 2 item 1 This node store item 1’s key

47 Previous scheme supports Min in O(1) time, Extract-Min and Insert in O(log n) time It can support Decrease-Key in O(log n) time E.g., Node storing key value of item x How do we decrease the key to k ?? Using Heap as Priority Queue

48 In algorithm classes (or perhaps later lectures), we will look at other ways to implement a priority queue with different time bounds for the operations Remark: Priority Queue can be used for finding MST or shortest paths, and job scheduling Other Schemes?

49 Homework Problem 6-2 (a, b, c) (Due Oct. 24) Bonus (1 point): Write a Heapsort program to sort n numbers (Due Oct. 31) Practice at home: 6.2-6, 6.4-4, 6.5-4, 6.5-9

50 Homework Consider the following balls-and-bin game. We start with one black ball and one white ball in a bin. We repeatedly do the following: choose one ball from the bin uniformly at random, and then put the ball back in the bin with another ball of the same color. We repeat until there are n balls in the bin. Show that the number of white balls is equally likely to be any number between 1 and n-1. (Hint: Prove by mathematical induction)