the time complexity of an algorithm

108
The Time Complexity of an Algorithm Specifies how the running time depends on the size of the input. CSE 3101Z Design and Analysis of Algorithms

Upload: louvain

Post on 07-Jan-2016

33 views

Category:

Documents


0 download

DESCRIPTION

CSE 3101Z Design and Analysis of Algorithms. The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input. Purpose. To estimate how long a program will run. To estimate the largest input that can reasonably be given to the program. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The Time Complexity of an Algorithm

The Time Complexity of an Algorithm

Specifies how the running time depends on the size of the input.

CSE 3101Z Design and Analysis of Algorithms

Page 2: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 2

Purpose

• To estimate how long a program will run.

• To estimate the largest input that can reasonably be given to the program.

• To compare the efficiency of different algorithms.

• To help focus on the parts of code that are executed the largest number of times.

• To choose an algorithm for an application.

Page 3: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 3

Time Complexity Is a Function

• Specifies how the running time depends on the size of the input.

• A function mapping “size” of input “time” T(n) executed .

Page 4: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 4

Example: Insertion Sort

Page 5: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 5

Example: Insertion Sort

Page 6: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 6

Example: Insertion Sort

2

2

( 1)Worst case (reverse order): : 1 ( ) ( )

2

n

jj

n nt j j T n n

Page 7: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 7

Example: Merge Sort

Page 8: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 8

Page 9: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 9

Example: Merge Sort

Page 10: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 10

Example: Merge Sort

( ) ( log )T n n n

Page 11: The Time Complexity of an Algorithm

Relevant Mathematics: Classifying Functions

Input Size

Tim

e

Page 12: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 12

Assumed Knowledge

See J. Edmonds, How to Think About Algorithms (HTA):

http://www.cse.yorku.ca/~jeff/courses/3101/

Chapter 22. Existential and Universal Quantifiers

Chapter 24. Logarithms and Exponentials

Loves( , )

Loves( , )

g b b g

g b b g

Page 13: The Time Complexity of an Algorithm

Classifying Functions

Describing how fast a function grows without going into too much detail.

Page 14: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 14

Which are more alike?

Page 15: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 15

Which are more alike?

Mammals

Page 16: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 16

Which are more alike?

Page 17: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 17

Which are more alike?

Dogs

Page 18: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 18

Classifying Animals

Vertebrates

Birds

Mam

mals

Reptiles

Fish

Dogs

Giraffe

Page 19: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 19

Classifying Functions

Note: The universe is estimated to contain ~1080 particles.

T(n) 10 100 1,000 10,000

log n   3 6 9 13

n1/2 3 10 31 100

10 100 1,000 10,000

n log n 30 600 9,000 130,000

n2 100 10,000 106 108

n3 1,000 106 109 1012

2n 1,024 1030 10300 103000

n

n

Page 20: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 20

Which are more alike?

n1000 n2 2n

Page 21: The Time Complexity of an Algorithm

End of Lecture 1

Wed, March 4, 2009

Page 22: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 22

Which are more alike?

Polynomials

n1000 n2 2n

Page 23: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 23

Which are more alike?

1000n2 3n2 2n3

Page 24: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 24

Which are more alike?

Quadratic

1000n2 3n2 2n3

Page 25: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 25

Classifying Functions?

Functions

Page 26: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 26

Classifying Functions

Functions

Constant

Logarithm

ic

Poly L

ogarithmic

Polynom

ial

Exponential

Exp

Double E

xponential

(log n)5 n5 25n5 log n5 2n5 25n

2

Page 27: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 27

Classifying Functions?

Polynomial

Page 28: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 28

Classifying Functions

Polynomial

Linear

Quadratic

Cubic

4th O

rder

5n25n 5n3 5n4

Page 29: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 29

Classifying Functions

Functions

Constant

Logarithm

ic

Poly L

ogarithmic

Polynom

ial

Exponential

Exp

Double E

xponential

