1 / 33

Loops, Summations, Order Reduction

Loops, Summations, Order Reduction. Chapter 2 Highlights. for x = 1 to n { operation 1; operation 2; operation 3; }. for x = 1 to n constant time operation Note : Constants are not a factor in evaluating algorithms. Why?. Constant times Linear. Linear-time Loop. for x = 1 to n {

joejones
Download Presentation

Loops, Summations, Order Reduction

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. Loops, Summations, Order Reduction Chapter 2 Highlights

  2. for x = 1 to n { operation 1; operation 2; operation 3; } for x = 1 to n constant time operation Note: Constants are not a factor in evaluating algorithms. Why? Constant times Linear

  3. Linear-time Loop for x = 1 to n { constant-time operation } Note: Constant-time mean independent of the input size.

  4. Linear-time Loop for x = 0 to n-1 constant-time operation Note: Don’t let starting at zero throw you off. Brackets not necessary for one statement.

  5. 2-Nested Loops  Quadratic for x = 0 to n-1 for y = 0 to n-1 constant-time operation The outer loop restarts the inner loop

  6. 3-Nested Loops  Cubic for x = 0 to n-1 for y = 0 to n-1 for z = 0 to n-1 constant-time operationf(n) = n3 The number of nested loops determines the exponent

  7. 4-Nested Loops  n4 for x = 0 to n-1 for y = 0 to n-1 for z = 0 to n-1 for w = 0 to n-1 constant-time operationf(n) = n4

  8. for x = 0 to n-1 constant-time op for y = 0 to n-1 for z = 0 to n-1 constant-time op for w = 0 to n-1 constant-time op f(n) = n + n2 + n = n2 + 2n Add independent loops

  9. Non-trivial loops for x = 1 to n for y = 1 to x constant-time operation Note: x is controlling the inner loop.

  10. for x = 1 to n for y = 1 to x constant-time op for x = 1 to n for y = x to n constant-time op Equivalent Loops • Note: • These two loops behave differently, but • They perform the same number of basic operations.

  11. Given the following function Constants don’t matter Only the leading exponent matters Thus Order reduction

  12. Given the following function Constants don’t matter Only the leading exponent matters Order reduction

  13. 0 Example for z = 1 to n for y = 1 to z for x = 1 to y constant-op

  14. 0 Example for z = 1 to n for y = 1 to z for x = 1 to y constant-op

  15. for z = 1 to n for y = 1 to z for x = 1 to y constant-op for z = 1 to n for y = 1 to z y operations 0 Example

  16. 0 Example for z = 1 to n for y = 1 to z y operations

  17. 0 Example for z = 1 to n for y = 1 to z y operations

  18. 0 Example for z = 1 to n z(z+1)/2 operations

  19. 0 Example for z = 1 to n z(z+1)/2 operations

  20. 0 Example for z = 1 to n z(z+1)/2 operations

  21. 0 Example for z = 1 to n z(z+1)/2 operations

  22. 0 Example for z = 1 to n z(z+1)/2 operations

  23. 0 Example for z = 1 to n z(z+1)/2 operations

  24. 0 Example for z = 1 to n z(z+1)/2 operations

  25. 0 Example for z = 1 to n z(z+1)/2 operations

  26. 0 Example for z = 1 to n z(z+1)/2 operations

  27. 0 Proof By Induction Prove the following:

  28. Another Example if (n is odd) { for x = 1 to n for y = x to n 1 operation } else { for x = 1 to n/2 for y = 1 to n/2 1 operation }

  29. 0 Another Example if (n is odd) { for x = 1 to n for y = x to n 1 operation } else { for x = 1 to n/2 for y = 1 to n/2 for z = 1 to n/2 1 operation }

  30. 0 Another Example Best Case if (n is odd) { for x = 1 to n for y = x to n 1 operation } else { for x = 1 to n/2 for y = 1 to n/2 for z = 1 to n/2 1 operation } Average Case Worst Case

  31. Algorithm Analysis Overview • Counting loops leads to summations • Summations lead to polynomials • N (or n) used to to characterize input size • Constants are not a factor • Only the leading exponent matters • Depending on input, algorithm can have different running times.

  32. Average Case • Depending on input algorithm could have a... • Best case: Fastest running time possible • Worst Case: Slowest running time possible • To compute the average case, you need to know how often the best and worst case occur. • This is not always known.

  33. Worst Case • Worst Case is more important. • This is how long you could be waiting • Always best to prepare for the worst • There are always easy input

More Related