Loops, Summations, Order Reduction
Chapter 2 Highlights
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?
nnn
x
1
33...333
Linear-time Loop
for x = 1 to n {
constant-time operation
}
Note: Constant-time mean independent of the input size.
n
x
n1
1...11111
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.
1
0
1...11111n
x
n
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
1
0
1
0
1n
x
n
y
nnn
1
0
n
x
n 2nnn
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
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 + n2 + n
= n2 + 2n
Non-trivial loops
for x = 1 to n
for y = 1 to x
constant-time operation
Note: x is controlling the inner loop.
n
x
x
y1 1
1
n
x
x1 2
)1(
nn
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.
Order reduction
• Given the following function
• Constants don’t matter
• Only the leading exponent matters
• Thus
2
2 nn
nn 2
22
2n
nn
Order reduction
• Given the following function
• Constants don’t matter
• Only the leading exponent matters
1552
642
423
nn
nnn
nn
nnn
2
423
22
4
nn
n
Example
for z = 1 to n
for y = 1 to z
for x = 1 to y
constant-op
Example
for z = 1 to n
for y = 1 to z
for x = 1 to y
constant-op
y
x
y1
1
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
y
x
y1
1
Example
for z = 1 to n
for y = 1 to z
y operations
z
y
y1
Example
for z = 1 to n
for y = 1 to z
y operations
n
z
z
y
y1 1
Example
for z = 1 to n
z(z+1)/2 operations
n
z
zz
1 2
)1(
Example
for z = 1 to n
z(z+1)/2 operations
n
z
zz1
2
2
1
Example
for z = 1 to n
z(z+1)/2 operations
n
z
n
z
zz1 1
2
2
1
Example
for z = 1 to n
z(z+1)/2 operations
n
z
nnz
1
2
2
)1(
2
1
Example
for z = 1 to n
z(z+1)/2 operations
2
)1(
6
)12)(1(
2
1 nnnnn
Example
for z = 1 to n
z(z+1)/2 operations
26
22
2
1 2223 nnnnnn
Example
for z = 1 to n
z(z+1)/2 operations
6
33
6
32
2
1 223 nnnnn
Example
for z = 1 to n
z(z+1)/2 operations
6
462
2
1 23 nnn
Example
for z = 1 to n
z(z+1)/2 operations
32323
6
23
12
462n
nnnnnn
Proof By Induction
Prove the following:
6
32
6
)12)(1( 23
1
2 nnnnnnx
n
x
Another Example
if (n is odd) {for x = 1 to n
for y = x to n1 operation
}else {
for x = 1 to n/2for y = 1 to n/2
1 operation}
Another Example
if (n is odd) {for x = 1 to n
for y = x to n1 operation
}else {
for x = 1 to n/2for y = 1 to n/2
for z = 1 to n/21 operation
}
n
x
n
xy1
1
n
x
x
y1 1
1
n
x
x1
2
2
)1(n
nn
2/
1
2/
1
2/
1
1n n n
2/
1
2/
1 2
n n n
2/
1
22/
1 422
nn nnn
8
3n
Another Example
if (n is odd) {for x = 1 to n
for y = x to n1 operation
}else {
for x = 1 to n/2for y = 1 to n/2
for z = 1 to n/21 operation
}
2
2
)1(n
nn
33
8n
n
Best Case
Worst Case
Average Case
33232
2
1
2
1nnnnn
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
Top Related