(log n)5 n5 25n5 log n5 2n5 25n

2

Page 30: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 30

Properties of the Logarithm

1 logChanging ba loe : gs s

loga bb

n na

Swapping base and arg1

logu entlog

m : ab

ba

Page 31: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 31

Logarithmic

• log10n = # digits to write n

• log2n = # bits to write n = 3.32 log10n

• log(n1000) = 1000 log(n)

Differ only by a multiplicative constant.

1 logChanging ba loe : gs s

loga bb

n na

Page 32: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 32

Classifying Functions

Functions

Constant

Logarithm

ic

Poly L

ogarithmic

Polynom

ial

Exponential

Exp

Double E

xponential

(log n)5 n5 25n5 log n5 2n5 25n

2

Page 33: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 33

Poly Logarithmic

(log n)5 = log5 n

Page 34: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 34

(Poly)Logarithmic << Polynomial

For sufficiently large n

log1000 n << n0.001

Page 35: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 35

Classifying Functions

Polynomial

Linear

Quadratic

Cubic

4th O

rder

5n25n 5n3 5n4

Page 36: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 36

Linear << Quadratic

For sufficiently large n

10000 n << 0.0001 n2

Page 37: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 37

Classifying Functions

Functions

Constant

Logarithm

ic

Poly L

ogarithmic

Polynom

ial

Exponential

Exp

Double E

xponential

(log n)5 n5 25n5 log n5 2n5 25n

2

Page 38: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 38

Polynomial << Exponential

For sufficiently large n

n1000 << 20.001 n

Page 39: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 39

Ordering Functions

Functions

Constant

Logarithm

ic

Poly L

ogarithmic

Polynom

ial

Exponential

Exp

Double E

xponential

(log n)5 n5 25n5 log n5 2n5 25n

2

<< << << << << <<

<< << << << << <<

Page 40: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 40

Which Functions are Constant?

• 5• 1,000,000,000,000• 0.0000000000001• -5• 0• 8 + sin(n)

YesYesYes

No

YesYes

Page 41: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 41

Which Functions are “Constant”?

• 5• 1,000,000,000,000• 0.0000000000001• -5• 0• 8 + sin(n)

YesYesYesNo

NoYes

Lies in between

7

9

The running time of the algorithm is a “Constant” It does not depend significantly

on the size of the input.

Page 42: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 42

Which Functions are Quadratic?

• n2

• … ?

Page 43: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 43

Which Functions are Quadratic?

• n2

• 0.001 n2

• 1000 n2

Some constant times n2.

Page 44: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 44

Which Functions are Quadratic?

• n2

• 0.001 n2

• 1000 n2

• 5n2 + 3n + 2log n

Page 45: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 45

Which Functions are Quadratic?

• n2

• 0.001 n2

• 1000 n2

• 5n2 + 3n + 2log n

Lies in between

Page 46: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 46

Which Functions are Quadratic?

• n2

• 0.001 n2

• 1000 n2

• 5n2 + 3n + 2log n

Ignore low-order termsIgnore multiplicative constants.

Ignore "small" values of n. Write θ(n2).

Page 47: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 47

Which Functions are Polynomial?

• n5

• … ?

Page 48: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 48

Which Functions are Polynomial?

• nc

• n0.0001

• n10000

n to some constant power.

Page 49: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 49

Which Functions are Polynomial?

• nc

• n0.0001

• n10000

• 5n2 + 8n + 2log n • 5n2 log n • 5n2.5

Page 50: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 50

Which Functions are Polynomial?

• nc

• n0.0001

• n10000

• 5n2 + 8n + 2log n • 5n2 log n • 5n2.5

Lie in between

Page 51: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 51

Which Functions are Polynomials?

• nc

• n0.0001

• n10000

• 5n2 + 8n + 2log n • 5n2 log n • 5n2.5

Ignore low-order termsIgnore power constant.

Ignore "small" values of n. Write nθ(1)

Page 52: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 52

