Document Sample

Algorithms and Complexity Theory UKZN - Computer Science - 2009 ALGORITHMS and COMPLEXITY THEORY Lecture 3 Algorithms Analysis Lecture 3: Algorithms Analysis 1 Algorithms and Complexity Theory UKZN - Computer Science - 2009 Running time and elementary operation • The running time of an algorithm is affected by many factors: input data, compiler, programming language, etc. • Exact time calculation : difﬁcult! • it is often preferable to count elementary operations. • The growth rate of the time or space required is generally used to classify algorithms provided that the size of input data is large. Lecture 3: Algorithms Analysis 2 Algorithms and Complexity Theory UKZN - Computer Science - 2009 Types of Analysis Let T (n) be the running time of an algorithm. 1. Worst case: It is the upper bound of its running time for any input. T (n) is the maximum time taken over all inputs of size n. 2. Average case: It is the expected running time, given some probability distribution on the input. T (n) is the average time taken over all inputs of size n. 3. Amortized: It is the running time for a series of executions, divided by the number of executions. Lecture 3: Algorithms Analysis 3 Algorithms and Complexity Theory UKZN - Computer Science - 2009 Detailed model of computing In the process evaluation the running time of an algorithm we will assume the following: • fetching time ( τf etch ) and storing time (τstore ) are constant. e.g: y = x has running time τf etch + τstore • elementary arithmetic operations time and comparison time are all constant. These times are denoted by τ+ , τ− , τx , τ÷ , and τ< . e.g: y = x + 2 has running time 2τf etch + τ+ + τstore • The time required to call a function is constant, τcall , and the time required to return from a method is a constant , τreturn . • The time required to pass an argument is same as storing time. e.g: y = f (x) has running time τf etch + 2τstore + τcall + Tf (x) where Tf (x) is the running time of f for the entry x. • The time required for the address calculation implied by an array subscribing operation, for example, a[i], is constant, τ[.] . This time does not include the time to compute the subscript expression, nor does it include the time to access (i.e., fetch or store) the array element. e.g: y = a[i] has running time 3τf etch + τstore + τ[.] Lecture 3: Algorithms Analysis 4 Algorithms and Complexity Theory UKZN - Computer Science - 2009 Example Compute the running time of the following algorithm and give its growth rate. 1. public class Example 2. { 3. public static int sum(int n) 4. { 5. int result = 0; 6. for(int i = 1 ; i <= n;++i) 7. result += i; 8. return result 9. } 10. } Statement 5 6a 6b 6c 7 8 TOTAL Time τf etch + τstore τf etch + τstore (2τf etch + τ< ) × (n + 1) (2τf etch + τ+ + τstore ) × n (2τf etch + τ+ + τstore ) × n τf etch + τreturn (6τf etch + 2τstore + τ< + 2τ+ ) × n +(5τf etch + 2τstore + τ< + τreturn ) Code result = 0 i=1 i <= n ++i result +=i return result Lecture 3: Algorithms Analysis 5 Algorithms and Complexity Theory UKZN - Computer Science - 2009 Example (cont’d) • The running time is then T (n) = t1 + t2 n where t1 = 6τf etch + 2τstore + τ< + 2τ+ and t2 = 5τf etch + 2τstore + τ< + τreturn • T (n) = O(n) Lecture 3: Algorithms Analysis 6 Algorithms and Complexity Theory UKZN - Computer Science - 2009 Simpliﬁed model of computing • In the detailed model of computing we have a certain number of parameters. • In real machine each of these parameters is a multiple of the basic clock period of the machine. • If T is the clock period , for example τstore = kstore T where kstore ∈ Z, kstore > 0. • The simpliﬁed model consider that all parameters are expressed in units of clock cycles means T=1 and the proportionality constant ,k ( kstore for store) is assume to be the same for all timing parameters. Lecture 3: Algorithms Analysis 7 Algorithms and Complexity Theory UKZN - Computer Science - 2009 Example Compute the running time of the following algorithm and give its growth rate using a simpliﬁed model of computing. 1. public class Example 2. { 3. public static int sum(int n) 4. { 5. int result = 0; 6. for(int i = 1; i <= n;++i) 7. result += i; 8. return result 9. } 10. } Statement 5 6a 6b 6c 7 8 TOTAL Time 2 2 3 × (n + 1) 4×n 4×n 2 11 × n + 9 Code result = 0 i=1 i <= n ++i result +=i return result T (n) = 11n + 9 = O(n) Lecture 3: Algorithms Analysis 8 Algorithms and Complexity Theory UKZN - Computer Science - 2009 General rules of evaluation 1. For Loop The running time of a for loop is at most the running time of the statements inside the for loop ( including tests ) times the number of iteration. for(i =1;i<=n;i++) Instr; if the Instr runs in constant time, then the running time of the loop T (n) = O(n) 2. Nested For loops Total running time of a statement inside a group of nested for loops is the running time of the statement multiply by the product of sizes of all loops. 3. Consecutive statements For the consecutive statements add the running times of the statements 4. IF/THEN fragment IF cond THEN S1 ELSE S2 Running time of an IF/THEN statement is never more than the running time of the test plus the larger running time of S1 and S2. Lecture 3: Algorithms Analysis 9 Algorithms and Complexity Theory UKZN - Computer Science - 2009 Comparing Algorithms comparison of two algorithms which compute ex public class example { public static float expo1(float x,int n) { float S =1; for(int i=1; i<=n;i++) { float p = 1; for(int j=1; j<=i;j++) p = p*(x/j); S = S+p; } return(S); } } The running time is then of expo1 is T1 (n) = 9 + 49 n + 13 n2 2 2 hence T1 (n) is θ(n2 ) expo2 has a running time T2 (n) = 11 + 17n then T2 (n) is θ(n), which shows an improvement of expo1 by expo2 . 1+ x 1! + x2 2! + ... x n! n public class example { public static float expo2(float x,int n) { float S =1; float p =1; for(int i=1; i<=n;i++) { p = p*(x/i); S = S+p; } return(S); } } Lecture 3: Algorithms Analysis 10 Algorithms and Complexity Theory UKZN - Computer Science - 2009 Your To do List • Read your notes Chapter 1: Introduction - sections: 4. Running Time Calculations. 5. Spatial complexity. Lecture 3: Algorithms Analysis 11

DOCUMENT INFO

Description:
Lecture 3

OTHER DOCS BY monkey6

How are you planning on using Docstoc?
BUSINESS
PERSONAL

By registering with docstoc.com you agree to our
privacy policy and
terms of service, and to receive content and offer notifications.

Docstoc is the premier online destination to start and grow small businesses. It hosts the best quality and widest selection of professional documents (over 20 million) and resources including expert videos, articles and productivity tools to make every small business better.

Search or Browse for any specific document or resource you need for your business. Or explore our curated resources for Starting a Business, Growing a Business or for Professional Development.

Feel free to Contact Us with any questions you might have.