CSE 417: Algorithms and Computational Complexity

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

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

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

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

Growth of Functions (CLRS 2.3,3)

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

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

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

CSC236 Week 4. Larry Zhang

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

Submit Growable Array exercise Answer Q1-3 from today's in-class quiz.

The Time Complexity of an Algorithm

Design and Analysis of Algorithms

Principles of Algorithm Analysis

An analogy from Calculus: limits

EECS 477: Introduction to algorithms. Lecture 5

What is Performance Analysis?

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

Running Time Evaluation

The Time Complexity of an Algorithm

CS 310 Advanced Data Structures and Algorithms

Algorithms and Their Complexity

Computational Complexity

CSC Design and Analysis of Algorithms. Lecture 1

CS 4407 Algorithms Lecture 2: Growth Functions

Advanced Algorithmics (6EAP)

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

Data Structures and Algorithms CSE 465

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

Asymptotic Analysis 1

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

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

Data Structures and Algorithms. Asymptotic notation

P, NP, NP-Complete, and NPhard

Algorithms Design & Analysis. Analysis of Algorithm

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

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

Lecture 2: Asymptotic Notation CSCI Algorithms I

csci 210: Data Structures Program Analysis

Fundamentals of Programming. Efficiency of algorithms November 5, 2017

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

2. ALGORITHM ANALYSIS

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

csci 210: Data Structures Program Analysis

Analysis of Algorithms Review

Introduction to Cryptography

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

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

Algorithms and Theory of Computation. Lecture 2: Big-O Notation Graph Algorithms

Analysis of Algorithms

Space Complexity 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

Computational Complexity - Pseudocode and Recursions

3.1 Asymptotic notation

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

COMP 9024, Class notes, 11s2, Class 1

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

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

The Growth of Functions and Big-O Notation

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019

Asymptotic Algorithm Analysis & Sorting

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

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Module 1: Analyzing the Efficiency of Algorithms

CS Data Structures and Algorithm Analysis

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

Copyright 2000, Kevin Wayne 1

Lecture 3: Big-O and Big-Θ

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

Algorithm runtime analysis and computational tractability

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

Analysis of Algorithms

COMP Analysis of Algorithms & Data Structures

Intro to Theory of Computation

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

Great Theoretical Ideas in Computer Science. Lecture 7: Introduction to Computational Complexity

CS173 Running Time and Big-O. Tandy Warnow

Copyright 2000, Kevin Wayne 1

COMP Analysis of Algorithms & Data Structures

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

COMP 382: Reasoning about algorithms

Ch01. Analysis of Algorithms

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

Asymptotic Analysis Cont'd

2.1 Computational Tractability. Chapter 2. Basics of Algorithm Analysis. Computational Tractability. Polynomial-Time

MATH 22 FUNCTIONS: ORDER OF GROWTH. Lecture O: 10/21/2003. The old order changeth, yielding place to new. Tennyson, Idylls of the King

Limitations of Algorithm Power

CSE 373: Data Structures and Algorithms. Asymptotic Analysis. Autumn Shrirang (Shri) Mare

Algorithm Analysis, Asymptotic notations CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

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

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

Data Structures and Algorithms Chapter 2

Asymptotic Analysis of Algorithms. Chapter 4

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

CSC236 Week 3. Larry Zhang

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

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2

Transcription:

CSE 417: Algorithms and Computational Complexity Lecture 2: Analysis Larry Ruzzo 1

Why big-o: measuring algorithm efficiency outline What s big-o: definition and related concepts Reasoning with big-o: examples & applications polynomials exponentials logarithms sums Polynomial Time 2

Why big-o: measuring algorithm efficiency 3

Let p n = n th prime, n >= 1, e.g.: p 1 = 2 p 2 = 3 p 3 = 5 p 4 = 7 p 5 = 11 p_n p n 0 50 100 150 200 1.3 * n ln n n ln n 0 10 20 30 40 50 n What is the n th prime number? n After much study, we know p n ~ n log n, even: Great precision! But, often a simple, smooth, upper bound, is even better, e.g.: p n = O(n log n) https://en.wikipedia.org/wiki/prime_number_theorem 4

Our correct TSP algorithm was incredibly slow No matter what computer you have As a 2 nd example, for large problems, mergesort beats insertion sort n log n vs n 2 matters a lot efficiency Even tho the alg is more complex & inner loop is slower No matter what computer you have We want a general theory of efficiency that is Simple Objective Relatively independent of changing technology Measures algorithm, not code But still predictive theoretically bad algorithms should be bad in practice and vice versa (usually) And very complex fns 5

