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

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

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

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

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

Growth of Functions (CLRS 2.3,3)

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

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

data structures and algorithms lecture 2

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Asymptotic Analysis and Recurrences

CS Non-recursive and Recursive Algorithm Analysis

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

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

3.1 Asymptotic notation

Analysis of Algorithms

CS473 - Algorithms I

Computational Complexity

EECS 477: Introduction to algorithms. Lecture 5

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

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

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

Methods for solving recurrences

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

Cpt S 223. School of EECS, WSU

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan

The Time Complexity of an Algorithm

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

Asymptotic Analysis 1

CS Data Structures and Algorithm Analysis

An analogy from Calculus: limits

Module 1: Analyzing the Efficiency of Algorithms

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

Data Structures and Algorithms CSE 465

Analysis of Algorithms

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

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

CSC236 Week 4. Larry Zhang

Lecture 3. Big-O notation, more recurrences!!

The Time Complexity of an Algorithm

Lecture 3: Big-O and Big-Θ

CS 4407 Algorithms Lecture 2: Growth Functions

COMP 9024, Class notes, 11s2, Class 1

Asymptotic Analysis. Thomas A. Anastasio. January 7, 2004

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

1 Substitution method

Data Structures and Algorithms. Asymptotic notation

Asymptotic Algorithm Analysis & Sorting

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

Notes for Recitation 14

Module 1: Analyzing the Efficiency of Algorithms

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

Written Homework #1: Analysis of Algorithms

CS F-01 Algorithm Analysis 1

Agenda. We ve discussed

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

Divide and Conquer. Recurrence Relations

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

Big O (Asymptotic Upper Bound)

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

CS 380 ALGORITHM DESIGN AND ANALYSIS

Omega notation. Transitivity etc.

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

Design and Analysis of Algorithms

CS 310 Advanced Data Structures and Algorithms

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

Lecture 1: Asymptotic Complexity. 1 These slides include material originally prepared by Dr.Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole.

Divide and Conquer. Arash Rafiey. 27 October, 2016

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

2.2 Asymptotic Order of Growth. definitions and notation (2.2) examples (2.4) properties (2.2)

Running Time Evaluation

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

Analysis of Algorithms

Asymptotic Analysis. Slides by Carl Kingsford. Jan. 27, AD Chapter 2

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

COMP 382: Reasoning about algorithms

Lecture 2: Asymptotic Notation CSCI Algorithms I

Introduction to Algorithms: Asymptotic Notation

Analysis of Algorithms - Using Asymptotic Bounds -

Divide and Conquer CPE 349. Theresa Migler-VonDollen

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

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

Review Of Topics. Review: Induction

COMPUTER ALGORITHMS. Athasit Surarerks.

CS173 Running Time and Big-O. Tandy Warnow

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

Asymptotic Analysis Cont'd

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Divide and Conquer. Andreas Klappenecker

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

Divide-and-Conquer Algorithms Part Two

Algorithms Design & Analysis. Analysis of Algorithm

CS 2110: INDUCTION DISCUSSION TOPICS

Introduction to Algorithms

CSC Design and Analysis of Algorithms. Lecture 1

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

Data Structures and Algorithms

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Transcription:

CS 124 Section #1 Big-Oh, the Master Theorem, and MergeSort 1/29/2018 1 Big-Oh Notation 1.1 Definition Big-Oh notation is a way to describe the rate of growth of functions. In CS, we use it to describe properties of algorithms (number of steps to compute or amount of memory required) as the size of the inputs to the algorithm increase. The idea is that we want something that is impervious to constant factors; for example, if your new laptop is twice as fast as your old one, you don t want to have to redo all your analysis. f(n) is O(g(n)) if there exist c, N such that f(n) c g(n) for all n N. f g f(n) is o(g(n)) if lim n f(n)/g(n) = 0. f < g f(n) is Θ(g(n)) if f(n) is O(g(n)) and g(n) is O(f(n)). f = g f(n) is Ω(g(n)) if there exist c, N such that f(n) c g(n) for all n N. f g f(n) is ω(g(n)) if lim n g(n)/f(n) = 0. f > g Note that in the definition above, all we care about is the long-term behavior of f versus g. That is why in the capital letters O and Ω, we have n N, and in the lowercase letters o and ω, we have are taking the limit as n. Keep in mind that o O and ω Ω. 1.2 Notes on notation When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation: n O(1) means n f(n) for some function f(n) such that f(n) = O(1). n 2 + Ω(n) means n 2 + g(n) for some function g(n) such that g(n) = Ω(n). 2 (1 o(1))n means 2 (1 ɛ(n)) g(n) for some function ɛ(n) such that ɛ(n) 0 as n. When we use asymptotic notation on both sides of an equation, it means that for all choices of the unspecified functions in the left-hand side, we get a valid asymptotic relation: n 2 /2 + O(n) = Ω(n 2 ) because for all f such that f(n) = O(n), we have n 2 /2 + f(n) = Ω(n 2 ). But it is not true that Ω(n 2 ) = n 2 /2 + O(n) (e.g. n 2 n 2 /2 + O(n)). The above is quite subtle, but it is important to think of the equal sign as more of a belongs to the family of functions described by the right hand side rather than a strict equality. 1

