Big-O notation.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Recursion COMP T1.
11.2 Complexity Analysis. Complexity Analysis As true computer scientists, we need a way to compare the efficiency of algorithms. Should we just use a.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 16: Recursion.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithm.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
Analysis of Algorithms
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Lecture 10 – Algorithm Analysis.  Next number is sum of previous two numbers  1, 1, 2, 3, 5, 8, 13, 21 …  Mathematical definition 2COMPSCI Computer.
COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Algorithm Complexity is concerned about how fast or slow particular algorithm performs.
Algorithm Analysis 1.
CS 302 Data Structures Algorithm Efficiency.
Recursion COMP T1 #6.
Mathematical Foundation
Analysis of Non – Recursive Algorithms
Analysis of Non – Recursive Algorithms
Introduction to Analysis of Algorithms
Design and Analysis of Algorithms Chapter -2
Analysis of Algorithms
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
Introduction to Search Algorithms
Introduction to Analysis of Algorithms
Introduction to complexity
Analysis of Algorithms
Introduction to Analysis of Algorithms
Lecture – 2 on Data structures
Introduction to Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
DATA STRUCTURES Introduction: Basic Concepts and Notations
CS 3343: Analysis of Algorithms
Algorithm Analysis and Big Oh Notation
Big-Oh and Execution Time: A Review
CHAPTER 2: Analysis of Algorithms
Chapter 12: Analysis of Algorithms
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
ITEC 2620M Introduction to Data Structures
What is CS 253 about? Contrary to the wide spread belief that the #1 job of computers is to perform calculations (which is why the are called “computers”),
Chapter 2 Analysis of Algorithms
Algorithm Efficiency Chapter 10.
Big O Notation.
CSE 1342 Programming Concepts
Recursion Data Structures.
Programming and Data Structure
Analysis of Algorithms
Programming and Data Structure
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Algorithmic Complexity
Complexity Analysis Text is mainly from Chapter 2 by Drozdek.
Algorithm Efficiency and Sorting
Introduction to Algorithm and its Complexity Lecture 1: 18 slides
Algorithm Analysis and Big Oh Notation
CSE 1342 Programming Concepts
CHAPTER 2: Analysis of Algorithms
8. Comparison of Algorithms
Algorithmic complexity
Analysis of Algorithms
Complexity Analysis (Part II)
Estimating Algorithm Performance
Analysis of Algorithms
Algorithms and data structures: basic definitions
Presentation transcript:

big-O notation

Definition Big-O Notation is the most widely used method which describes algorithm complexity: the execution time required or the space used in memory or in disk by an algorithm Big O notation is used describe the rough estimate of the number of “steps” to complete the algorithm

Assume that doSomething() takes C steps to complete Part 1: It does doSomething() So it takes constant C steps C is independent to the parameter n When the time complexity is independent to the parameter, big-O notation is O(1) Part 2: It does doSomething() n times Each time it takes C steps So in total it takes n*C steps to complete Then big-O is n*O(1) due to Part1 Important rule, a*O(n) = O(an) So the time complexity is n*O(1) is O(n) Part 3: It has two loops. Inner one is same as Part2. Outer loop does Part2 n times So the time complexity is n*O(n)=O(n2) Part 4: It takes exactly 1 step to return So time complexity O(1) void exampleBigO(int n) { // part 1 doSomething(); // part 2 for(int i=0; i<n; i++) // part 3 for(int j=0; j<n; j++) // part 4 return; }

Big-O for time complexity So total time complexity for exampleBigO(int n) function is equal to sum of time complexities of all parts: O(1)+ O(n)+O(n2)+O(1) If the parameter n becomes a very large number Then Part 1 and Part 4 can be ignored because they are not dependent to the parameter n and spend very less time comparing to Part 2 and Part 3 When the parameter n becomes extremely large number, then Part 2 also can be ignored So when add up big-O notations, the notation with slower increase speed could be ignored by the notation with faster increase speed. So result big-O notation for exampleBigO() is O(n2)

Rules summary for big-O O(1) * O(n) = O(n) O(n) * O(n) = O(n2) O(1) + O(n) = O(n) O(n) + O(n2) = O(n2) O(1) + O(n) + O(n2) = O(n2) Comparison of increase speed: O(1) < O(log(n)) < O(n) < O(nlog(n)) < O(n2)

Comparison of Algorithms Often algorithm with smaller big-O notation is more efficient But it may not be correct for small scale of data But this rule will be efficient for very large number of data Because computer science is always dealing with large scale of data this rule is applicable

big-O space complexity It is easier to understand using big-O to estimate space complexity as it is more concrete When number of elements of an array is constant C and which is independent to n, then the space complexity for using the array is Cn which is O(n)*O(C) = O(n)*O(1) =O(n) When comparing different algorithms, often compare how much ‘extra’ space complexity is needed to solve the problem The algorithm that needs an extra array of size n is not as good as the one which only needs two variables O(1) is more efficient then O(n)