Growth-rate Functions

Slides:



Advertisements
Similar presentations
Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search.
Advertisements

Algorithms Algorithm: what is it ?. Algorithms Algorithm: what is it ? Some representative problems : - Interval Scheduling.
12-Apr-15 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
CSE115/ENGR160 Discrete Mathematics 03/01/12
CSE Lecture 3 – Algorithms I
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Fundamentals of Python: From First Programs Through Data Structures
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
CS50 SECTION: WEEK 3 Kenny Yu. Announcements  Watch Problem Set 3’s walkthrough online if you are having trouble.  Problem Set 1’s feedback have been.
Analysys & Complexity of Algorithms Big Oh Notation.
Computational Complexity 1. Time Complexity 2. Space Complexity.
1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then.
CS107 Introduction to Computer Science
Cmpt-225 Algorithm Efficiency.
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.
Abstract Data Types (ADTs) Data Structures The Java Collections API
COMP s1 Computing 2 Complexity
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Searching – Linear and Binary Searches. Comparing Algorithms Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”? P1P2.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Analysis of Algorithms
Algorithm Evaluation. What’s an algorithm? a clearly specified set of simple instructions to be followed to solve a problem a way of doing something What.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
Ch 18 – Big-O Notation: Sorting & Searching Efficiencies Our interest in the efficiency of an algorithm is based on solving problems of large size. If.
Arrays Tonga Institute of Higher Education. Introduction An array is a data structure Definitions  Cell/Element – A box in which you can enter a piece.
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.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Analysis of Algorithms [ Section 4.1 ] Examples of functions important in CS: the constant function:f(n) =
Nattee Niparnan Dept. of Computer Engineering, Chulalongkorn University.
New Mexico Computer Science For All Algorithm Analysis Maureen Psaila-Dombrowski.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
3.3 Complexity of Algorithms
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
Sorting.
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.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
Searching Topics Sequential Search Binary Search.
Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Copyright © 2009 Curt Hill Look Ups A Recurring Theme.
Concepts of Algorithms CSC-244 Unit 3 and 4 Algorithm Growth Rates Shahid Iqbal Lone Computer College Qassim University K.S.A.
Section 1.7 Comparing Algorithms: Big-O Analysis.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
CMPT 438 Algorithms.
Algorithmic Efficency
COMP108 Algorithmic Foundations Algorithm efficiency
Computer Science 112 Fundamentals of Programming II
Introduction to Algorithms
Lesson Objectives Aims Understand the following: The big O notation.
Sorting by Tammy Bailey
Efficiency (Chapter 2).
Introduction to Data Structures
Algorithm Analysis Bina Ramamurthy CSE116A,B.
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Algorithmic Complexity
8. Comparison of Algorithms
Data Structures & Programming
Presentation transcript:

Growth-rate Functions O(1) – constant time, the time is independent of n, e.g. array look-up O(log n) – logarithmic time, usually the log is base 2, e.g. binary search O(n) – linear time, e.g. linear search O(n*log n) – e.g. efficient sorting algorithms O(n2) – quadratic time, e.g. selection sort O(nk) – polynomial (where k is some constant) O(2n) – exponential time, very slow! Order of growth of some common functions O(1) < O(log n) < O(n) < O(n * log n) < O(n2) < O(n3) < O(2n) Show that the order of function is strict. Another example: which one is bigger n^1.001 or n*log n?

Order-of-Magnitude Analysis and Big O Notation A comparison of growth-rate functions: a) in tabular form

Order-of-Magnitude Analysis and Big O Notation A comparison of growth-rate functions: b) in graphical form

Note on Constant Time We write O(1) to indicate something that takes a constant amount of time E.g. finding the minimum element of an ordered array takes O(1) time, because the min is either at the beginning or the end of the array Important: constants can be huge, and so in practice O(1) is not necessarily efficient --- all it tells us is that the algorithm will run at the same speed no matter the size of the input we give it

Arithmetic of Big-O Notation If f(n) is O(g(n)) then c.f(n) is O(g(n)), where c is a constant. Example: 23*log n is O(log n) If f1(n) is O(g(n)) and f2(n) is O(g(n)) then also f1(n)+f2(n) is O(g(n)) Example: what is order of n2+n? n2 is O(n2) n is O(n) but also O(n2) therefore n2+n is O(n2) Example: log_a(n) and log_b(n) -> base usually omitted. 2) f_i(n) is O(g_i(n)), then f_1(n)+f_2(n) is O(max{g_1(n),g_2(n)}) 2) Example: sum_{i=0}^n c_i x^i is O(x^n)

Arithmetic of Big-O Notation If f1(n) is O(g1(n)) and f2(n) is O(g2(n)) then f1(n)*f2(n) is O(g1(n)*g2(n)). Example: what is order of (3n+1)*(2n+log n)? 3n+1 is O(n) 2n+log n is O(n) (3n+1)*(2n+log n) is O(n*n)=O(n2) 3) Example: (n+1)^5 Example: log(poly(n)) is O(log(n)) Question: is 3^n of order O(2^n)? CONCLUSION: some constants are important! n^c and n^d, c^n and d^n, even c^an and c^bn

Using Big O Notation It’s not correct to say: f(n) · O(g(n)), f(n) = O(g(n)) It’s completely wrong to say: f(n) ¸ O(g(n)) f(n) > O(g(n)) Just use: f(n) is (in) O(g(n)), or f(n) is of order O(g(n)), or f(n) 2 O(g(n))

Using Big O Notation Sometimes we need to be more specific when comparing the algorithms. For instance, there might be several sorting algorithms with time of order O(n.log n). However, an algorithm with cost function 2n.log n + 10n + 7log n + 40 is better than one with cost function 5n.log n + 2n +10log n +1 That means: We care about the constant of the main term. But we still don’t care about other terms. In such situations, the following notation is often used: 2n.log n + O(n) for the first algorithm 5n.log n + O(n) for the second one

Searching costs using O-notation Linear search Best case: O(1) Average case: O(n) Worst case: O(n) Binary search Average case: O(log n) Worst case: O(log n)

Sorting cost in O-notation Selection sort Best case: O(n2) (can vary with implementation) Average case: O(n2) Worst case: O(n2) Insertion sort Best case: O(n) (can vary with implementation)