Ternary Search
   HOME

TheInfoList



OR:

A ternary search algorithm is a technique in
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
for finding the minimum or maximum of a
unimodal In mathematics, unimodality means possessing a unique mode. More generally, unimodality means there is only a single highest value, somehow defined, of some mathematical object. Unimodal probability distribution In statistics, a unimodal p ...
function. A ternary search determines either that the minimum or maximum cannot be in the first third of the domain or that it cannot be in the last third of the domain, then repeats on the remaining two thirds. A ternary search is an example of a
divide and conquer algorithm In computer science, divide and conquer is an algorithm design paradigm. A divide-and-conquer algorithm recursively breaks down a problem into two or more sub-problems of the same or related type, until these become simple enough to be solved direc ...
(see
search algorithm In computer science, a search algorithm is an algorithm designed to solve a search problem. Search algorithms work to retrieve information stored within particular data structure, or calculated in the search space of a problem domain, with eith ...
).


The function

Assume we are looking for a maximum of f(x) and that we know the maximum lies somewhere between A and B. For the algorithm to be applicable, there must be some value x such that * for all a, b with A \leq a < b \leq x, we have f(a) < f(b), and * for all a, b with x \leq a < b \leq B, we have f(a) > f(b).


Algorithm

Let f(x) be a
unimodal In mathematics, unimodality means possessing a unique mode. More generally, unimodality means there is only a single highest value, somehow defined, of some mathematical object. Unimodal probability distribution In statistics, a unimodal p ...
function on some interval ; r/math>. Take any two points m_1 and m_2 in this segment: l < m_1 < m_2 < r. Then there are three possibilities: * if f(m_1) < f(m_2), then the required maximum can not be located on the left side – ; m_1/math>. It means that the maximum further makes sense to look only in the interval _1; r/math> * if f(m_1) > f(m_2), that the situation is similar to the previous, up to symmetry. Now, the required maximum can not be in the right side – _2; r/math>, so go to the segment ; m_2/math> * if f(m_1) = f(m_2), then the search should be conducted in _1; m_2/math>, but this case can be attributed to any of the previous two (in order to simplify the code). Sooner or later the length of the segment will be a little less than a predetermined constant, and the process can be stopped. choice points m_1 and m_2: * m_1 = l + (r - l) / 3 * m_2 = r - (r - l) / 3 ; Run time order : T(n) = T(2n/3) + 1 = \Theta(\log n)


Recursive algorithm

def ternary_search(f, left, right, absolute_precision) -> float: """Left and right are the current bounds; the maximum is between them. """ if abs(right - left) < absolute_precision: return (left + right) / 2 left_third = (2*left + right) / 3 right_third = (left + 2*right) / 3 if f(left_third) < f(right_third): return ternary_search(f, left_third, right, absolute_precision) else: return ternary_search(f, left, right_third, absolute_precision)


Iterative algorithm

def ternary_search(f, left, right, absolute_precision) -> float: """Find maximum of unimodal function f() within eft, right To find the minimum, reverse the if/else statement or reverse the comparison. """ while abs(right - left) >= absolute_precision: left_third = left + (right - left) / 3 right_third = right - (right - left) / 3 if f(left_third) < f(right_third): left = left_third else: right = right_third # Left and right are the current bounds; the maximum is between them return (left + right) / 2


See also

*
Newton's method in optimization In calculus, Newton's method is an iterative method for finding the roots of a differentiable function , which are solutions to the equation . As such, Newton's method can be applied to the derivative of a twice-differentiable function to fi ...
(can be used to search for where the derivative is zero) *
Golden-section search The golden-section search is a technique for finding an extremum (minimum or maximum) of a function inside a specified interval. For a strictly unimodal function with an extremum inside the interval, it will find that extremum, while for an interv ...
(similar to ternary search, useful if evaluating f takes most of the time per iteration) *
Binary search algorithm In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the m ...
(can be used to search for where the derivative changes in sign) *
Interpolation search Interpolation search is an algorithm for Search algorithm, searching for a key in an array that has been Collation, ordered by numerical values assigned to the keys (''key values''). It was first described by W. W. Peterson in 1957. Interpolation ...
*
Exponential search In computer science, an exponential search (also called doubling search or galloping search or Struzik search) is an algorithm, created by Jon Bentley and Andrew Chi-Chih Yao in 1976, for searching sorted, unbounded/infinite lists. There are num ...
*
Linear search In computer science, a linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched. A linear search runs in at ...

N Dimensional Ternary Search Implementation


References

{{Unreferenced, date=May 2007 Search algorithms Optimization algorithms and methods