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

Similar documents
Lecture 1: Asymptotics, Recurrences, Elementary Sorting

3.1 Asymptotic notation

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

Growth of Functions (CLRS 2.3,3)

The Time Complexity of an Algorithm

Lecture 2: Asymptotic Notation CSCI Algorithms I

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

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

The Time Complexity of an Algorithm

Big O (Asymptotic Upper Bound)

COMP 9024, Class notes, 11s2, Class 1

CS473 - Algorithms I

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

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

Computational Complexity

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Algorithms Design & Analysis. Analysis of Algorithm

Analysis of Algorithms

Data Structures and Algorithms. Asymptotic notation

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

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

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

CS 380 ALGORITHM DESIGN AND ANALYSIS

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

Running Time Evaluation

Analysis of Algorithms

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

Algorithms and Their Complexity

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

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

CS 344 Design and Analysis of Algorithms. Tarek El-Gaaly Course website:

Cpt S 223. School of EECS, WSU

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

CS 4407 Algorithms Lecture 2: Growth Functions

Big-O Notation and Complexity Analysis

Analysis of Algorithms

Asymptotic Analysis 1

Data Structures and Algorithms CSE 465

Data Structures and Algorithms

Problem Set 1 Solutions

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

CSC Design and Analysis of Algorithms. Lecture 1

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Introduction to Algorithms: Asymptotic Notation

EECS 477: Introduction to algorithms. Lecture 5

Runtime Complexity. CS 331: Data Structures and Algorithms

data structures and algorithms lecture 2

CS Non-recursive and Recursive Algorithm Analysis

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

Analysis of Algorithms

COMP 382: Reasoning about algorithms

Time complexity analysis

Written Homework #1: Analysis of Algorithms

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

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

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

On my honor I affirm that I have neither given nor received inappropriate aid in the completion of this exercise.

CSE 417: Algorithms and Computational Complexity

CSC236 Week 4. Larry Zhang

Ch01. Analysis of Algorithms

Module 1: Analyzing the Efficiency of Algorithms

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

An analogy from Calculus: limits

Lecture 5: Loop Invariants and Insertion-sort

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

The Growth of Functions and Big-O Notation

Introduction to Computer Science Lecture 5: Algorithms

Data Structures and Algorithms Chapter 2

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

Mergesort and Recurrences (CLRS 2.3, 4.4)

Principles of Algorithm Analysis

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

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

Algorithm. Executing the Max algorithm. Algorithm and Growth of Functions Benchaporn Jantarakongkul. (algorithm) ก ก. : ก {a i }=a 1,,a n a i N,

Asymptotic Analysis of Algorithms. Chapter 4

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

Lecture 10: Big-Oh. Doina Precup With many thanks to Prakash Panagaden and Mathieu Blanchette. January 27, 2014

Asymptotic Algorithm Analysis & Sorting

CS 161 Summer 2009 Homework #2 Sample Solutions

P, NP, NP-Complete, and NPhard

2. ALGORITHM ANALYSIS

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

Divide and Conquer CPE 349. Theresa Migler-VonDollen

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

Omega notation. Transitivity etc.

csci 210: Data Structures Program Analysis

Analysis of Algorithms - Using Asymptotic Bounds -

COMPUTER ALGORITHMS. Athasit Surarerks.

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

Asymptotic Analysis Cont'd

Topic 17. Analysis of Algorithms

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

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 1: Introduction

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

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

csci 210: Data Structures Program Analysis

Transcription:

/4/3 Administrative Big O David Kauchak cs3 Spring 3 l Assignment : how d it go? l Assignment : out soon l CLRS code? l Videos Insertion-sort Insertion-sort Does it terminate?

/4/3 Insertion-sort Loop invariant Loop invariant: A statement about a loop that is true before the loop begins and after each iteration of the loop. Upon termination of the loop, the invariant should help you show something useful about the algorithm. Loop invariant? Is it correct? Can you prove it? Loop invariant Loop invariant: A statement about a loop that is true before the loop begins and after each iteration of the loop. At the start of each iteration of the for loop of lines -7 the subarray A[..j ] is the sorted version of the original elements of A[..j ] Loop invariant At the start of each iteration of the for loop of lines -7 the subarray A[..j ] is the sorted version of the original elements of A[..j ] Proof by induction - Base case: invariant is true before loop - Inductive case: it is true after each iteration Proof?

/4/3 Insertion-sort How long will it take to run? Asymptotic notation l How do you answer the question: what is the running time of algorithm x? l We need a way to talk about the computational cost of an algorithm that focuses on the essential parts and ignores irrelevant details l You ve seen some of this already: l l linear n log n l n Asymptotic notation Precisely calculating the actual steps is tedious and not generally useful Different operations take different amounts of time. Even from run to run, things such as caching, etc. cause variations We want to identify categories of algorithmic runtimes For example f n takes n steps f n takes n + steps f 3 n takes 3n+ steps Which algorithm is better? Is the difference between f and f 3 important/ significant? 3

