Docsity

Efficiency Analysis: Measuring Input Size and Running Time in Computer Science, Study notes of Algorithms and Programming

Partial preview of the text

Download Efficiency Analysis: Measuring Input Size and Running Time in Computer Science and more Study notes Algorithms and Programming in PDF only on Docsity!

CS 483 - Data Structures and Algorithm Analysis

Lecture II: Chapter 2

R. Paul Wiegand

George Mason University, Department of Computer Science

February 1, 2006

R. Paul Wiegand George Mason University, Department of Computer Science

Outline

1 Analysis Framework

2 Asymptotic Notation

3 Nonrecursive Algorithms

4 Recursive Algorithms & Recurrence Relations

5 Empirical Analysis

6 Homework

R. Paul Wiegand George Mason University, Department of Computer Science

Measuring input size

What do we measure to judge the size of the problem?

R. Paul Wiegand George Mason University, Department of Computer Science

Measuring input size

What do we measure to judge the size of the problem?

Sort(a 1 , a 2 , · · · , an )

a 11 · · · an 1

a 1 m · · · anm

b 11 · · · bm 1

b 1 k · · · bmk

p(x) = anx

n

  • · · · + a 1 x + a 0

R. Paul Wiegand George Mason University, Department of Computer Science

Measuring input size

What do we measure to judge the size of the problem?

Sort(a 1 , a 2 , · · · , an) Number of list elements

a 11 · · · an 1

a 1 m · · · anm

b 11 · · · bm 1

b 1 k · · · bmk

Order of matrices?

Total number of elements?

p(x) = an x

n

  • · · · + a 1 x + a 0

R. Paul Wiegand George Mason University, Department of Computer Science

Measuring input size

What do we measure to judge the size of the problem?

Sort(a 1 , a 2 , · · · , an) Number of list elements

a 11 · · · an 1

a 1 m · · · anm

b 11 · · · bm 1

b 1 k · · · bmk

Order of matrices?

Total number of elements?

p(x) = an x

n

  • · · · + a 1 x + a 0

Degree of polynomial?

Total number of coefficients?

R. Paul Wiegand George Mason University, Department of Computer Science

Measuring Running Time

What do we measure to judge the running time on an algorithm?

Could count all operations executed ...

But we typically concern ourselves with the basic operation, the one

contributing the most to the total running time

Sort(a 1 , a 2 , · · · , an )

a 11 · · · an 1

a 1 m · · · anm

b 11 · · · bm 1

b 1 k · · · bmk

R. Paul Wiegand George Mason University, Department of Computer Science

Measuring Running Time

What do we measure to judge the running time on an algorithm?

Could count all operations executed ...

But we typically concern ourselves with the basic operation, the one

contributing the most to the total running time

Sort(a 1 , a 2 , · · · , an) Key comparisons

a 11 · · · an 1

a 1 m · · · anm

b 11 · · · bm 1

b 1 k · · · bmk

R. Paul Wiegand George Mason University, Department of Computer Science

Measuring Running Time

What do we measure to judge the running time on an algorithm?

Could count all operations executed ...

But we typically concern ourselves with the basic operation, the one

contributing the most to the total running time

Sort(a 1 , a 2 , · · · , an) Key comparisons

a 11 · · · an 1

a 1 m · · · anm

b 11 · · · bm 1

b 1 k · · · bmk

Additions?

Multiplications?

In general, we can approximate running time as: T (n) ≈ cop C (n)

Example: Given C (n) :=

1

2

n (n − 1), what is the run-time effect of

doubling the input size?

R. Paul Wiegand George Mason University, Department of Computer Science

Sidebar: Logarithm Basics

If b

x

= y then log

b

y = x

We can change bases: log

a

x =

log b x

logb a

R. Paul Wiegand George Mason University, Department of Computer Science

How Does Your Running Time Grow?

0 20 40 60 80 100

0e+

2e+

4e+

6e+

8e+

1e+

x

y

y =x

y =x 2

y =x 3

y =x 4

y =

x

We aren’t so interested in running times

on small inputs, but in how running time

scales on very large inputs

We are interested in an algorithm’s order

of growth, ignoring constant factors

R. Paul Wiegand George Mason University, Department of Computer Science

How Does Your Running Time Grow?

0 20 40 60 80 100

0e+

2e+

4e+

6e+

8e+

1e+

x

y

y =x

y =x 2

y =x 3

y =x 4

y =

x

We aren’t so interested in running times

on small inputs, but in how running time

scales on very large inputs

We are interested in an algorithm’s order

of growth, ignoring constant factors

1 Constant

log n Logarithmic (sub-linear)

n Linear

n lg n n-log-n

n

k , for some constant k Polynomial

k

n , for some constant k Exponential

n! Factorial (“Exponential”)

R. Paul Wiegand George Mason University, Department of Computer Science

Worst-Case Efficiency

SequentialSearch(A[0... n − 1], K )

i ← 0

while i < n and A[i] 6 = K do

i ← i + 1

if i < n return i

else return − 1

R. Paul Wiegand George Mason University, Department of Computer Science

Worst-Case Efficiency

SequentialSearch(A[0... n − 1], K )

i ← 0

while i < n and A[i] 6 = K do

i ← i + 1

if i < n return i

else return − 1

Worst case is when K is not in A[]

Search every element, requiring C

worst

(n) = n comparisons

Worst case analysis provides an upper bound

R. Paul Wiegand George Mason University, Department of Computer Science