CMSC 341 Asymptotic Analysis. 2 Mileage Example Problem: John drives his car, how much gas does he use?

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

 The running time of an algorithm as input size approaches infinity is called the asymptotic running time  We study different notations for asymptotic.
Chapter 10 Algorithm Efficiency
1 ICS 353 Design and Analysis of Algorithms Spring Semester (062) King Fahd University of Petroleum & Minerals Information & Computer Science.
Asymptotic Growth Rate
Cutler/HeadGrowth of Functions 1 Asymptotic Growth Rate.
Lecture 3. kf(n) is O(f(n)) for any positive constant k f(n) + g(n) is O(f(n)) if g(n) is O(f(n)) T 1 (n) is O(f(n)), T 2 (n) is O(g(n)) T 1 (n) T 2 (n)
Algorithm Analysis. Math Review – 1.2 Exponents –X A X B = X A+B –X A /X B =X A-B –(X A ) B = X AB –X N +X N = 2X N ≠ X 2N –2 N+ 2 N = 2 N+1 Logarithms.
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
Tirgul 2 Asymptotic Analysis. Motivation: Suppose you want to evaluate two programs according to their run-time for inputs of size n. The first has run-time.
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
CHAPTER 2 ANALYSIS OF ALGORITHMS Part 1. 2 Big Oh and other notations Introduction Classifying functions by their asymptotic growth Theta, Little oh,
Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Program Performance & Asymptotic Notations CSE, POSTECH.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Mathematics Review and Asymptotic Notation
CMSC 341 Asymptotic Analysis. 8/3/07 UMBC CMSC 341 AA2-color 2 Complexity How many resources will it take to solve a problem of a given size?  time 
CS 3343: Analysis of Algorithms
CMSC 341 Asymptotic Analysis. 2 Complexity How many resources will it take to solve a problem of a given size? –time –space Expressed as a function of.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Asymptotic Analysis-Ch. 3
Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright ©
O, , and  Notations Lecture 39 Section 9.2 Wed, Mar 30, 2005.
Algorithms Growth of Functions. Some Notation NNatural numbers RReal numbers N + Positive natural numbers R + Positive real numbers R * Non-negative real.
Nattee Niparnan Dept. of Computer Engineering, Chulalongkorn University.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program.
Asymptotic Growth Rates  Themes  Analyzing the cost of programs  Ignoring constants and Big-Oh  Recurrence Relations & Sums  Divide and Conquer 
CMSC 341 Asymptotic Analysis. 2 Complexity How many resources will it take to solve a problem of a given size? –time –space Expressed as a function of.
Asymptotic Analysis (based on slides used at UMBC)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
General rules: Find big-O f(n) = k = O(1) f(n) = a k n k + a k-1 n k a 1 n 1 + a 0 = O(n k ) Other functions, try to find the dominant term according.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Analysis of Algorithm. Why Analysis? We need to know the “behavior” of algorithms – How much resource (time/space) does it use So that we know when to.
Algorithm Complexity L. Grewe 1. Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them?
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
8/3/07CMSC 341 Asymptotic Anaylsis1 CMSC 341 Asymptotic Analysis.
CMSC 341 Asymptotic Anaylsis1 CMSC 341 Asymptotic Analysis.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Asymptotic Complexity
Chapter 2 Algorithm Analysis
Introduction to Algorithms
Analysis of algorithms
Growth of functions CSC317.
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Time Complexity Analysis Neil Tang 01/19/2010
Algorithm Analysis Neil Tang 01/22/2008
CSC 413/513: Intro to Algorithms
Asymptotic Growth Rate
CMSC 341 Asymptotic Analysis.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
CS 3343: Analysis of Algorithms
Defining Efficiency Asymptotic Complexity - how running time scales as function of size of input Two problems: What is the “input size” ? How do we express.
Advanced Analysis of Algorithms
Chapter 2.
CMSC 341 Asymptotic Analysis.
Intro to Data Structures
At the end of this session, learner will be able to:
Analysis of algorithms
Advanced Analysis of Algorithms
Big-O & Asymptotic Analysis
Big Omega, Theta Defn: T(N) = (g(N)) if there are positive constants c and n0 such that T(N)  c g(N) for all N  n0 . Lingo: “T(N) grows no slower than.
Algorithm Course Dr. Aref Rashad
Presentation transcript:

CMSC 341 Asymptotic Analysis

2 Mileage Example Problem: John drives his car, how much gas does he use?

3 Complexity How many resources will it take to solve a problem of a given size? –time –space Expressed as a function of problem size (beyond some minimum size) –how do requirements grow as size grows? Problem size –number of elements to be handled –size of thing to be operated on

4 Growth Functions Constant f(n) = c ex: getting array element at known location trying on a shirt calling a friend for fashion advice Linear f(n) = cn [+ possible lower order terms] ex: finding particular element in array (sequential search) trying on all your shirts calling all your n friends for fashion advice

5 Growth Functions (cont) Quadratic f(n) = cn 2 [ + possible lower order terms] ex: sorting all the elements in an array (using bubble sort) trying all your shirts (n) with all your ties (n) having conference calls with each pair of n friends Polynomial f(n) = cn k [ + possible lower order terms] ex:looking for maximum substrings in array trying on all combinations of k separates (n of each) having conferences calls with each k-tuple of n friends