Which Functions are Exponential?

• 2n

• … ?

Page 53: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 53

Which Functions are Exponential?

• 2n

• 20.0001 n

• 210000 n

2 raised to a linear function of n.

Page 54: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 54

Which Functions are Exponential?

• 2n

• 20.0001 n

• 210000 n

• 8n

• 2n / n100 •2n · n100

too small?too big?

Page 55: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 55

Which Functions are Exponential?

• 2n

• 20.0001 n

• 210000 n

• 8n

• 2n / n100 •2n · n100

= 23n

> 20.5n

< 22n

Lie in between

Page 56: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 56

Which Functions are Exponential?

• 2n

• 20.0001 n

• 210000 n

• 8n

• 2n / n100 •2n · n100

= 23n

> 20.5n

< 22n

20.5n > n100

2n = 20.5n · 20.5n > n100 · 20.5n

2n / n100 > 20.5n

Page 57: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 57

Which Functions are Exponential?

•Ignore low-order terms

•Ignore base.

•Ignore "small" values of n.

•Ignore polynomial factors.

•Write 2θ(n)

• 2n

• 20.0001 n

• 210000 n

• 8n

• 2n / n100 •2n · n100

Page 58: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 58

Classifying Functions

Rank constants in significance.

f(n) = 8·24n / n100 + 5·n3

Tell me the most significant thing

about your function

Page 59: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 59

Classifying Functions

f(n) = 8·24n / n100 + 5·n3

Tell me the most significant thing

about your function

2θ(n)

23n < < 24nf(n)because

Page 60: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 60

Classifying Functions

f(n) = 8·24n / n100 + 5·n3

Tell me the most significant thing

about your function

2θ(n)

24n/nθ(1)

θ(24n/n100)

8·24n / n100 + θ(n3)8·24n / n100 + nθ(1)

8·24n / n100 + 5·n3

Page 61: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 61

Notations

Theta f(n) = θ(g(n)) f(n) ≈ c g(n)

Big Oh f(n) = O(g(n)) f(n) ≤ c g(n)

Big Omega f(n) = Ω(g(n)) f(n) ≥ c g(n)

Little Oh f(n) = o(g(n)) f(n) << c g(n)

Little Omega f(n) = ω(g(n)) f(n) >> c g(n)

Page 62: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 62

Definition of Theta

f(n) = θ(g(n))

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 63: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 63

Definition of Theta

f(n) is sandwiched between c1g(n) and c2g(n)

f(n) = θ(g(n))

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 64: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 64

Definition of Theta

f(n) is sandwiched between c1g(n) and c2g(n)

for some sufficiently small c1 (= 0.0001)

for some sufficiently large c2 (= 1000)

f(n) = θ(g(n))

,1 2 0 0 1 2, 0 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 65: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 65

Definition of Theta

For all sufficiently large n

f(n) = θ(g(n))

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 66: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 66

Definition of Theta

For all sufficiently large n

For some definition of “sufficiently large”

f(n) = θ(g(n))

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 67: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 67

Definition of Theta

3n2 + 7n + 8 = θ(n2)

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 68: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 68

Definition of Theta

3n2 + 7n + 8 = θ(n2)

c1·n2 3n2 + 7n + 8 c2·n2? ?

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 69: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 69

Definition of Theta

3n2 + 7n + 8 = θ(n2)

3·n2 3n2 + 7n + 8 4·n23 4

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 70: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 70

Definition of Theta

3n2 + 7n + 8 = θ(n2)

3·12 3·12 + 7·1 + 8 4·121False

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 71: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 71

Definition of Theta

3n2 + 7n + 8 = θ(n2)

3·72 3·72 + 7·7 + 8 4·727False

,1 2 0 0 1 2, 0 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 72: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 72

Definition of Theta

3n2 + 7n + 8 = θ(n2)

3·82 3·82 + 7·8 + 8 4·828

True

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 73: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 73

Definition of Theta

