Discrete Mathematics CS 2610 February 26, 2009 -- part 1.

Slides:



Advertisements
Similar presentations
CSE115/ENGR160 Discrete Mathematics 03/01/12
Advertisements

CMSC 341 Asymptotic Analysis. 2 Mileage Example Problem: John drives his car, how much gas does he use?
Discrete Structures CISC 2315
 The running time of an algorithm as input size approaches infinity is called the asymptotic running time  We study different notations for asymptotic.
February 19, 2015Applied Discrete Mathematics Week 4: Number Theory 1 The Growth of Functions Question: If f(x) is O(x 2 ), is it also O(x 3 )? Yes. x.
Estimating Running Time Algorithm arrayMax executes 3n  1 primitive operations in the worst case Define a Time taken by the fastest primitive operation.
Chapter 3 Growth of Functions
The Growth of Functions
Asymptotic Growth Rate
Cutler/HeadGrowth of Functions 1 Asymptotic Growth Rate.
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Real-Valued Functions of a Real Variable and Their Graphs
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 2 Elements of complexity analysis Performance and efficiency Motivation: analysis.
Algorithms Chapter 3 With Question/Answer Animations.
CSE 373 Data Structures and Algorithms Lecture 4: Asymptotic Analysis II / Math Review.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Algorithm analysis and design Introduction to Algorithms week1
Asymptotics Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Big-O Analysis Order of magnitude analysis.
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
Mathematics Review and Asymptotic Notation
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
The Growth of Functions Rosen 2.2 Basic Rules of Logarithms log z (xy) log z (x/y) log z (x y ) If x = y If x < y log z (-|x|) is undefined = log z (x)
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 
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
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.
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesFunctions.
Fall 2002CMSC Discrete Structures1 Enough Mathematical Appetizers! Let us look at something more interesting: Algorithms.
MCA-2012Data Structure1 Algorithms Rizwan Rehman CCS, DU.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
O, , and  Notations Lecture 39 Section 9.2 Wed, Mar 30, 2005.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CompSci 102 Discrete Math for Computer Science
Nattee Niparnan Dept. of Computer Engineering, Chulalongkorn University.
O -Notation April 23, 2003 Prepared by Doug Hogan CSE 260.
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)
Introduction to Analysis of Algorithms CS342 S2004.
Time Complexity of Algorithms
Time Complexity of Algorithms (Asymptotic Notations)
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.
Big-O. Algorithm Analysis Exact analysis: produce a function f(n) measuring how many basic steps are needed for a given inputs n On any input of size.
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.
Big-Oh Notation. Agenda  What is Big-Oh Notation?  Example  Guidelines  Theorems.
Review 2 Basic Definitions Set - Collection of objects, usually denoted by capital letter Member, element - Object in a set, usually denoted by lower.
13 February 2016 Asymptotic Notation, Review of Functions & Summations.
8/3/07CMSC 341 Asymptotic Anaylsis1 CMSC 341 Asymptotic Analysis.
Chapter 3. Chapter Summary Algorithms Example Algorithms Growth of Functions Big-O and other Notation Complexity of Algorithms.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Lecture 7. Asymptotic definitions for the analysis of algorithms 1.
Asymptotic Bounds The Differences Between (Big-O, Omega and Theta) Properties.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
Chapter 3 With Question/Answer Animations 1. Chapter Summary Algorithm and Growth of Function (This slide) Algorithms - Sec 3.1 – Lecture 13 Example Algorithms.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 2.
Jessie Zhao Course page: 1.
Big-O. Speed as function Function relating input size to execution time – f(n) = steps where n = length of array f(n) = 4(n-1) + 3 = 4n – 1.
Algorithms Chapter 3.
Relations, Functions, and Matrices
Analysis of Algorithms & Orders of Growth
The Growth of Functions
The Growth of Functions
Enough Mathematical Appetizers!
Applied Discrete Mathematics Week 6: Computation
Enough Mathematical Appetizers!
Enough Mathematical Appetizers!
Algorithms Chapter 3 With Question/Answer Animations.
Big-O & Asymptotic Analysis
Presentation transcript:

