简体   繁体   English

将相同的函数应用于C中数组中的每个元素

[英]Applying same function to every element in an array in C

Say I need to find the euclidean distance from one (x,y) coordinate to every coordinate in an array of million coordinates and then select the coordinate with the smallest distance. 假设我需要找到一个(x,y)坐标到一百万个坐标数组中每个坐标的欧几里得距离,然后选择距离最小的坐标。

At present I loop though the million element array, calculate distance keeping track of the minimum. 目前我遍历百万元素数组,计算距离保持最小。 Is there a way I could do it differently and faster. 有没有一种方法可以使我做得更快,更不同。

Thanks 谢谢

You can improve your algorithm significantly by using a more complex data structure for instance a kd tree . 通过使用更复杂的数据结构(例如kd树),可以显着改善算法。 Still if what you expect to do is to simply search once for the nearest neighbour, you can not possibly perform better than iterating over all points. 即使您希望做的只是简单搜索一次最近的邻居,也可能无法对所有点进行迭代。

What you can do, though is optimize the function that computes the distance and also(as mentioned in comments) you may omit the square root as comparing the squares of two non-negative integers is just the same as comparing the values. 但是,您可以做的是优化计算距离的函数,并且(如注释中所述)您也可以省略平方根,因为比较两个非负整数的平方与比较值相同。

What I understand from the question is that you wanna find closest pair of point. 我从这个问题中了解到,您想找到最接近的一点。 There is an algorithm Closest pair of points problem to solve this. 有一个算法最近点对问题可以解决。

在此处输入图片说明

Closest Pair of a set of points: 一组点中最接近的对:

  • Divide the set into two equal sized parts by the line l , and recursively compute the minimal distance in each part. 通过行l将集合分成两个大小相等的部分,然后递归计算每个部分的最小距离。
  • Let d be the minimal of the two minimal distances. d为两个最小距离中的最小距离。
  • Eliminate points that lie farther than d apart from l 消除比d从位于相距较远分l
  • Sort the remaining points according to their y-coordinates 根据剩余点的y坐标对它们进行排序
  • Scan the remaining points in the y order and compute the distances of each point to its five neighbors. 按y顺序扫描其余点,并计算每个点到其五个相邻点的距离。
  • If any of these distances is less than d then update d . 如果这些距离中的任何一个小于d则更新d

The whole of algorithm Closest Pair takes O(logn*nlogn) = O(nlog 2 n) time. 整个算法最接近对的时间为O(logn*nlogn) = O(nlog 2 n)

We can improve on this algorithm slightly by reducing the time it takes to achieve the y-coordinate sorting in Step 4. This is done by asking that the recursive solution computed in Step 1 returns the points in sorted order by their y coordinates. 我们可以通过减少在第4步中进行y坐标排序所需的时间来稍微改进此算法。这是通过要求在第1步中计算出的递归解返回按其y坐标排序的点来完成的。 This will yield two sorted lists of points which need only be merged (a linear time operation) in Step 4 in order to yield a complete sorted list. 这将产生两个已排序的点列表,这些点仅需在步骤4中合并(线性时间操作)即可产生一个完整的已排序列表。 Hence the revised algorithm involves making the following changes: 因此,修订后的算法涉及以下更改:

  • Step 1: Divide the set into..., and recursively compute the distance in each part, returning the points in each set in sorted order by y-coordinate. 步骤1:将集合划分为...,然后递归计算每个部分的距离,以y坐标的排序顺序返回每个集合中的点。
  • Step 4: Merge the two sorted lists into one sorted list in O(n) time. 步骤4:在O(n)时间将两个排序列表合并为一个排序列表。

Hence the merging process is now dominated by the linear time steps thereby yielding an O(nlogn) algorithm for finding the closest pair of a set of points in the plane. 因此,合并过程现在由线性时间步控制,从而产生了O(nlogn)算法,用于查找平面中一组点的最接近的一对。

You could save quite a chunk of time by first checking if both the distance along x and along y are <= than the last distance (squared) you stored. 通过首先检查沿x和沿y的距离是否均小于您存储的最后距离(平方),可以节省大量时间。 If it's true, then you carry on with calculating the distance (squared). 如果为真,则继续计算距离(平方)。 Of course the amount of time you save depends on how the points are distributed. 当然,您节省的时间取决于点的分布方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM