Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithm Efficiency Algorithm efficiency

Similar presentations


Presentation on theme: "Algorithm Efficiency Algorithm efficiency"— Presentation transcript:

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

2 Algorithm Efficiency Linear loop i=1 loop (i<=1000)
iteration Value of i 1 2 3 1000 n=1000 i=1 loop (i<=1000) application code i=i+1 end loop f(n)=n

3 Algorithm Efficiency Logarithmic loop Multiply i=1 loop (i<1000)
iteration Value of i 1 2 3 4 8 5 16 6 32 7 64 128 9 256 10 512 Exit 1024 Logarithmic loop Multiply 2 iteration < 1000 i=1 loop (i<1000) application code i=i x 2 end loop f(n)= log2 n

4 Algorithm Efficiency Logarithmic loop Divide i=1000 loop (i>=1)
iteration Value of i 1 1000 2 500 3 250 4 125 5 62 6 31 7 15 8 9 10 Exit Logarithmic loop Divide 1000 /2 iteration >=1 i=1000 loop (i>=1) application code i=i / 2 end loop f(n)= log2 n

5 Algorithm Efficiency Nested loop Linear logarithmic i=1
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 10 f(n)= n [log2 n] log2 10

6 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 10 f(n)= n2 10

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

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

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


Download ppt "Algorithm Efficiency Algorithm efficiency"

Similar presentations


Ads by Google