I am implementing a divide and conquer approach to convex hull in CUDA. This is my approach:
Bottom up:
Create a list of lists to store convex hulls;
curSize = size of input (all points);
for i: 1 to log N
begin
curSize = curSize / 2;
Take every 2 adjacent convex hulls in list of lists and merge them
into bigger hull (using standard upper and lower common tangent
approach for Divide & Conquer Convex hull) in curSize threads
//Initially it merges every 2 adjacent points in list to a hull, then
in next iteration, it merges convex hulls of size 2 into bigger
convex hulls and so on.., finally the list of lists will have a
single list of points on the hull
end
But its getting too complicated and I feel I am not utilizing the parallel power of CUDA as at every level of the tree I create N/2^i threads whose complexity is O(N) in merging all adjacent hulls at this level. Hence net complexity is still O(N logN).
Can you tell me how to make it better or give any alternative neater parallel algorithm for convex hull (it'll be great if I can get algorithm for parallel version of graham's scan)?
The complexity of your algorithm is still O(N) (not changed in comparison to one-threaded version) because you make 3 things:
Manage threads: O(N)
Remove some vertices from lists: amortized O(N) since there are only N points.
Look at points adjacent to removed, but not removed during current step: O(N) since there are not more than 2 such points for each merge.
But if your points are not sorted, you should better parallelize sorting.
Related
assume that we have two (or more) convex hulls in the plane and we want merging them. but the convex hull result has minimum perimeter. is any algorithm available for this?
You can run any optimized convex hull algorithm on the boundary point of two convex hulls, you should get a new convex hull covering both of the previous ones. To maintain the convex property, there will be a unique resultant convex hull, so the minimum perimeter should not be a constraint.
Split all K hulls in upper and lower hulls. Now you have 2K sorted sequences of vertices. Merge the upper and lower sequences separately, using a K-way merge. This is done in time O(N log(K)) where N is the total number of vertices.
Now two Graham scans give the global hulls in time O(N).
[This is an adaptation of the monotone-chain algorithm.]
I want to find the minimum distance between two polygons with million number of vertices(not the minimum distance between their vertices). I have to find the minimum of shortest distance between each vertex of first shape with all of the vertices of the other one. Something like the Hausdorff Distance, but I need the minimum instead of the maximum.
Perhaps you should check out (PDF warning! Also note that, for some reason, the order of the pages is reversed) "Optimal Algorithms for Computing the Minimum Distance Between Two Finite Planar Sets" by Toussaint and Bhattacharya:
It is shown in this paper that the
minimum distance between two finite
planar sets if [sic] n points can be
computed in O(n log n) worst-case
running time and that this is optimal
to within a constant factor.
Furthermore, when the sets form a
convex polygon this complexity can be
reduced to O(n).
If the two polygons are crossing convex ones, perhaps you should also check out (PDF warning! Again, the order of the pages is reversed) "An Optimal Algorithm for Computing the Minimum Vertex Distance Between Two Crossing Convex Polygons" by Toussaint:
Let P = {p1,
p2,..., pm} and Q = {q1, q2,...,
qn} be two intersecting polygons whose vertices are specified
by their cartesian coordinates in
order. An optimal O(m + n)
algorithm is presented for computing
the minimum euclidean distance between
a vertex pi in P and a
vertex qj in Q.
There is a simple algorithm that uses Minkowski Addition that allows calculating min-distance of two convex polygonal shapes and runs in O(n + m).
Links:
algoWiki, boost.org, neerc.ifmo.ru (in russian).
If Minkowski subtraction of two convex polygons covers (0, 0), then they intersect
Let P be a 3D convex polyhedron with n vertices.
1. Given an algorithm that takes an arbitrary point q as input, how can I decide in O(n) time whether q is inside or outside the convex polyhedron?
2. Can I do some processing to make it O(logn)?
For (1), if you have a convex polyhedron with n vertices then you have also O(n) faces. One could triangulate each face and in total it would still be O(n) triangles. Now take the query point q and check on which side of a triangle q lies. This check takes O(1) for one triangle, thus O(n) for all triangles.
EDIT: The O(n) faces define O(n) planes. Just check if q lies on the same side for all planes.
For (2), (I did not find source for this but it seem reasonable) one could project the polyhedron P as P' onto a plane. P' can be seen as two separate planar graphs, one graph U' for the upper part of the polyhedron and the second graph L' for the lower part. In total there are O(n) faces in L' and U'. Now one can preprocess L' and U' via Kirkpatrick optimal planar subdivision algorithm. (Other sources for it: 1 and 2) This enables O(log n) point in PSLG (planar straight line graph) checks.
Now using the query point q and projecting it to the same plane with the same projection one can look up the face of L' and U' it lies in in O(log n) time. In 3D each face lies in exactly one plane. Now we check on which side q lies in 3D an know if q is inside the polyhedron.
Another approach for (2) will be spacial subdivision of the polyhedron into slubs - pyramids with their vertex in the polyhedron centroid and polyhedron faces as their bases. Number of such slabs is O(n). You can build a binary space partitioning tree (BSP), consisting of these slubs (probably divided into sub-slubs) - if it's balanced, then the point location will work in O(logn) time.
Of course, it will make sense, if you need to call the point location function many times - because the pre-processing step here will take O(n) time (or more).
Assume we have an array that holds n vectors. We want to calculate the maximum euclidean distance between those vectors.
The easiest (naive?) approach would be to iterate the array and for each vector calculate its distance with the all subsequent vectors and then find the maximum.
This algorithm, however, would grow (n-1)! with respect to the size of the array.
Is there any other more efficient approach to this problem?
Thanks.
Your computation of the naive algorithm's complexity is wonky, it should be O(n(n-1)/2), which reduces to O(n^2). Computing the distance between two vectors is O(k) where k is the number of elements in the vector; this still gives a complexity well below O(n!).
Complexity is O(N^2 * K) for brute force algorithm (K is number of elem in vector). But we can do better by knowing that in euclidean space for points A,B and C:
|AB| + |AC| >= |BC|
Algorithm should be something like this:
If max distance found so far is MAX and for a |AB| there is a point C, such that distance |AC| and |CB| already computed and MAX > |AC|+|CB|, then we can skip calculation for |AB|.
It is difficult to tell complexity of this algorithm, but my gut feeling tells me it is not far from O(N*log(N)*K)
This question has been here before, see How to find two most distant points?
And the answer is: is can be done in less than O(n^2) in Euclidean space. See also http://mukeshiiitm.wordpress.com/2008/05/27/find-the-farthest-pair-of-points/
So suppose you have a pair of points A and B. Consider the hypersphere that have A and B at the north and south pole respectively. Could any point C contained in the hypersphere be farther from A than B?
Further suppose we partition the pointset into sqrt(N) hyperboxes with sqrt(N) points each. For any pair of hyperboxes, we can calculate in k time the maximum distance possible between any two points of the infinite set of points contained within them - by simply calculating the distance between their furthest corners. If we already have a candidate better than this we can discard all pairs of points from those hyperboxes.
I need to get two points that have biggest distance between.
The easiest method is to compute distance between each of them, but that solution would have an quadratic complexity.
So i'm looking for any faster solution.
How about:
1 Determine the convex hull of the set of points.
2 Find the longest distance between points on the hull.
That should allow you to ignore all points not on the hull when checking for distance.
To elaborate on rossom's answer:
Find the convex hull of the points which can be found in O(n log n) time with an algorithm like Graham's Scan or O(n log h) time with other algorithm's which I assume are harder to implement
Start at a point, say A, and loop through the other points to find the one furthest from it, say B.
Advance A to the next point and advance B until it is furthest from A again. If this distance is larger than the one in part 2, store it as the largest. Repeat until you have looped through all points A in the set
Parts 2 and 3 take amortized O(n) time and therefore the overall algorithm takes O(n log n) or O(n log h) time depending on how much time you can be bothered spending on implementing convex hull.
This is great and all but if you only have a few thousand points (like you said), O(n^2) should work fine (unless you're executing it many times).