the time complexity of an algorithm - Çankaya Üniversitesi · 2013. 4. 9. · the time complexity...

106
The Time Complexity of an Algorithm Specifies how the running time depends on the size of the input. Analysis of Algorithms

Upload: others

Post on 26-Mar-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

The Time Complexity of an Algorithm

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

Analysis of Algorithms

Page 2: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

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 - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

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 - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

4

Example: Insertion Sort

Page 5: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

5

Example: Insertion Sort

Page 6: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

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 - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

7

Example: Merge Sort

Page 8: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

8

Page 9: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

9

Example: Merge Sort

Page 10: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

10

Example: Merge Sort

( ) ( log )T n n nθ∈

Page 11: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

Relevant Mathematics: Classifying Functions

Input Size

Tim

e

Page 12: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

12

Assumed Knowledge

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 - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

Classifying Functions

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

Page 14: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

14

Which are more alike?

Page 15: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

15

Which are more alike?

Mammals

Page 16: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

16

Which are more alike?

Page 17: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

17

Which are more alike?

Dogs

Page 18: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

18

Classifying Animals

Vertebrates

Birds

Mam

mals

Reptiles

Fish

Dogs

Giraffe

Page 19: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

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 - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

20

Which are more alike?

n1000 n2 2n

Page 21: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

21

Which are more alike?

Polynomials

n1000 n2 2n

Page 22: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

22

Which are more alike?

1000n2 3n2 2n3

Page 23: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

23

Which are more alike?

Quadratic

1000n2 3n2 2n3

Page 24: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

24

Classifying Functions?

Functions

Page 25: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

25

Classifying Functions Functions

Constant

Logarithmic

Poly Logarithmic

Polynomial

Exponential

Exp

Double Exponential

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

2

Page 26: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

26

Classifying Functions?

Polynomial

Page 27: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

27

Classifying Functions

Polynomial

Linear

Quadratic

Cubic

4th O

rder

5n2 5n 5n3 5n4

Page 28: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

28

Classifying Functions Functions

Constant

Logarithmic

Poly Logarithmic

Polynomial

Exponential

Exp

Double Exponential

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

2

Page 29: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

29

Properties of the Logarithm

1 logChanging ba loe : gs sloga b

b

n na

= ×

Swapping base and arg 1 logu entlog

m : ab

ba

=

Page 30: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

30

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 sloga b

b

n na

= ×

Page 31: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

31

Classifying Functions Functions

Constant

Logarithmic

Poly Logarithmic

Polynomial

Exponential

Exp

Double Exponential

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

2

Page 32: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

32

Poly Logarithmic

(log n)5 = log5 n

Page 33: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

33

(Poly)Logarithmic << Polynomial

For sufficiently large n

log1000 n << n0.001

Page 34: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

34

Classifying Functions

Polynomial

Linear

Quadratic

Cubic

4th O

rder

5n2 5n 5n3 5n4

Page 35: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

35

Linear << Quadratic

For sufficiently large n

10000 n << 0.0001 n2

Page 36: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

36

Classifying Functions Functions

Constant

Logarithmic

Poly Logarithmic

Polynomial

Exponential

Exp

Double Exponential

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

2

Page 37: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

37

Polynomial << Exponential

For sufficiently large n

n1000 << 20.001 n

Page 38: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

38

Ordering Functions

Functions

Constant

Logarithmic

Poly Logarithmic

Polynomial

Exponential

Exp

Double Exponential

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

2

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

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

Page 39: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

39

Which Functions are Constant?

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

Yes Yes Yes

No

Yes Yes

Page 40: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

40

Which Functions are “Constant”?

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

Yes Yes Yes No No Yes 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 41: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

41

Which Functions are Quadratic?

•  n2

•  … ?

Page 42: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

42

Which Functions are Quadratic?

•  n2

•  0.001 n2

•  1000 n2

Some constant times n2.

Page 43: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

43

Which Functions are Quadratic?

•  n2

•  0.001 n2

•  1000 n2

•  5n2 + 3n + 2log n

Page 44: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

44

Which Functions are Quadratic?

•  n2

•  0.001 n2

•  1000 n2

•  5n2 + 3n + 2log n

Lies in between

Page 45: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

45

Which Functions are Quadratic?

•  n2

•  0.001 n2

•  1000 n2

•  5n2 + 3n + 2log n

Ignore low-order terms Ignore multiplicative constants.

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

Page 46: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

46

Which Functions are Polynomial?

•  n5

•  … ?

Page 47: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

47

Which Functions are Polynomial?

•  nc

•  n0.0001

•  n10000

n to some constant power.

Page 48: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

48

Which Functions are Polynomial?

•  nc

•  n0.0001

•  n10000

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

Page 49: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

49

Which Functions are Polynomial?

•  nc

•  n0.0001

•  n10000

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

Lie in between

Page 50: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

50

Which Functions are Polynomials?

•  nc

•  n0.0001

•  n10000

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

Ignore low-order terms Ignore power constant.

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

Page 51: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

51

Which Functions are Exponential?

•  2n

