Editing Ternary search

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 27: Line 27:
 
* If <math>f(m_1) < f(m_2)</math>, then the maximum can't possibly occur in the first interval, since then the function would be increasing from <math>f(a)</math> to the maximum, then decreasing from the maximum to <math>f(m_1)</math>, and then at some point increasing again since <math>f(m_2) > f(m_1)</math>. So we discard the first interval.
 
* If <math>f(m_1) < f(m_2)</math>, then the maximum can't possibly occur in the first interval, since then the function would be increasing from <math>f(a)</math> to the maximum, then decreasing from the maximum to <math>f(m_1)</math>, and then at some point increasing again since <math>f(m_2) > f(m_1)</math>. So we discard the first interval.
 
* If <math>f(m_1) > f(m_2)</math>, then the maximum can't possibly occur in the third interval, by similar reasoning.
 
* If <math>f(m_1) > f(m_2)</math>, then the maximum can't possibly occur in the third interval, by similar reasoning.
* If <math>f(m_1) = f(m_2)</math>, then the maximum has to be in the second interval; if it were in the first interval then the function would not be strictly decreasing afterward, and if it were in the third interval then the function would not be strictly increasing at the beginning. Here, we can either discard the first or the third interval; it doesn't matter which. That's why we don't need a case in the code for this case.
+
* If <math>f(m_1) = f(m_2)</math>, then the maximum has to be in the second interval; if it were in the first interval then the function would not be strictly decreasing afterward, and if it were in the third interval then the function would not be strictly increasing at the beginning.
 +
Here, we can either discard the first or the third interval; it doesn't matter which. That's why we don't need a case in the code for this case.
 
Intuitively, evaluating the function at the two intermediate points tips us off as to which "direction" to look in, as though we are climbing a hill. When the function is higher at the first than at the second, we know the maximum lies in one of the two left subintervals, and when it is higher at the second than at the first, we know the maximum lies in one of the two right subintervals. If the bitonicity is not strict, then we might not be able to obtain any useful information from the comparison, so this is not allowed.
 
Intuitively, evaluating the function at the two intermediate points tips us off as to which "direction" to look in, as though we are climbing a hill. When the function is higher at the first than at the second, we know the maximum lies in one of the two left subintervals, and when it is higher at the second than at the first, we know the maximum lies in one of the two right subintervals. If the bitonicity is not strict, then we might not be able to obtain any useful information from the comparison, so this is not allowed.
  

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)