To install click the Add extension button. That's it.

The source code for the WIKI 2 extension is being checked by specialists of the Mozilla Foundation, Google, and Apple. You could also do it yourself at any point in time.

4,5
Kelly Slayton
Congratulations on this excellent venture… what a great idea!
Alexander Grigorievskiy
I use WIKI 2 every day and almost forgot how the original Wikipedia looks like.
Live Statistics
English Articles
Improved in 24 Hours
Added in 24 Hours
What we do. Every page goes through several hundred of perfecting techniques; in live mode. Quite the same Wikipedia. Just better.
.
Leo
Newton
Brights
Milds

Ternary search

From Wikipedia, the free encyclopedia

A ternary search algorithm[1] is a technique in computer science for finding the minimum or maximum of a unimodal function.

YouTube Encyclopedic

  • 1/3
    Views:
    1 870
    104 015
    45 339
  • Ternary Search | Code Tutorial
  • Trie Data Structure
  • 5.3 How to use Ternary Operator in Java Tutorial

Transcription

The function

Assume we are looking for a maximum of and that we know the maximum lies somewhere between and . For the algorithm to be applicable, there must be some value such that

  • for all with , we have , and
  • for all with , we have .

Algorithm

Let be a unimodal function on some interval . Take any two points and in this segment: . Then there are three possibilities:

  • if , then the required maximum can not be located on the left side – . It means that the maximum further makes sense to look only in the interval
  • if , that the situation is similar to the previous, up to symmetry. Now, the required maximum can not be in the right side – , so go to the segment
  • if , then the search should be conducted in , 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 and :

Run time order

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 [left, 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

References

  1. ^ "Ternary Search". cp-algorithms.com. Retrieved 21 August 2023.
This page was last edited on 24 August 2023, at 16:21
Basis of this page is in Wikipedia. Text is available under the CC BY-SA 3.0 Unported License. Non-text media are available under their specified licenses. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. WIKI 2 is an independent company and has no affiliation with Wikimedia Foundation.