closest-points

Closest Pair Implemetation Python

為{幸葍}努か 提交于 2021-02-06 04:52:24
问题 I am trying to implement the closest pair problem in Python using divide and conquer, everything seems to work fine except that in some input cases, there is a wrong answer. My code is as follows: def closestSplitPair(Px,Py,d): X = Px[len(Px)-1][0] Sy = [item for item in Py if item[0]>=X-d and item[0]<=X+d] best,p3,q3 = d,None,None for i in xrange(0,len(Sy)-2): for j in xrange(1,min(7,len(Sy)-1-i)): if dist(Sy[i],Sy[i+j]) < best: best = (Sy[i],Sy[i+j]) p3,q3 = Sy[i],Sy[i+j] return (p3,q3,best

Closest Pair Implemetation Python

故事扮演 提交于 2021-02-06 04:51:57
问题 I am trying to implement the closest pair problem in Python using divide and conquer, everything seems to work fine except that in some input cases, there is a wrong answer. My code is as follows: def closestSplitPair(Px,Py,d): X = Px[len(Px)-1][0] Sy = [item for item in Py if item[0]>=X-d and item[0]<=X+d] best,p3,q3 = d,None,None for i in xrange(0,len(Sy)-2): for j in xrange(1,min(7,len(Sy)-1-i)): if dist(Sy[i],Sy[i+j]) < best: best = (Sy[i],Sy[i+j]) p3,q3 = Sy[i],Sy[i+j] return (p3,q3,best

Closest Pair Implemetation Python

坚强是说给别人听的谎言 提交于 2021-02-06 04:50:57
问题 I am trying to implement the closest pair problem in Python using divide and conquer, everything seems to work fine except that in some input cases, there is a wrong answer. My code is as follows: def closestSplitPair(Px,Py,d): X = Px[len(Px)-1][0] Sy = [item for item in Py if item[0]>=X-d and item[0]<=X+d] best,p3,q3 = d,None,None for i in xrange(0,len(Sy)-2): for j in xrange(1,min(7,len(Sy)-1-i)): if dist(Sy[i],Sy[i+j]) < best: best = (Sy[i],Sy[i+j]) p3,q3 = Sy[i],Sy[i+j] return (p3,q3,best

Get the two closest points from a list of points

跟風遠走 提交于 2020-01-11 14:30:10
问题 I am given a list of integers/floats and I need to find the two numbers closest together. How would I do that using only nested for loops? 回答1: For each element, you have to compare the distance of it to each of the other elements with your previous "closest" value - any time this comparison yields smaller values, you remember that pair as the "two closest" ones. So, it is straightforward: def find_two_closest(numbers): # most distant points: delta = max(numbers), min(numbers) for i, element

Finding the two closest numbers in a list using sorting

不问归期 提交于 2019-12-31 05:25:12
问题 If I am given a list of integers/floats, how would I find the two closest numbers using sorting? 回答1: Such a method will do what you want: >>> def minDistance(lst): lst = sorted(lst) index = -1 distance = max(lst) - min(lst) for i in range(len(lst)-1): if lst[i+1] - lst[i] < distance: distance = lst[i+1] - lst[i] index = i for i in range(len(lst)-1): if lst[i+1] - lst[i] == distance: print lst[i],lst[i+1] In the first for loop we find out the minimum distance, and in the second loop, we print

Nearest Neighbor Search: Python

╄→гoц情女王★ 提交于 2019-12-17 22:08:04
问题 I have a 2 dimensional array: MyArray = array([6588252.24, 1933573.3, 212.79, 0, 0], [6588253.79, 1933602.89, 212.66, 0, 0], etc...) The first two elements MyArray[0] and MyArray[1] are the X and Y coordinates of the points. For every element in the array, I would like to find the quickest way to return its single nearest neighbor in a radius of X units. We are assuming this is in 2D space. lets say for this example X = 6 . I have solved the problem by comparing every element to every other

optimize nearest neighbor query on 70 million extremely high density spatial point cloud on SQL Server 2008

柔情痞子 提交于 2019-12-02 06:28:55
问题 I have about 75 million records in a SQL Server 2008 R2 Express database. Each is a lat long corresponding to some value. The table has geography column. I am trying to find one nearest neighbor for a given latitude longitude (point). I already have a query with spatial index in place. But depending on where the record is in the database, say first quarter or last quarter, the query can take about from 3 to 30 seconds to find the nearest neighbor. I feel this can be optimized to give lot