Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Similar documents
Asymptotic Analysis and Recurrences

Data Structures and Algorithms

CS 2110: INDUCTION DISCUSSION TOPICS

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

CS 395T Computational Learning Theory. Scribe: Mike Halcrow. x 4. x 2. x 6

Methods for solving recurrences

Selection and Adversary Arguments. COMP 215 Lecture 19

5 + 9(10) + 3(100) + 0(1000) + 2(10000) =

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Data selection. Lower complexity bound for sorting

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

Solving with Absolute Value

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

Quick Sort Notes , Spring 2010

CS 4349 Lecture August 30th, 2017

1 Terminology and setup

Divide and Conquer Algorithms

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

Design and Analysis of Algorithms

CS1800: Mathematical Induction. Professor Kevin Gold

Divide and Conquer. Recurrence Relations

Proof Techniques (Review of Math 271)

Concrete models and tight upper/lower bounds

CSC236 Week 3. Larry Zhang

1 Closest Pair of Points on the Plane

Divide and Conquer Algorithms

An analogy from Calculus: limits

CSE 4502/5717: Big Data Analytics

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

CS Data Structures and Algorithm Analysis

Notes on induction proofs and recursive definitions

Problem 5. Use mathematical induction to show that when n is an exact power of two, the solution of the recurrence

CS361 Homework #3 Solutions

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

N/4 + N/2 + N = 2N 2.

Quiz 3 Reminder and Midterm Results

arxiv: v1 [math.co] 22 Jan 2013

CS173 Running Time and Big-O. Tandy Warnow

University of the Virgin Islands, St. Thomas January 14, 2015 Algorithms and Programming for High Schoolers. Lecture 5

Lecture 1 : Data Compression and Entropy

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

Lecture 4: Constructing the Integers, Rationals and Reals

Induction and Recursion

Chapter Summary. Mathematical Induction Strong Induction Well-Ordering Recursive Definitions Structural Induction Recursive Algorithms

Hon.Algorithms (CSCI-GA 3520), Professor Yap Fall 2012 MIDTERM EXAM. Oct 11, 2012 SOLUTION

8.1 Polynomial Threshold Functions

CS Data Structures and Algorithm Analysis

CPSC 121: Models of Computation. Module 9: Proof Techniques (part 2) Mathematical Induction

Algorithm Design and Analysis

Math 324 Summer 2012 Elementary Number Theory Notes on Mathematical Induction

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

CS383, Algorithms Spring 2009 HW1 Solutions

CS173 Strong Induction and Functions. Tandy Warnow

Sequences of Real Numbers

Binary Decision Diagrams. Graphs. Boolean Functions

Lecture 2 September 4, 2014

CS/COE 1501 cs.pitt.edu/~bill/1501/ Integer Multiplication

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Lecture 2: Divide and conquer and Dynamic programming

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Compute the Fourier transform on the first register to get x {0,1} n x 0.

Preparing for the CS 173 (A) Fall 2018 Midterm 1

CS 173: Induction. Madhusudan Parthasarathy University of Illinois at Urbana-Champaign. February 7, 2016

Fall 2017 Test II review problems

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015

Induction and recursion. Chapter 5

Divide & Conquer. CS 320, Fall Dr. Geri Georg, Instructor CS320 Div&Conq 1

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

Solving Recurrences. Lecture 23 CS2110 Fall 2011

Exercise Sheet #1 Solutions, Computer Science, A M. Hallett, K. Smith a k n b k = n b + k. a k n b k c 1 n b k. a k n b.

Reductions, Recursion and Divide and Conquer

Solving Recurrences. 1. Express the running time (or use of some other resource) as a recurrence.

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

Lecture 3: Big-O and Big-Θ

Module 9: Mathematical Induction

1 Introduction (January 21)

Analysis of Algorithms - Using Asymptotic Bounds -

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Day 1: Over + Over Again

AN ALGEBRA PRIMER WITH A VIEW TOWARD CURVES OVER FINITE FIELDS

Finding the Median. 1 The problem of finding the median. 2 A good strategy? lecture notes September 2, Lecturer: Michel Goemans

Design and Analysis of Algorithms

Solving Recurrences. 1. Express the running time (or use of some other resource) as a recurrence.

10-704: Information Processing and Learning Fall Lecture 10: Oct 3

STEP Support Programme. Hints and Partial Solutions for Assignment 1

We have been going places in the car of calculus for years, but this analysis course is about how the car actually works.

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Park School Mathematics Curriculum Book 9, Lesson 2: Introduction to Logarithms

