Cmpt-225 Algorithm Efficiency.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Chapter 1 – Basic Concepts
the fourth iteration of this loop is shown here
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 10 Algorithm Efficiency
Complexity Analysis (Part I)
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
The Efficiency of Algorithms
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Cmpt-225 Simulation. Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Algorithm Analysis (Big O)
COMP s1 Computing 2 Complexity
Analysis of Performance
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
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.
Program Performance & Asymptotic Notations CSE, POSTECH.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
Lecture 2 Computational Complexity
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Mathematics Review and Asymptotic Notation
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)
Complexity Analysis Chapter 1.
Analysis of Algorithms
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
© 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.
Asymptotic Analysis-Ch. 3
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
MS 101: Algorithms Instructor Neelima Gupta
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
RUNNING TIME 10.4 – 10.5 (P. 551 – 555). RUNNING TIME analysis of algorithms involves analyzing their effectiveness analysis of algorithms involves analyzing.
Algorithm Analysis Part of slides are borrowed from UST.
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
Asymptotic Notations By Er. Devdutt Baresary. Introduction In mathematics, computer science, and related fields, big O notation describes the limiting.
Algorithm Analysis (Big O)
Searching Topics Sequential Search Binary Search.
Concepts of Algorithms CSC-244 Unit 3 and 4 Algorithm Growth Rates Shahid Iqbal Lone Computer College Qassim University K.S.A.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Section 1.7 Comparing Algorithms: Big-O Analysis.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Lecture 3COMPSCI.220.S1.T Running Time: Estimation Rules Running time is proportional to the most significant term in T(n) Once a problem size.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Algorithm Analysis 1.
Introduction to Analysis of Algorithms
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
COMP108 Algorithmic Foundations Algorithm efficiency
Analysis of Algorithms
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
GC 211:Data Structures Algorithm Analysis Tools
Analysis of Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Analysis of Algorithms
Presentation transcript:

cmpt-225 Algorithm Efficiency

Timing Algorithms It can be very useful to time how long an algorithm takes to run In some cases it may be essential to know how long a particular algorithm takes on a particular system However, it is not a good general method for comparing algorithms Running time is affected by numerous factors How are the algorithms coded? What computer should we use? CPU speed, memory, specialized hardware (e.g. graphics card) Operating system, system configuration (e.g. virtual memory), programming language, algorithm implementation Other tasks (i.e. what other programs are running), timing of system tasks (e.g. memory management) What data should we use? Input used: example with linear and binary search.

Cost Functions Because of the sorts of reasons just discussed for general comparative purposes we will count, rather than time, the number of operations that an algorithm performs Note that this does not mean that actual running time should be ignored! Note: running time can depend on 2 parameters; example c(n,k) “choosing k items from n”.

Cost Functions For simplicity we assume that each operation take one unit of time. If algorithm (on some particular input) performs t operations, we will say that it runs in time t. Usually running time t depends on the data size (the input length). We express the time t as a cost function of the data size n We denote the cost function of an algorithm A as tA(), where tA(n) is the time required to process the data with algorithm A on input of size n Typical example of the input size: number of nodes in a linked list, number of disks in a Hanoi Tower problem, the size of an array, the number of items in a stack, the length of a string, … Note: running time can depend on 2 parameters; example c(n,k) “choosing k items from n”.

Nested Loop for (i=1 through n){ for (j=1 through i){ for (k=1 through 5){ Perform task T; } If task T requires t units of time, the inner most loop requires 5*t time units and the loop on j requires 5*t*i time units. Therefore, the outermost loop requires

Algorithm Growth Rates. We often want to compare the performance of algorithms When doing so we generally want to know how they perform when the problem size (n) is large So it’s simpler if we just find out how the algorithms perform as the input size grows- the growth rate.

E.g. Algorithm A requires n2/5 time units to solve a problem of size n Algorithm B requires 5*n time units to solve a problem of size n It may be difficult to come up with the above conclusions and besides they do not tell us the exact performance of the algorithms A and B. It will be easier to come up with the following conclusion for algorithms A and B Algorithm A requires time proportional to n2 Algorithm B requires time proportional to n From the above you can determine that for large problems B requires significantly less time than A.

Algorithm Growth Rates Figure 10-1 Time requirements as a function of the problem size n

Since cost functions are complex, and may be difficult to compute, we approximate them using O notation – O notation determines the growth rate of an algorithm time.

Example of a Cost Function Cost Function: tA(n) = n2 + 20n + 100 Which term dominates? It depends on the size of n n = 2, tA(n) = 4 + 40 + 100 The constant, 100, is the dominating term n = 10, tA(n) = 100 + 200 + 100 20n is the dominating term n = 100, tA(n) = 10,000 + 2,000 + 100 n2 is the dominating term n = 1000, tA(n) = 1,000,000 + 20,000 + 100

Big O Notation O notation approximates the cost function of an algorithm The approximation is usually good enough, especially when considering the efficiency of algorithm as n gets very large Allows us to estimate rate of function growth Instead of computing the entire cost function we only need to count the number of times that an algorithm executes its barometer instruction(s) The instruction that is executed the most number of times in an algorithm (the highest order term)

Big O Notation Given functions tA(n) and g(n), we can say that the efficiency of an algorithm is of order g(n) if there are positive constants c and m such that tA(n) < c.g(n) for all n > n0 we write tA(n) is O(g(n)) and we say that tA(n) is of order g(n) e.g. if an algorithm’s running time is 3n + 12 then the algorithm is O(n). If c=3 and n0=12 then g(n) = n: 4 * n  3n + 12 for all n  12

In English… The cost function of an algorithm A, tA(n), can be approximated by another, simpler, function g(n) which is also a function with only 1 variable, the data size n. The function g(n) is selected such that it represents an upper bound on the efficiency of the algorithm A (i.e. an upper bound on the value of tA(n)). This is expressed using the big-O notation: O(g(n)). For example, if we consider the time efficiency of algorithm A then “tA(n) is O(g(n))” would mean that A cannot take more “time” than O(g(n)) to execute or that (more than c.g(n) for some constant c) the cost function tA(n) grows at most as fast as g(n)

The general idea is … when using Big-O notation, rather than giving a precise figure of the cost function using a specific data size n express the behaviour of the algorithm as its data size n grows very large so ignore lower order terms and constants

O Notation Examples In general for polynomial functions All these expressions are O(n): n, 3n, 61n + 5, 22n – 5, … All these expressions are O(n2): n2, 9 n2, 18 n2+ 4n – 53, … In general for polynomial functions t(n)=aknk+ ak-1nk-1+…+ a1n+a0 is of O(nk) If c=(ak+ ak-1+…+a+a0 ) and n0=1 then t(n) < cnk for n>1 All these expressions are O(n log n): n(log n), 5n(log 99n), 18 + (4n – 2)(log (5n + 3)), …

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

An intuitive example Suppose the running time of algorithm A and B are 2n and n12 respectively For small input size A performs better (n=10) For n=100 10012 is comparable to the number of molecules in a teaspoon of water 2100 is comparable to the number of molecules in a backyard swimming pool. For n=1000 100012 is comparable is comparable to the number of molecules in a lake 21000 is already many orders of magnitudes greater than the number of particles in the whole universe!!

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

Big O notation Note that Big O notation represents an upper bound of a cost function E.g T(n)=3n+5 is of O(n), O(n2), O(n.log(n)), O(2n) However we usually use the tightest one.

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 21000 is of O(1)

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 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