How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Similar documents
CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

1 Tree Sort LECTURE 4. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

CS2 Algorithms and Data Structures Note 6

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Choice of C++ as Language

Data Structures and Algorithms Week 4

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Data Structures and Algorithms Chapter 4

CS2 Algorithms and Data Structures Note 6

Chapter 6 Heapsort 1

Data Structures Question Bank Multiple Choice

OF VICTORIA EXAMINATIONS- DECEMBER 2010 CSC

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

Binary Heaps in Dynamic Arrays

CS:3330 (22c:31) Algorithms

Heaps. Heaps. A heap is a complete binary tree.

Priority Queues. T. M. Murali. January 29, 2009

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

Quiz 1 Practice Problems

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Priority Queues. T. M. Murali. January 26, T. M. Murali January 26, 2016 Priority Queues

COMP Data Structures

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

Priority queues. Priority queues. Priority queue operations

Priority Queues and Heaps. Heaps of fun, for everyone!

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

Data Structures and Algorithms. Roberto Sebastiani

Priority Queues Heaps Heapsort

Introduction to Data Structure

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Algorithms and Data Structures

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Priority Queues. T. M. Murali. January 23, T. M. Murali January 23, 2008 Priority Queues

CS 303 Design and Analysis of Algorithms

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

Recall from Last Time: Big-Oh Notation

Lecture 5: Sorting Part A

Partha Sarathi Manal

arxiv: v3 [cs.ds] 18 Apr 2011

Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

Lecture 3. Recurrences / Heapsort

CISC 320 Midterm Exam

Basic Data Structures (Version 7) Name:

CS301 - Data Structures Glossary By

The Heap Data Structure

Complexity of Algorithms

Algorithms and Data Structures

Properties of a heap (represented by an array A)

Collection of priority-job pairs; priorities are comparable.

COMP 250 Fall heaps 2 Nov. 3, 2017

Data Structures Lecture 8

CSL 201 Data Structures Mid-Semester Exam minutes

Quiz 1 Practice Problems

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Programming II (CS300)

Heaps, Heapsort, Priority Queues

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Lecture: Analysis of Algorithms (CS )

UNIVERSITY REGULATIONS

Priority Queues Heaps Heapsort

Operations. Priority Queues & Heaps. Prio-Q as Array. Niave Solution: Prio-Q As Array. Prio-Q as Sorted Array. Prio-Q as Sorted Array

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Nicki Dell Spring 2014

CH 8. HEAPS AND PRIORITY QUEUES

Complexity of Algorithms. Andreas Klappenecker

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

Transform & Conquer. Presorting

2016 Spring COP 3502 Final Exam. Date: 4/28/2016. Name:

Next. 1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

CH. 8 PRIORITY QUEUES AND HEAPS

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

Sorting and Searching

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

BM267 - Introduction to Data Structures

Sorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

O(n): printing a list of n items to the screen, looking at each item once.

Priority Queues, Binary Heaps, and Heapsort

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

Algorithms: Efficiency, Analysis, techniques for solving problems using computer approach or methodology but not programming

COT 5407: Introduction to Algorithms. Giri Narasimhan. ECS 254A; Phone: x3748

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

Chapter 4: Trees. 4.2 For node B :

Sorting and Searching

Chapter 6 Heap and Its Application

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Big-O-ology. Jim Royer January 16, 2019 CIS 675. CIS 675 Big-O-ology 1/ 19

Lecture 2: Algorithm Analysis

Transcription:

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A; } if (n<=1) return; use_space(n/2); use_space(n/2);

Heapsort A Compost Heap Pictures from: Madame Trash Heap http://www.compostinfo.com/tutorial/methods.htm 2 http://linguiniontheceiling.blogspot.com/

Max-heap: The data value at each node is greater than or equal to the data values of its children. 3

Left complete binary tree- fill in last level of a complete binary tree from left to right. A heap is always stored in a tree with the shape of a left-complete binary tree. 4

Left complete binary tree- fill in last level of a complete binary tree from left to right. 5

Left complete binary tree- fill in last level of a complete binary tree from left to right. 6