3n2 + 7n + 8 = θ(n2)

3·92 3·92 + 7·9 + 8 4·929True

,1 2 0 0 1 2, 0 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 74: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 74

Definition of Theta

3n2 + 7n + 8 = θ(n2)

3·102 3·102 + 7·10 + 8 4·10210True

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 75: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 75

Definition of Theta

3n2 + 7n + 8 = θ(n2)

3·n2 3n2 + 7n + 8 4·n2?

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 76: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 76

Definition of Theta

3n2 + 7n + 8 = θ(n2)

3·n2 3n2 + 7n + 8 4·n2n 88

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 77: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 77

Definition of Theta

3n2 + 7n + 8 = θ(n2)

3·n2 3n2 + 7n + 8 4·n2

Truen 8

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 78: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 78

Definition of Theta

3n2 + 7n + 8 = θ(n2)

3·n2 3n2 + 7n + 8 4·n2

7n + 8 1·n2

7 + 8/n 1·nn 8

True

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 79: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 79

Definition of Theta

3n2 + 7n + 8 = θ(n2)

3·n2 3n2 + 7n + 8 4·n23 4 8 n 8

True

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 80: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 80

Asymptotic Notation

Theta f(n) = θ(g(n)) f(n) ≈ c g(n)

BigOh f(n) = O(g(n)) f(n) ≤ c g(n)

Omega f(n) = Ω(g(n)) f(n) ≥ c g(n)

Little Oh f(n) = o(g(n)) f(n) << c g(n)

Little Omega f(n) = ω(g(n)) f(n) >> c g(n)

Page 81: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 81

Definition of Theta

3n2 + 7n + 8 = θ(n)

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 82: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 82

Definition of Theta

3n2 + 7n + 8 = θ(n)

c1·n 3n2 + 7n + 8 c2·n? ?

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 83: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 83

Definition of Theta

3n2 + 7n + 8 = θ(n)

3·n 3n2 + 7n + 8 100·n3 100

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 84: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 84

Definition of Theta

3n2 + 7n + 8 = θ(n)

3·n 3n2 + 7n + 8 100·n?

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 85: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 85

Definition of Theta

3n2 + 7n + 8 = θ(n)

3·100 3·1002 + 7·100 + 8 100·100100

False

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 86: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 86

Definition of Theta

3n2 + 7n + 8 = θ(n)

3·n 3n2 + 7n + 8 10,000·n3 10,000

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 87: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 87

Definition of Theta

3n2 + 7n + 8 = θ(n)

3·10,000 3·10,000 2 + 7·10,000 + 8 10,000·10,00010,000

False

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 88: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 88

Definition of Theta

3n2 + 7n + 8 = θ(n)

What is the reverse statement?

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 89: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 89

Understand Quantifiers!!!

Sam Mary

Bob Beth

JohnMarilyn Monroe

Fred Ann

Sam Mary

Bob Beth

JohnMarilyn Monroe

Fred Ann

b, Loves(b, MM)

b, Loves(b, MM)

b, Loves(b, MM)]

b, Loves(b, MM)]

Page 90: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 90

Definition of Theta

3n2 + 7n + 8 = θ(n)

The reverse statement

, ,1 2 0 0 1 20 : : ( ) ( ) or ( ) ( )c c n n n f n c g n f n c g n

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 91: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 91

Definition of Theta

3n2 + 7n + 8 = θ(n)

?

, ,1 2 0 0 1 20 : : ( ) ( ) or ( ) ( )c c n n n f n c g n f n c g n

0 2Satisfi ed f or max( , )n n c

223 7 8n n c n

22

7 83

cn n n

Page 92: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 92

Order of Quantifiers

f(n) = θ(g(n))

?

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

,0 0 1 2 1 20 : , : ( ) ( ) ( )n n n c c c g n f n c g n

Page 93: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 93

g b loves b g, , ( , ) b g loves b g, , ( , )

Understand Quantifiers!!!

