Running Time - Discrete Math - Lecture Slides, Slides of Discrete Mathematics

Some concept of Discrete Math are Unique Path, Addition Rule, Clay Mathematics, Complexity Theory, Correspondence Principle, Discrete Mathematics, Group Theory, Random Variable, Major Concepts. Main points of this lecture are: Running Time, Approximate Running Time, Big-Oh, Programs, Analyzing Function Calls, Analyzing Recursive Functions, Hanoi, Towers, Running Time, Single Answer

Typology: Slides

2012/2013

Uploaded on 04/27/2013

emee
emee 🇮🇳

4.3

(30)

134 documents

1 / 97

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Discrete Maths
Objective
to describe the Big-Oh notation for estimating
the running time of programs
4. Running Time of
Programs
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61

Partial preview of the text

Download Running Time - Discrete Math - Lecture Slides and more Slides Discrete Mathematics in PDF only on Docsity!

Discrete Maths

  • Objective
    • to describe the Big-Oh notation for estimating

the running time of programs

4. Running Time of

Programs

Overview

  1. Running Time

  2. Big-Oh and Approximate Running Time

  3. Big-Oh for Programs

  4. Analyzing Function Calls

  5. Analyzing Recursive Functions

  6. Towers of Hanoi

  7. Further Information

  • There is no single answer!
    • the running time depends on the size of the n value
  • Instead of a time answer in seconds, we

want a time answer which is related to the size of the input.

continued

  • For example:
    • programTime(n) = constant * n
    • this means that as n gets bigger, so does the program time
    • running time is linearly related to the input

size of n

running time constant * n

  • A typical result is:
    • T(n) = c*n, where c is some constant
      • but often we just ignore c
    • this means the program has linear running time
  • T(n) values for different programs can be used

to compare their relative running times

  • selection sort: T(n) = n^2
  • merge sort: T(n) = n log n
  • so, merge sort is “better” for larger n sizes

1 .1. Different Kinds of Running Time

  • Usually T(n) is the worst-case running time
    • the maximum running time on any input of size n
  • Tavg (n) is the average running time of the program over all inputs of size n - more realistic - very hard to calculate - not considered by us

Calculation

• The for loop executes n-1 times

  • each loop carries out (in the worse case) 4 ops
    • test of j < n, if test, small assign, j increment
  • total loop time = 4(n-1)
  • plus 3 ops at start and end
    • small assign (line 2), init of j (line 3), final j < n test

• Total time T(n) = 4(n-1) + 3

= 4n -

  • running time is linear with the size of the array

1 .3. Comparing Different T()’s

  • If input size < 50, program B is faster.
  • But for large n’s, which are more common in real code, program B gets worse and worse.

T(n)

value

input size n

Ta(n) = 100n

Tb (n) = 2n^2

1 .5. Execution Times

n 3 9 50 100 1ms 1sec n^2 9 81 2.5ms 10ms 1sec 12 days n^3 27 729 125ms 1sec 16.7 min 31,710yr 2 n^8 512 36yr 410^16 yr 310^287 yr 3*10^301016 yr log n 2 3 6 7 10 20

n (no. of instructions)

growth formula T()

if n is 50, you will wait 36 years for an answer!

Assume 1 instruction takes 1 microsec (10-6^ secs) to execute. How long will n instructions take?

Notes

• Logarithmic running times are best.

• Polynomial running times are acceptable, if

the power isn’t too big

  • e.g. n^2 is ok, n^100 is terrible

• Exponential times mean sl oooooooo w code.

  • some size problems may take longer to finish than

the lifetime of the universe!

Arguments against T(n)

• Algorithms often perform much better on

average than the worst case used in T()

  • quicksort is n log n on a “random” array, but n^2 in the worse case
  • but for most algorithms, the worst case is a good predictor of its running time
  • average case analyses can be done, but they are harder mathematically

continued

  • Some people say:
    • “Who cares about running time? In a few years,

machines will be so fast that even bad algorithms

will be fast.”

  • History shows this argument to be wrong. As

machines get faster, problem sizes get bigger.

  • Most interesting problems (e.g. computer vision,

natural language processing) always require more

resources

  • fast algorithms will always be needed

continued

  1. Big-Oh and Approximate Running Time
  • Big-Oh mathematical notation simplifies

the process of estimating the running time of programs

  • it uses T(n), but ignores constant factors which depend on compiler/machine behaviour

continued

  • The Big-Oh value specifies running time

independent of:

  • machine architecture
    • e.g. don’t consider the running speed of individual machine operations
  • machine load (usage)
    • e.g. time delays due to other users
  • compiler design effects
    • e.g. gcc versus Borland C