A question on the midterm that I know I didn't do well on was very closely related to the second question on A2 which I think would be valuable to go through now:
For some context the part of the question that is relevant to time complexity question I had issues with was to show that the closed form for binary search was in the O(logn). Lets show that now...
A closed form of the recurrence function for Binary Search is: T(n) = logn + 1
To show that T ∈ Θ(logn) one can demonstrate 1) T ∈ O(logn) and 2) T ∈ Ω(logn).
Using the fact that T(n) has been proved to be a non-decreasing function I will use a Lemma discussed in lecture (http://www.cdf.toronto.edu/~heap/236/F12/Lectures/W6/thursday-annotated.pdf Page 6) that when n is not necessarily an integer power of 2 we have possible values of T(n) within a range of ň/2 < n ≤ ň. (Where ň represents 2⌈ logn ⌉, and n represents 2 logn).
So for part 1 I will show T ∈ O(logn)
Claim T(n) ≤ clogn
T(n) ≤ T(ň) # Because we proved T is non-decreasing
= lgň + 1 # Closed form of T
≤ lg2n + 1 # Because n > ň/2 from above fact
= (lgn + log2) + 1 # By log rules
= (lgn + 1) + 1
= lgn + 2
T(n) ≤ lgn + 2
Thus T ∈ O(lgn) with c = 1 & B = 1 (Breakpoint)
Now the second part 2
Claim T(n) ≥ clogn
T(n) ≥ T(ň/2) # Because we proved T is non-decreasing
≥ log(ň/2) + 1 # Closed form of T
= (logň – log2) + 1 # Log rules
= logň – 1 + 1
= logň
T(n) ≥ logn # Because ň is the ceiling of n (ň = 2⌈ logn ⌉ & n = 2logn )
Thus T ∈ Ω(logn) with c = 1 & B = 1 (Breakpoint)
Wow, now we're done
By showing that T is both in O(logn) & Ω (logn) then this implies T ∈ Θ(logn)!
I dunno why this simple kind of proof messed me up during the term test. I blaim it on the time constraint or maybe I was just having a bad day! haha
No comments:
Post a Comment