  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # Closest Pairs by sweeping vertically  » cpp » Closest Pairs by sweeping vertically

By : zhang935989088
Date : November 18 2020, 01:01 AM
it fixes the issue It does work correctly(because the points are deleted from the set only when they can be safely deleted). However, it has O(n ^ 2) time complexity, because the points are not always deleted when they should be.
This simple generator(written in python3): code :
``````from sys import argv

n = int(argv)
dy = 1
dx = -n
print(n)
for i in range(n):
print(dx * i, dy * i)
``````
``````temp\$ python3 gen.py 10000 > input
temp\$ time ./a.out < input

real    0m0.805s
user    0m0.797s
sys     0m0.008s

temp\$ python3 gen.py 30000 > input
temp\$ time ./a.out < input

real    0m7.195s
user    0m7.198s
sys     0m0.004s

temp\$ python3 gen.py 50000 > input
temp\$ time ./a.out < input

real    0m23.711s
user    0m23.725s
sys     0m0.004s
`````` ## Recursively compute closest pairs

By : Keitarou Okuda
Date : March 29 2020, 07:55 AM
wish of those help The steps are a bit misleading, in that steps 2-5 are all part of the recursion. At every level of recursion, you need to calculate dLmin, dRmin, and dLRmin. The minimum of these is returned as the answer for that level of recursion.
To use your example, you would calculated dLmin as the distance between points 1 and 2, dRmin as the distance between points 3 and 4, and then dLRmin as the distance between points 2 and 3. Since dLRmin is the smallest in your hypothetical case, it would be returned. ## finding all closest point pairs if multiple closest pairs exist

By : user2959992
Date : March 29 2020, 07:55 AM
around this issue The following code is for finding the closest point pairs problem in Introduction to Programming with C++ liang textbook. and I'm trying to edit it so it can find all closest point pairs if multiple closest pairs exist. , You may change your algorithm to stock results in vector:
code :
``````std::vector<int> p1, p2;
double shortestDistance = getDistance(points[p1], points[p1],
points[p2], points[p2]); // Initialize

for (int i = 0; i < NUMBER_OF_POINTS; i++) {
for (int j = i + 1; j < NUMBER_OF_POINTS; j++) {
const double distance = getDistance(points[i], points[i],
points[j], points[j]);

if (shortestDistance >= distance) {
if (shortestDistance > distance) {
p1.clear();
p2.clear();
shortestDistance = distance; // Update shortestDistance
}
p1.push_back(i); // Update p1
p2.push_back(j); // Update p2
}
}
}
`````` ## Finding the closest pair of points - How to implement split pairs inside recursive side pairs function calls

By : Asmaa Magdy
Date : March 29 2020, 07:55 AM
this one helps. you have a couple of bugs in the closest_side_pair routine.
When you call eucledian_closest routine, the length of vector passed to that function is delimiter whereas that should be size. Similarly, when you call closest_split_pair, the length of vector passed is delimiter. That should be size. ## Find the closest distance between every galaxy in the data and create pairs based on closest distance between them

By : Hugh
Date : March 29 2020, 07:55 AM
To fix this issue Your friend's code seems to be generating a 2D array of the distances between each pair, and taking advantage of the symmetry (distance(x,y) = distance(y,x)). It would be slightly better if it used itertools to generate combinations, and assigned your_formula(coords1, coords2) to separation_matrix[i,j] and separation_matrix[j,i] within the same iteration, rather than having separate iterations for both i,j and j,i.
Even better would probably be this package that uses a tree-based algorithm: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.KDTree.html . It seems to be focused on rectilinear coordinates, but that should be addressable in linear time. ## Find closest value pairs and calculate mean in Python

By : user2457934
Date : March 29 2020, 07:55 AM
seems to work fine Assuming that you require an additional column D having the mean of the value pair which has the least difference among the three possible pairs: (colA, colB), (colB, colC) and (colC, colA), following code should work:
Updated:
code :
``````def meanFunc(row):
nonNanValues = [x for x in list(row) if str(x) != 'nan']
numOfNonNaN = len(nonNanValues)
if(numOfNonNaN == 0): return 0
if(numOfNonNaN == 1): return nonNanValues
if(numOfNonNaN == 2): return np.mean(nonNanValues)
if(numOfNonNaN == 3):
minDiffPairIndex = np.argmin( [abs(row['A']-row['B']), abs(row['B']-row['C']), abs(row['C']-row['A']) ])
meanDict = {0: np.mean([row['A'], row['B']]), 1: np.mean([row['B'], row['C']]), 2: np.mean([row['C'], row['A']])}
return meanDict[minDiffPairIndex]

df['D'] = df.apply(meanFunc, axis=1)
``````
``````def meanFunc(row):
minDiffPairIndex = np.argmin( [abs(row['A']-row['B']), abs(row['B']-row['C']), abs(row['C']-row['A']) ])
meanDict = {0: np.mean([row['A'], row['B']]), 1: np.mean([row['B'], row['C']]), 2: np.mean([row['C'], row['A']])}
return meanDict[minDiffPairIndex]

df['D'] = df.apply(meanFunc, axis=1)
`````` 