Algorithm Search⌘K

Proofs: Ternary search

These are the human proofs of Ternary search, at full length.

Ternary search keeps the mode in its interval and shrinks the interval by a factor of at least 2/3 full

This proof shows: correctness.

We use the pseudocode of the page. Let ff be strictly unimodal for a maximum on its domain, with mode mm, as defined on the page unimodal function. We use its comparison lemma: for x1<x2x_1 < x_2, f(x1)<f(x2)f(x_1) < f(x_2) implies m>x1m > x_1; f(x1)>f(x2)f(x_1) > f(x_2) implies m<x2m < x_2; and f(x1)=f(x2)f(x_1) = f(x_2) implies x1<m<x2x_1 < m < x_2.

Claim 1 (real version). If m∈[lo,hi]m \in [lo, hi] before the call of TERNARY-MAX(f,lo,hi,steps)(f, lo, hi, steps), then after the loop m∈[lo,hi]m \in [lo, hi] and hi−lo=(2/3)stepshi - lo = (2/3)^{steps} times the initial length. The returned point is within half of that length of mm.

Proof. Invariant: before each iteration, m∈[lo,hi]m \in [lo, hi]. It holds at the start by assumption. In an iteration, lines L2 and L3 give lo<m1<m2<hilo < m_1 < m_2 < hi when lo<hilo < hi, with m1−lo=hi−m2=(hi−lo)/3m_1 - lo = hi - m_2 = (hi - lo)/3.

  • If f(m1)<f(m2)f(m_1) < f(m_2) (line L4), then m>m1m > m_1, so m∈(m1,hi]⊆[m1,hi]m \in (m_1, hi] \subseteq [m_1, hi], the new interval.
  • Else f(m1)≥f(m2)f(m_1) \ge f(m_2). If the inequality is strict, m<m2m < m_2. If the values are equal, m1<m<m2m_1 < m < m_2. In both cases m∈[lo,m2)m \in [lo, m_2), the new interval of line L5.

So the invariant holds after the iteration. In both branches the new length is (hi−lo)−(hi−lo)/3=23(hi−lo)(hi - lo) - (hi - lo)/3 = \tfrac23 (hi - lo). After stepssteps iterations the length is (2/3)steps(2/3)^{steps} times the initial one. Line L6 returns the midpoint of an interval that holds mm, so its distance to mm is at most half the length. If lo=hilo = hi at some point, then m=lom = lo and every later iteration keeps lo=hilo = hi. □\square

The proof uses exact arithmetic. In floating point the computed m1m_1 and m2m_2 can be equal when the interval holds few doubles; the case analysis then still keeps mm in the interval, but the length stops shrinking.

Claim 2 (integer version). For integers lo≤hilo \le hi with m∈{lo,…,hi}m \in \{lo, \ldots, hi\}, TERNARY-INT(f,lo,hi)(f, lo, hi) returns mm. With n=hi−lo+1≥2n = hi - lo + 1 \ge 2 candidates it makes at most ⌈log⁡3/2n⌉\lceil \log_{3/2} n \rceil iterations and 2⌈log⁡3/2n⌉2 \lceil \log_{3/2} n \rceil evaluations of ff.

Proof. Invariant: lo≤m≤hilo \le m \le hi before each test of line L7. It holds at the start. In an iteration lo<hilo < hi; let ℓ=hi−lo≥1\ell = hi - lo \ge 1 and d=⌊ℓ/3⌋≥0d = \lfloor \ell/3 \rfloor \ge 0 (line L8). Line L9 sets m1=lo+dm_1 = lo + d and m2=hi−dm_2 = hi - d, so m2−m1=ℓ−2d≥ℓ/3>0m_2 - m_1 = \ell - 2d \ge \ell/3 > 0 and lo≤m1<m2≤hilo \le m_1 < m_2 \le hi.

  • If f(m1)<f(m2)f(m_1) < f(m_2), then m>m1m > m_1, so m≥m1+1m \ge m_1 + 1, the new lolo of line L10. The bound m≤him \le hi is unchanged.
  • Else, as in Claim 1, m<m2m < m_2, so m≤m2−1m \le m_2 - 1, the new hihi of line L11.

So the invariant holds. When the loop stops, lo≥hilo \ge hi; together with lo≤m≤hilo \le m \le hi this gives lo=m=hilo = m = hi, and line L12 returns mm.

Termination and count. Let s=hi−lo+1s = hi - lo + 1 be the number of candidates. One iteration removes at least d+1d + 1 of them, so the new count is at most s−1−⌊(s−1)/3⌋=⌈2(s−1)/3⌉≤2(s−1)+23=2s3s - 1 - \lfloor (s - 1)/3 \rfloor = \lceil 2(s-1)/3 \rceil \le \tfrac{2(s-1) + 2}{3} = \tfrac{2s}{3}. The count drops by at least 1 per iteration, so the loop ends. After tt iterations s≤(2/3)tns \le (2/3)^t n. The loop runs only while s≥2s \ge 2, so if it makes t≥1t \ge 1 iterations then 2≤(2/3)t−1n2 \le (2/3)^{t-1} n, that is t≤1+log⁡3/2(n/2)<log⁡3/2nt \le 1 + \log_{3/2}(n/2) < \log_{3/2} n, because log⁡3/22>1\log_{3/2} 2 > 1. Hence t≤⌈log⁡3/2n⌉t \le \lceil \log_{3/2} n \rceil. Each iteration evaluates ff twice and nothing else does, which gives the bound on evaluations. □\square

Back to Ternary search