373 HW2soln
373 HW2soln
Solution
The graphs describing the behavior of these algorithms start out with A higher (slower)
than B, and eventually cross. After the point where they cross, B is always higher than
A. Therefore we need to find the point where they cross, that is the value where
8nlogn = 2n2. Applying algebra we get:
8nlogn = 2n2
4nlogn = n2
4 logn = n
4 = n / logn
Solving for n we get n=16, since 4 = 16/log216 = 16/4 = 4
Thus n0 = 17, since for all n >= 17, A will be faster than B (at 16 they’re equal.)
Another acceptable solution was to show the number of operations for each algorithm for
all n up to the point where they cross.
Common problems
Many people used logs with bases other than 2. Common ones were log10 and ln. It’s
common in algorithmic analysis to use log base 2, as many algorithms use a divide and
conquer strategy that naturally produces log base 2 behavior. Some people had the right
idea but made an algebraic mistake. Partial credit was given. No credit was lost if you
put 16 instead of 17 (i.e. if you confused > with >=.)
Solution
The approach is the same as in 3.2. Equate the formulae and solve for n.
40n2 = 2n3
20 n2 = n3
20 = n
Thus n0 = 21, since for all n >= 21, A will be faster than B (at 20, they’re equal.)
What is the sum of all the even numbers from 0 to 2n for any positive integer n? Use
induction to prove your answer.
Solution
Base case: n = 1
Sum(2*i) for i = 0, 1 is 0 + 2 = 2.
n(n+1) = 1(1+1) = 1(2) = 2
So the base case works.
Inductive hypothesis:
Assume: Sum(2*i) for i=0..n = n(n+1)
Show: same holds for n+1
Sum(2*i) for i=0..n+1 = (n+1)((n+1)+1) = (n+1)( n+2) = n2 + 3n + 2
Common problems
Many people simply didn’t do any inductive proof.
Solution
Ordered from least to greatest:
Explanation:
210 is constant time
2logn is linear O(n) by the definition of log
3n+100logn is O(n)
4n is also O(n) and larger than 3n+100logn because the 4n term is larger than the 3n term
nlogn is O(nlogn)
4nlogn+2n is also O(nlogn) because the nlogn term dominates the 2n term
n2+10n is O(n2)
n3 is O(n3)
2n is O(2n) - exponential
Common mistakes
Many people missed the fact that 2logn is linear. If that was your only mistake, I didn’t
take off. I also didn’t penalize as heavily in comparing within an order (e.g. 3n+100logn
and 4n are both O(n) so I didn’t count off as much is you flipped them but kept them in
the same place relative to all the other entries.)