Divide and Conquer Algorithms

Category: Algorithms ..

Divide and Conquer

Master Theorem

Given any recurrence of the form T(n)=aT(nb)+cnkT(n) = a T(\frac{n}{b}) + c n^k for all n>bn > b, we have:

  • If a>bka > b^k, then T(n)=Θ(nlogba)T(n) = \Theta(n^{\log_b a})
  • If a<bka < b^k, then T(n)=Θ(nk)T(n) = \Theta(n^k)
  • If a=bka = b^k, then T(n)=Θ(nklogn)T(n) = \Theta(n^k \log n)

Root Finding

Given a continuous function ff and two points a<ba < b such that f(a)f(b)<0f(a) \cdot f(b) < 0, there exists a root of ff in the interval [a,b]\lbrack a, b \rbrack by the intermediate value theorem. Since said root may be irrational, we aim to approximate it with an arbitrary precision ϵ\epsilon.

  • Algorithm: Bisect(a,b,ϵ)Bisect(a, b, \epsilon)
  • If ba<ϵb - a < \epsilon, aa is a suitable approximation
  • Otherwise, calculate the midpoint m=(a+b)/2m = (a + b)/2
  • If f(m)0f(m) \le 0 then return Bisect(m,b,ϵ)Bisect(m, b, \epsilon)
  • else return Bisect(a,m,ϵ)Bisect(a, m, \epsilon)
  • Time: T(n)=T(n2)+O(1)=O(log(baϵ))T(n) = T(\frac{n}{2}) + O(1) = O(\log(\frac{b - a}{\epsilon}))
  • Proof:
  • P(k)=P(k) = For any a,ba, b such that kϵab(k+1)ϵk\epsilon \le |a - b| \le (k + 1)\epsilon, if f(a)f(b)0f(a)f(b) \le 0, then we find an ϵ\epsilon approx to a root using logk\log k queries to ff.
  • P(1)P(1): Output a+ϵa + \epsilon, since the whole interval is at most epsilonepsilon. This requires 00 calls to ff.
  • Suppose P(k)P(k) and consider an arbitrary aa, bb s.t. 2kϵab(2k+1)ϵ2k\epsilon \le |a - b| \le (2k + 1)\epsilon.
  • If f(a+kϵ)=0f(a + k\epsilon) = 0, output a+kϵa + k\epsilon.
  • If f(a)f(a+kϵ)<0f(a)f(a + k\epsilon) < 0, solve on the interval [a,a+kϵ]\lbrack a, a + k\epsilon \rbrack. By I.H. this takes at most log(k)\log(k) queries of ff.
  • Otherwise, we have f(b)f(a+kϵ)<0f(b)f(a + k\epsilon) < 0, since f(a)f(b)<0f(a)f(b) < 0 and f(a)f(a+kϵ)0f(a)f(a + k\epsilon) \ge 0. Solve the interval [a+kϵ,b]\lbrack a + k\epsilon, b \rbrack.
  • In any case, we used at most log(k)+1=\log(k) + 1 = \log(2k)queriesto queries to f$.

kth Smallest Element

  • Algorithm: f(SRn,kR)f(S \in \mathbb{R}^n, k \in \mathbb{R})
  • Select an approximate median element ww using median of n5medianswithsubarraysofsize\frac{n}{5} medians with subarrays of size 5$
  • Partition each element into three sets, S>,S<,S=S_{>}, S_{<}, S_{=}
  • If kS<k \le |S_{<}|, recurse on f(S<,k)f(S_{<}, k)
  • Else, if kS<+S=k \le |S_{<}| + |S_{=}|, return ww
  • Else, recurse on f(S>,kS<S=)f(S_{>}, k - |S_{<}| - |S_{=}|)