If we wanted to be extra careful we would add safeguards against the case where our point has the same x-coord as a segment. We would want to do this as we can't make a triangle with three colinear points.
We can identify this case by noting when our dist(t,.)
function is zero. We can resolve it by keeping track of two previous points at any time (that we ensure have different x-values). When we find a distance of zero, we simply compare it 'two-back'. We can perform a similar process in the other direction, storing a point until we have two points with dist!=0
on either side. We have omitted this process from our algorithm for brevity.
\(L \leftarrow S \cup P \cup p_{x=\infty}: O(m+n)\)
LeftPoint
as belowT \(\leftarrow\) Empty List, LeftPoint
\(\leftarrow -\infty\)
Sort \(L\) by ascending x-coordinate: \(O((m+n) \log (m + n) )\)
For each element in \(L: O(m+n) \)
RightPoint
:
dist(t,LeftPoint)
and dist(t, rightPoint)
. Emit a tuple of \(t\) and the argmin of these two distances: \(O(1)\).t
from the T
: \(O(1)\)LeftPoint
\(\leftarrow\) RightPoint
Our runtime depends on the inner-loop being amortised \(O(1)\). This is clear as an interval is included in a list exactly once before being removed. The total number of elements in the list is \(O(n)\) and so per outer-loop iteration we have amortised \(O(1))\).
Time: Running time is \(O((m+n) \log (m + n) )\) for the preprocessing and \(O(m+n)\) in the loop. Total of \(O((m+n) \log (m + n) )\).
Space: We require \(O(m+n)\) space for the sorted elements and an additional \(O(m+n)\) in total for the list \(T\). We have overall \(O(m+n)\) space complexity.
Just thought I'd let you know; After emailing you about ambiguity in the original question, I proceeded to totally misread the updated version. I spent like four hours working out an algorithm for determining the smallest triangle for each point. It's a much more challenging question.