Runtime Analysis: Understanding Big O, Θ and Simple Sums in CS 260 - Prof. Kurt Schmidt, Study notes of Data Structures and Algorithms

The concept of runtime analysis in cs 260, focusing on big o and θ notation, as well as simple sums. It includes examples, algorithms, and code adapted from data structures and other objects using c++ by main & savitch. The document aims to help students understand how to analyze the efficiency of algorithms as the input size grows.

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-h9v
koofers-user-h9v 🇺🇸

10 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 260 1
Runtime Analysis
Big O, Θ, some simple sums.
(see section 1.2 for motivation)
Notes, examples and code adapted from Data Structures
and Other Objects Using C++ by Main & Savitch
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Runtime Analysis: Understanding Big O, Θ and Simple Sums in CS 260 - Prof. Kurt Schmidt and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CS 260

Runtime Analysis

Big O,

, some simple sums. (see section 1.2 for motivation)

Notes, examples and code adapted from Data Structuresand Other Objects Using C++ by Main & Savitch

CS 260

Sums - review

-^

Some quick identities (just like integrals):

(^

)^

∑ ∑

=

= +

) ( ) ( ) ( ) ( constant is c

where, ) (

) (

i g i f i g i f

i f

c

i cf i^

i

-^

Not like the integral:

=

=

b ai

a bi

i

f

i

f^

CS 260

Example (motivation), sect 1.

•^

Problem:

to count the # of steps in the

Eiffel Tower

-^

Setup:

Jack and Jill are at the top of the

tower w/paper and a pencil

  • Let n

of stairs to climb the Eiffel Tower

CS 260

Eiffel Tower, alg. 1

st

attempt:

  • Jack takes the paper and pencil, walks stairs– Makes mark for each step– Returns, shows Jill
    • Runtime:
      • steps Jack took: 2n– # marks: n– So,

T

(n) = 2n + n = 3n 1

CS 260

Eiffel Tower, alg. 2 (cont.)

  • Analysis:
    • marks = n– # steps = 2 ( 1+2+3+…+n )

= 2 * n(n+1)/2= n

2

  • n
  • So

T

(n) = n 2

2

  • 2n

CS 260

Eiffel Tower, alg. 3

rd

try:

  • Jack & Jill see their friend Steve on the

ground

  • Steve points to a sign– Jill copies the number down
    • Runtime:
      • steps Jack took: 0– # marks: log

10

( n )

  • So,

T

(n) = log 3

10

( n )

CS 260

Wrap-up

• T

(n) is a line (^1) – So, if we double the # of stairs, the runtimedoubles

• T

(n) is a parabola (^2) – So, if we double the # of stairs, the runtimequadruples (roughly)

• T

(n) is a logarithm (^3) – We’d have to multiply the number of stairs bya factor of 10 to increase T by 1 (roughly)– Very nice function

CS 260

Some “what ifs”

-^

Suppose 3 stairs (or 3 marks) can be made per 1second

-^

(There are really 2689 steps)

2 sec

2 sec

2 sec

T

(n) 3

252 days

112 days

28 days

T

(n) 2

135 min

90 min

45 min

T

(n) 1

3n

2n

n

CS 260

Asymptotic behavior

  • When evaluating algorithms (from a

design point of view) we don’t want toconcern ourselves with lower-level details:– processor speed– the presence of a floating-point coprocessor– the phase of the moon

  • We are concerned simply with how a

function grows as n grows

arbitrarily large

  • I.e., we are interested in its

asymptotic

behavior

CS 260

Asymptotic behavior (cont.)

  • As n gets large, the function is dominated

more and more by its highest-order term(so we don’t really need to consider lower-order terms)

  • The coefficient of the leading term is not

really of interest either. A line w/a steepslope will eventually be overtaken by eventhe laziest of parabolas (concave up).That coefficient is just a matter of scale.Irrelevant as n

CS 260

  • Further, if f(n) = O( T(n) ), then T(n) grows

no slower than f(n)

  • We can then say that T(n) =

(n)

  • I.e., T(n) can be bound both above and

below with f(n)

CS 260

Setup

  • First we have to decide what it is we’re

counting, what might vary over thealgorithm, and what actions are constant

  • Consider:

for(

i=0;

i<5;

++i

++cnt;

  • i=0 happens exactly once– ++i happens 5 times– i<5 happens

^6

times

  • ++cnt happens 5 times

CS 260

Setup (cont.)

  • We decide that ++cnt is a constant-

time operation (integer addition, theninteger assignment)

  • So, a single execution of the loop is

done in constant time

  • Let this cost be

c:

CS 260

Setup (cont.)

  • So, the total cost of executing that loop

can be given by:

c

c

n

T

i

4 0

=

  • Constant time• Makes sense. Loop runs a set number of

times, and each loop has a constant cost