CSC323 Sp2015 Module 1 Algorithm Efficiency
CSC323 Sp2015 Module 1 Algorithm Efficiency
Algorithm
Visiting a vertex or
Typical graph problem #vertices and/or edges
traversing an edge
Orders of Growth
• We are more interested in the order of growth on the number of times
the basic operation is executed on the input size of an algorithm.
• Because, for smaller inputs, it is difficult to distinguish efficient
algorithms vs. inefficient ones.
• For example, if the number of basic operations of two algorithms to
solve a particular problem are n and n2 respectively, then
– if n = 3, then we may say there is not much difference between requiring
3 basic operations and 9 basic operations and the two algorithms have
about the same running time.
– On the other hand, if n = 10000, then it does makes a difference whether
the number of times the basic operation is executed is n or n2.
Exponential-growth
functions
• Worst-Case: Cworst(n) = n
• Best-Case: Cbest(n) = 1
Probability-based Average-Case
Analysis of Sequential Search
• If p is the probability of finding an element in the list, then (1-p) is the
probability of not finding an element in the list.
• Also, on an n-element list, the probability of finding the search key as
the ith element in the list is p/n for all values of 1 ≤i ≤ n
• If p = 1 (the element that we will search for always exists in the list),
then Cavg(n) = (n+1)/2. That is, on average, we visit half of the entries
in the list to search for any element in the list.
• If p = 0 (all the time, the element that we will search never exists),
then Cavg(n) = n. That is, we visit all the elements in the list.
YouTube Link: https://www.youtube.com/watch?v=8V-bHrPykrE
Asymptotic Notations: Intro
2n ≤ 0.05 n2
for n ≥ 40
2n = O(n2)
0.05n2 ≥ 2n
for n ≥ 40
0.05n2 = Ω(n)
Asymptotic Notations: Intro
2n ≤ 5n
5n for n ≥ 1
2n = O(n)
2n ≥ n
for n ≥ 1
2n 2n = Ω(n)
n As 2n = O(n)
and 2n = Ω(n),
we say
2n = Θ(n)
n
Asymptotic Notations: Formal Intro
Note: If t(n) = O(g(n)) g(n) = Ω(t(n)); also, if t(n) = Ω(g(n)) g(n) = O(t(n))
Asymptotic Notations: Formal Intro
t(n) = Θ(g(n))
c2*g(n) ≤ t(n) ≤ c1*g(n) for all n ≥ n0
c1 and c2 are positive constants (> 0)
and n0 is a non-negative integer
Asymptotic Notations: Examples
• Let t(n) and g(n) be any non-negative functions defined on a set of all
real numbers.
• We say t(n) = O(g(n)) for all functions t(n) that have a lower or the
same order of growth as g(n), within a constant multiple as n ∞.
– Examples:
• We say t(n) = Ω(g(n)) for all functions t(n) that have a higher or the
same order of growth as g(n), within a constant multiple as n ∞.
– Examples:
• We say t(n) = Θ(g(n)) for all functions t(n) that have the same order of
growth as g(n), within a constant multiple as n ∞.
– Examples: an2 + bn + c = Θ(n2);
n2 + logn = Θ(n2)
Useful Property of Big-Oh Notation
• If t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)) , then
t1(n) + t2(n) ∈ O(max{g1(n), g2(n)})
L’Hopital’s Rule
Note: t’(n) and g’(n) are first-order derivatives of t(n) and g(n)
Stirling’s Formula
Example 1: To Determine the
Order of Growth
Example 1: To Determine the
Order of Growth (continued…)
Example 2: To Determine the
Order of Growth
Example 2: To Determine the
Order of Growth (continued…)
Examples to Compare the Order of Growth
b)
c)
d)
Some More Examples: Order of Growth
∑1 = (u − l + 1)
i =l
Examples on Summation
• 1 + 3 + 5 + 7 + …. + 999
• 2 + 4 + 8 + 16 + … + 1024
Example 1: Finding Max. Element
After discarding the constants and lower order terms, the overall time
complexity is Θ(worst-case comparisons) = Θ(n).
After discarding the constants and lower order terms, the overall time
complexity is O(worst-case comparisons) = O(n).
Example 4: Element Uniqueness Problem
Best-case situation:
If the two first elements of the array are the same, then we can exit
after one comparison. Best case = 1 comparison.
Worst-case situation:
• The basic operation is the comparison in the inner loop. The worst-
case happens for two-kinds of inputs:
– Arrays with no equal elements
– Arrays in which only the last two elements are the pair of equal
elements
Example 4: Element Uniqueness Problem
• For these kinds of inputs, one comparison is made for each repetition
of the innermost loop, i.e., for each value of the loop’s variable j
between its limits i+1 and n-1; and this is repeated for each value of the
outer loop i.e., for each value of the loop’s variable i between its limits 0
and n-2. Accordingly, we get,
• Check whether the number of times the basic op. is executed may vary
on different inputs of the same size. (If it may, the worst, average, and
best cases must be investigated separately.)
• Solve the recurrence (or, at the very least, establish its solution’s order
of growth) by backward substitutions or another method.
Recursive Evaluation of n!
Definition: n ! = 1 ∗ 2 ∗ … ∗(n-1) ∗ n for n ≥ 1 and 0! = 1
• Recursive definition of n!: F(n) = F(n-1) ∗ n for n ≥ 1 and
F(0) = 1
# Divisions
Since the recursive calls end when n is equal to 1 and there are no divisions
made, the initial condition is: A(1) = 0.
=Θ(2n)
2)
3)
4)
5)
6)