6 Growth Functions (cont) Exponential f(n) = c n [+ possible lower order terms ex:constructing all possible orders of array elements Logarithmic f(n) = logn [ + possible lower order terms] ex:finding a particular array element (binary search) trying on all Garanimal combinations getting fashion advice from n friends using phone tree

7 Asymptotic Analysis What happens as problem size grows really, really large? (in the limit) –constants don’t matter –lower order terms don’t matter

8 Analysis Cases What particular input (of given size) gives worst/best/average complexity? Mileage example: how much gas does it take to go 20 miles? –Worst case: all uphill –Best case: all downhill, just coast –Average case: “average terrain”

9 Cases Example Consider sequential search on an unsorted array of length n, what is time complexity? Best case: Worst case: Average case:

10 Complexity Bounds Upper bound (big O): T(n) = O(f(n)) if T(n)  cf(n)for some constants c, n 0 and n  n 0 Lower bound (omega  T(n)  (g(n)) if T(n)  cg(n) for some constants c, n 0 and n  n 0 “Exact” bound (theta):  (n)  (h(n)) if T(n) = O(h(n)) and T(n) =  (h(n)) “Greater” bound (little o): T(n) = o(p(n)) if T(n) = O(p(n)) and T(n)   (p(n))

11 Simplifying Assumptions 1. If f(n) = O(g(n)) and g(n) = O(h(n)), then f(n) = O(h(n)) 2. If f(n) = O(kg(n) for any k > 0, then f(n) = O(g(n)) 3. If f 1 (n) = O(g 1 (n)) and f 2 (n) = O(g 2 (n)), then f 1 (n)+f 2 (n) = O(max(g 1 (n),g 2 (n))) 4. If f 1 (n) = O(g 1 (n)) and f 2 (n) = O(g 2 (n)), then f 1 (n)f 2 (n) = O(g 1 (n) g 2 (n))

12 Example Code: a = b; Complexity:

13 Example Code: sum = 0; for (i=1; i <=n; i++) sum += n; Complexity:

14 Example Code: sum = 0; for (j=1; j<=n; j++) for (i=1; i<=j; i++) sum++; for (k=0; k<n; k++) A[k] = k; Complexity:

15 Example Code: sum1 = 0; for (i=1; i<=n; i++) for (j=1; j<=n; j++) sum1++; Complexity:

16 Example Code: sum2 = 0; for (i=1; i<=n; i++) for (j=1; j<=i; j++) sum2++; Complexity:

17 Example Code: sum1 = 0; for (k=1; k<=n; k*=2) for (j=1; j<=n; j++) sum1++; Complexity:

18 Example Code: sum2 = 0; for (k=1; k<=n; k*=2) for (j=1; j<=k; j++) sum2++; Complexity:

19 Some Questions 1. Is upper bound the same as worst case? 2. Does lower bound happen with shortest input? 3. What if there are multiple parameters? Ex: Rank order of p pixels in c colors for (i = 0; i < c; i++) count[i] = 0; for (i = 0; i < p; i++) count[value(i)]++; sort(count)

20 Space Complexity Does it matter? What determines space complexity? How can you reduce it? What tradeoffs are involved?

21 Constants in Bounds Theorem: O(cf(x) = O(f(x)) Proof: –T(x) = O(cf(x)) implies that there are constants c 0 and n 0 such that T(x)  c 0 (cf(x)) when x  n 0 –Therefore, T(x)  c 1 (f(x)) when x  n 0 where c 1 = c 0 c –Therefore, T(x) = O(f(x))

22 Sum in Bounds Theorem: Let T 1 (n) = O(f(n) and T 2 (n) = O(g(n)). Then T 1 (n) + T 2 (n) = O(max(f(n),g(n)). Proof: –From the definition of O, T 1 (n)  c 1 f (n) for n  n 1 and T 2 (n)  c 2 g(n) for n  n 2 –Let n 0 = max(n 1, n 2 ). –Then, for n  n 0, T 1 (n) + T 2 (n)  c 1 f (n) + c 2 g(n) –Let c 3 = max(c 1, c 2 ). –Then, T 1 (n) + T 2 (n)  c 3 f (n) + c 3 g (n)  2c 3 max(f (n), g (n))  c max(f (n), g (n))

23 Products in Bounds Theorem: Let T 1 (n) = O(f(n) and T 2 (n) = O(g(n)). Then T 1 (n)*T 2 (n) = O(f(n)*g(n)). Proof: –T 1 (n)* T 2 (n)  c 1 c 2 f (n) g(n) when n  n 0 –Therefore, T 1 (n)*T 2 (n) = O(f(n)*g(n)).

24 Polynomials in Bounds Theorem: If T (n) is a polynomial of degree x, then T(n) = O(n x ). Proof: –T (n) = n x + n x-1 + … + k is a polynomial of degree x. –By the sum rule, the largest term dominates. –Therefore, T(n) = O(n x ).

25 L’Hospital’s Rule Finding limit of ratio of functions as variable approaches  Use to determine O or  ordering of two functions f(x = O(g(x)) if f(x) =  (g(x)) if

26 Polynomials of Logarithms in Bounds Theorem: lg x n = O(n) for any positive constant k Proof: –Note that lg k n means (lg n) k. –Need to show lg k n  cn for n  n 0. Equivalently, can show lg n  cn 1/k –Letting a = 1/k, we will show that lg n = O(n a ) for any positive constant a. Use L’Hospital’s rule: Ex: lg (n) = O(n)

27 Polynomials vs Exponentials in Bounds Theorem: n k = O(a n ) for a > 1 Proof: –Use L’Hospital’s rule =... = 0 Ex: n = O( n )

28 Relative Orders of Growth n (linear) log k n for 0 < k < 1 constant n 1+k for k > 0 (polynomial) 2 n (exponential) n log n log k n for k > 1 n k for 0 < k < 1 log n

29 Relative Orders of Growth constant log k n for 0 < k < 1 log n log k n for k> 1 n k for k < 1 n (linear) n log n n 1+k for k > 0 (polynomial) 2 n (exponential)