Discrete Mathematics CS 2610 February 26, part 1

2 Big-O Notation Big-O notation is used to express the time complexity of an algorithm We can assume that any operation requires the same amount of time. The time complexity of an algorithm can be described independently of the software and hardware used to implement the algorithm.

3 Big-O Notation Def.: Let f, g be functions with domain R  0 or N and codomain R. f(x) is O(g(x)) if there are constants C and k st  x > k, |f (x )|  C  |g (x )| f (x ) is asymptotically dominated by g (x ) C|g(x)| is an upper bound of f(x). C and k are called witnesses to the relationship between f & g. C|g(x)| |f(x)| k

4 Big-O Notation To prove that a function f(x) is O(g(x)) Find values for k and C, not necessarily the smallest one, larger values also work!! It is sufficient to find a certain k and C that works In many cases, for all x  0, if f(x)  0 then |f(x)| = f(x) Example: f(x) = x 2 + 2x + 1 is O(x 2 ) for C = 4 and k = 1

5 Big-O Notation Show that f(x) = x 2 + 2x + 1 is O(x 2 ). When x > 1 we know that x ≤ x 2 and 1 ≤ x 2 then 0 ≤ x 2 + 2x + 1 ≤ x 2 + 2x 2 + x 2 = 4x 2 so, let C = 4 and k = 1 as witnesses, i.e., f(x) = x 2 + 2x Could try x > 2. Then we have 2x ≤ x 2 & 1 ≤ x 2 then 0 ≤ x 2 + 2x + 1 ≤ x 2 + x 2 + x 2 = 3x 2 so, C = 3 and k = 2 are also witnesses to f(x) being O(x 2 ). Note that f(x) is also O(x 3 ), etc.

6 Big-O Notation Show that f(x) = 7x 2 is O(x 3 ). When x > 7 we know that 7x 2 7 by x 2 ) so, let C = 1 and k = 7 as witnesses. Could try x > 1. Then we have 7x 2 < 7x 3 so, C = 7 and k = 1 are also witnesses to f(x) being O(x 3 ). Note that f(x) is also O(x 4 ), etc.

7 Big-O Notation Show that f(n) = n 2 is not O(n). Show that no pair of C and k exists such that n 2 ≤ Cn whenever n > k. When n > 0, divide both sides of n 2 ≤ Cn by n to get n ≤ C. No matter what C and k are, n ≤ C will not hold for all n with n > k.

8 Big-O Notation Observe that g(x) = x 2 is O(x 2 + 2x + 1) Def:Two functions f(x) and g(x) have the same order iff g(x) is O(f(x)) and f(x) is O(g(x))

9 Big-O Notation Also, the function f(x) = 3x 2 + 2x + 3 is O(x 3 ) What about O(x 4 ) ? In fact, the function Cg(x) is an upper bound for f(x), but not necessarily the tightest bound. When Big-O notation is used, g(x) is chosen to be as small as possible.

10 Big-Oh - Theorem Theorem: If f(x) = a n x n + a n-1 x n-1 +…+ a 1 x+ a 0 where a i  R, i=0,…n; then f(x) is O(x n ). Leading term dominates! Proof: if x > 1 we have |f(x)| = |a n x n + a n-1 x n-1 +…+ a 1 x+ a 0 | ≤ |a n |x n + |a n-1 |x n-1 +…+ |a 1 |x+ |a 0 | = x n (|a n | + |a n-1 |/x +…+ |a 1 |/x n-1 + |a 0 |/x n ) ≤ x n (|a n | + |a n-1 | +…+ |a 1 | + |a 0 |) So,|f(x)| ≤ Cx n where C = |a n | + |a n-1 | +…+ |a 1 | + |a 0 | whenever x > 1 (what’s k? k = 1, why?) What’s this: |a + b| ≤ |a| + |b|

