loops, summations, order reduction chapter 2 highlights

33
Loops, Summations, Order Reduction Chapter 2 Highlights

Post on 21-Dec-2015

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Loops, Summations, Order Reduction Chapter 2 Highlights

Loops, Summations, Order Reduction

Chapter 2 Highlights

Page 2: 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

Page 3: Loops, Summations, Order Reduction Chapter 2 Highlights

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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