•  … ?

Page 52: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

52

Which Functions are Exponential?

•  2n

•  20.0001 n

•  210000 n

2 raised to a linear function of n.

Page 53: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

53

Which Functions are Exponential?

•  2n

•  20.0001 n

•  210000 n

•  8n •  2n / n100 • 2n · n100

too small? too big?

Page 54: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

54

Which Functions are Exponential?

•  2n

•  20.0001 n

•  210000 n

•  8n •  2n / n100 • 2n · n100

= 23n > 20.5n < 22n

Lie in between

Page 55: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

55

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 56: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

56

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 57: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

57

Classifying Functions

Rank constants in significance.

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

Tell me the most significant thing

about your function

Page 58: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

58

Classifying Functions

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

Tell me the most significant thing

about your function

2θ(n)

23n < < 24n f(n) because

Page 59: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

59

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 60: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

60

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 61: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

61

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 62: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

62

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 63: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

63

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 64: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

64

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 65: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

65

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 66: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

66

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 67: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

67

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 68: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

68

Definition of Theta

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

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

∃ > ∀ ≥ ≤ ≤, ,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 - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

69

Definition of Theta

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

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

∃ > ∀ ≥ ≤ ≤, ,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 - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

70

Definition of Theta

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

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

∃ > ∀ ≥ ≤ ≤,1 2 0 0 1 2, 0 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 71: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

71

Definition of Theta

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

3·82 ≤ 3·82 + 7·8 + 8 ≤ 4·82 8 True

∃ > ∀ ≥ ≤ ≤, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 72: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

72

Definition of Theta

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

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

∃ > ∀ ≥ ≤ ≤,1 2 0 0 1 2, 0 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 73: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

73

Definition of Theta

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

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

∃ > ∀ ≥ ≤ ≤, ,1 2 0 0 1 20 : , ( ) ( ) ( )c c n n n c g n f n c g n

Page 74: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

74

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 75: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

75

Definition of Theta

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

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

∃ > ∀ ≥ ≤ ≤, ,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 - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

76

Definition of Theta

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

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

True n ≥ 8

∃ > ∀ ≥ ≤ ≤, ,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 - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

77

Definition of Theta

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

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

7n + 8 ≤ 1·n2

7 + 8/n ≤ 1·n n ≥ 8

True

∃ > ∀ ≥ ≤ ≤, ,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 - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

78

Definition of Theta

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

3·n2 ≤ 3n2 + 7n + 8 ≤ 4·n2 3 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 79: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

79

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 80: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

80

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 81: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

81

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 82: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

82

Definition of Theta

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

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

∃ > ∀ ≥ ≤ ≤, ,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 - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

83

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 84: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

84

Definition of Theta

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

3·100 ≤ 3·1002 + 7·100 + 8 ≤ 100·100 100

False

∃ > ∀ ≥ ≤ ≤, ,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 - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

85

Definition of Theta

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

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

∃ > ∀ ≥ ≤ ≤, ,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 - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

86

Definition of Theta

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

3·10,000 ≤ 3·10,000 2 + 7·10,000 + 8 ≤ 10,000·10,000 10,000 False

∃ > ∀ ≥ ≤ ≤, ,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 - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

87

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 88: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

88

Understand Quantifiers!!!

Sam Mary

Bob Beth

John Marilyn Monroe

Fred Ann

Sam Mary

Bob Beth

John Marilyn Monroe

Fred Ann

∃ b, ¬Loves(b, MM) ∀ b, Loves(b, MM)

¬[∃ b, ¬Loves(b, MM)]

¬[∀ b, Loves(b, MM)]

Page 89: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

89

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 90: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

90

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 2Satisfied for max( , )n n c=

223 7 8n n c n+ + >

22

7 83 cn n n

↔ + + >

Page 91: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

91

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 92: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

92

∃ ∀g b loves b g, , ( , ) ∀ ∃b g loves b g, , ( , )

Understand Quantifiers!!!

One girl Could be a separate girl for each boy.

Sam Mary

Bob Beth

John Marilin Monro

Fred Ann

Sam Mary

Bob Beth

John Marilin Monro

Fred Ann

Page 93: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

93

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 94: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

94

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 95: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

95

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 96: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

96

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 97: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

97

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 98: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

98

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 99: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

99

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 100: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

100

Definition of Time?

Page 101: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

101

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 102: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

102

Definition of Input Size

•  A good definition: #bits

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

it e.g

intege.,

rs.k=16.

Input size length( )n k A= ×

Note that, since n length(A), sufficient to defi lengt Ane .h( )n =∝

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

A

B

n An

=

=

input size Then A Bn n n= +

;;

; 2 2

2 2

Note that log and logT input size loghus + log

A B

n An A n B

B

Page 103: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

103

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 104: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

104

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 is the largest time required on any input of size n. (Worst case analysis.)

Page 105: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

105

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 106: The Time Complexity of an Algorithm - Çankaya Üniversitesi · 2013. 4. 9. · The Time Complexity of an Algorithm. Specifies how the running time depends on the size of the input

106

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.