0
$\begingroup$

I have a function $f(x)$, has been sampled into a sequence $$ y_0, y_1, ..., y_{n - 1} $$ at points $x_k = k \Delta x$. $f(x)$ is neither smooth or monotone.

Under this assumption, what is the fastest way of finding $$ \min_{k=0}^{n - 1}|P - (x_k, y_k)| $$ where $P$ is an arbitrary point?

I could do a linear search, but I need to compute the minimum distance for $n^2$ different points which makes the overall algorithm $n^3$.

$\endgroup$
2
  • $\begingroup$ Is there any relation between the $n^2$ points or are they "random" ? $\endgroup$
    – user16034
    Commented Sep 25, 2023 at 20:05
  • $\begingroup$ @user16034 In fact, they are coordinates in an image, and f(x) goes across the image (from left to right). $\endgroup$
    – user877329
    Commented Sep 29, 2023 at 19:11

2 Answers 2

3
$\begingroup$

You can construct a Voronoi diagram for your points, and then use a point location algorithm. Because you need quadratic work regardless you don't even need an efficient algorithm for the Voronoi diagram.

$\endgroup$
2
$\begingroup$

If You sample the function once beforehand, the problem breaks down to a simple Nearest Neighbor Search. There are many different data structures and algorithms for solving the nearest neighbor problem.

As long as $x_k$ and $y_k$ are fairly low-dimensional, one popular solution is the kd-tree which can be build in $\mathcal{O}(n \log{n})$ for your $n$ sample points. Querying the closest sample points to the $n²$ search points usually takes $\mathcal{O}(n^2\log{n})$.

Kd-tree libraries are available in most programming languages, e.g. for NumPy, there is scipy.spatial.KDTree. Kd-trees are a fast general purpose solution but likely not the fastest specific solution.

$\endgroup$
8
  • 1
    $\begingroup$ Interestingly a kd-tree could reduce the number of iterations spent on finding the minimum, but not sufficiently to compensate for the higher overhead at each iteration, making it slower. $\endgroup$
    – user877329
    Commented Sep 29, 2023 at 19:12
  • $\begingroup$ Without knowing the data and the implementation its hard to say. Maybe $n$ is too small for kd-tree to be useful? Are You fitting the function to the points? In that case You could build a kd-tree of the points instead of for the function values. $\endgroup$
    – DirkT
    Commented Sep 30, 2023 at 0:38
  • 1
    $\begingroup$ Here, n is in the range 1024 to 4096. $\endgroup$
    – user877329
    Commented Sep 30, 2023 at 9:40
  • $\begingroup$ That feels relatively small for kd-trees to me. Building the tree may be more expensive than the savings in query time. $\endgroup$
    – DirkT
    Commented Sep 30, 2023 at 9:43
  • 1
    $\begingroup$ Actually building the tree is quick enough. However, individual lookups take way longer than they should. $\endgroup$
    – user877329
    Commented Oct 1, 2023 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.