11 Big-O Example: Prove that f(n) = n! is O(n n ) Proof (easy): n! = 1 · 2 · 3 · 4 · 5 · · · n ≤ n · n · n · n · n · · · n = n n where our witnesses are C = 1 and k = 1 Example: Prove that log(n!) is O(nlogn) Using the above, take the log of both sides: log(n!) ≤ log( n n ) which is equal to n log(n)

12 Big-O Lemma:A constant function is O(1). Proof: Left to the viewer The most common functions used to estimate the time complexity of an algorithm. (in increasing O() order): 1, (log n), n, (n log n), n 2, n 3, … 2 n, n!

13 Big-O Properties Transitivity:if f is O(g) and g is O(h) then f is O(h) Sum Rule: If f 1 is O(g 1 ) and f 2 is O(g 2 ) then f 1 +f 2 is O(max(|g 1 |,|g 2 |)) If f 1 is O(g) and f 2 is O(g) then f 1 +f 2 is O(g) Product Rule If f 1 is O(g 1 ) andf 2 is O(g 2 ) then f 1 f 2 is O(g 1 g 2 ) For all c > 0,O(cf), O(f + c),O(f  c) are O(f)

14 Big-O – Properties Example Example: Give a big-O estimate for 3n log (n!) + (n 2 +3)log n, n>0 1) For 3n log (n!) we know log(n!) is O(nlogn) and 3n is O(n) so we know 3n log(n!) is O(n 2 logn) 2) For (n 2 +3)log n we have (n 2 +3) 2 so it’s O(n 2 ); and (n 2 +3)log n is O(n 2 log n) 3) Finally we have an estimate for 3n log (n!) + (n 2 +3)log n that is: O(n 2 log n)

15 Big-O Notation Def.:Functions f and g are incomparable, if f(x) is not O(g) and g is not O(f). f: R +  R, f(x) = 5 x 1.5 g: R +  R, g(x) = |x 2 sin x| -- 5 x |x 2 sin x| -- x 2

16 Big-Omega Notation Def.: Let f, g be functions with domain R  0 or N and codomain R. f(x) is  (g(x)) if there are positive constants C and k such that  x > k, C  |g (x )|  |f (x )|  C  |g(x)| is a lower bound for |f(x)| C|g(x)| |f(x)| k

17 Big-Omega Property Theorem: f(x) is  (g(x)) iff g(x) is O(f(x)). Is this trivial or what?

18 Big-Omega Property Example: prove that f(x) = 3x 2 + 2x + 3 is  (g(x)) where g(x) = x 2 Proof: first note that 3x 2 + 2x + 3 ≥ 3x 2 for all x ≥ 0. That’s the same as saying that g(x) = x 2 is O(3x 2 + 2x + 3)

19 Big-Theta Notation Def.:Let f, g be functions with domain R  0 or N and codomain R. f(x) is  (g(x)) if f(x) is O(g(x)) and f(x) is  (g(x)). C 2 |g(x)| C 1 |g(x)| |f(x)|

20 Big-Theta Notation When f(x) is  (g(x)), we know that g(x) is  (f(x)). Also, f(x) is  (g(x)) iff f(x) is O(g(x)) and g(x) is O(f(x)). Typical g functions: x n, c x, log x, etc.

21 Big-Theta Notation To prove that f(x) is order g(x) Method 1  Prove that f is O(g(x))  Prove that f is  (g(x)) Method 2  Prove that f is O(g(x))  Prove that g is O(f(x))

22 Big-Theta Example show that 3x 2 + 8x log x is  (x 2 ) (or order x 2 ) 0 ≤ 8x log x ≤ 8x 2 so 3x 2 + 8x log x ≤ 11x 2 for x > 1. So, 3x 2 + 8x log x is O(x 2 ) (can I get a witness?) Is x 2 O(3x 2 + 8x log x)? You betcha! Why? Therefore, 3x 2 + 8x log x is  (x 2 )

23 Big Summary Upper Bound – Use Big-Oh Lower Bound – Use Big-Omega Upper and Lower (or Order of Growth) – Use Big-Theta

24 Time to Shift Gears Again Number Theory Livin’ Large