Approximate overview
For assignment 2, we had to order functions according to their order. Should we be able to do that without graphing them?
I’m comfortable with you using whatever strategy you find best. However, it’s nice if you can do the mathematical manipulation to make the argument.
Let’s choose an example, sqrt(n) vs 2^sqrt(log2(n)). Are you willing to make a fool of yourself trying to solve this one?
2^(x^y): Are there other ways to think of that? This did not help
I know that 2^(log2(n)) = n by definition
Let k be log2(n). This is the important step, I think.
sqrt(2^k) vs 2^sqrt(k)
2^(k/2) vs. 2^sqrt(k)
k/2 dominates sqrt(k), so 2^(k/2) dominates 2^sqrt(k) Should I prove this? Nah
I think 2^sqrt(log2(n)) is in O(sqrt(n))
Can number eight be optional because Sam typed it wrong?
Yes.
If I can answer questions when you call on me and wake me up, can I sleep in class?
I’d prefer that you didn’t.
I’m not sure how to approach 2.1. Can we go over that one?
Summary: Suppose f(n) is in O(g(n)) and some other stuff.
Is f(n)*log2(f(n)^c) in O(g(n)*log2(g(n)))
Four options: Yes, no, sometimes/depends on c; sometimes/depends on f and/or g.
As you learned taking the SAT/ACT/…, multiple choice questions can be evil. We sometimes try to discard the obvious false choices.
Or we can try to see how to analyze. log2(f(n)^c) = c*log2(f(n)). That’s why there are handy-dandy log rules.
Am I screwed if c is negative? (Suggests answer 3.) Fortunately, we’ve been told that c is positive.
c*f(n)*log2(f(n)) vs. g(n)*log2(g(n))
Big Oh says I can throw away constants.
f(n)*log2(f(n)) vs. g(n)*log2(g(n))
The stupid c answer is wrong.
If f(n) is in O(g(n)), is log2(f(n)) in O(log2(g(n)))? Yes, provided g(n) is at least 2.
The book says f(n) and g(n) are at least 2.
So the answer is “Yes”, the original relation holds.
Moral: Math is necessary. Sometimes you have to look it up. Develop intuition?
Growth mindset says we can all develop mathematical intuition.
What questions do you have about the Nearest Neighbors algorithm?
Did we find the running time?
No. We might do that today. Or maybe Friday.
What is the running time?
Preview: O(nlogn)
Why do we need the points sorted by x and y?
We have them sorted by x to make it easy to split into “left half” and “right half”.
We have them sorted by y to make it easier to check the “crossover” pairs.
We can split the y list by identifying the largest x on the left half and taking only the points that are <= that (left) and > (right)
What’s a crossover pair?
One in which one point is on the left and one is on the right.
Was there something we were supposed to remember about crossover pairs?
Yes.
First, you look for them by iterating through the relevant points vertically. If a point is on the left, you also have to look at potentially nearby points on the right. If a point is on the right, you have to look at potentially nearby points on the right.
Second, you will only have to look for at most four nearby points on the other side.
Can it be less than four?
Yes. It could be zero.
Can we do an example in class?
Maybe Friday.
Do the coordinates have to be integers?
I don’t think so.
Are there any steps we do before finding the split pairs?
You filter out any points that are not within delta of the dividing line between left and right.
What did you learn from the Nearest Neighbors algorithm?
While you are used to loop counting for iterative algorithms, recursive algorithms generally require a different strategy.
We define a function, T(n), that represents the running time on an inputs of size n.
We write a recursive formulation of T(n) based on the behavior of the algorithm. For examples, I might say that because merge sort requires two recursive calls on half the size plus about n work to merge, T(n) <= 2*T(n/2) + n. I also know that T(1) = 1.
We attempt to convert that relation to a “fixed form” that does not involve a recursive reference.
Some other common (or otherwise interesting) recurrence relations
There are five main techniques people use when they encounter a recurrence relation.
We’ll look at the first three today and the last tomorrow.