CIS 121 Data Structures and Algorithms with Java Spring Big-Oh Notation Monday, January 22/Tuesday, January 23

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

Growth of Functions (CLRS 2.3,3)

CS 4407 Algorithms Lecture 2: Growth Functions

CS473 - Algorithms I

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

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

Time complexity analysis

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

Principles of Algorithm Analysis

Data Structures and Algorithms Running time and growth functions January 18, 2018

COMP 9024, Class notes, 11s2, Class 1

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

EECS 477: Introduction to algorithms. Lecture 5

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

Computational Complexity

Big O (Asymptotic Upper Bound)

CS 380 ALGORITHM DESIGN AND ANALYSIS

Data Structures and Algorithms CSE 465

Data Structures and Algorithms. Asymptotic notation

The Time Complexity of an Algorithm

The Time Complexity of an Algorithm

Analysis of Algorithms

with the size of the input in the limit, as the size of the misused.

Written Homework #1: Analysis of Algorithms

Cpt S 223. School of EECS, WSU

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

Big-O Notation and Complexity Analysis

Review Of Topics. Review: Induction

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

Asymptotic Analysis 1

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Analysis of Algorithms

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

Data Structures and Algorithms Chapter 2

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

Defining Efficiency. 2: Analysis. Efficiency. Measuring efficiency. CSE 421: Intro Algorithms. Summer 2007 Larry Ruzzo

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

Analysis of Algorithms [Reading: CLRS 2.2, 3] Laura Toma, csci2200, Bowdoin College

data structures and algorithms lecture 2

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

Algorithms and Their Complexity

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

COMP Analysis of Algorithms & Data Structures

3.1 Asymptotic notation

CS 161 Summer 2009 Homework #2 Sample Solutions

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text.

Asymptotic Analysis Cont'd

P, NP, NP-Complete, and NPhard

Introduction to Algorithms: Asymptotic Notation

COMP Analysis of Algorithms & Data Structures

Lecture 2: Asymptotic Notation CSCI Algorithms I

CSE 421: Intro Algorithms. 2: Analysis. Winter 2012 Larry Ruzzo

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

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

Algorithms, CSE, OSU. Introduction, complexity of algorithms, asymptotic growth of functions. Instructor: Anastasios Sidiropoulos

Algorithms Design & Analysis. Analysis of Algorithm

Asymptotic Analysis of Algorithms. Chapter 4

Analysis of Algorithms - Using Asymptotic Bounds -

Define Efficiency. 2: Analysis. Efficiency. Measuring efficiency. CSE 417: Algorithms and Computational Complexity. Winter 2007 Larry Ruzzo

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.

2. ALGORITHM ANALYSIS

CSC Design and Analysis of Algorithms. Lecture 1

Agenda. We ve discussed

Ch01. Analysis of Algorithms

The Growth of Functions and Big-O Notation

Divide and Conquer CPE 349. Theresa Migler-VonDollen

CS 310 Advanced Data Structures and Algorithms

Asymptotic Algorithm Analysis & Sorting

An analogy from Calculus: limits

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

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

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

Running Time Evaluation

Analysis of Algorithms

csci 210: Data Structures Program Analysis

Foundations II: Data Structures and Algorithms

CSE 417: Algorithms and Computational Complexity

Introduction to Algorithms and Asymptotic analysis

csci 210: Data Structures Program Analysis

CS Non-recursive and Recursive Algorithm Analysis

Introduction to Algorithms

CISC 235: Topic 1. Complexity of Iterative Algorithms

This chapter covers asymptotic analysis of function growth and big-o notation.

Md Momin Al Aziz. Analysis of Algorithms. Asymptotic Notations 3 COMP Computer Science University of Manitoba

Analysis of Algorithms

Bucket-Sort. Have seen lower bound of Ω(nlog n) for comparisonbased. Some cheating algorithms achieve O(n), given certain assumptions re input

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

Math 391: Midterm 1.0 Spring 2016

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

Divide and Conquer Problem Solving Method

Electrical & Computer Engineering University of Waterloo Canada February 26, 2007

CSE332: Data Abstrac0ons Sec%on 2. HyeIn Kim Spring 2014

Problem Set 1 Solutions

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

Advanced Algorithmics (6EAP)

What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asympt

Recursion. Computational complexity

CSED233: Data Structures (2017F) Lecture4: Analysis of Algorithms

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

