Like linear search and binary search, ternary search is a searching technique that is used to determine the position of a specific value in an array. In binary search, the sorted array is divided into two parts while in ternary search, it is divided into parts and then you determine in which part the element exists.
Ternary search, like binary search, is a divide-and-conquer algorithm. It is mandatory for the array (in which you will search for an element) to be sorted before you begin the search. In this search, after each iteration it neglects part of the array and repeats the same operations on the remaining .
Implementation
int ternary_search(int l,int r, int x)
{
if(r>=l)
{
int mid1 = l + (r-l)/3;
int mid2 = r - (r-l)/3;
if(ar[mid1] == x)
return mid1;
if(ar[mid2] == x)
return mid2;
if(x<ar[mid1])
return ternary_search(l,mid1-1,x);
else if(x>ar[mid2])
return ternary_search(mid2+1,r,x);
else
return ternary_search(mid1+1,mid2-1,x);
}
return -1;
}
Let us consider the following example to understand the code.
Let the sorted array be = with indices from 0 to 8. You are required to find the position of in this array. Divide the sorted array into the following parts by evaluating the values of and :
Here and . As is not equal to and and it is also not smaller than , you can safely assume that it lies in the part of the array as it is greater than .
Run the ternary search again with and .
Now, and .
As , is the required answer.
If the value is not in the array, it returns as the answer.
Complexity
, where is the size of the array
, where is the size of the array
Use of ternary search:
This concept is used in unimodal functions to determine the maximum or minimum value of that function. Unimodal functions are functions that, have a single highest value.
Let us consider a function in the interval , and you are required to determine the for which is maximized. The function is unimodal in nature, i.e. it strictly increases in the interval and strictly decreases in the interval .
This can be done by various other methods like double differentiation or by using a modified binary search. In the case when the function cannot be differentiated easily, ternary search is useful. It is less prone to errors and easy to implement when:
- Dealing with floating point integers
- Required maximum value is reached at the end of the interval.
Implementation
double func(double x)
{
return -1*1*x*x + 2*x +3;
}
double ts(double start, double end)
{
double l = start, r = end;
for(int i=0; i<200; i++) {
double l1 = (l*2+r)/3;
double l2 = (l+2*r)/3;
//cout<<l1<<" "<<l2<<endl;
if(func(l1) > func(l2)) r = l2; else l = l1;
}
double x = l;
return func(x);
}
The code is making iterations because at each step the interval is reduced to of its previous size. After iterations, the answer has an error of at most of the original interval, which is a good precision! You can modify this based on your requirements.
Let's understand the code.
You are first dividing the interval into the following 3 parts:
At each iteration you search for the part in which the maximum lies and ignore ⅓ part of the current interval.
If , you can observe that the maximum value does not lie in the last interval. Therefore, ignore it by assigning , otherwise the maximum value does not lie in the first part.
Consider a unimodal function, where . The maximum value of between the interval and will be .
Không có nhận xét nào:
Đăng nhận xét