1 Big-Oh Notation CS 105 Introduction to Data Structures and Algorithms.

Slides:



Advertisements
Similar presentations
Algorithms Algorithm: what is it ?. Algorithms Algorithm: what is it ? Some representative problems : - Interval Scheduling.
Advertisements

Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
© 2004 Goodrich, Tamassia 1 Lecture 01 Algorithm Analysis Topics Basic concepts Theoretical Analysis Concept of big-oh Choose lower order algorithms Relatives.
Introduction to Analysis of Algorithms
Analysis of Algorithms Algorithm Input Output. Analysis of Algorithms2 Outline and Reading Running time (§1.1) Pseudo-code (§1.1) Counting primitive operations.
Analysis of Algorithms (Chapter 4)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Analysis of Algorithms
Analysis of Algorithms (pt 2) (Chapter 4) COMP53 Oct 3, 2007.
Fall 2006CSC311: Data Structures1 Chapter 4 Analysis Tools Objectives –Experiment analysis of algorithms and limitations –Theoretical Analysis of algorithms.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
CHAPTER 2 ANALYSIS OF ALGORITHMS Part 1. 2 Big Oh and other notations Introduction Classifying functions by their asymptotic growth Theta, Little oh,
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Algorithm/Running Time Analysis. Running Time Why do we need to analyze the running time of a program? Option 1: Run the program and time it –Why is this.
CSE 373 Data Structures and Algorithms Lecture 4: Asymptotic Analysis II / Math Review.
Analysis of Performance
CS2210 Data Structures and Algorithms Lecture 2:
Asymptotic Notations Iterative Algorithms and their analysis
Program Performance & Asymptotic Notations CSE, POSTECH.
For Wednesday Read Weiss chapter 3, sections 1-5. This should be largely review. If you’re struggling with the C++ aspects, you may refer to Savitch, chapter.
Analysis Tools Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Lecture 2 Computational Complexity
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Analysis of Algorithms
Analysis of Algorithms1 The Goal of the Course Design “good” data structures and algorithms Data structure is a systematic way of organizing and accessing.
Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)
Iterative Algorithm Analysis & Asymptotic Notations
Asymptotic Analysis-Ch. 3
1 Analysis of Algorithms CS 105 Introduction to Data Structures and Algorithms.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Analysis of Algorithms [ Section 4.1 ] Examples of functions important in CS: the constant function:f(n) =
Zeinab EidAlgorithm Analysis1 Chapter 4 Analysis Tools.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Introduction to Analysis of Algorithms CS342 S2004.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
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 algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
CSE 373: Data Structures and Algorithms Lecture 4: Math Review/Asymptotic Analysis II 1.
Lecture aid 1 Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
Algorithms Lecture #05 Uzair Ishtiaq. Asymptotic Notation.
Announcement We will have a 10 minutes Quiz on Feb. 4 at the end of the lecture. The quiz is about Big O notation. The weight of this quiz is 3% (please.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
1 COMP9024: Data Structures and Algorithms Week Two: Analysis of Algorithms Hui Wu Session 2, 2014
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
Introduction to Algorithms: Asymptotic Notation
COMP9024: Data Structures and Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
Algorithms Algorithm Analysis.
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
Analysis of Algorithms
Introduction to Algorithms Analysis
Analysis of Algorithms
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
Analysis of Algorithms
CS 201 Fundamental Structures of Computer Science
Advanced Analysis of Algorithms
Analysis of Algorithms
CSE 2010: Algorithms and Data Structures Algorithms
Analysis of Algorithms
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Advanced Analysis of Algorithms
Big-O & Asymptotic Analysis
Analysis of Algorithms
Presentation transcript:

1 Big-Oh Notation CS 105 Introduction to Data Structures and Algorithms

2 Order Arithmetic Big-Oh notation provides a way to compare two functions “f(n) is O( g(n) )” means: f(n) is less than or equal to g(n) up to a constant factor for large values of n

3 Categorizing functions Big-Oh can be used for categorizing or characterizing functions For example, the statements: 2n + 3 is O(n) and 5n is O(n) place 2n + 3 and 5n in the same category Both functions are less than or equal to g(n) = n, up to a constant factor, for large values of n If the functions are running times of two algorithms, the algorithms are thus comparable

4 Definition of Big-Oh f(n) is O( g(n) ) if there is a real constant c > 0 and an integer constant n 0 >= 1 such that f(n) = n 0 less than or equal up to a constant factor for large values of n

5 Example f(n) = 2n + 5 g(n) = n Consider the condition 2n + 5 <= n will this condition ever hold? No! How about if we tack a constant to n? 2n + 5 <= 3n the condition holds for values of n greater than or equal to 5 This means we can select c = 3 and n 0 = 5

6 2n+5 n 3n point where 3n “beats” 2n+5 Example 2n+5 is O( n )

7 Use of Big-Oh notation Big-Oh allows us to ignore constant factors and lower order (or less dominant) terms 2n 2 + 5n – 4 is O( n 2 ) constants lower order terms

8 Back to arrayMax example Algorithm arrayMax(A,n): Input: An array A storing n integers. Output: The maximum element in A. currentMax  A[0] for i  1 to n - 1 do if currentMax < A[i] then currentMax  A[i] return currentMax

9 Back to arrayMax example Depending on what operations we decide to count, running time function f(n) = a n + b Regardless, f(n) is O(n) Or, equivalently, the running time of algorithm arrayMax is O(n)

10 Function categories revisited The constant function:f(n) = 1 The linear function:f(n) = n The quadratic function:f(n) = n 2 The cubic function:f(n) = n 3 The exponential function:f(n) = 2 n The logarithm function:f(n) = log n The n log n function:f(n) = n log n

11 Comparing function categories Linear (n) is better than quadratic (n 2 ) which is better than exponential (2 n ) Are there any function categories better than linear? Yes! Constant (1) Logarithmic (log n) “Better” means resulting values are smaller (slower growth rates)

12 Functions by increasing growth rate The constant function:f(n) = 1 The logarithm function:f(n) = log n The linear function:f(n) = n The n log n function:f(n) = n log n The quadratic function:f(n) = n 2 The cubic function:f(n) = n 3 The exponential function:f(n) = 2 n

13 Big-Oh in this course For this course, you will be expected to assess the running time of an algorithm and classify it under one of the categories, using Big-Oh notation You should be able to recognize, for instance, that, most of the time (not always): Algorithms with single loops are O(n) Algorithms with double-nested loops are O(n 2 )

14 Big-Oh as an upper bound The statement f(n) is O( g(n) ) indicates that g(n) is an upper bound for f(n) Which means it is also correct to make statements like: 3n+5 is O(n 2 ) 3n+5 is O(2 n ) 3n+5 is O(5n + log n - 2) But the statement 3n+5 is O(n) is the “tightest” statement one can make

15 Relatives of Big-Oh Big Omega  : lower bound Big Theta  : the function is both a lower bound and an upper bound For this course, only Big-Oh notation will be used for algorithm analysis

16 Summary Big-Oh notation compares functions It provides for a mechanism to precisely characterize functions according to typical function categories Running time functions of algorithms are assessed more precisely using Big-Oh The notation allows us to ignore constants and lower order terms