Java, C + + and the performance of Ruby PK (continued) - on the convex hull algorithm (convex hull) efficiency
Translator sequence
This blog is actually Uncle Bob xreborner on to the post in a series of responses (xreborner blog in the last chapter on Uncle Bob made a series of sharp and the maintenance of the rights and interests of C + + view).
Text
I blog in a recent comparison of C + +, Java, and Ruby time consumption, one of the participants (xreborner) submitted a convex hull of the convex hull algorithm code. I have spent a long time to study them to tamper with until the algorithm in Figure Drawing, found himself the dark.
Xreborner algorithm used as a Graham Scan algorithm, it was the complexity of O (nLogn) level. In fact, this algorithm is the time in which the rapid consumption on the order.
Another algorithm called Jarvis March, also known as the giftwrapping algorithm. It was the complexity of O (kn) level, k represents the convex hull of the number of vertices.
I am very curious about these two algorithms which will be faster and more, it is clear that only in logn <k time, O (nLogn) will than O (kn) fast. But there is another a consideration, because Jarvis March than Graham Scan simple, I think in the vast majority of cases, the algorithm will Jarvis March is a better choice, because only in the convex hull of the very large number of vertices circumstances Graham algorithm efficiency will lead. So I prepared Jarvis March algorithm, and the Graham Scan and xreborner done, the results are as follows:

Is not very interesting? I constructed points and 1,000 randomly drawn time and convex hull of the number of vertices. Just look at the intersection of these random convex hull number of vertices in the middle position, and is probably at 37 points! I guess this is a coincidence, but also enough magic ah.
Oh, as an exercise to see if you are not able to understand Jarvis March, but also can get a look at Graham Scan.
Import java.util .*;
(Public class JarvisMarch
Points pts;
Private Points hullPoints = null;
Private List <Double> hy;
Private List <Double> hx;
Private int startingPoint;
Private double currentAngle;
Private static final double MAX_ANGLE = 4;
Public JarvisMarch (Points pts) (
This.pts = pts;
)
/ **
* The Jarvis March, sometimes known as the Gift Wrap Algorithm.
* The next point is the point with the next largest angle.
* <p/>
* Imagine wrapping a string around a set of nails in a board. Tie the string to the leftmost nail
* And hold the string vertical. Now move the string clockwise until you hit the next, then the next, then
* The next. When the string is vertical again, you will have found the hull.
* /
Public int calculateHull () (
InitializeHull ();
StartingPoint = getStartingPoint ();
CurrentAngle = 0;
AddToHull (startingPoint);
For (int p = getNextPoint (startingPoint); p! = StartingPoint; getNextPoint p = (p))
AddToHull (p);
BuildHullPoints ();
Return hullPoints.x.length;
)
Public int getStartingPoint () (
Return pts.startingPoint ();
)
Private int getNextPoint (int p) (
Double minAngle = MAX_ANGLE;
Int minP = startingPoint;
For (int i = 0; i <pts.x.length; i + +) (
If (i! = P) (
RelativeAngle double thisAngle = (i, p);
If (thisAngle> = currentAngle & thisAngle <= minAngle) (
MinP = i;
MinAngle = thisAngle;
)
)
)
CurrentAngle = minAngle;
Return minP;
)
Private double relativeAngle (int i, int p) (return pseudoAngle (pts.x [i] - pts.x [p], [i] pts.y - pts.y [p]);)
Private void initializeHull () (
Hx = new LinkedList <Double> ();
Hy = new LinkedList <Double> ();
)
Private void buildHullPoints () (
Double ax [] = new double [hx.size ()];
Ay double [] = new double [hy.size ()];
Int n = 0;
For (Iterator <Double> ix = hx.iterator (); ix.hasNext ();)
Ax [n + +] = ix.next ();
N = 0;
For (Iterator <Double> iy = hy.iterator (); iy.hasNext ();)
Ay [n + +] = iy.next ();
HullPoints = new Points (ax, ay);
)
Private void addToHull (int p) (
Hx.add (pts.x [p]);
Hy.add (pts.y [p]);
)
/ **
* The PseudoAngle is a number that increases as the angle from vertical increases.
* The current implementation has the maximum pseudo angle <4. The pseudo angle for each quadrant is one.
* The algorithm is very simple. It just finds where the angle intesects a square and measures the
* Perimeter of the square at that point. The math is in my Sept'06 notebook. UncleBob.
* /
Public static double pseudoAngle (double dx, double dy) (
If (dx> = 0 & & dy> = 0)
Return quadrantOnePseudoAngle (dx, dy);
If (dx> = 0 & & dy <0)
Return 1 + quadrantOnePseudoAngle (Math.abs (dy), dx);
If (dx <0 & & dy <0)
Return 2 + quadrantOnePseudoAngle (Math.abs (dx), Math.abs (dy));
If (dx <0 & & dy> = 0)
Return 3 + quadrantOnePseudoAngle (dy, Math.abs (dx));
Throw new Error ( "Impossible");
)
Public static double quadrantOnePseudoAngle (double dx, double dy) (
Return dx / (dy + dx);
)
Public Points getHullPoints () (
Return hullPoints;
)
(Public static class Points
Public double x [];
Public double y [];
Public Points (double [] x, y double []) (
This.x = x;
This.y = y;
)
/ / The starting point is the point with the lowest X
/ / With ties going to the lowest Y. This guarantees
/ / That the next point over is clockwise.
Int startingPoint () (
Double minY = y [0];
Double minX = x [0];
Int iMin = 0;
For (int i = 1; i <x.length; i + +) (
If (x [i] <minX) (
MinX [i] = x;
IMin = i;
) Else if (x == minX [i] & & y [i] <minY) (
MinY [i] = y;
IMin = i;
)
)
Return iMin;
)
)
)
(Original link URL: http://www.butunclebob.com/ArticleS.UncleBob.ConvexHullTiming; Robert C. Martin English blog URL: http://www.butunclebob.com/ArticleS.UncleBob)
About the author: Robert C. Martin, president of the Object Mentor, object-oriented design, model, UML and Agile and Extreme Programming methodology in the field of senior advisers. Not only is he Jolt award-winning book, "Agile Software Development: Principles, Patterns and Practice" (Chinese version) (the "Agile Software Development" (English version of photocopying)), writer or the best-selling book Designing Object-Oriented C + + Applications Using the Booch Method the author. Martin is Pattern Languages of Program Design 3 and More C + + Gems editor-in-chief and co-author with James Newkirk of XP in Practice. He is well-known on the international programmers A spokesman for the General Assembly, and in the C + + Report magazine four years as editor.
Tags: Performance