CSCE 750, Spring 2001 Notes 2 Page 1 4 Chapter 4 Sorting ffl Reasons for studying sorting is a big deal pedagogically useful Λ the application itself

MIDTERM Fundamental Algorithms, Spring 2008, Professor Yap March 10, 2008

Find an Element x in an Unsorted Array

Asymptotic Running Time of Algorithms

4.4 Recurrences and Big-O

Transcription:

CS17 Integrated Introduction to Computer Science Klein Contents Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018 1 Tree definitions 1 2 Analysis of mergesort using a binary tree 1 3 Analysis of mergesort using induction 2 4 The importance of O(n log n) 4 5 Returning to analysis of recurrence relation 4 6 Lower bound on comparison sorting 5 1 Tree definitions Depth of node Is 1 Internal nodes v Depth of node Is 1 Internal nodes v Subtree (consists of descendants of v) Depth of node Is 1 Internal nodes v Subtree (consists of descendants of v) Leaf Leaf Leaf Leaf Depth of tree is 3 Depth of tree is 3 Depth of node is 3 Depth of node is 3... Let s begin by reviewing some tree terminology. Here are some terms Leaf Leaf Subtree Depth of tree is 3 Depth of node is 3... ˆ node: an entity in a tree that contains a value or data ˆ edge: branches that connect one node to another ˆ : the topmost node in the tree ˆ leaves: nodes that do not have child nodes ˆ internal node: any node of a tree that has child nodes (any nodes that are not leaves) ˆ children: nodes that have parent nodes (all nodes are children except for the node) ˆ parents: nodes that have at least one edge to a child node

ˆ depth: depth of a node is the length of the path to its ˆ arity: maximum number of children (binary tree has arity two) 2 Analysis of mergesort using a binary tree Sometimes we consider the order of children significant. This is the case of binary trees, which come up all the time in computer science. A nonempty binary tree has a, and possibly a left subtree and possibly a right subtree. In lab you saw one use for binary trees. Here we use binary trees to help us analyze mergesort. As we went over briefly in last lecture, consider the tree representing the invocations resulting from an initial call to mergesort with a list of n numbers. The represents the initial call. That initial call makes two recursive calls, one with the first half of the list (represented by the left child) and one with the second half of the list (represented by the right child). The recursions continue similarly, until you get to calls for lists of size one or zero. At each depth d, for each item in the original input list, there is at most one invocation at that depth that handles that item. Therefore, for any depth d, the sum over the invocations of depth d of the number of items handled is at most n. In each invocation, the number of operations (aside from recursive invocations) is proportional to the number of items in the input list. Therefore, for each depth d, the total number of operations done by invocations at depth d is at most cn where c is a constant and n is the size of the original input. Therefore the total number of operations is at most cn times the depth of the tree. How large is the depth of the tree? Every time you go from parent to child, the number of items in the input list goes down by a factor of two (the number of items that you are dealing with is halved). This means that in order to find our maximum depth, we must ask How many times can you divide n by two before it gets down to 1 or less This is the same as asking the question: how many 2 s do you have to multiply together before the product is n or greater? That is, what is the smallest integer x such that 2 x n? The inverse of the function that maps x to 2 x is the function that maps y to log 2 y. x 0 1 1 2 2 4 3 8 4 16 2 x.. 20 1048576 y log 2 y 0 1 1 2 2 4 3 8 4 16.. 20 1048576 These functions also take real numbers, e.g. 2 3.5 = 11.313708... and log 2 100.5 = 6.65105... Starting with the equation 2 x = n, we take logs of both sides, and we get x = log 2 n. Now log 2 n is 2

not necessarily an integer, so the depth of the tree is log 2 n. This shows that the total number of operations is at most cn log 2 n. Therefore we say that mergesort is O(n log 2 n). 3 Analysis of mergesort using induction Now that we have an idea of what the running time should be, we can use a recurrence relation and induction to prove it. Let s use the following recurrence relation. It s a little sketchy but we ll come back to that. Let f(n) be the worst-case number of operations done by mergesort on a list of length n. Aside from the recursive calls, the number of operations done by mergesort is O(n). That is, there are positive constants n 0 and c such that, for n n 0, the number of operations is at most cn (again, not counting recursive calls.) f(n) a for n n 0 f(n) cn + f(n/2) + f(n/2) for n n 0 Note that f(n/2) occurs twice on the right-hand side because there are two recursive calls. That seems good. Anything wrong with this recurrence relation? What if n is an odd number? Then the recurrence relation is not quite right. So for now let s only state it for even n: f(n) a for n n 0 f(n) cn + f(n/2) + f(n/2) for even n > n 0 To simplify, set b = max{a, c}. That way, if n > 1 and n n 0, a bn, so we can state the recurrence as: f(n) a for n 1 f(n) cn + f(n/2) + f(n/2) for even n > 1 The fact tht we only state the recurrence for even n limits what we can prove using induction. We need all the list sizes in all recursive calls to be either even or 1. This means we can only prove something using this recurrence for values of n that are powers of 2, i.e. numbers of the form 2 k where k is a nonnegative integer. Claim: For n a power of two, f(n) an + b n log 2 n. 3

