Loops, Summations, Order Reduction - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Loops, Summations, Order Reduction

Description:

... of the input size. Linear-time Loop. for ... N (or n) used to to characterize input size. Constants are not a factor. Only the leading exponent matters ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 32
Provided by: ebre
Learn more at: http://www.cs.siena.edu
Category:

less

Transcript and Presenter's Notes

Title: Loops, Summations, Order Reduction


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 Dont 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 operationf(n) n3
  • The number of nested loops determines the
    exponent

7
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

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 n2 n
  • n2 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 dont matter
  • Only the leading exponent matters
  • Thus

12
Order reduction
  • Given the following function
  • Constants dont matter
  • Only the leading exponent matters

13
Example
0
  • for z 1 to n
  • for y 1 to z
  • for x 1 to y
  • constant-op

14
Example
0
  • for z 1 to n
  • for y 1 to z
  • for x 1 to y
  • constant-op

15
Example
0
  • 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
0
  • for z 1 to n
  • for y 1 to z
  • y operations

17
Example
0
  • for z 1 to n
  • for y 1 to z
  • y operations

18
Example
0
  • for z 1 to n
  • z(z1)/2 operations

19
Example
0
  • for z 1 to n
  • z(z1)/2 operations

20
Example
0
  • for z 1 to n
  • z(z1)/2 operations

21
Example
0
  • for z 1 to n
  • z(z1)/2 operations

22
Example
0
  • for z 1 to n
  • z(z1)/2 operations

23
Example
0
  • for z 1 to n
  • z(z1)/2 operations

24
Example
0
  • for z 1 to n
  • z(z1)/2 operations

25
Example
0
  • for z 1 to n
  • z(z1)/2 operations

26
Example
0
  • for z 1 to n
  • z(z1)/2 operations

27
Proof By Induction
0
  • 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
0
  • 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
0
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
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.
Write a Comment
User Comments (0)
About PowerShow.com