Divide and Conquer
Master Theorem
Given any recurrence of the form T(n)=aT(bn)+cnk for all n>b, we have:
- If a>bk, then T(n)=Θ(nlogba)
- If a<bk, then T(n)=Θ(nk)
- If a=bk, then T(n)=Θ(nklogn)
Root Finding
Given a continuous function f and two points a<b such that f(a)⋅f(b)<0, there exists a root of f in the interval [a,b] by the intermediate value theorem. Since said root may be irrational, we aim to approximate it with an arbitrary precision ϵ.
- Algorithm: Bisect(a,b,ϵ)
- If b−a<ϵ, a is a suitable approximation
- Otherwise, calculate the midpoint m=(a+b)/2
- If f(m)≤0 then return Bisect(m,b,ϵ)
- else return Bisect(a,m,ϵ)
- Time: T(n)=T(2n)+O(1)=O(log(ϵb−a))
- Proof:
- P(k)= For any a,b such that kϵ≤∣a−b∣≤(k+1)ϵ, if f(a)f(b)≤0, then we find an ϵ approx to a root using logk queries to f.
- P(1): Output a+ϵ, since the whole interval is at most epsilon. This requires 0 calls to f.
- Suppose P(k) and consider an arbitrary a, b s.t. 2kϵ≤∣a−b∣≤(2k+1)ϵ.
- If f(a+kϵ)=0, output a+kϵ.
- If f(a)f(a+kϵ)<0, solve on the interval [a,a+kϵ]. By I.H. this takes at most log(k) queries of f.
- Otherwise, we have f(b)f(a+kϵ)<0, since f(a)f(b)<0 and f(a)f(a+kϵ)≥0. Solve the interval [a+kϵ,b].
- In any case, we used at most log(k)+1=\log(2k)queriestof$.
kth Smallest Element
- Algorithm: f(S∈Rn,k∈R)
- Select an approximate median element w using median of 5nmedianswithsubarraysofsize5$
- Partition each element into three sets, S>,S<,S=
- If k≤∣S<∣, recurse on f(S<,k)
- Else, if k≤∣S<∣+∣S=∣, return w
- Else, recurse on f(S>,k−∣S<∣−∣S=∣)