Overview
Ternary search is an optimization algorithm for finding the maximum or minimum of a unimodal function. Unlike binary search (which works on monotonic functions), ternary search works on functions with a single peak (or valley).When to Use Ternary Search
Use ternary search when:- Function has exactly one local extremum (maximum or minimum)
- Function is unimodal: strictly increases then strictly decreases (or vice versa)
- You need to find the point where the extremum occurs
- Can’t compute derivative or don’t have closed-form solution
Unimodal Function
A function f(x) is unimodal if:- One peak: Has exactly one maximum (or minimum)
- Monotonic on both sides: Strictly increases before peak, strictly decreases after
Algorithm Intuition
Ternary search divides the range into three parts:- Split range [L, R] at two points: m1 and m2
- Compare f(m1) and f(m2)
- Eliminate one third of the range
- Repeat until convergence
- If f(m1) > f(m2): minimum is in [m1, R]
- If f(m1) < f(m2): minimum is in [L, m2]
- If f(m1) < f(m2): maximum is in [m1, R]
- If f(m1) > f(m2): maximum is in [L, m2]
Integer Ternary Search
For discrete integer domains.func(m1) > func(m2) to func(m1) < func(m2)
Usage:
Real-Valued Ternary Search
Template I: Fixed Iterations
Template II: Epsilon Threshold
Applications
1. Minimize Distance to Point
2. Maximize Product
3. Minimize Time
4. Allocation Problem
5. Golden Ratio Search (Variation)
Ternary Search vs Binary Search
| Feature | Binary Search | Ternary Search |
|---|---|---|
| Function type | Monotonic | Unimodal |
| Divisions | 2 parts | 3 parts |
| Comparisons/iteration | 1 | 2 |
| Convergence rate | (1/2)^n | (2/3)^n |
| Iterations needed | log₂(range) | log₃/₂(range) ≈ 1.585×log₂(range) |
| Use case | Find value | Find extremum |
Common Mistakes
1. Non-Unimodal Function
2. Discrete vs Continuous
3. Insufficient Iterations
4. Wrong Comparison for Maximum
Optimization Tips
-
Cache function evaluations if expensive
-
Choose appropriate epsilon based on problem constraints
- Use integer search when possible (faster, no floating point issues)
- Consider golden ratio search for slightly better constant factor
Complexity Analysis
| Version | Time Complexity | Space Complexity |
|---|---|---|
| Integer search | O(log₃/₂(n) × f) | O(1) |
| Real-valued (iterations) | O(k × f) | O(1) |
| Real-valued (epsilon) | O(log₃/₂(range/ε) × f) | O(1) |
Why Ternary Search Works
Why Ternary Search Works
Key insight: For unimodal function with maximum at x*:
-
If x₁ < x₂ < x*:
- Function is increasing: f(x₁) < f(x₂)
- Maximum is to the right: eliminate [L, x₁]
-
If x* < x₁ < x₂:
- Function is decreasing: f(x₁) > f(x₂)
- Maximum is to the left: eliminate [x₂, R]
-
If x₁ < x* < x₂:
- Can’t determine which third to eliminate
- But both comparisons narrow the range
- After k iterations: range = initial_range × (2/3)^k
- Reaches epsilon when: (2/3)^k × range < ε
- Iterations needed: k = log₃/₂(range/ε)
Practice Problems
- Minimize maximum distance: Place k points to minimize maximum distance
- Optimal meeting point: Find location minimizing total travel distance
- Resource allocation: Divide resources to minimize worst-case load
- Geometric optimization: Find point on curve closest to target
- Physical simulation: Optimize angle/force for maximum range