Left complete binary tree- fill in last level of a complete binary tree from left to right. 7

Left complete binary tree- fill in last level of a complete binary tree from left to right. 8

Left complete binary tree- fill in last level of a complete binary tree from left to right. 9

Left complete binary tree- fill in last level of a complete binary tree from left to right. 10

Left complete binary tree- fill in last level of a complete binary tree from left to right. 11

Left complete binary tree- fill in last level of a complete binary tree from left to right. 12

One way to build a heap: Add the new entry in the position which is the next slot of a left-complete binary tree. Then bubble-up to restore the heap property. Max-heap: The data value at each node is greater than or equal to the data values of its children. 13

Bubble-up pseudocode (swim): While the current node is not the root and current.data is greater than the data value of the parent of current { 1. Swap the data values in nodes current and the parent of current. 2. Set the current node to be the parent of current. } 14

Then to sort (like a Max Sort but now the heap is used to determine the max): Repeat: Delete the max from the heap. Fix the heap. Until the heap is empty. 15

Problem of the Day 1. Insert the following keys into a heap using a bubbleup approach: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Apply repeated deletemax operations to this heap in order to sort the data: 17

DeleteMax: To fix the heap after deleting the max: Take the data value which is in the slot which will disappear when the leftcomplete binary tree has one node removed. Place it at the top of the heap (where the data value was just deleted). Then Bubble-down this data value in order to restore the heap property. 18

To Bubble-down (sink): Repeat: Swap the data value with the largest of its two children until either the data value moves to the bottom of the tree, or it is greater than or equal to its children. Implementation note: Near the bottom of the tree, a node may have 0 or 1 children instead of the usual two children. 19

Timing: To build the heap: The roughly n/2 nodes at the bottom of the heap may all have to bubble up to the top of the heap. So the time to insert all the nodes is in θ(n log 2 (n)) in the worst case. The DeleteMax operation is repeated n times and in the worst case, results in the value bubbling down to the bottom of the tree. Since roughly half the nodes may take log 2 (n) time, the time complexity in the worst case is in θ(n log 2 (n)). 20

Show the result of inserting these values into a heap one at a time using bubble up with each insertion: 1, 2, 3, 4, 5, 6, 7 If you get finished early: finish off the heap sort by doing 7 deletemax operations using bubble down to fix the heap at each step. 21

http://www.biztechmagazine.com/article.asp?item_id=318 22

http://www.eionwireless.com/vip_release/10_reasons.html 23

http://swc.scipy.org/lec/img/shell01 /operating_system.png http://www.cse.chalmers.se/edu/os/pic/06-09-2001.gif 24

What does a worst case example look like? 25

Show what happens to this heap if you 1. Delete the max. 2. Insert 28. 26

How much work is done to create the heap in the worst case using the bubble up strategy? S(n) is proportional to the time taken: S(n)= 1*1 + 2*2 +4*3 + 8*4 + + 2 h *(h+1) where n= 1 + 2 + 4 + + 2 h = 2 h+1 1. How can we prove that S(n) θ(n log 2 (n))? 27

Assume that T, f and g are functions mapping the natural numbers {0, 1, 2, 3,...} into the reals. Definition: Omega A function T(n) is in Ω(f(n)) if there exist constants n 0 0, and c > 0, such that for all n n 0, T(n) c * f(n). 28

Recall: n= 1 + 2 + 4 + + 2 h = 2 h+1 1. Proof that S(n) Ω(n log 2 (n)) : Use technique for getting a lower bound on a sum: S(n)= 1*1 + 2*2 +4*3 + 8*4 + + 2 h-1 *(h) + 2 h *(h+1) 0 + 0 + 0 + 0 + + 0 + 2 h *(h+1) = (n+1)/2 * log 2 (n+1) ½ * n * log 2 (n) since (n+1)/2 ½ * n and log 2 (n+1) log 2 (n). This is true for n 1, so for the proof that S(n) Ω(n log 2 (n)) we have that c= ½ and n 0 = 1. 29

This is a typical lower bounding argument. The key concept: Because for approximately ½ of the insertions the time is in Ω(f(n)), the total time is in Ω(n* f(n)). 30

