Editing Convex hull trick

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 120: Line 120:
 
<p>It turns out, however, that it is possible to support arbitrary insertions in amortized logarithmic time. To do this, we store the lines in an ordered dynamic set (such as C++'s <code>std::set</code>). Each line possesses the attributes of slope and y-intercept, the former being the key, as well as an extra <math>left</math> field, the minimum <math>x</math>-coordinate at which this line is the lowest in the set. To insert a new line, we merely insert it into its correct position in the set, and then all the lines to be removed, if any, are contiguous with it. The procedure is then largely the same as for the case in which we always inserted lines of minimal slope: if the line to be added is <math>l_3</math>, the line to the left is <math>l_2</math>, and the line to the left of that is <math>l_1</math>, then we check if the <math>l_1</math>-<math>l_3</math> intersection is to the left of the <math>l_1</math>-<math>l_2</math> intersection; if so, <math>l_2</math> is discarded and we repeat; similarly, if lines <math>l_4</math> and <math>l_5</math> are on the right, then <math>l_4</math> can be removed if the <math>l_3</math>-<math>l_5</math> intersection is to the left of the <math>l_3</math>-<math>l_4</math> intersection, and this too is performed repeatedly until no more lines are to be discarded. We compute the new <math>left</math> values (for <math>l_3</math>, it is the <math>l_2</math>-<math>l_3</math> intersection, and for <math>l_4</math>, it is the <math>l_3</math>-<math>l_4</math> intersection).</p>
 
<p>It turns out, however, that it is possible to support arbitrary insertions in amortized logarithmic time. To do this, we store the lines in an ordered dynamic set (such as C++'s <code>std::set</code>). Each line possesses the attributes of slope and y-intercept, the former being the key, as well as an extra <math>left</math> field, the minimum <math>x</math>-coordinate at which this line is the lowest in the set. To insert a new line, we merely insert it into its correct position in the set, and then all the lines to be removed, if any, are contiguous with it. The procedure is then largely the same as for the case in which we always inserted lines of minimal slope: if the line to be added is <math>l_3</math>, the line to the left is <math>l_2</math>, and the line to the left of that is <math>l_1</math>, then we check if the <math>l_1</math>-<math>l_3</math> intersection is to the left of the <math>l_1</math>-<math>l_2</math> intersection; if so, <math>l_2</math> is discarded and we repeat; similarly, if lines <math>l_4</math> and <math>l_5</math> are on the right, then <math>l_4</math> can be removed if the <math>l_3</math>-<math>l_5</math> intersection is to the left of the <math>l_3</math>-<math>l_4</math> intersection, and this too is performed repeatedly until no more lines are to be discarded. We compute the new <math>left</math> values (for <math>l_3</math>, it is the <math>l_2</math>-<math>l_3</math> intersection, and for <math>l_4</math>, it is the <math>l_3</math>-<math>l_4</math> intersection).</p>
  
<p>To handle queries, we keep ''another'' set, storing the same data but this time ordered by the <math>left</math> value. When we insert or remove lines from that set (or update, in the case of <math>l_4</math> above), we use the <math>left</math> value from the element in that set to associate it with an element in this one. Whenever a query is made, therefore, all we have to do is to find the greatest <math>left</math> value in this set that is less than the query value; the corresponding line is the optimal one .</p>
+
<p>To handle queries, we keep ''another'' set, storing the same data but this time ordered by the <math>left</math> value. When we insert or remove lines from that set (or update, in the case of <math>l_4</math> above), we use the <math>left</math> value from the element in that set to associate it with an element in this one. Whenever a query is made, therefore, all we have to do is to find the greatest <math>left</math> value in this set that is less than the query value; the corresponding line is the optimal one.</p>
  
 
==References==
 
==References==

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)

Templates used on this page: