Summations COSC 3101, PROF. J. ELDER 2 Recall: Insertion Sort.
Loops, Summations, Order Reduction Chapter 2 Highlights.
-
date post
21-Dec-2015 -
Category
Documents
-
view
222 -
download
0
Transcript of Loops, Summations, Order Reduction Chapter 2 Highlights.
![Page 1: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/1.jpg)
Loops, Summations, Order Reduction
Chapter 2 Highlights
![Page 2: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/2.jpg)
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
![Page 3: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/3.jpg)
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
![Page 4: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/4.jpg)
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
![Page 5: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/5.jpg)
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
![Page 6: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/6.jpg)
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
![Page 7: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/7.jpg)
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
![Page 8: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/8.jpg)
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
![Page 9: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/9.jpg)
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
![Page 10: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/10.jpg)
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.
![Page 11: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/11.jpg)
Order reduction
• Given the following function
• Constants don’t matter
• Only the leading exponent matters
• Thus
2
2 nn
nn 2
22
2n
nn
![Page 12: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/12.jpg)
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
![Page 13: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/13.jpg)
Example
for z = 1 to n
for y = 1 to z
for x = 1 to y
constant-op
![Page 14: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/14.jpg)
Example
for z = 1 to n
for y = 1 to z
for x = 1 to y
constant-op
y
x
y1
1
![Page 15: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/15.jpg)
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
![Page 16: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/16.jpg)
Example
for z = 1 to n
for y = 1 to z
y operations
z
y
y1
![Page 17: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/17.jpg)
Example
for z = 1 to n
for y = 1 to z
y operations
n
z
z
y
y1 1
![Page 18: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/18.jpg)
Example
for z = 1 to n
z(z+1)/2 operations
n
z
zz
1 2
)1(
![Page 19: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/19.jpg)
Example
for z = 1 to n
z(z+1)/2 operations
n
z
zz1
2
2
1
![Page 20: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/20.jpg)
Example
for z = 1 to n
z(z+1)/2 operations
n
z
n
z
zz1 1
2
2
1
![Page 21: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/21.jpg)
Example
for z = 1 to n
z(z+1)/2 operations
n
z
nnz
1
2
2
)1(
2
1
![Page 22: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/22.jpg)
Example
for z = 1 to n
z(z+1)/2 operations
2
)1(
6
)12)(1(
2
1 nnnnn
![Page 23: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/23.jpg)
Example
for z = 1 to n
z(z+1)/2 operations
26
22
2
1 2223 nnnnnn
![Page 24: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/24.jpg)
Example
for z = 1 to n
z(z+1)/2 operations
6
33
6
32
2
1 223 nnnnn
![Page 25: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/25.jpg)
Example
for z = 1 to n
z(z+1)/2 operations
6
462
2
1 23 nnn
![Page 26: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/26.jpg)
Example
for z = 1 to n
z(z+1)/2 operations
32323
6
23
12
462n
nnnnnn
![Page 27: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/27.jpg)
Proof By Induction
Prove the following:
6
32
6
)12)(1( 23
1
2 nnnnnnx
n
x
![Page 28: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/28.jpg)
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}
![Page 29: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/29.jpg)
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
![Page 30: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/30.jpg)
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
![Page 31: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/31.jpg)
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.
![Page 32: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/32.jpg)
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.
![Page 33: Loops, Summations, Order Reduction Chapter 2 Highlights.](https://reader035.fdocuments.in/reader035/viewer/2022062313/56649d605503460f94a41b05/html5/thumbnails/33.jpg)
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