Upgrade to Pro — share decks privately, control downloads, hide ads and more …

lecture05.pdf

Avatar for William Albritton William Albritton
August 13, 2014
170

 lecture05.pdf

Avatar for William Albritton

William Albritton

August 13, 2014
Tweet

Transcript

  1. Memory Upload • Factorial algorithm and corresponding method • Analyze

    the time requirements of algorithms and methods with Big-O • Big-O for factorial algorithm and factorial method • Big-O for grocery list program
  2. Algorithms • An algorithm is the sequence of steps used

    for solving a problem within a finite amount of time. • For example, finding the factorial of a number • 3! = 3 * 2 * 1 = 6 • 4! = 4 * 3 * 2 * 1 = 24 • 5! = 5 * 4 * 3 * 2 * 1 = 120
  3. Factorial in Terms of n • Since we want our

    factorial algorithm to be general (able to solve any factorial problem), we need to write the algorithm in terms of n, where n is the number used as input to the factorial • n! = n * n-1 * n-2 * n-3 * …* 3 * 2 * 1
  4. Algorithm for Factorial 1. If n is negative, return -1

    (error) 2. If n is 0, return 1 (mathematical definition of a factorial) 3. Initialize the product to 1 4. Loop from i = n to i = 1, count down by 1 each loop
  5. Factorial Method • After we write our algorithm, we can

    use the algorithm as a guide to writing our methods. • See FactorialProgram.java for the factorial() method
  6. Algorithm Execution Time • How can we measure the efficiency

    of a particular algorithm?  Count the number of repeated operations  For example, the factorial program has a for loop, which loops n times, where n is the number that the user entered on the command line.
  7. Algorithm Execution Time • The execution time of an algorithm

    depends on the problem size  For small sets of data, different algorithms may take roughly the same amount of time
  8. Algorithm Execution Time • However, for large sets of data,

    the choice of algorithm is particularly important
  9. Algorithm Execution Time • Need to think about an algorithm’s

    growth rate • The growth rate is how fast the algorithm’s execution time increases as the problem size increases
  10. Terminology • Order of an algorithm  A way to

    specify the time requirement of an algorithm in terms of the problem size
  11. Terminology • Big-O notation  Mathematical notation that uses the

    capital letter O to specify the upper bound of an algorithm  For example, “Algorithm A has O(f(n))” or “A(n) = O(f(n))” means that “algorithm A has order f(n)”
  12. Mathematical Definition • Big-O means “A(n) = O(f(n))”  Algorithm

    A(n) is order f(n), if for constants k & n0 (initial value of n), A(n) takes no more than k * f(n) time units to solve a problem of size n ≥ n0
  13. Mathematical Definition • Big-O means “A(n) = O(f(n))”  For

    constants k & n0 (initial value of n), as n gets larger & larger (n ≥ n0 ), then some constant k exists, so that A(n) < k*f(n)
  14. Mathematical Definition • Since k*f(n) is an upper bound on

    the algorithm’s performance/speed, the algorithm will take less time than k*f(n) time units to complete
  15. In Plain English • For a given plan (algorithm), Big-O

    is answering this question: • “How much time will this take in the worst case?”
  16. Line Example • For example, if you are waiting in

    a line for movie tickets, your waiting time is proportional to the number of people in the line  So you will have to wait O(n), where n is the number of people in the line
  17. Big-O Comparisons • Common growth-rate functions in order of increasing

    time requirement 1. O(1) – Constant, so time does not depend on size n – Doubling n leaves the same run time – Processing time is super fast!!!
  18. O(1) Examples • As long as an instruction does not

    loop in some way, then it is O(1) • Each of the instructions below is O(1) int n = 1000000000; //1 billion int x = n; int y = x * 10 + 2 / x – 3 % x; if(x < y){x++;} else{--y;} System.out.println(y);
  19. Big-O Comparisons 2. O(log2 n) – Logarithmic, so time doubles

    for square (power of 2) of n – Doubling n increases the run time by 1 (one) – Processing time is fast, but not as fast as O(1)
  20. O(log2 n) Example • For example, if we have a

    loop in which the counter doubles, then this loop repeats O(log2 n) times int n = 1000000000; //1 billion for(int i=1;i<n;i=i*2){ System.out.println(i);}
  21. O(log2 n) Example • Since Big-O is an upper bound,

    we can still use O(log2 n) for any loop in which the counter is multiplied by a constant, so even if we multiply by 10 instead of 2, we still have O(log2 n) int n = 1000000000; //1 billion for(int i=1;i<n;i=i*10){ System.out.println(i);}
  22. O(log2 n) Example • Since division is the opposite of

    multiplication, loops that divide the counter by a constant are O(log2 n) int n = 1000000000; //1 billion for(int i=n;i>1;i=i/7){ System.out.println(i);} for(int i=n;i>1;i=i/17){ System.out.println(i);}
  23. Why log2 n? • What is r = logb n

    anyway? • log is short for logarithm • Take the base (b) of the log to the result (r) of the log to get number (n) • In other words, logs are directly related to exponents • r = logb n is the same as br = n
  24. Log Examples • Example logs and exponents • log2 n

    = r, because 2r = n • log2 1 = 0, because 20 = 1 • log2 2 = 1, because 21 = 2 • log2 4 = 2, because 22 = 4 • log2 8 = 3, because 23 = 8 • log2 16 = 4, because 24 = 16
  25. Log Examples • As the size (n) of a problem

    doubles, the time (r) only increases by 1
  26. O(1) Example • This loop will repeat 5 times, because

    log2 32 = 5, so i = 1, 2, 4, 8, 16, 32 int n = 1000000000; //1 billion for(int i=1;i<32;i=i*2){ System.out.println(i);} • Note that this loop is O(1), because it is NOT dependent on n
  27. Big-O Comparisons 3. O(n) – Linear, so time increases proportionally

    to n – Doubling n also doubles the run time – Processing time is “average” fast, but not as fast as O(log2 n)
  28. O(n) Example • For example, if we have a loop

    in which the counter increments by 1 (one), then this loop repeats O(n) times int n = 1000000000; //1 billion for(int i=1;i<n;i++){ System.out.println(i);} • Note that i++ is the same as i=i+1
  29. O(n) Example • Likewise, if we have a loop in

    which the counter decrements by 1 (one), then this loop repeats O(n) times int n = 1000000000; //1 billion for(int i=n;i>0;i--){ System.out.println(i);} • Note that i-- is the same as i=i-1
  30. O(n) Example • In general, if you add to or

    subtract from a counter by a constant amount, then the loop repeats O(n) times int n = 1000000000; //1 billion for(int i=0;i<n;i=i+108){ System.out.println(i);}
  31. O(1) Example • Note that this loop is O(1), because

    it is not dependent on n, as it will only loop 108 times int n = 1000000000; //1 billion for(int i=0;i<108;i++){ System.out.println(i)}
  32. O(1) Example • Note that this loop is O(1), because

    it is not dependent on n, as it will only loop 1 time int n = 1000000000; //1 billion for(int i=n;i<=n;i++){ System.out.println(i);}
  33. Big-O Comparisons 4. O(n*log2 n) – Typical of algorithms that

    divide a problem into smaller sub-problems, which are then solved – Slower processing time than O(n), but faster processing time than O(n2)
  34. O(n*log2 n) Example • If we have nested loops, then

    we multiply the Big-O for both loops int n = 1000000000; //1 billion for(int i=1;i<n;i++){ System.out.println(i); for(int j=1;j<n;j=j*2){ System.out.println(j); }//inner loop: O(log2 n) }//outer loop: O(n)
  35. O(n*log2 n) Example • Of course, we can reverse the

    two loops and still have O(n*log2 n) int n = 1000000000; //1 billion for(int i=1;i<n;i=i*2){ System.out.println(i); for(int j=1;j<n;j++){ System.out.println(j); }//inner loop: O(n) }//outer loop: O(log2 n)
  36. Big-O Comparisons 5. O(n2)  Quadratic, so time increases quite

    rapidly with n  Doubling n increases the run time by a factor of four (4) – Slower processing time than O(n*log2 n), but faster processing time than O(n3)
  37. O(n2) Example • Again, if we have nested loops, then

    we multiply the Big-O for both loops int n = 1000000000; //1 billion for(int i=1;i<n;i++){ System.out.println(i); for(int j=1;j<n;j++){ System.out.println(j); }//inner loop: O(n) }//outer loop: O(n)
  38. Big-O Comparisons 6. O(n3)  Cubic, so time increases even

    more rapidly with n  Doubling n increases the run time by a factor of eight (8)  Slower processing time than O(n2), but faster processing time than O(2n)
  39. O(n3) Example • Again, if we have nested loops, then

    we multiply the Big-O for all loops
  40. O(n3) Example • int n = 1000000000; //1 billion for(int

    i=1;i<n;i++){ for(int j=1;j<n;j++){ for(int k=1;k<n;k++){ System.out.println(k); }//2nd inner loop: O(n) }//inner loop: O(n) }//outer loop: O(n)
  41. Big-O Comparisons 7. O(2n)  Exponential, so time increases super-duper

    fast with n  Increasing n by one (1) will double the run time – Processing time for O(2n) is super slow!!!
  42. O(2n) Example • This example simply loops 2n times •

    Later on in the class, we will show more typical examples of O(2n) int n = 1000000000; //1 billion for(int i=0;i<Math.pow(2,n);i++){ System.out.println(i);}
  43. General Rules for Big-O 1. Ignore low-order terms  O(2n

    + n2 + n + 1) = O(2n) 2. Ignore constants when multiplying or dividing  O(5*n3) = O(n3)  O(n3/7) = O(n3)
  44. Big-O for Factorial • What is the Big-O for the

    factorial algorithm?  The factorial algorithm has a loop that repeats n times, where n is the number entered by the user
  45. Big-O for Factorial Algorithm 1. If n is negative, return

    -1 (error) O(1) 2. If n is 0, return 1 (mathematical definition of a factorial) O(1) 3. Initialize the product to 1 O(1) 4. Loop from i = n to i = 1, count down by 1 each loop O(n)
  46. Big-O for Factorial Algorithm • We need to add up

    the Big-O for each separate step, and multiply the Big-O for any steps within a loop  step1 + step2 + step3 + (step4 * step5) + step6  O(1) + O(1) + O(1) + (O(n) * O(1)) + O(1)  = O(n) * O(1) = O(n*1) = O(n)
  47. Grocery List Methods • See GroceryListProgram4.java for grocery list program

    • Method displayMenu()  No looping, so O(1) • Method readFromFile()  The while loop will loop one time for each line in the file, so O(n), where n is the number of lines in the file
  48. Grocery List Methods • Method add()  No looping, so

    O(1) • Method delete()  In the worst case, the user will delete the first item in the list  The for loop will loop from 1 to listSize, so O(n), where n is the size of the list (which is an array)
  49. Grocery List Methods • Method display()  The for loop

    is used to display all of the contents in the array, so O(n), where n is the size of the array • Method writeToFile()  The for loop is used to write all of the contents from the array to the file, so O(n), where n is the size of the array
  50. Timed Example • See MoreBigO.java which counts how many milliseconds

    a loop takes to execute  Try these numbers as commandline input: 10, 100, 1000, etc.  Notice how long the loops take to execute
  51. Memory Defragmenter • Algorithm and corresponding method for factorial function

    • How to use Big-O to analyze time requirements of algorithms and methods • Big-O for factorial algorithm and factorial method • Big-O for the grocery list methods
  52. Task Manager • Before the next class, you need to:

    1.Complete the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture