yohandi
A recent graduate with a strong interest in algorithms and data structures.

[Tutorial] Golden-section Search

You might have heard about the Golden Ratio. It is this special number that often shows up in mathematics. The Golden Section Search uses this very ratio to solve a particular kind of problem, such as finding the minimum or maximum of a unimodal function.

Motivational Problem

Suppose we have a unimodal function

KaTeX can only parse string typed expression
, where the function increases on
KaTeX can only parse string typed expression
and decreases on
KaTeX can only parse string typed expression
, with
KaTeX can only parse string typed expression
being the point where the function reaches its only local minimum/maximum (or it can be considered as global minimum/maximum in that particular function). We aim to find
KaTeX can only parse string typed expression
, where
KaTeX can only parse string typed expression
represents an acceptable error ranging in the interval
KaTeX can only parse string typed expression
, serving as the satisfaction threshold. We achieve this by only querying the value of the function at specific points, i.e., evaluating
KaTeX can only parse string typed expression
for chosen values of
KaTeX can only parse string typed expression
in the interval
KaTeX can only parse string typed expression
, as efficiently as possible.

Ternary Search Method

One of the well-known algorithms, Ternary Search, uses a strategy of picking two pivot points

KaTeX can only parse string typed expression
and
KaTeX can only parse string typed expression
such that
KaTeX can only parse string typed expression
, and then evaluating
KaTeX can only parse string typed expression
and
KaTeX can only parse string typed expression
. This evaluation leads to one of three possible cases, which are listed below. For the sake of simplicity, let's assume that the unimodal function has a local maximum that we are seeking to find (if we were looking for a local minimum, we would simply invert each comparison).

  • Case

    KaTeX can only parse string typed expression
    :

    Considering that the function is increasing before the maximum point, we are faced with one of the following scenarios:

    • KaTeX can only parse string typed expression
      , which is a contradiction under the current assumption that
      KaTeX can only parse string typed expression
      , since the function would be increasing from
      KaTeX can only parse string typed expression
      until
      KaTeX can only parse string typed expression
      ; hence, it is impossible to have a maximum point on
      KaTeX can only parse string typed expression
      and before
      KaTeX can only parse string typed expression
      .
    • KaTeX can only parse string typed expression
      , which implies that the maximum must lie in
      KaTeX can only parse string typed expression
      .
    • KaTeX can only parse string typed expression
      , which implies that the function is still increasing until
      KaTeX can only parse string typed expression
      , meaning that the maximum must lie in
      KaTeX can only parse string typed expression
      .

    Figure 1 & 2
    Figure 1 & 2

    This case describes the possibilities and illustrates why, in a case where

    KaTeX can only parse string typed expression
    , we confidently narrow down our search space into
    KaTeX can only parse string typed expression
    .

  • Case

    KaTeX can only parse string typed expression
    :

    We now deal with a similar situation to the previous case. We have either:

    • KaTeX can only parse string typed expression
      , which implies that the function is now in the state of decreasing after
      KaTeX can only parse string typed expression
      , meaning that the maximum must lie in
      KaTeX can only parse string typed expression
      .
    • KaTeX can only parse string typed expression
      , which implies that the maximum must lie in
      KaTeX can only parse string typed expression
      .
    • KaTeX can only parse string typed expression
      , which is a contradiction under the current assumption that
      KaTeX can only parse string typed expression
      , since the function would be decreasing from
      KaTeX can only parse string typed expression
      onwards; hence, it is impossible to have a maximum point on
      KaTeX can only parse string typed expression
      and after
      KaTeX can only parse string typed expression
      .

    Figure 3 & 4
    Figure 3 & 4

    This case describes the possibilities and illustrates why, in a case where

    KaTeX can only parse string typed expression
    , we confidently narrow down our search space into
    KaTeX can only parse string typed expression
    .

  • Case

    KaTeX can only parse string typed expression
    :

    This case is quite simple as out of the possibilities below, only one is deemed valid:

    • KaTeX can only parse string typed expression
      , which is not a valid possibility, as this case implies that the function is now decreasing from
      KaTeX can only parse string typed expression
      onwards. Consequently, we have
      KaTeX can only parse string typed expression
      , which is a contradiction under the current assumption that
      KaTeX can only parse string typed expression
      .
    • KaTeX can only parse string typed expression
      , which is valid as the maximum lies in
      KaTeX can only parse string typed expression
      .
    • KaTeX can only parse string typed expression
      , which is not a valid possibility, as this case implies that the function must be increasing before
      KaTeX can only parse string typed expression
      . Consequently, we have
      KaTeX can only parse string typed expression
      , which is a contradiction under the current assumption that
      KaTeX can only parse string typed expression
      .

    Figure 5
    Figure 5

    This allows us to narrow down the search space into

    KaTeX can only parse string typed expression
    .

Ternary Search partitions the current search space into three equal-sized intervals. Denote

KaTeX can only parse string typed expression
as the length of the search space, i.e.,
KaTeX can only parse string typed expression
, then the size of the partitioned interval is
KaTeX can only parse string typed expression
. This means that we want to choose
KaTeX can only parse string typed expression
and
KaTeX can only parse string typed expression
. Denote
KaTeX can only parse string typed expression
as the number of queries used to solve the motivational problem using the Ternary Search method.

KaTeX can only parse string typed expression

Instead of dividing the interval into three equal parts, which typically lead to reducing the search space to either the first two-thirds, the last two-thirds, or the middle one-third, we could consider a variation where the pivots

KaTeX can only parse string typed expression
and
KaTeX can only parse string typed expression
are chosen closer to the center of the interval. This resembles the idea of Binary Search.

By choosing both

KaTeX can only parse string typed expression
and
KaTeX can only parse string typed expression
as close as possible to the middle of the interval, we want to set the limits as
KaTeX can only parse string typed expression
approaches just above
KaTeX can only parse string typed expression
:
KaTeX can only parse string typed expression
and
KaTeX can only parse string typed expression
. Then, the number of queries (
KaTeX can only parse string typed expression
) used can be calculated as follows:

KaTeX can only parse string typed expression

Golden-section Search Method

The approach of the Golden-section Search is quite similar to the Ternary Search, as the idea of maintaining two pivots is inherited. For the sake of simplicity, we will once again assume that the unimodal function has a local maximum that we are seeking to find. Let's denote the two pivots as

KaTeX can only parse string typed expression
and
KaTeX can only parse string typed expression
, which satisfy
KaTeX can only parse string typed expression
, and be certain that the point
KaTeX can only parse string typed expression
is located in the interval
KaTeX can only parse string typed expression
. Assume that by using the previous queries, we already have the value of
KaTeX can only parse string typed expression
,
KaTeX can only parse string typed expression
, and one of the
KaTeX can only parse string typed expression
and
KaTeX can only parse string typed expression
such that
KaTeX can only parse string typed expression
or
KaTeX can only parse string typed expression
. Consider that we have
KaTeX can only parse string typed expression
(in a case where we have
KaTeX can only parse string typed expression
, we only need to mirror the domain toward
KaTeX can only parse string typed expression
, i.e., the middle point), our next step is to query the value of
KaTeX can only parse string typed expression
, and we will handle it based on the following similar cases in Ternary Search Method:

  • Case

    KaTeX can only parse string typed expression
    :

    As we agreed that in this case the point

    KaTeX can only parse string typed expression
    lies in
    KaTeX can only parse string typed expression
    , we may include this case to either
    KaTeX can only parse string typed expression
    or
    KaTeX can only parse string typed expression
    as
    KaTeX can only parse string typed expression
    and
    KaTeX can only parse string typed expression
    .

  • Case

    KaTeX can only parse string typed expression
    :

    As explained, this case indicates that the point

    KaTeX can only parse string typed expression
    lies in
    KaTeX can only parse string typed expression
    . Then, we can set
    KaTeX can only parse string typed expression
    ,
    KaTeX can only parse string typed expression
    , and
    KaTeX can only parse string typed expression
    , utilizing the value of
    KaTeX can only parse string typed expression
    as the new value of
    KaTeX can only parse string typed expression
    and the value of
    KaTeX can only parse string typed expression
    as the new value of
    KaTeX can only parse string typed expression
    . The choice of pivot is made accordingly; the reason is shown later.

    Figure 6
    Figure 6

  • Case

    KaTeX can only parse string typed expression
    :

    Similarly, as explained, this case indicates that the point

    KaTeX can only parse string typed expression
    lies in
    KaTeX can only parse string typed expression
    . Then, we can set
    KaTeX can only parse string typed expression
    ,
    KaTeX can only parse string typed expression
    , and
    KaTeX can only parse string typed expression
    , utilizing the value of
    KaTeX can only parse string typed expression
    as the new value of
    KaTeX can only parse string typed expression
    and the value of
    KaTeX can only parse string typed expression
    as the new value of
    KaTeX can only parse string typed expression
    in the new interval. Again, the choice of pivot is made accordingly; the reason is shown later.

    Figure 7
    Figure 7