/4/3 Runtime examples Big O: Upper bound Ogn is the set of functions: # Ogn f n : such that ' & f n cgn for all n n Big O: Upper bound Ogn is the set of functions: # Ogn f n : such that ' & f n cgn for all n n Big O: Upper bound Ogn is the set of functions: # Ogn f n : such that ' & f n cgn for all n n We can bound the function fn above by some constant factor of gn We can bound the function fn above by some constant multiplied by gn For some increasing range 4

/4/3 Big O: Upper bound Ogn is the set of functions: # Ogn f n : such that ' & f n cgn for all n n Big O: Upper bound Ogn is the set of functions: # Ogn f n : such that ' & f n cgn for all n n O n 3 4 3n / n + n + 5n + 4 6n Generally, we re most interested in big O notation since it is an upper bound on the running time Omega: Lower bound Ωgn is the set of functions: & Ωgn f n : such that & '& cgn f n for all n n Omega: Lower bound Ωgn is the set of functions: & Ωgn f n : such that & '& cgn f n for all n n We can bound the function fn below by some constant factor of gn 5

/4/3 Omega: Lower bound Ωgn is the set of functions: & Ωgn f n : such that & '& cgn f n for all n n Theta: Upper and lower bound Θgn is the set of functions: & there exists positive constants c Θgn f n :, c and n such that & '& c gn f n c gn for all n n Ω n 3 4 3n / n 6n + n + 5n + 4 3 Theta: Upper and lower bound Θgn is the set of functions: & there exists positive constants c Θgn f n :,c and n such that & '& c gn f n c gn for all n n Theta: Upper and lower bound Θgn is the set of functions: & there exists positive constants c Θgn f n :, c and n such that & '& c gn f n c gn for all n n We can bound the function fn above and below by some constant factor of gn though different constants Note: A function is theta bounded iff it is big O bounded and Omega bounded 6

/4/3 Theta: Upper and lower bound Visually Θgn is the set of functions: & there exists positive constants c Θgn f n :,c and n such that & '& c gn f n c gn for all n n Θ n 3 4 3n / n + n + 5n + 4 3n + nlog n fn Visually: upper bound Visually: lower bound fn fn n n 7

/4/3 worst-case vs. best-case vs. average-case worst-case: what is the worst the running time of the algorithm can be? best-case: what is the best the running time of the algorithm can be? average-case: given random data, what is the running time of the algorithm? Don t confuse this with O, Ω and Θ. The cases above are situations, asymptotic notation is about bounding particular situations Proving bounds: find constants that satisfy inequalities Show that 5n 5n + is Θn Step : Prove On Find constants c and n such that 5n 5n + cn for all n > n cn 5n 5n + c 5 5 / n + / n Let n and c 5 + 5. /n only get smaller as n increases and we ignore -5/n since it only varies between -5 and Proving bounds Step : Prove Ωn Find constants c and n such that 5n 5n + cn for all n > n cn 5n 5n + c 5 5 / n + / n Bounds Is 5n On? No How would we prove it? # Ogn f n : such that ' & f n cgn for all n n Let n 4 and c 5 5/4.5 or anything less than.5. 5/n is always decreasing and we ignore /n since it is always between and. 8

/4/3 Disproving bounds Some rules of thumb Is 5n On? # Ogn f n : & Assume it s true. such that ' f n cgn for all n n Multiplicative constants can be omitted l 4n becomes n l 7 log n become log n Lower order functions can be omitted l n + 5 becomes n l n + n becomes n That means there exists some c and n such that 5n cn for n > n 5n c contradiction! n a dominates n b if a > b l n dominates n, so n +n becomes n l n.5 dominates n.4 Some rules of thumb a n dominates b n if a > b l 3 n dominates n Any exponential dominates any polynomial l 3 n dominates n 5 l n dominates n c Any polynomial dominates any logorithm l n dominates log n or log log n l n dominates n log n l n / dominates log n Do not omit lower order terms of different variables n + m does not become n Big O n + n log n + 5 n -5n + n 3 log n n log n + n + 5n 3 n 5 + n! + n n 9

/4/3 Some examples l O constant. Fixed amount of work, regardless of the input size l add two 3 bit numbers l determine if a number is even or odd l sum the first elements of an array l delete an element from a doubly linked list l Olog n logarithmic. At each iteration, discards some portion of the input i.e. half l binary search Some examples l On linear. Do a constant amount of work on each element of the input l find an item in a linked list l determine the largest element in an array l On log n log-linear. Divide and conquer algorithms with a linear amount of work to recombine l Sort a list of number with MergeSort l FFT Some examples l On quadratic. Double nested loops that iterate over the data l Insertion sort l O n exponential l Enumerate all possible subsets l Traveling salesman using dynamic programming l On! l Enumerate all permutations l determinant of a matrix with expansion by minors