Transcription:

CIS 11 Data Structures and Algorithms with Java Spring 018 Big-Oh Notation Monday, January /Tuesday, January 3 Learning Goals Review Big-Oh and learn big/small omega/theta notations Discuss running time analysis of algorithms What are Algorithms? What is an algorithm? An algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output. Therefore, an algorithm is a sequence of computational steps that transform the input into output. We can also view an algorithm as a tool for solving a well-specified computational problem. The statement of the problem specifies the desired input/output relationship, and the algorithm describes a specific computational procedure for achieving that relationship. Best, Average, Worst Case Run Time Analysis When analyzing algorithms, we are often interested in analyzing the best, average, and worst cases of running time. Typically, best case performance is not really of concern due to triviality. Algorithms may be modified to make best case performance trivial for small input by hardcoding. In these cases, the best case performance is effectively meaningless! Often, it is of more concern to perform worst case analysis, i.e. identifying the inputs that cause the algorithm to have the longest running time (or use the most space) and identifying the running time and usage bounds. Why is this useful? The worst case running time of an algorithm gives an upper bound on the running time for any input. Knowing this provides a guarantee that the algorithm never takes any longer. For some algorithms, the worst case may occur fairly often. Often, the average case is roughly as bad as the worst case. Finally, in some cases we may be interested in the average case running time of an algorithm, where we would use probabilistic analysis to examine particular algorithms. This doesn t surface too much, as what constitutes an average input is often not apparent. Often, we would then assume that all inputs of a particular size are equally likely (uniform distribution). This assumption is often violated in practice, so we might modify an algorithm to be randomized, to enable a probabilistic analysis and expected running time. Big-Oh Definitions Presented below is a brief overview of asymptotic notation that will be fundamental in this course. Big-Oh Notation 1

Definition (1). O(g(n)) if there exist positive constants n 0 and c such that f(i) cg(i) for all i n 0. Definition (). O(g(n)) if lim n g(n) = 0 or a constant. Simplified: If is in O(g(n)), g(n) is an asymptotic upper bound for. Little-o Notation Definition (1). o(g(n)) if for any positive constant c, there exists a positive constant n 0 such that f(i) < cg(i) for all i n 0. Definition (). o(g(n)) if lim n g(n) = 0. Note: o(g(n)) O(g(n)) Big-Omega Notation Definition (1). Ω(g(n)) if there exist positive constants n 0 and c such that f(i) cg(i) for all i n 0. Definition (). O(g(n)) if lim n g(n) = or a constant. Simplified: If is Ω(g(n)), g(n) is an asymptotic lower bound for. Little-ω Notation Definition (1). ω(g(n)) if for any positive constant c, there exists a positive constant n 0 such that f(i) > cg(i) for all i n 0. Definition (). ω(g(n)) if lim n g(n) =. Note: ω(g(n)) Ω(g(n)) Big-Theta Notation Definition (1). Θ(g(n)) if and only if O(g(n)) and Ω(g(n)). Definition (). Θ(g(n)) if lim n g(n) = a nonzero constant. Simplified: If is Θ(g(n)), g(n) is an asymptotically tight bound for. The notations refer to classes of functions. When you read = O(g(n)), this is equivalent to the statement: O(g(n)). Specifically, is in the class of functions which are asymptotically bounded above by g(n). Likewise, Big-Ω, Big-Θ, etc., all reflect classes of functions. Problems (Big-Oh) Problem 1a Prove that 3n + 100n = Θ(5n )

Solution. 3n + 100n 3n + 100 lim n 5n = lim n 5n = lim n = 3/5 3n 5n 3n + 100n = Θ(5n ) Problem 1b Prove that n log n = Ω(n) Solution. We will prove this by induction. Base Case: n = 4. 4 log 4 = 8 > 4, so this holds. Induction Hypothesis: Assume that k log k k for some k 4. Induction Step: We need to show that (k + 1) log(k + 1) k + 1 Log is monotonically increasing so log(k + 1) > logk (k + 1) log(k + 1) > (k + 1) log k = k log k + log k > k log k + 1 > k + 1 Problem You are given the following algorithm for Bubble-Sort: Algorithm 1 Bubble Sort function Bubble-Sort(A, n) for i 0 to n do for j 0 to n i do if A[j] > A[j + 1] then swap(a[j], A[j + 1]) end if end for end for end function Given some sequence a 1, a,..., a n in A, we say an inversion has occurred if a j < a i for some i < j. At each iteration, Bubble-Sort checks the array A for an inversion and performs a swap if it finds one. How many swaps does Bubble-Sort perform in the worst-case and in the average-case? 3