One girlCould be a separate girl

for each boy.

Sam Mary

Bob Beth

JohnMarilin Monro

Fred Ann

Sam Mary

Bob Beth

JohnMarilin Monro

Fred Ann

Page 94: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 94

Order of Quantifiers

No! It cannot be a different c1 and c2 for each n.

f(n) = θ(g(n))

, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

,0 0 1 2 1 20 : , : ( ) ( ) ( )n n n c c c g n f n c g n

Page 95: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 95

Other Notations

Theta f(n) = θ(g(n)) f(n) ≈ c g(n)

BigOh f(n) = O(g(n)) f(n) ≤ c g(n)

Omega f(n) = Ω(g(n)) f(n) ≥ c g(n)

Little Oh f(n) = o(g(n)) f(n) << c g(n)

Little Omega f(n) = ω(g(n)) f(n) >> c g(n)

Page 96: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 96

Definition of “Big Omega”

, 0 00 : , ( ) ( )c n n n f n cg n

( )f n

( )g n

( )cg n

n

( ) ( ( ))f n g n

Page 97: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 97

Definition of “Big Oh”

, 0 00 : , ( ) ( )c n n n f n cg n

( )f n

( )g n

( )cg n

n

( ) ( ( ))f n O g n

Page 98: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 98

Definition of “Little Omega”

0 00, 0 : , ( ) ( )c n n n f n cg n

( )f n

( )g n

( )cg n

n

( ) ( ( ))f n g n

Page 99: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 99

Definition of “Little Oh”

0 00, 0 : , ( ) ( )c n n n f n cg n

( )f n

( )g n

( )cg n

n

( ) ( ( ))f n o g n

Page 100: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 100

Time Complexity Is a Function

• Specifies how the running time depends on the size of the input.

• A function mapping “size” of input “time” T(n) executed .

Page 101: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 101

Definition of Time?

Page 102: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 102

Definition of Time

• # of seconds (machine dependent).

• # lines of code executed.

• # of times a specific operation is performed (e.g., addition).

• These are all reasonable definitions of time, because they are within a constant factor of each other.

Page 103: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 103

Definition of Input Size

• A good definition: #bits

• Examples:1. Sorting an array A of k-b

it

e.g

intege

.,

rs.

k=16.

I nput size length( )n k A

Note that, since n length(A), suffi cient to defi lengt Ane .h( )n

2. Multiplying A,B . Let #bits used to represent

Let #bits used to represent BA

B

n A

n

input size Then A Bn n n

2 2

2 2

Note that log and log

T input size loghus + logA B

n A

n A n B

B

Page 104: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 104

Time Complexity Is a Function

• Specifies how the running time depends on the size of the input.

• A function mapping:

– “size” of input “time” T(n) executed .

• Or more precisely:

– # of bits n needed to represent the input # of operations T(n) executed .

Page 105: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 105

Time Complexity of an Algorithm

• O(n2): For any input size n>n0, the algorithm takes no more than cn2 time on every input.

• Ω(n2): For any input size n>n0, the algorithm takes at least cn2 time on at least one input.

• θ (n2): Do both.

The time complexity of an algorithm isthe largest time required on any input of size n. (Worst case analysis.)

Page 106: The Time Complexity of an Algorithm

End of Lecture 2

Monday, Mar 9, 2009

Page 107: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 107

What is the height of tallest person in the class?

Bigger than this?

Need to find only one person who is taller

Need to look at every person

Smaller than this?

Page 108: The Time Complexity of an Algorithm

COSC 3101, PROF. J. ELDER 108

Time Complexity of a Problem

• O(n2): Provide an algorithm that solves the problem in no more than this time.

– Remember: for every input, i.e. worst case analysis!

• Ω(n2): Prove that no algorithm can solve it faster.

– Remember: only need one input that takes at least this long!

• θ (n2): Do both.

The time complexity of a problem is the time complexity of the fastest algorithm that solves the problem.