Why is ternary search used to find the max/min of an unimodal function?

884 Views Asked by At

I have learned that finding the minimum/maximum of an unimodal function can be done with ternary search, an algorithm that runs in O(logN) time (where N is the size of the given search range).

However, I recently had the thought that maybe we could accomplish finding the max/min of an unimodal function with binary search as well. Specifically, for functions that have a definite maximum, we could find and return the first x-value for which f(x) >= f(x+Epsilon) (where Epsilon is the allowed error bound). On the other hand, for functions that have a definite minimum, we could find and return the first x-value such that f(x) <= f(x+Epsilon).

Overall, my question is, why is ternary search used at all for this search operation if binary search can accomplish the same applications? Am I missing something here, or have I made some other logical mistake?

2

There are 2 best solutions below

0
Serial Lazer On

There are some scenarios where binary search wont be helpful but ternary search can yield desired results.

Here is a resource that helps you understand in detail about it.

2
גלעד ברקן On

Let f(x) = -x^2 + 3. Let Epsilon = 2.

The first x-value for which f(x) >= f(x+Epsilon) is -1:

f(-1) = 2 = f(-1 + 2) = f(1) = 2

But the answer is x = 0.