Solution. It should be fairly obvious that the worst-case scenario for bubble-sort occurs when A contains elements in reverse-sorted order. Let I denote the number of inversions. In this situation, the total number of inversions is the exact number of possible pairs of elements, as each swap removes exactly one inversion: ( ) n I worst = In the average-case scenario, we determine the expected number of inversions in a random array of n elements. Specifically, we consider a random permutation of n distinct elements, a 1, a,..., a n. Let X ij denote an indicator R.V. such that, { 1 if a i, a j inverted X ij = 0 otherwise Thus, we have I average = E[I] = i,j : i<j i,j : i<j = i,j : i<j ( n ) X ij = n(n 1) = 4 E[X ij ] Pr[X ij = 1] In the above, we let Pr[X ij = 1] = 1 because in a random permutation (uniform probability), the probability of a i < a j is the same as a i > a j. Problem 3 Prove or disprove the following statement: lg(n!) is Θ(n lg n). Solution. We first show lg(n!) is O(n lg n). Picking c = 1 and n 0 = 1, we have lg(n!) = n lg i n lg n i=1 This is clearly true for all n > n 0. Therefore, we are done. We then show that lg(n!) is Ω(n lg n). Our strategy is to find an easier to work with lower-bound for lg n! that is larger than some cn lg n. lg n! = lg 1 + lg + + lg n lg n + lg(n + 1) + + lg n delete the first half of the terms n lg n replace remaining terms by smallest one 4

Choosing c = 1 4 and N = 4, it is clear that n lg n n 4 lg n with some algebraic manipulation: Therefore, lg(n!) is Ω(n lg n). Problem 4 n lg n n 4 lg n n lg n n n 4 lg n n lg n n lg n Prove that a function (which is eventually positive for all n n p ) is polynomially bounded iff log() O(log n). In other words: where k is some constant. Solution. O(n k ) log() O(log n) ( ) Suppose O(n k ) for some k. Then c, n 0 such that c n k for all n n 0. Since is eventually positive for all n n p, consider all n max(n 0, n p ). Take the log (a monotonically increasing function for positive values) of both sides, and we get that: log() log(c n k ) log(c) + log(n k ) log(c) + k log(n) O(log n) ( ) Suppose log() O(log n). Note here that logarithm, no matter which base, is all in O(log n). WLOG, we can assume here that the base is e, and these are natural logs. Then c, n 0 such that ln() c ln n for all n n 0. Exponentiating both sides (a monotonically increasing function), we get that: which is in O(n k ) for k c. Problem 5 Prove or disprove the following: 1. lg n! is polynomially bounded.. lg lg n! is polynomially bounded. e ln() e c ln n e ln(nc ) n c 5

Solution. You can use the results from problem 3 and 4 to solve this question. 1. Let g(n) = lg n!, and let y = lg n. Then lg g(n) = lg y! by the result from problem 3. Substituting y back in, Θ(y lg y) (y lg y) = ( lg n )(lg lg n ) / O(lg n), since lg lg n will eventually exceed any constant c. (So c ( lg n )(lg lg n ) c lg n.) Also if you calculate the limit ( lg n )(lg lg n ) lim = lim lg lg n =, n lg n n it shows that ( lg n )(lg lg n ) = ω(lg n). So clearly ( lg n )(lg lg n ) / O(lg n). By the if and only if statement from problem 4, since lg g(n) / O(lg n), we can also say that g(n) is not polynomially bounded.. Let h(n) = lg lg n!, and let y = lg lg n. Similar to part 1, Substituting y back in, Or if you calculate the limit lg h(n) = lg y! Θ(y lg y), (y lg y) = ( lg lg n )(lg lg lg n ) = O( lg lg n lg lg n ) = O((lg lg n) ) = O( lg lg n ) = O(lg n) ( lg lg n )(lg lg lg n ) lim = 0. n lg n This shows that ( lg lg n )(log lg lg n ) = o(lg n) ( lg lg n )(lg lg lg n ) O(lg n). By the if and only if statement from problem 4, since lg h(n) O(lg n), we can also say that h(n) is polynomially bounded. 6