Proof: By induction on n. For n 1, f(n) a. In this case, the right-hand side of the claimed inequality is a, so the claim holds. For n 2, f(n) bn + 2f(n/2) by recurrence bn + 2 (a (n/2) + b (n/2) log 2 (n/2)) by inductive hypothesis bn + 2 (a (n/2) + b (n/2)(log 2 n 1)) because log 2 (n/2) = log 2 n 1 bn + a n + b n(log 2 n 1)by multiplying through bn + an + bn log 2 n bn an + bn log 2 n This completes the induction step and the proof. QED. What about sorting when the number n of items is not a power of two? Two approaches to this: ˆ Can show that the worst-case running time for input size n is no more than the worst-case running time for larger inputs, and for any positive integer n, there is a power of two 2 k n such that 2 k 2n. Therefore, the running time for for arbitrary n is no more than a (2n) + b (2n) log 2 (2n). ˆ One can get a better recurrence for mergesort, one that does not depend on n being even. Then can analyze that recurrence. I will show this in a little bit. 4 The importance of O(n log n) For some of the most important algorithmic problems, we don t have a good way to achieve lineartime algorithms but we do know of O(n log n) algorithms. That extra factor of log n is not negligible but it is not very big. When you go from an O(n 2 ) algorithm to an O(n log n) algorithm, it is tremendous. One example (which we will not do in this class but which you can study in Algorithms class (and sometimes in Coding the Matrix) is the Fast Fourier Transform (FFT). This is an O(n log n) algorithm for something that you would think would require O(n 2 ). This algorithm is core to the whole field of digital signal processing. It comes up in GPS. It comes up in image analysis. It even comes up in Bignum multiplication: it is used in an O(n log n) algorithm for multiplication of n-digit numbers. 5 Returning to analysis of recurrence relation Referring back to our code for mergesort, you can give a recurrence relation that works for odd numbers. The two recursive calls are: 4

ˆ (mergesort (take (quotient (length L) 2))) ˆ (mergesort (drop (quotient (length L) 2))) The first call is on a list of length n/2 and the second is on a list of length n n/2. Here x is the greatest integer less than or equal to x: 17.5 = 17 and 17 = 17. Note that n n/2 = n/2. So we get a recurrence relation f(n) a for n 1 f(n) bn + f( n/2 ) + f(n n/2 ) Claim: For any n 1, f(n) an + b n log 3/2 n. Proof: By induction on n. For n 1, f(n) a. In this case, the right-hand side of the claimed inequality is a, so the claim holds. For n 2, f(n) bn + f( n/2 ) + f(n n/2 ) by recurrence ( ) bn + a ( n/2 ) + b ( n/2 ) log 3/2 n/2 + ( ) a (n n/2 ) + b (n n/2 ) log 3/2 (n n/2 ) by inductive hypothesis For n = 2, n/2 = n/2. For n 3, n/2 (n 1)/2 1 3 n. Therefore n n/2 2 3n. Therefore 2 log 3/2 (n n/2 ) log 3/2 3 n log 3/2 n 1. Of course, n/2 n/2 2 3 n so also log 3/2 n/2 log 3/2 n 1. Using these, and continuing with the sequence of inequalities, we get ( ) ( ) f(n) bn + a ( n/2 ) + b ( n/2 ) log 3/2 n/2 + a (n n/2 ) + b (n n/2 ) log 3/2 (n n/2 ) ( ) ( ) bn + a ( n/2 ) + b ( n/2 )(log 3/2 n 1) + a (n n/2 ) + b (n n/2 )(log 3/2 n 1) bn + a ( n/2 + n n/2 ) + b ( n/2 + n n/2 )(log 3/2 n 1) bn + a n + b n(log 3/2 n 1) bn + an + bn log 3/2 n bn an + bn log 3/2 n This completes the induction step and the proof. QED. 6 Lower bound on comparison sorting How fast can we expect to sort things? To answer that question, we will think about an algorithm in a different way. We use the idea of ed trees. Rooted trees come up all the time in computer science, so you need to learn about them. We will use them in three completely different ways in the coming lectures. 5

Idea: Consider a given value of n. For the purpose of analysis, we represent the algorithm by a decision tree. This is a binary ed tree (a tree where every node has at most two children). Each internal node represents a comparison between two input items. Each represents a conclusion the algorithm makes about the order of the input items. I ll give an example of a decision tree. Suppose a sorting algorithm is given the list with numbers a, b, c. At the end, the algorithm has to produce an ordered list. To do so, it must discover the ordering among the input items. It discovers that ordering by comparing pairs of items. a < b? b < c? a < c? a < b < c a < c? b < a < c b < c? a < c < b c < a < b b < c < a c < b < a Try out this algorithm. Let s say a = 25, b = 15, c = 7. For any given input, the algorithm must start at the and make its way to a. Every internal node represents a comparison that the algorithm makes. Because each comparison takes at least one operation, the number of operations on a given input is the number of internal nodes on a -to- path. For any given, the number of comparisons is then the depth of that. The worst-case number of comparisons is the maximum depth. For every value of n, you need a separate decision tree. The number of leaves is the number of orderings of n numbers, which is n! (n factorial). For any algorithm that sorts items by comparing them, for every value of n, there is a decision tree that mimics the algorithm s behavior on inputs of size n. Here s a fact about binary trees: Lemma Every binary tree of depth d has at most 2 d leaves. Proof By induction on d. Base case: Any binary tree of depth 0 has only one. Induction Step: Assume it for d = k 1, and prove it for d = k. Induction hypothesis: Assume that every binary tree of depth k 1 has at most 2 k 1 leaves. Let T be any tree of depth k where k > 0. It consists of a and some internal nodes... 6

The descendants of the left child form a left subtree and the descendants of the right child form a right subtree. The left subtree is a ed tree of depth k 1 so it has at most 2 k 1 leaves. The right subtree is a ed tree of depth k 1 so it has at most 2 k 1 leaves. The total number of leaves is just the number of leaves in the two subtrees, which is at most 2 k 1 + 2 k 1, which is 2 2 k 1, which is 2 k. This proves the induction step, completing the proof. QED For example, a binary tree with 6 leaves cannot have depth 2 because 2 2 is less than 6. It must have depth at least three (and our decision tree for sorting three numbers has depth three). Suppose we want to sort 4 numbers. There are 4 3 2 1 different outcomes, so any decision tree for this has this many leaves. It s 24, so any decision tree with this many leaves has depth at least 5 because 2 4 < 24. In general, for any positive integer n, when sorting n numbers, there are n! possible results, so a decision tree for sorting n numbers has n! leaves. This can tell us how big the depth must be. Let the depth be d. Then we must have 2 d n!. Stirling s Approximation tells us that n! is at least How do we find out how large d must be? (n/e) n The function log 2 y is monotonically increasing (higher y means higher log 2 y). That means we can start with an inequality and take logs of both sides, and we get another inequality. We have Take logs of both sides: which is 2 d n! (n/e) n log 2 (2 d ) log 2 ((n/e) n ) d n log 2 (n/e) = n(log 2 n log 2 e) = n log 2 n n log 2 e This shows that the depth must be at least n log 2 n n log 2 e, which is at least 1 2 n log 2 n for n > 7. 7

This shows that any sorting algorithm that makes order decisions based on comparing pairs of numbers has a running time that is Ω(n log 2 n). After lecture, a student in class, David Gao, pointed out that you don t really need Stirling s approximation to prove that log 2 n! is Ω(n log n). Here s an outline: For n 6, log 2 n! = log 2 (n (n 1) (n 2) ( n/2 ) ( n/2 1) 3 2 1) = log 2 n + log 2 (n 1) + log 2 (n 2) + + log( n/2 ) + log( n/2 1) + + log 3 + log 2 + log 1 log 2 n + log 2 (n 1) + log 2 (n 2) + + log( n/2 ) log( n/2 ) }{{} (n n/2 +1times (n/2) log( 1 3 n) (n/2)(log n log 3) (n/2)(log n 1.585) (n/2) 2 3 log n 1 3 n log n which shows that log n! is Ω(n log n). Can this running time be beaten? Is there a linear-time sorting algorithm? Remember, we assumed that the only way the algorithm knows about the numbers is by comparing them in pairs. There is an algorithm that beats this running time by doing things differently. Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CS 17 document by filling out the anonymous feedback form: http://cs.brown.edu/ courses/csci0170/feedback. 8