100 likes | 272 Views
Algorithm Efficiency. Algorithm efficiency A function of the number of elements to be processed f(n)=efficiency If a function is linear – that is, if it contains no loop – then its efficiency is a functions of the number of instructions it contains.
E N D
Algorithm Efficiency • Algorithm efficiency • A function of the number of elements to be processed • f(n)=efficiency • If a function is linear – that is, if it contains no loop – then its efficiency is a functions of the number of instructions it contains. • On the other hand, functions that loop vary widely in their efficiency.
Algorithm Efficiency • Linear loop n=1000 i=1 loop (i<=1000) application code i=i+1 end loop f(n)=n
Algorithm Efficiency • Logarithmic loop • Multiply 2 iteration < 1000 i=1 loop (i<1000) application code i=i x 2 end loop f(n)= log2 n
Algorithm Efficiency • Logarithmic loop • Divide 1000 /2 iteration >=1 i=1000 loop (i>=1) application code i=i / 2 end loop f(n)= log2 n
Algorithm Efficiency • Nested loop • Iterations = inner loop iterations x outer loop iterations • Linear logarithmic i=1 loop (i<=10) j=1 loop (j<=10) application code j= j x 2 end loop i=i + 1 end loop 10 f(n)= n [log2 n] log2 10
Algorithm Efficiency • Nested loop • Quadratic i=1 loop (i<=10) j=1 loop (j<=10) application code j= j +1 end loop i=i + 1 end loop 10 f(n)= n2 10
Big-O Notation The simplification of efficiency is known as big-O analysis. We don’t need to determine the complete measure of efficiency, only the factor that determines the magnitude. This factor is the big-O, and expressed as O(n) – that is, on the order of n.
Big-O Notation • The big O notation can be derived from f(n) using the following steps: • In each term, set the coefficient of the term to 1. • Keep the largest term in the function and discard the others. Terms are ranked from lowest to highest as shown below: Log2n n nlog2n n2 n3… nk 2n n!
Big-O Notation • Example: • Calculate the big-O notation for: • f(n) = n [(n+1)/2] Solution: ½ n2 + ½ n (Remove the coefficient) n2 + n (keep the largest term) n2 So, the big-O notation is stated as O( f(n) ) = O(n2)
Complexity analysis • The same problems can frequently be solved with algorithms that differ in efficiency. • To compare the efficiency of algorithms, a measure of the degree of difficulty of an algorithm called computational complexity was developed by JurisHartmains and Richard E. Stearns. • Computational complexity indicates how much effort is needed to apply an algorithm or how costly it is. • The two efficiency criteria: • Time • Space • The factor of time is usually more important than that of space. • To evaluate an algorithm’s efficiency, real time units such as microseconds and nanoseconds should not be used. • Logical unit that express a relationship between the size n of a file or an array and the amount of time t required to process the data should be used.