A divide-and-conquer algorithm divides an input into smaller instances, solves those instances recursively, and combines their answers. Directly solvable base cases terminate the recursion. The total time complexity includes both the recursive work and the work of dividing and combining; balanced sizes alone do not determine the running time.
For one element, return it as both extrema without a comparison. Otherwise compare consecutive pairs once, put each smaller element in a candidate array and each larger element in a candidate array . If one element is unpaired, put it in both candidate arrays. This simultaneous minimum and maximum construction computes the minimum of and maximum of by balanced divide-and-conquer algorithms: split the candidate array, recursively find the desired extremum in each half, and compare the two returned values. These extremum routines use one comparison per merge and therefore comparisons on candidates.
Every element excluded from is at least its paired element, so a global minimum remains in ; likewise a global maximum remains in . The recursive extremum routines are correct by structural induction. For even , there are pair comparisons and two searches on candidates, givingFor odd , there are pair comparisons and two searches on candidates, givingTogether with , both cases giveFor powers of two, the equivalent direct divide-and-conquer algorithm returns the ordered pair of extrema for each half, then combines the two minima with one comparison and the two maxima with one comparison. Its base cases must include , rather than performing two unnecessary comparisons on a pair. The recurrence relation is for , whence . Arbitrary near-equal splitting without the initial pairing can exceed the desired comparison bound for other values of ; the pairing construction above handles every positive size.
Put . With a nonnegative toll bounded by , the Master theorem gives the upper boundsIf the base cost is and the toll is , these become the usual tight bounds. The printed toll by itself does not imply those lower bounds; for example, the toll could vanish.
For the two requested proofs, let as permitted. Expanding the recurrence relation through levels givesWhen , the ratio is one. The sum has terms, so . With matching positive lower bounds on each level's work, the same expansion gives .
When , the ratio is strictly below one. The geometric series is at most , so the internal work is ; the leaf term is also . A root toll supplies the matching lower bound in the tight version. This proves both requested cases.
There is a small termination qualification in the printed generality: if , the literal base case only at may leave
ceil(n/b) = n at a small integer, for example . The standard rounded Master theorem assumes a sufficiently large fixed base-case cutoff so that recursive sizes decrease. The allowed unrounded powers-of- proof above does not use that problematic rounded step. Also, a literal number of recursive calls is integral, though the algebraic recurrence expansion remains meaningful for positive real .Maintain an inclusive interval with these recursion invariants: if , then ; if , then . The original full interval satisfies both conditions vacuously.In the left-recursion branch, the new right endpoint satisfies and the old left boundary is retained. The right-recursion branch is symmetric. Thus both recursion invariants persist.
LocalMaximum(A):
require length(A) >= 1
return Peak(0, length(A)-1)
Peak(lo, hi):
m = floor((lo+hi)/2)
if m > lo and A[m-1] > A[m]:
return Peak(lo, m-1)
if m < hi and A[m+1] > A[m]:
return Peak(m+1, hi)
return mIf neither branch is taken, exceeds its neighbours within the current interval because all entries are distinct. If is an endpoint of that interval and there is a neighbour outside, the boundary recursion invariant supplies the missing strict inequality. Hence the returned index is an array local maximum with respect to the original array, not just the restricted interval. Every branch shortens the interval, so termination is guaranteed; this also establishes existence, including the singleton case.
Each call performs at most two comparisons, and a recursive interval has at most half as many elements, up to rounding. Thus andThe conventional bound is for , with constant work for a singleton. Distinctness is essential to this proof of a strict local maximum.
Articles by others on the same topic
There are currently no matching articles.