Notice that after the transition, the assumptions we made are all still satisfied. With that, we manage to remove either

KaTeX can only parse string typed expression
or
KaTeX can only parse string typed expression
from our search space with only
KaTeX can only parse string typed expression
additional query, whereas the Ternary Search uses
KaTeX can only parse string typed expression
additional queries. However, it is still quite tricky to choose the value of
KaTeX can only parse string typed expression
and
KaTeX can only parse string typed expression
as we want the values to be optimal when we inherit them for the next interval query.

Suppose

KaTeX can only parse string typed expression
is located in
KaTeX can only parse string typed expression
where
KaTeX can only parse string typed expression
and a constant, then,
KaTeX can only parse string typed expression
should also be located in
KaTeX can only parse string typed expression
. Assuming that we manage to remove the interval
KaTeX can only parse string typed expression
from our search space, then, we want
KaTeX can only parse string typed expression
to be located either in
KaTeX can only parse string typed expression
as the new
KaTeX can only parse string typed expression
or in
KaTeX can only parse string typed expression
as the new
KaTeX can only parse string typed expression
. Let's consider these two cases:

  • Case
    KaTeX can only parse string typed expression
    is assigned as the new
    KaTeX can only parse string typed expression
    :
KaTeX can only parse string typed expression

  Since

KaTeX can only parse string typed expression
, we only consider
KaTeX can only parse string typed expression
.

  • Case
    KaTeX can only parse string typed expression
    is assigned as the new
    KaTeX can only parse string typed expression
    :
KaTeX can only parse string typed expression

  Since

KaTeX can only parse string typed expression
, this case is not considered. With this, we have shown the reason for the choice of the new pivot.

The only

KaTeX can only parse string typed expression
that satisfies our needs is in the case where
KaTeX can only parse string typed expression
is assigned as the new
KaTeX can only parse string typed expression
. With that, we can calculate the number of queries used to solve the motivational problem using the Golden-section Search Method. Denote
KaTeX can only parse string typed expression
as the number of queries and
KaTeX can only parse string typed expression
as the length of the search space, i.e.,
KaTeX can only parse string typed expression
.

KaTeX can only parse string typed expression

Notice that the additional

KaTeX can only parse string typed expression
queries in this method are used for the initial
KaTeX can only parse string typed expression
,
KaTeX can only parse string typed expression
, and one of
KaTeX can only parse string typed expression
and
KaTeX can only parse string typed expression
. It is clear that
KaTeX can only parse string typed expression
as
KaTeX can only parse string typed expression
; hence, the Golden-section Search Method provides a better and more efficient way to solve the motivational problem.

And there we have it! By strategically positioning our pivots and leveraging the mathematics of the golden ratio, we have managed to achieve an even more efficient solution.

You may compare the number of queries used for all the explained methods side-by-side in the below neat little table.

MethodNumber of Queries Used
Ternary Search
KaTeX can only parse string typed expression
Adapted Ternary Search
KaTeX can only parse string typed expression
Golden-section Search
KaTeX can only parse string typed expression

In the upcoming post, I am planning to write about the Variation of the Golden-section Search on a Density Function. Stay tuned!

© 2023 Yohandi. All rights reserved.