Algorithms Analysis
Algorithms Analysis
performance?
• Algorithms help us to understand scalability.
• Performance often draws the line between what
is feasible and what is impossible.
• Algorithmic mathematics provides a language
for talking about program behavior.
• Performance is the currency of computing.
• The lessons of program performance generalize
to other computing resources.
• Speed is fun!
Asymptotic Complexity
• Running time of an algorithm as a function of
input size n for large n.
• Expressed using only the highest-order term
in the expression for the exact running time.
– Instead of exact running time, say Θ(n2).
• Describes behavior of function in the limit.
• Written using Asymptotic Notation.
Asymptotic Notation
• Θ, O, Ω, o, ω
• Defined for functions over the natural numbers.
– Ex: f(n) = Θ(n2).
– Describes how f(n) grows in comparison to n2.
• Define a set of functions; in practice used to compare
two function sizes.
• The notations describe different rate-of-growth
relations between the defining function and the
defined set of functions.
Comp 122
Θ-notation
For function g(n), we define Θ(g(n)),
big-Theta of n, as the set:
Θ(g(n)) = {f(n) :
∃ positive constants c1, c2, and
n0, such that ∀n ≥ n0,
we have 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n)
}
Intuitively: Set of all functions that
have the same rate of growth as g(n).
Comp 122
O-notation
For function g(n), we define O(g(n)),
big-O of n, as the set:
O(g(n)) = {f(n) :
∃ positive constants c and n0,
such that ∀n ≥ n0,
we have 0 ≤ f(n) ≤ cg(n) }
Intuitively: Set of all functions whose rate of
growth is the same as or lower than that of
g(n).
Comp 122
Relations Between Θ, O, Ω
Comp 122
Relations Between Θ, Ω, O
Theorem : For any two functions g(n) and f(n),
f(n) = Θ(g(n)) iff
f(n) = O(g(n)) and f(n) = Ω(g(n)).
Comp 122
Examples
• Consider the following functions:
f(n) = n
g(n) = n/8
For f(n) = θ(g(n)), find c1, c2 and n0.
C1 = 1, C2=8, n0 =1
• Consider the following functions:
f(n) = n2
g(n) = n
For f(n) = big omega(g(n))
C=1 and n0 =1
Exercise
• 10n4 + 5n + 7 = O(n4) Find C and n0
• 10n4 + 5n + 7 = Ω(n4) Find C and n0
•
Examples
– n2/2 –n/2 = Θ(n2)
• ½ n2 - ½ n ≤ ½ n2 ∀n ≥ 0 ⇒ c2= ½
• ½ n2 - ½ n ≥ ½ n2 - ½ n * ½ n ( ∀n ≥ 2 ) = ¼ n2 ⇒ c1= ¼
– n ≠ Θ(n2): c1 n2 ≤ n ≤ c2 n2
12
Examples
– 6n ≠ Θ(n ): c1 n ≤ 6n ≤ c2 n2
3 2 2 3
⇒ c2 ≥ n/logn, ∀ n≥ n0 – impossible
13
Example of log n!
Recall that 1! = 1 and n! = (n-1)! n.
17
Asymptotic Notations in Equations
• On the right-hand side
– Θ(n2) stands for some anonymous function in Θ(n2)
2n2 + 3n + 1 = 2n2 + Θ(n) means:
There exists a function f(n) ∈ Θ(n) such that
2n2 + 3n + 1 = 2n2 + f(n)
• On the left-hand side
2n2 + Θ(n) = Θ(n2)
No matter how the anonymous function is chosen on
the left-hand side, there is a way to choose the
anonymous function on the right-hand side to make
the equation valid.
18
Common Summations
• Arithmetic series:
• Geometric series:
• Harmonic series:
19
Mathematical Induction
• A powerful, rigorous technique for proving that
a statement S(n) is true for every natural
number n, no matter how large.
• Proof: