350 likes | 390 Views
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 {
E N D
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 { constant-time operation } Note: Constant-time mean independent of the input size.
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.
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
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
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
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
Non-trivial loops for x = 1 to n for y = 1 to x constant-time operation Note: x is controlling the inner loop.
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.
Given the following function Constants don’t matter Only the leading exponent matters Thus Order reduction
Given the following function Constants don’t matter Only the leading exponent matters Order reduction
0 Example for z = 1 to n for y = 1 to z for x = 1 to y constant-op
0 Example 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 for x = 1 to y constant-op for z = 1 to n for y = 1 to z y operations 0 Example
0 Example for z = 1 to n for y = 1 to z y operations
0 Example for z = 1 to n for y = 1 to z y operations
0 Example for z = 1 to n z(z+1)/2 operations
0 Example for z = 1 to n z(z+1)/2 operations
0 Example for z = 1 to n z(z+1)/2 operations
0 Example for z = 1 to n z(z+1)/2 operations
0 Example for z = 1 to n z(z+1)/2 operations
0 Example for z = 1 to n z(z+1)/2 operations
0 Example for z = 1 to n z(z+1)/2 operations
0 Example for z = 1 to n z(z+1)/2 operations
0 Example for z = 1 to n z(z+1)/2 operations
0 Proof By Induction Prove the following:
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 }
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 }
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
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.
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.
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