computational complexity The time complexity of an algorithm associates a number T(n), the worst-case time the algorithm takes, with each problem size n. Mathematically, T: N+ R i.e.,t is a function mapping positive integers (problem sizes) to positive real numbers (number of steps). Reals so, e.g., we can say sqrt(n) instead of sqrt(n) Positive so, e.g., log(n) and 2 n /n aren t problematic 6

Time computational complexity T(n) Problem size 7

computational complexity: general goals Asymptotic growth rate, i.e., characterize growth rate of worst-case run time as a function of problem size, up to a constant factor, e.g. T(n) = O(n 2 ) Why not try to be more precise? Average-case, e.g., is hard to define, analyze Technological variations (computer, compiler, OS, ) easily 10x or more Being more precise is much more work A key question is scale up : if I can afford this today, how much longer will it take when my business is 2x larger? (E.g. today: cn 2, next year: c(2n) 2 = 4cn 2 : 4 x longer.) Big-O analysis is adequate to address this. 8

What s big-o: definition and related concepts 9

Given two functions f and g: N+ R O-notation, etc. f(n) is O(g(n)) iff there is a constant c > 0 so that f(n) is eventually always c g(n) Upper Bounds f(n) is W(g(n)) iff there is a constant c > 0 so that f(n) is eventually always ³ c g(n) Lower Bounds f(n) is Q(g(n)) iff there is are constants c 1, c 2 > 0 so that Both eventually always c 1 g(n) f(n) c 2 g(n) Eventually always P(n) means n 0 s.t. n>n 0 P(n) is true. I.e., there can be exceptions, but only for finitely many small values of n. 10

Time computational complexity T(n) Problem size 11

Time computational complexity Example: T(n) = Q(n log n) since for all problem sizes n > n 0, the worst case run time T(n) is between n log 2 n and 2 n log 2 n 2n log 2 n T(n) n log 2 n (Irrelevant) Problem size n 0 12

Time 0 10000 20000 30000 40000 50000 A typical program with initialization and two nested loops might have runtime something like this example n 2 + 30n + 5000 Inner Loop Outer Loop Initialization 0 50 100 150 200 n 13

example Time 0 10000 20000 30000 40000 50000 If T(n) = n 2 + 30n + 5000, then T(n) = Q(n 2 ), since for all n 135, we have n 2 T(n) 1.5 n 2 (Basically Irrelevant) 1.5 n 2 n 2 + 30n + 5000 n 2 Inner Loop Outer Loop Initialization n 0 =135 0 50 100 150 200 n 14

Reasoning with big-o: examples & applications polynomials exponentials logarithms sums 15

0 200 400 600 800 1000 1200 1400 Show 10n 2-16n+100 is O(n 2 ) : 10n 2-16n+100 10n 2 + 100 examples = 10n 2 + 10 2 10n 2 + n 2 = 11n 2 for all n ³ 10 O(n 2 ) [ and also O(n 3 ), O(n 4 ), O(n 2.5 ), ] 11n 2 10n 2-16n+100 0 2 4 6 8 10 12 16

0 200 400 600 800 1000 1200 1400 Show 10n 2-16n+100 is W(n 2 ) : 10n 2-16n+100 ³ 10n 2-16n ³ 10n 2 - n 2 = 9n 2 for all n ³16 W(n 2 ) [ and also W(n), W(n 1.5 ), ] Therefore also 10n 2-16n+100 is Q(n 2 ) [but not Q(n 1.999 ) or Q(n 2.001 ) ] examples 11n 2 10n 2-16n+100 9n 2 0 2 4 6 8 10 12 17

asymptotic bounds for polynomials Polynomials: p(n) = a 0 + a 1 n + + a d n d is Q(n d ) if a d > 0 Proof: p(n) = a 0 a 0 + a 1 n + + a d n d + a 1 n + + a d n d a 0 n d + a 1 n d + + a d n d (for n 1) = c n d, where c = ( a 0 + a 1 + + a d-1 + a d ) p(n) = O(n d ) Exercise: show that p(n) = Ω(n d ) Hint: this direction is trickier; focus on the worst case where all coefficients except a d are negative. 18

another example of working with O-W-Q notation Example: For any a, and any b > 0, (n+a) b is Q(n b ) (n+a) b (2n) b for n ³ a = 2 b n b = cn b for c = 2 b so (n+a) b is O(n b ) (n+a) b ³ (n/2) b for n ³ 2 a (even if a < 0) = 2 -b n b = c n for c = 2 -b so (n+a) b is W (n b ) 19

