Download presentation
Presentation is loading. Please wait.
Published bySamantha McKinney Modified over 8 years ago
1
LECTURE 2 : fundamentals of analysis of algorithm efficiency Introduction to design and analysis algorithm 1
2
Theoretical analysis of time efficiency(time complexity) Time efficiency is analyzed by determining the number of repetitions of the basic operation as a function of input size Basic operation: the operation that contributes most towards the running time of the algorithm T(n) ≈ c op C(n) n:input size C(n):Number of times basic operation is executed T(n) running time C op: constants T(n) ≈ C(n) 2
3
Example 2.1:for the following code a- what is the basic operation? b- what is C(n)? d-what is the running time ? 3
4
Input size and basic operation examples Problem Input size measure Basic operation Searching for key in a list of n items Number of list’s items, i.e. n Key comparison Multiplication of two matrices Matrix dimensions or total number of elements Multiplication of two numbers Checking primality of a given integer n n’size = number of digits (in binary representation) Division Typical graph problem #vertices and/or edges Visiting a vertex or traversing an edge
5
Order of growth Most important: Order of growth within a constant multiple as n→∞ Example 2.2: page 45 in book. 5
6
Function of Growth rate 6
7
Comparer growth function 7
8
8
9
9
10
Example 2.3: what is the ordered of growth for following running time a. 5log 2 n B.√n c. 2n 3 +5n d.2n f.10 2 n 10
11
Best-case, average-case, worst-case For some algorithms efficiency depends on form of input: Worst case: C worst (n) – maximum over inputs of size n Best case: C best (n) – minimum over inputs of size n Average case: C avg (n) – “average” over inputs of size n – Number of times the basic operation will be executed on typical input – NOT the average of worst and best case 11
12
Example2.4: Sequential search What is Worst case Best case Average case
13
Asymptotic order of growth A way of comparing functions that ignores constant factors and small input sizes O(g(n)): class of functions f(n) that grow no faster than g(n) Θ (g(n)): class of functions f(n) that grow at same rate as g(n) Ω (g(n)): class of functions f(n) that grow at least as fast as g(n) 13
14
Example 2.5: n ∈ O(n 2 ) → n =0 n 2 ∈ Ω(3n 3 ) → n 2 =1 1/2n(n-1) ∈ (n 2 ) →1/4 n 2 <=1/2n(n-1<= 1/2 n 2 14
15
Big-oh 15
16
Big-omega 16
17
Big-theta 17
18
Establishing order of growth using the definition O –notation: Definition: f(n) is in O(g(n)) if order of growth of f(n) ≤ order of growth of g(n) (within constant multiple), i.e., there exist positive constant c and non-negative integer n 0 such that f(n) ≤ c g(n) for every n ≥ n 0 Examples 2.6 : prove that (by definition) 10n is O(5n 2 ) 5n+20 is O(n) 18
19
Establishing order of growth using limits lim T(n)/g(n) = 0T(n)g(n) 0 order of growth of T(n) < order of growth of g(n) c > 0T(n)g(n) c > 0 order of growth of T(n) = order of growth of g(n) ∞T(n)g(n) ∞ order of growth of T(n) > order of growth of g(n) Examples 2.7: what is the asymptotic relation (by limits) Examples 2.7: what is the asymptotic relation (by limits) 10n vs. n 2 1- 10n vs. n 2 n(n+1)/2 vs. n 2 2 - n(n+1)/2 vs. n 2 n→∞ 19
20
Orders of growth of some important functions All logarithmic functions log a n belong to the same class (log n) no matter what the logarithm’s base a > 1 is Ex: All polynomials of the same degree k belong to the same class: a k n k + a k-1 n k-1 + … + a 0 (n k ) Ex: 20
21
Time efficiency of nonrecursive algorithms General Plan for Analysis Decide on parameter n indicating input size Identify algorithm’s basic operation Set up a sum for the number of times the basic operation is executed Simplify the sum using standard formulas and rules) 21
22
Useful summation formulas and rules 22
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.