Summing Algorithmic Execution - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Summing Algorithmic Execution

Description:

Summing Algorithmic Execution. More big-Oh discussion. Making an Identity Matrix ... Why do small rain drops stay on your car window? Why do large ones roll off? ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 21
Provided by: timoth90
Category:

less

Transcript and Presenter's Notes

Title: Summing Algorithmic Execution


1
Summing Algorithmic Execution
  • More big-Oh discussion

2
Making an Identity Matrix
  • void makeIdentityMatrix (double m)
  • int n m.length
  • for (int i 0 i lt n i)
  • for (int j 0 j lt n j)
  • mij 0.0
  • for (int i 0 i lt n i)
  • mii 1.0

3
Adding asymoptotic times
  • Clearly first loop is O(n2), second loop is O(n).
  • MakeIdentityMatrix is O(n2).
  • Why not O(n2 n)?

4
Toss out everything but the dominant function
  • Remember how we tossed out constant factors?
  • Also toss out everything except the dominate
    function
  • So what functions dominate each other?

5
A hierarchy of Dominance
  • n! factorial
  • 2n exponential
  • nd, - d gt 3 Polynomial n3 Cubic n2 Quadratic
  • n sqrt(n)-no name, just n root n
  • n log n -just n log n
  • n linear
  • sqrt(n) root-n
  • log n Logarithmic
  • 1 constant

6
Justifications for Summing Rule
  • Why do small rain drops stay on your car window?
  • Why do large ones roll off?
  • What are the forces on the drop?

7
Characteristic Curves
  • Each function has a characteristic curve
  • Multiplying by a constant, or adding a constant,
    only makes it larger or smaller, doesn't change
    essential shape

8
A dominate function always wins
  • As n gets large, the characteristic shape of a
    function will always assert itself.
  • The dominant function will always get larger

9
Another Argument
  • Assume we can perform a task every microsecond
    (106 operations per second).
  • Assume that n is 105.
  • How long to execute an algorithm?

10
Function and Running time
  • 2n -more than a century
  • n3  31.7 years
  • n2  2.8 hours
  • n sqrt(n) 31.6 seconds
  • n log(n)  1.2 seconds
  • n  0.1 second
  • sqrt(n) - 3.2 10-4 seconds
  • log(n) -1.2 10-5 seconds

11
BUT!!!!!
  • All this assumes that n is or can become large.
  • How fast can you do matrix multiplication?
  • There are O(n 2.5) algorithms, but the constants
    are huge, so for practical n the normal algorithm
    is faster.

12
Adding terms
  • So if we were talking about adding O(n3) O(n2)
  • The second part would be adding 2.8 hours to 31.7
    years
  • The smaller term just becomes irrelevant

13
New Topic - Correctness
  • Ok. Now we know how fast an algorithm will
    execute -- how do we know it is correct?
  • Key tool - invariants (and assertions)
  • Simply a statement of what we know about the
    state of the variables when execution reaches a
    given point in a program.

14
An Example Program
  • int triangle(int a, int b, int c)
  • if (a b)
  • if (b c) return 1
  • else
  • return 2
  • else if (b c)
  • return 2
  • else
  • return 3

15
Keep track of what you know
  • int triangle(int a, int b, int c)
  • if (a b)
  • if (b c) / we know ab bc
    /return 1
  • else / we know ab and b ! c /
  • return 2
  • else if (b c) / we know a!b and b c/
  • return 2
  • else / we know a ! b and b ! c /
  • return 3 / HA-- THERE IS AN ERROR! /

16
How to show this is correct.
  • double minimum(double values)
  • int n values.length
  • double minValue values0
  • for (int i 1 i lt n i)
  • if (valuesi lt minValue)
  • minValue valuesi
  • return minValue

17
Loop Invariants
  • double minValue values0
  • for (int i 1 i lt n i)
  • // minValue is smallest element in 0 .. i-1
  • if (valuesi lt minValue)
  • minValue valuesi
  • // minValue is smallest element in 0 .. i
  • // minValue is smallest element in 0 .. (n-1)
  • return minValue

18
Binary Search
  • int binarySearch(double data, double test)
  • int low 0
  • int high data.length
  • while (low lt high)
  • mid (low high) / 2
  • if (datamid lt testValue) low mid 1
  • else high mid
  • return low

19
The Invariant
  • int low 0 int high data.length
  • // index lt low are smaller than test value
  • // index gt high are bigger or equal to test
    value
  • while (low lt high)
  • mid (low high) / 2
  • if (datamid lt testValue) low mid 1
  • else high mid
  • // what is true here?

20
Questions??
Write a Comment
User Comments (0)
About PowerShow.com