Reference: Tremblay and Cheston: Section 5.1 T IMING A NALYSIS.

Slides:



Advertisements
Similar presentations
Discrete Structures CISC 2315
Advertisements

HST 952 Computing for Biomedical Scientists Lecture 10.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 3 Growth of Functions
1 ICS 353 Design and Analysis of Algorithms Spring Semester (062) King Fahd University of Petroleum & Minerals Information & Computer Science.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Complexity Analysis (Part I)
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
Cmpt-225 Algorithm Efficiency.
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
The Efficiency of Algorithms
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithm.
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
Instructor Neelima Gupta
Asymptotic Growth Rates Themes –Analyzing the cost of programs –Ignoring constants and Big-Oh –Recurrence Relations & Sums –Divide and Conquer Examples.
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure.
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.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Analysis Tools Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Mathematics Review and Asymptotic Notation
Iterative Algorithm Analysis & Asymptotic Notations
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
CSC 205 Java Programming II Algorithm Efficiency.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Divide & Conquer  Themes  Reasoning about code (correctness and cost)  recursion, induction, and recurrence relations  Divide and Conquer  Examples.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Asymptotic Growth Rates  Themes  Analyzing the cost of programs  Ignoring constants and Big-Oh  Recurrence Relations & Sums  Divide and Conquer 
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.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
ADVANCED ALGORITHMS REVIEW OF ANALYSIS TECHNIQUES (UNIT-1)
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
Algorithm Analysis: Running Time Big O and omega ( 
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
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.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Algorithm Analysis 1.
Analysis of Algorithms
Analysis of Algorithms
Introduction to Algorithms
CS 3343: Analysis of Algorithms
Introduction to Algorithms Analysis
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
Programming and Data Structure
Analysis of Algorithms
Programming and Data Structure
CSE 1342 Programming Concepts
At the end of this session, learner will be able to:
Analysis of Algorithms
Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases.
Presentation transcript:

Reference: Tremblay and Cheston: Section 5.1 T IMING A NALYSIS

Quality of a project/method is determined by many factors: Simplicity Readability Verifiability Correctness: yields the correct results according to its specification Validity Solves the original problem Robustness Ability to handle errors Modifiability Reusability Portability Ease in moving the project/method to another machine, system, language, etc Integrity Able to withstand unauthorized access Efficiency Time Space In this segment, the focus is on measuring time efficiency. It is an important measure of quality, but certainly not the most important.

Time efficiency estimate the order of magnitude for the time requirements usually expressed a rate of growth The rate of growth is expressed as a function that is dependent upon the size of the problem, the amount of data, and/or the size of structures involved. Example public static int count(String[ ] a, String s) { int count = 0; int i = 0; while (i < a.length) { if (s.equals(a[i])) count = count + 1; i = i + 1; } return count; }

I. Statement count approach If all statements take about the same amount of time then count the number of statements executed and use it as a measure of the amount of time required to execute the algorithm. Usually a reasonable approach, provided that there are no method calls. For the count method, what determines the size of the problem? how many statements are executed?

Size of the problem for the count method: size of the array Let n = the size of the array Let T count (n) = number of statements executed in running method count on an array of size n T count (n) = 3n + q + 4 where q equals the number of times “count = count + 1” is done Best case: q = 0 T count B (n) = 3n + 4 Worst case: q = n T count W (n) = 4n + 4 Average/expected case: ??

Notation Definition: O(n) = { function t |  c, n 0 >0, such that t(n) ≤ c*n,  n ≥ n 0 } Intuitively: O(n) is the set of functions which grow linearly or less than linearly O(n) = { n, 5n, 100n , log(n), 2n + 500*log(n), (log(n)) 2, 5, , … } Note that the constant on the fastest growing term doesn’t matter and terms with slower growth don’t matter. Note that T count (n)  O(n) Alternate notation: T count (n) = O(n) Alternate definition: t(n)  O(n) if lim t(n) <  n  n Note that L’Hopitals rule is often needed if f(n)  and g(n)  as n , then lim n  f(n)/g(n) = lim n  f (n)/g (n)

II. Statement count approach Determine an operation that is done is often as any other operation (within a constant factor) and is central to the algorithm. This operation is called an active operation. Count the number of times that this operation is done, and use it as a measure of the order of magnitude of the time requirements of the algorithm. What makes a good active operation for the count method?

The active operation for the count method is s.equals(a[i]) Let K(n) = number of active operations done by the count method for an array of size n It is easy to see that K(n) = n Note that K(n)  O(n) The active operation approach is usually much easier and yields the same result. The key is the selection of the active operation. If it isn’t clear what is the active operation, include the count for each possible active operation and add them together. To sure to include the active operations in methods called.

Summation notation: Reference Appendix C.1 of text  i=1 n x i = x 1 + x 2 + x 3 + … + x n by definition Summation manipulation:  i=1 n x i =  i=1 k-1 x i + x k + x k+1 +  j=k+2 n x j  i=1 n (2x i + 3) =  i=1 n 2x i +  i=1 n 3 = 2*  i=1 n x i +  i=1 n 3 = 2*  i=1 n x i + 3*n Useful summation formulae:  i=1 n i = n(n+1)/2  i=1 n i 2 = n(n+1)(2n+1)/6  i=0 n a i = (a n+1 – 1)/(a – 1) for a ≠ 1  i=1 n i*a i = na n+1 /(a-1) + a(a n – 1)/(a – 1) 2 for a ≠ 1 (each of these can be proved by mathematical induction)

Insertion sort algorithm General algorithm: For each item to be sorted insert it into its proper place relative to previous items i.e., move larger items one position further from the front public static > void insertionSort(T[ ] a) { int i = 1; while (i < a.length) { T temp = a[i]; int j = i - 1; while (j >= 0 && temp.compareTo(a[j]) < 0) { a[j+1] = a[j]; j = j - 1; } a[j+1] = temp; i = i + 1; }

Timing analysis using statement count approach: Let T insort W (n) = worst case time for the insertionSort algorithm for an array of size n T insort W (n) =  i=1 n -1 (  j=i -1 0 (Decreasing) ( ) ) = 2 +  i=1 n -1 ( 4 +  j=0 i ) = 2 +  i=1 n -1 (6 + 3*i) = 2 +  i=1 n  i=1 n -1 3*i = 2 + 6*(n-1) + 3*  i=1 n -1 i = 2 + 6*(n-1) + 3*(n-1)(n)/2 = 2 + 6n /2*n 2 - 3/2n = 1.5 n n – 4 Note that T insort W (n)  O(n)

Definition For f(n) : I +  R + where I + is the set of positive integers and R + is the set of positive real values O(f(n)) = { function t |  c, n 0 >0, such that t(n) ≤ c*f(n),  n ≥ n 0 } Intuitively: O(f(n)) is the set of functions which grow no faster than f(n). If t(n)  O(f(n)), then t(n) is in the order of f(n), or more simply, t(n) is order f(n). Alternate definition: t(n)  O(f(n)) if lim n  t(n)/f(n) <  T insort W (n) = 1.5 n n – 4  O(n 2 ) 50 n  O(n 2 ) n*log(n)  O(n 2 ) (log(n)) j  O(n) for j <  n 5  O(2 n ) 2 n  O(n!) n!  O(n n )

Definition For f(n) : I +  R + where I + is the set of positive integers and R + is the set of positive real values  (f(n)) = { function t |  b, c, n 0 >0, such that b*f(n) ≤ t(n) ≤ c*f(n),  n ≥ n 0 } If t(n)   (f(n)), then t(n) is in the exact order of f(n). Alternate definition: t(n)   (f(n)) if lim n  t(n)/f(n) = c, 0 < c <  T count W (n) = 4n + 4   (n) T insort W (n)   (n 2 ) T count W (n)   (n 2 ) Often = and ≠ are used instead of  and .

Action operation analysis of insertion sort algorithm Active operation j >= 0 && temp.compareTo(a[j]) < 0 T insort W (n) =  i=1 n -1 ( 1 +  j=i -1 0 (Decreasing) (1)) =  i=1 n -1 ( 1 + i ) = (n – 1) + (n – 1)(n)/2 = n 2 /2 + n/2 – 1   (n 2 ) T insort B (n) =  i=1 n -1 ( 1 ) = n – 1   (n) T insort E (n) =  i=1 n -1 ( 1 + (1/2)  j=i -1 0 (Decreasing) (1)) assuming usually insert ½ way back ~ n 2 /4 + 3n/4   (n 2 )

Does rate of growth make a difference, or are computers so fast that any algorithm can be done quickly? Consider how large a problem can be solved in one minute using algorithms of different time complexity: Linear algorithm: 1,000,000,000 Quadric algorithm: 50,000 Factorial algorithm (n!): 11 Some algorithms take huge amounts of time on even very small problems. Even the difference between linear and quadratic can make a big difference.

Combining growth functions For sets A and B and element c, define the following: A + B = { a + b | a  A and b  B} A*B = { a * b | a  A and b  B} c * B = { c * b | b  B} Using these operations, the following are implied: O(f(n)) + O(g(n)) = O(f(n) + g(n)) k * O(f(n)) = O(k*f(n)) = O(f(n)) for k a constant O(f(n)) + O(g(n)) = O(2*max(f(n), g(n))) = O(max(f(n), g(n)))

More elaborate example: Given int method k(int i) with time T k (m) = O(log(m)) for some m independent of i. Given void method p with time T p (n) = O(n 2 ) for some n independent of m. What is the time requirement for each of the following methods: public void q() { int c = 0; for (int i = 1; i < n + 1; i++) c = c + k(i); } public void r() { q(); for (int i = 1; i < m; i = 2*i) p(); s(); } public void s() { int x = 0; for (int i = 0; i < m + 1; i++) x = x + k(i)*k(2*m - i); System.out.println(x); }

Analysis of method q: active operation: c = c + k(i) number of times that it is done: n cost of doing the active operation: O(log(m)) total cost: n*O(log(m)) = O(n*log(m)) Analysis of method s: active operation: x = x + k(i)*k(2*m - i) number of times that it is done: m + 1 cost of doing the active operation: 2*O(log(m)) total cost: (m + 1)*2*O(log(m)) = O(m*log(m))

T q (n, m) = n*O(log(m)) = O(n*log(m)) T s (m) = m*2*O(log(m)) = O(m*log(m)) Analysis of method r: Three possible active operations, so analyze all three: active operation: q() number of times that it is done: 1 cost of doing the active operation: O(n*log(m)) cost for q: O(n*log(m)) active operation: s() number of times that it is done: 1 cost of doing the active operation: O(m*log(m)) cost for s: O(m*log(m)) active operation: p(); number of times that it is done: ?? cost of doing the active operation: O(n 2 ) cost for p: ?? * O(n 2 ) Total cost for r: cost for active op q + cost for active op s + cost for active op p T r (n, m) = O(n*log(m)) + O(n*log(m)) + ?? * O(n 2 )

How many times is the loop in method r done? Let x = number of times that the loop in method r is done Consider the value of i 1, 2, 4, 8, 16, …, 2 x-1, 2 x Consider when the loop is exited; when i  m; i.e., 2 x  m 2 x-1 < m  2 x x-1 < log 2 (m)  x (taking the log to the base 2 of each term) x is log 2 (m) or the next integer value larger than it Therefore x =  log 2 (m)  Notation:  p  = smallest integer greater than or equal to p, called the ceiling function  p  = largest integer less than or equal to p, called the floor function

T r (n, m) = T q (n, m) +  log 2 (m)  *T p (n) + T s (m) = O(n*log(m)) +  log 2 (m)  *O(n 2 ) + O(m*log(m)) = O(n*log(m) + n 2 *log(m) + m*log(m)) = O(n 2 *log(m) + m*log(m)) = O(max(n 2, m)*log(m))

Example What is the time complexity of the method? How many times is the loop done? Let r = number of times that the loop is done static public int f(int n) { int result = 0; double p =n; while (p > 1) { p = p/2; result = result + 1; } return result; }

Consider the values of p n, n/2, n/4, n/8, …, n/2 r-1, n/2 r <= 1 When the method finishes n/2 r <= 1 < n/2 r-1 i.e, n <= 2 r and 2 r-1 < n and taking log to the base 2 of each term r – 1 < log 2 (n) <= r Therefore, since r is an integer r =  log 2 (n)  Thus, f is the function  log 2 (n)  for n>= 1, and T f (n) =  (  log 2 (n)  ).