Exercise 1. Using Big-Oh notation, describe the rate of growth of each of the following: n 124 vs. 1.24 n 124 n vs. (log n) 124 n log n vs. n log n n vs. 2 log n n vs. n sin n Exercise 2. Which of the following hold? f(n) = ω(2 n ) implies that f(n) = Ω(n 2 ) If g(n) = o(f(n)), then f(n) + g(n) = Θ(f(n)) (You can assume f and g are positive functions) Exercise 3. Give a counterexample to the following statement. Then propose an easy fix. If f(n) and g(n) are positive functions, then f(n) + g(n) = Θ(min(f(n), g(n)) 2

Exercise 4. (Challenge) Let f(n) = 1 k + 2 k +... + n k for some constant k N; find a simple function g : N N such that f(n) = Θ(g(n)). Find a constant c such that f(n) cg(n) 1 as n. 2 Recurrence relations 2.1 General guidelines There is no single best method for solving recurrences. There s a lot of guesswork and intuition involved. That being said, here are some tips that will help you out most of the time: Guess! You can get surprisingly far by comparing recurrences to other recurrences you ve seen, and by following your gut. Often quadratic, exponential, and logarithmic recurrences are easy to eyeball. Graph it. Try plugging in some values and graphing the result, alongside some familiar functions. This will often tell you the form of the solution. Substitute. If a recurrence looks particularly tricky, try substituting various parts of the expression until it begins to look familiar. Solve for constants. Using the previous methods, you can often determine the form of the solution, but not a specific function. However, there is often enough information in the recurrence to solve for the remainder of the information. For instance, let s say a recurrence looked quadratic. By substituting T (n) = an 2 + bn + c for the recurrence and solving for a, b, and c, you ll likely have enough information to write the exact function. Exercise 5. Solve the following recurrence relations, assuming that T (n) = 1 for n 1. T (n) = T (n/3) + 2 T (n) = 3T (n/4) T (n) = T (n 1) + n 2 3

2.2 The Master Theorem The previous section stressed methods for finding the exact form of a recurrence, but usually when analyzing algorithms, we care more about the asymptotic form and aren t too picky about being exact. For example, if we have a recurrence T (n) = n 2 log n + 4n + 3 n + 2, what matters most is that T (n) is Θ(n 2 log n). In cases like these, if you re only interested in proving Θ bounds on a recurrence, the Master Theorem is very useful. The solution to the recurrence relation T (n) = at (n/b) + cn k where a 1, b 2 are integers, and c and k are positive constants, satisfies Θ(n log b a ) if a > b k T (n) is Θ(n k log n) if a = b k Θ(n k ) if a < b k In general, the idea is that the relationship between a and b k determines which term of the recurrence dominates; you can see this if you expand out a few layers of the recurrence. For more information about the proof, see the supplement posted with the section notes. Exercise 6. Use the Master Theorem to solve T (n) = 4T (n/9) + 7 n. Exercise 7. How might you remember the above cases if you forgot them during an exam? Exercise 8. For each of the following algorithms, come up with a recurrence relationship in the form of the Master Theorem and solve it. Verify that your answer makes sense. Binary Search Finding the maximum element of a list by recursively finding the maximum element of the left and right halves and then taking the max of those two. 4

3 MergeSort MergeSort 1 is a sorting algorithm that runs in O(n log n) time and may be helpful on this week s problem set. The input is a list of n numbers, and the output is a list of the given numbers sorted in increasing order. The main data structure used by the algorithm will be a queue. We will assume that each queue operation takes 1 step, and that each comparison (is x > y?) takes 1 step. The procedure MergeSort relies on a function merge which takes as input two sorted (in increasing order) lists of numbers and outputs a single sorted list containing all the given numbers (with repetition). merge (s, t) Require: s, t are both sorted lists 1: if s = [ ] then 2: return t 3: else if t = [ ] then 4: return s 5: else if s(1) t(1) then 6: u pop(s) 7: else 8: u pop(t) 9: end if 10: return push(u, merge(s, t)) MergeSort (s) Require: s is a list 1: list s, q 2: q [ ] 3: for x in s do 4: inject(q, [x]) 5: end for 6: while size(q) 2 do 7: u pop(q) 8: v pop(q) 9: inject(q, merge(u, v)) 10: end while 11: if q = [ ] then 12: return [ ] 13: else 14: return q(1) 15: end if The correctness of merge follows from the following fact: the smallest number in the input is either s(1) or t(1), and must be the first number in the output list. The rest of the output list is just the list obtained by merging s and t after deleting that smallest number. 1 Adapted from the CS 124 lecture notes of Prof. Michael Mitzenmacher. 5

The number of steps for each invocation of merge is O(1) steps. Since each recursive invocation of merge removes an element from either s or t, it follows that merge halts in O( s + t ) steps. The iterative algorithm MergeSort (above) uses q as a queue of lists. (Note that it is perfectly acceptable to have lists of lists!) It repeatedly merges together the two lists at the front of the queue, and puts the resulting list at the tail of the queue. The correctness of the algorithm follows from the fact that we start with sorted lists (of length 1 each), and merge them in pairs to get longer and longer sorted lists, until only one list remains. Exercise 9. Perform the above version of MergeSort on the array [3, 2, 4, 1, 5]. An alternative analysis of MergeSort depends on a recursive, rather than iterative, description. (This is probably the version you re more familiar with.) Suppose we have an operation split that takes a list and splits it into two equal-size parts. (We will assume our list size is a power of 2, so that all sublists we ever obtain have even size or are of length 1.) Then a recursive version of MergeSort would do the following: MergeSort (s) Require: s is a list 1: list s, s 1, s 2 2: if size(s) = 1 then 3: return s 4: end if 5: s 1 MergeSort(s 1 ) 6: s 2 MergeSort(s 2 ) 7: return merge(s 1, s 2 ) Exercise 10. Come up with a recurrence relationship for the recursive variation of MergeSort in the form of the Master Theorem and solve it. 6