Assume that T, f and g are functions mapping the natural numbers {0, 1, 2, 3,...} into the reals. Definition: Big Oh A function T(n) is in O(f(n)) if there exist constants n 0 0, and c > 0, such that for all n n 0, T(n) c * f(n). 31

Recall: n= 1 + 2 + 4 + + 2 h = 2 h+1 1. Proof that S O(n log 2 (n)) : Use technique for getting a upper bound on a sum: S= 1*1 + 2*2 + + 2 h-1 *(h) + 2 h *(h+1) 1*(h+1) + 2*(h+1)+ + 2 h-1 *(h+1) + 2 h *(h+1) = (h+1) * [2 h+1 1] = n * log 2 (n+1) It is obvious that it is not true that n * log 2 (n+1) 1* n * log 2 (n). So for our proof, let s try to find n large enough so that: n * log 2 (n+1) 2* n * log 2 (n). 32

Find n large enough: log 2 (n+1) 2* log 2 (n) Making the math easier by looking at integers: h n n+1 log 2 (n+1) log 2 (n) 2* log 2 (n) 0 1 2 1 0 0 1 3 4 2 1 2 2 7 8 3 2 4 3 15 16 4 3 6 4 31 32 5 4 8 We can see that for n 3 (h 1), log 2 (n+1) 2* log 2 (n) 2* log 2 (n) 33

Theoretical justification that for n 3, log 2 (n+1) 2* log 2 (n) 2* log 2 (n) Recall: n= 1 + 2 + 4 + + 2 h = 2 h+1 1. log 2 (n+1)= h+1 log 2 (n) = h So we have h+1 2* h which is true for h 1 (meaning n 3) And the last inequality is true because of the properties of the floor function. 34

Recall: n= 1 + 2 + 4 + + 2 h = 2 h+1 1. Proof that S O(n log 2 (n)) : Use technique for getting a upper bound on a sum: S= 1*1 + 2*2 + + 2 h-1 *(h) + 2 h *(h+1) 1*(h+1) + 2*(h+1)+ + 2 h-1 *(h+1) + 2 h *(h+1) = (h+1) * [2 h+1 1] = n * log 2 (n+1) 2* n * log 2 (n) for n 3. So for the proof that S is in O(n log 2 (n)) we have that c= 2 and n 0 = 3. 35

Assume that T, f and g are functions mapping the natural numbers {0, 1, 2, 3,...} into the reals. Definition: Theta The set θ(g(n)) of functions consists of Ω(g(n)) O(g(n)). 36

Heaps can be stored in arrays with implicit parent/child pointers. http://scienceblogs.com/goodmath/heap-array-example.png Our text starts the array at 1 probably as an anachronism from Pascal. We will start at 0 since we are programming in Java or C. 37

What is the array index of: The parent of the node in position k? 38

What is the array index of: The left child of the node in position k? 39

What is the array index of: The right child of the node in position k? 40

Example: 41

Deleting the max using an array: 42

43

44

45

46

47

It s possible to build a heap in O(n) time: 48

To heapify at a node r: Heapify the left subtree. Heapify the right subtree. Bubble down the data value at node r. Non-recursively: For (i=?? ; i >=0; i--) Bubble-down the data value at position i. How long does this take in the worst case? 49

Let T(h) be the time to heapify a complete binary tree of height h. T(0)= 0 since nothing has to be done with only one node present to make it a heap. T(h) = h + 2 T(h-1) Since we first make the left and right subtrees into a heap in 2T(h-1) time and then bubble-down the data value at the root taking time proportional to h. 50

T(h) = h + 2 T(h-1), T(0)= 0. This is not a fun recurrence to solve, but you should be able to get to the point where the recurrence is expressed as a sum. You also should be able to prove by induction that the solution to this recurrence is: T(h) = 2 h+1 h 2. What does this say in terms of n about the worst case time complexity for constructing a heap using this approach? 51

Suppose that one node in the priority queue (heap) has its data value changed. What should you do to fix a max-heap if its data value: 1. increases? 2. decreases? 3. How much time does this take in the worst case? How hard is it to find a minimum key value in a max-heap? 52