1 / 9

Algorithm Efficiency

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.

trapper
Download Presentation

Algorithm Efficiency

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 n=1000 i=1 loop (i<=1000) application code i=i+1 end loop f(n)=n

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

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

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

  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 end loop 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)

More Related