Example: 1 i n i = Q(n 2 ) Proof: more examples: tricks for sums (a) An upper bound: each term is the max term 1 i n i 1 i n n = n 2 = O(n 2 ) (b) A lower bound: each term is the min term 1 i n i 1 i n 1 = n = Ω(n) This is valid, but a weak bound. Better: pick a large subset of large terms E.g. : for i = 1..n { for j=1 to i {... }} 1 i n i n/2 i n n/2 n/2 2 = Ω(n 2 ) 20

Transitivity. If f = O(g) and g = O(h) then f = O(h). If f = W(g) and g = W(h) then f = W(h). If f = Q(g) and g = Q(h) then f = Q(h). properties Additivity. If f = O(h) and g = O(h) then f + g = O(h). If f = W(h) and g = W(h) then f + g = W(h). If f = Q(h) and g = O(h) then f + g = Q(h). Proofs are left as exercises. Useful, e.g., for analyzing programs with subroutines. 21

polynomial vs exponential For all r > 1 (no matter how small) and all d > 0, (no matter how large) n d = O(r n ) In short, every exponential grows faster than every polynomial! 1.01 n n 100 (To prove this, use calculus tricks like L Hospital s rule.) 22

logarithms Example: For any a, b>1 log a n is Q(log b n) log a b = x means a x = b a log a b = b (a log a b ) log b n = b log b n = n (log a b)(log b n) = log a n c log b n = log a n for the constant c = log a b So : log b n = Θ(log a n) = Θ(logn) definition Corollary: base of a log factor is usually irrelevant, asymptotically. E.g. O(n log n) [but n log 2 8 O(n log 8 8 )] 23

polynomial vs logarithm Logarithms: For all x > 0, (no matter how small) log n = O(n x ) log grows slower than every polynomial 0 5 10 15 20 25 30 n^.50 n^.33 log(n) 0 5 10 15 20 25 30 n^.50 n^.33 log(n) 0 200 400 600 800 1000 n 1 10 100 1000 n (log scale) 24

big-theta, etc. are not always nice " f (n) = n 2, n even% # & $ n, n odd ' f(n) ¹ Q(n a ) for any a. Fortunately, such nasty cases are rare n log n ¹ Q(n a ) for any a, either, but at least it s simpler. 25

Polynomial Time 26

the complexity class P: polynomial time P: The set of problems solvable by algorithms with running time O(n d ) for some constant d (d is a constant independent of the input size n) Nice scaling property: there is a constant c s.t. doubling n, time increases only by a factor of c. (E.g., c ~ 2 d ) Contrast with exponential: For any constant c, there is a d such that n n+d increases time by a factor of more than c. (E.g., c = 100 and d = 7 for 2 n vs 2 n+7 ) 27

polynomial vs exponential growth 2 2n 2 n/10 2 2n 2 n/10 1000n 2 1000n 2 28

why it matters not only get very big, but do so abruptly, which likely yields erratic performance on small instances 29

another view of poly vs exp Next year s computer will be 2x faster. If I can solve problem of size n 0 today, how large a problem can I solve in the same time next year? Complexity Size Increase E.g. T=10 12 O(n) n 0 2n 0 10 12 2 x 10 12 O(n 2 ) n 0 Ö2 n 0 10 6 1.4 x 10 6 O(n 3 ) n 0 3 Ö2 n 0 10 4 1.25 x 10 4 2 n /10 n 0 n 0 +10 400 410 2 n n 0 n 0 +1 40 41 30

why polynomial? Point is not that n 2000 is a nice time bound, or that the differences among n and 2n and n 2 are negligible. Rather, simple theoretical tools may not easily capture such differences, whereas exponentials are qualitatively different from polynomials, so more amenable to theoretical analysis. My problem is in P is a starting point for a more detailed analysis My problem is not in P may suggest that you need to shift to a more tractable variant, or otherwise readjust expectations 31

Summary 32

summary A typical initial goal for algorithm analysis is to find a reasonably tight, asymptotic, bound on worst case running time i.e., Θ if possible i.e., O or Θ usually upper bound as a function of problem size This is rarely the last word, but often helps separate good algorithms from blatantly poor ones so you can concentrate on the good ones! As one important example, poly time algorithms are almost always preferable to exponential time ones. 33