Documente Academic
Documente Profesional
Documente Cultură
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
Georgy Gimelfarb
COMPSCI 220 Algorithms and Data Structures
1 / 15
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
2 / 15
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
Running time is proportional to: T (n) log n T (n) n T (n) n log n T (n) n2 T (n) n3 T (n) nk T (n) 2n T (n) k n ; k > 1
T (n) Complexity: logarithmic linear linearithmic quadratic cubic polynomial exponential exponential
3 / 15
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
algorithm, or its running time. The running time of a program implementation: cf (n)
The constant factor c can rarely be determined and depends
on a computer, operating system, language, compiler, etc. When the input size increases from n = n1 to n = n2 , all other factors being equal, the relative running time of the program (n2 ) cf (n1 ) f (n1 ) increases by a factor of T T (n1 ) = cf (n2 ) = f (n2 ) .
4 / 15
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
f (n) f (5)
of Running Time
The approximate running time for large input sizes gives enough information to distinguish between a good and a bad algorithm. 52 = 25 1 2 5 10 52 = 25 53 = 125 220 106 Input size n 53 = 125 1 3 25 75 54 = 625 56 = 15, 625 2120 1036 54 = 625 1 4 125 500 56 = 15, 625 59 = 1, 953, 125 2620 10187
5 / 15
Function f (n) 5 Constant 1 1 Q Logarithmic log5 n 1 Q Linear n 1 Q Linearithmic n log5 n 1 Q Quadratic n2 1 Q Cubic n3 1 Q Exponential 2n 1
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
Big Oh O(. . .) Formal Denition The function g (n) is O(f (n)) (read: g (n) is Big Oh of f (n)) i there exists a positive real constant c and a positive integer n0 such that g (n) cf (n) for all n > n0 .
The notation i abbreviates if and only if.
6 / 15
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
7 / 15
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
and dier for dierent particular functions g (n) in O(f (n)). Notations g (n) = O (f (n)) or g (n) is O (f (n)) mean actually
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
and dier for dierent particular functions g (n) in O(f (n)). Notations g (n) = O (f (n)) or g (n) is O (f (n)) mean actually
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
Big Omega (. . .)
The function g (n) is (f (n)) i there exists a positive real constant c and a positive integer n0 such that g (n) cf (n) for all n > n0 .
(. . .) is complementary to O (. . .). It generalises the concept of lower bound () in the same
way as O(. . .) generalises the concept of upper bound (): if g (n) is (f (n)) then f (n) is O(g (n)).
Example 1: 5n2 is (n) because 5n2 5n for n 1. Example 2: 0.01n is (log n) because 0.01n 0.5 log10 n for
n 100.
9 / 15
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
Big Omega (. . .)
The function g (n) is (f (n)) i there exists a positive real constant c and a positive integer n0 such that g (n) cf (n) for all n > n0 .
(. . .) is complementary to O (. . .). It generalises the concept of lower bound () in the same
way as O(. . .) generalises the concept of upper bound (): if g (n) is (f (n)) then f (n) is O(g (n)).
Example 1: 5n2 is (n) because 5n2 5n for n 1. Example 2: 0.01n is (log n) because 0.01n 0.5 log10 n for
n 100.
9 / 15
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
Big Omega (. . .)
The function g (n) is (f (n)) i there exists a positive real constant c and a positive integer n0 such that g (n) cf (n) for all n > n0 .
(. . .) is complementary to O (. . .). It generalises the concept of lower bound () in the same
way as O(. . .) generalises the concept of upper bound (): if g (n) is (f (n)) then f (n) is O(g (n)).
Example 1: 5n2 is (n) because 5n2 5n for n 1. Example 2: 0.01n is (log n) because 0.01n 0.5 log10 n for
n 100.
9 / 15
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
Big Theta (. . .)
The function g (n) is (f (n)) i there exists two positive real constants c1 and c2 and a positive integer n0 such that c1 f (n) g (n) c2 f (n) for all n > n0 .
If g (n) is (f (n)) then g (n) is O (f (n)) and f (n) is O (g (n))
or, what is the same, g (n) is O(f (n)) and g (n) is (f (n)):
g (n) is O (f (n)) g (n) c f (n) for n > n . g (n) is (f (n)) f (n) c g (n) for n > n . 1 g (n) is (f (n)) c = c ; c = c2 , and n0 = max{n , n }. 1
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
Big Theta (. . .)
The function g (n) is (f (n)) i there exists two positive real constants c1 and c2 and a positive integer n0 such that c1 f (n) g (n) c2 f (n) for all n > n0 .
If g (n) is (f (n)) then g (n) is O (f (n)) and f (n) is O (g (n))
or, what is the same, g (n) is O(f (n)) and g (n) is (f (n)):
g (n) is O (f (n)) g (n) c f (n) for n > n . g (n) is (f (n)) f (n) c g (n) for n > n . 1 g (n) is (f (n)) c = c ; c = c2 , and n0 = max{n , n }. 1
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
Big Theta (. . .)
The function g (n) is (f (n)) i there exists two positive real constants c1 and c2 and a positive integer n0 such that c1 f (n) g (n) c2 f (n) for all n > n0 .
If g (n) is (f (n)) then g (n) is O (f (n)) and f (n) is O (g (n))
or, what is the same, g (n) is O(f (n)) and g (n) is (f (n)):
g (n) is O (f (n)) g (n) c f (n) for n > n . g (n) is (f (n)) f (n) c g (n) for n > n . 1 g (n) is (f (n)) c = c ; c = c2 , and n0 = max{n , n }. 1
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
asymptotically, for large n, but not near the origin where it may make no sense.
If the constants c involved are very large, the asymptotical
has to show that the desired constants do not exist, i.e. lead to a contradiction. g (n) and f (n) in the Big-Oh, -Omega, and -Theta denitions mostly relate, respectively, to exact and rough approximate (like log n, n, n2 , etc) running time on inputs of size n.
11 / 15
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
Big-Theta, describes an asymptotic behaviour for large problem sizes. Only the dominant terms as n need to be shown as the argument of Big-Oh, Big-Omega, and Big-Theta.
12 / 15
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
Is it also O(n3 )? T (n) = 3n2 + 5n + 1 is O(n2 ) 8 3 8 2 3 T (n) = 10 n + 10 n + 30 is O(n ) Is it also (n2 )? T (n) = 108 n8 + 1000n + 1 is O(n8 ) Is it also (n8 )? O(nm ); m k (nk ); T (n) = Pk (n) is (nm ); m k
13 / 15
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
Is it also O(n3 )? T (n) = 3n2 + 5n + 1 is O(n2 ) 8 3 8 2 3 T (n) = 10 n + 10 n + 30 is O(n ) Is it also (n2 )? T (n) = 108 n8 + 1000n + 1 is O(n8 ) Is it also (n8 )? O(nm ); m k (nk ); T (n) = Pk (n) is (nm ); m k
13 / 15
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
mn+k ln+k = lk ln for any constant k . A brute-force search for the best combination of n binary decisions by exhausting all the 2n possible combinations has exponential time complexity!
230 109 = 1, 000, 000, 000 and
14 / 15
Outline
Complexity
Basic tools
Big-Oh
Big Omega
Big Theta
Examples
For each m > 1, the logarithmic function g (n) = logm (n) has the same rate of increase as lg(n), i.e. log2 n, because logm (n) = logm (2) lg(n) for all n > 0.
Omit the logarithm base when using Big-Oh, Big-Omega, and Big-Theta notation: logm n is O(log n), (log n), and (log n).
You will nd later that the most ecient search for data in an ordered array has logarithmic time complexity.
15 / 15