Analysis of Algorithms: BIG Notation
Analysis of Algorithms: BIG Notation
Dr : Mohammed Al-Hubaishi
Analysis of Algorithms
BIG O notation
DR . MOHAMMED AL-HUBAISHI
2
Key Topics:
► Introduction
► Running Time of growth known functions.
► Big-Oh, Big Oh Omega and Big Theta Notations
► Doing a Timing Analysis
► Analyzing Some Simple Programs.
Dr : Mohammed Al-Hubaishi
4
Analysis of running time
Dr : Mohammed Al-Hubaishi
5
Measurement of Complexity of an Algorithm
Dr : Mohammed Al-Hubaishi
6
Measuring Speed
Dr : Mohammed AL-HUBAISHI
8
Running time for small inputs
Dr : Mohammed Al-Hubaishi
9
Generalizing Running Time
n)
O(2n)
g
n 2)
O( )
lo
n3
(n
O(
O
O(n)
O(√n)
O(log n)
O(1)
Dr: Mohammed Al-Hubaishi
19
Common plots of the growth functions
Big O, Big Ω, and Big Θ
Relate the growth of functions
https://drive.google.com/file/d/1kru9dOYiaSQBqUzsdAhd8SrutU50ybN7/view
page 80
25
Real vs Integer functions
(1)
In graph, n0 is the minimum possible value to get valid bounds, but any greater value will work. n0 here is 3
Dr: Mohammed Al-Hubaishi
27
F(n) is Big Omega of g(n):
Bound from below
we can bound f(n) from below, if we find
constant (c) such that when we multiply with
g(n), it will bound f(n) below from some point
(n0) onward.
In this case we say:
(2)
In graph, n0 is the minimum possible value to get valid bounds, but any greater value will work. n0 here is 7
Dr: Mohammed Al-Hubaishi
28
F(n) is Big Theta of g(n):
Bound between
If there are constants such that
g(n) bound f(n) both from below
and above, then we say :
F(n) is Big Theta of g(n)
Of course the constants are
different. So that we label them
as c1 and c2
(3)
In graph, n0 is the minimum possible value to get valid bounds, but any greater value will work. n0 here is 8
Dr: Mohammed Al-Hubaishi
29
Mappings for n2
f(n) is Big Oh Mega
⚫F(n)= n2 Ω (n2 )
Θ(n2)
4n + 4n = 8n > (4n + 5 )
C=8
Since we have produced a constant C that works for all n, we can conclude:
nj <= nd if j <= d
Then since we can change the exponents of all the terms to the highest degree (the original
function must be less than this too).
Finally, we add these terms together to get the largest constant C we need to find a function
that is an upper bound on the original one.
Dr: Mohammed Al-Hubaishi
37
For Loop Nested
► If you have a single for loop nested in another for loop, the run time for
the inner for loop is O(n), happens for each iteration of the outer for
loop, which again is O(n).
► The reason each of these individually are O(n) is because they take a
linear amount of time given the size of the input.
► The larger the input the longer it takes on a linear scale, n.
► To work out the math, which in this case is trivial, just multiple the
complexity of the inner loop by the complexity of the outer loop.
n * n = n2.
Because remember, for each n in the outer loop, you must again do n for
the inner. To clarify: n times for each n :: O(n * n) == O(n2)
https://en.wikipedia.org/wiki/Gear
40
Book References
41