I was reading convex hull for the first time from CLRS.I thought for another algorithm that would work in O(n) time.I tested it for many test cases and it works fine.Can anyone point out flaws in the algorithm or support it.(Sorry for my bad english)
Lokesh-Scan(Points P)
{
1.find the leftmost,rightmost,uppermost and lowermost points.In case of ties take the lower-left and upper right most points;//Since these points are the corner ones they have to be in the hull/
2.Join the above mentioned points;//i.e include them in the hull
3.Now treat the four lines as reference axes individually.i.e if suppose p1,p2,p3,p4 are the lower,right,upper and left points respectively then firstly take p2 as origin and the line p2->p1 as the positive x-axis.If there are any points in the 1st quadrant of this axis system, include the one which has the maximum positive height from the axis in the hull.
4.Do this for all the four lines.(p2->p1,p3->p2,p4->p3 and p1->p4)
}
This algorithm (if correct) works in O(n) time.Line 1 takes O(n) time and line 3 takes O(n) time..
I hope I have made myself clear.Looking for your replies......
Keep such questions in mind next time you invent any algorithm. Try to answer them yourself. Try to answer "why nobody except me invented such a great idea" yourself... And who knows, maybe you once really invent something really new.
However you described your idea well so it is easy to understand you. It is very important ability - so I give "plus" to your post.
If there is a point then it puts the point which is farthest from the line segment thus enclosing all points...
It could appear that there is two such points - neither of them could complete convex hull alone. Read the second example kindly presented by Ivan.
About first paragraph. I told that proper algorithm should be independent of "leftmost/rightmost" etc points. Algorithm should give the same convex hull if you rotate your set by 45 degrees, for example. In case of your algorithm it is not so - you could get different results for the same set, if it is rotated. It lead us to the conclusion that algorithm could not be correct.
See book "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. If one can build convex hull in O(n) time, then he can to sort numbers in O(n) time, but it is impossible.
The idea is ok, it's an incomplete version of the QuickHull algorithm.
Here is an image showing when your algorithm fails:
Thanks Diego....So the changes in the algorithm should be:
*After 1 eliminate all points in the interior of the hull.
*Then do step 3 with the idea of elimination i.e. after choosing the reference line (say with p2->p1) 3.1 Find the point (pk)that is farthest from the axes system and in its first quadrant.
3.2 Join p2,p1 and pk to form a triangle.Eliminate all points in the interior of this triangle.
3.3 Put pk in the hull.
3.4 If there are still points remaining in this quadrant repeat 3.1
4.Do step 3 for all 4 reference lines
The problem remains to implement it and calculate its complexity.Please help me with this because I dont know how to find the number of points within a triangle