Download presentation
Presentation is loading. Please wait.
1
Loops, Summations, Order Reduction Chapter 2 Highlights
2
Constant times Linear 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?
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 operation f(n) = n 3 The number of nested loops determines the exponent
7
4-Nested Loops n 4 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 operation f(n) = n 4
8
Add independent loops 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 + n 2 + n = n 2 + 2n
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
Equivalent Loops 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 Note: These two loops behave differently, but They perform the same number of basic operations.
11
Order reduction Given the following function Constants don’t matter Only the leading exponent matters Thus
12
Order reduction Given the following function Constants don’t matter Only the leading exponent matters
13
Example for z = 1 to n for y = 1 to z for x = 1 to y constant-op
14
Example for z = 1 to n for y = 1 to z for x = 1 to y constant-op
15
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 y operations
16
Example for z = 1 to n for y = 1 to z y operations
17
Example for z = 1 to n for y = 1 to z y operations
18
Example for z = 1 to n z(z+1)/2 operations
19
Example for z = 1 to n z(z+1)/2 operations
20
Example for z = 1 to n z(z+1)/2 operations
21
Example for z = 1 to n z(z+1)/2 operations
22
Example for z = 1 to n z(z+1)/2 operations
23
Example for z = 1 to n z(z+1)/2 operations
24
Example for z = 1 to n z(z+1)/2 operations
25
Example for z = 1 to n z(z+1)/2 operations
26
Example for z = 1 to n z(z+1)/2 operations
27
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
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
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 } Best Case Worst Case Average 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.