Skip navigation.

Software Development

Correcting The Future

Easy Convex Hull Construction

If you've ever tried to look at P=NP and related problems, you've probably encountered the traveling salesman problem. And if you've ever tried to find a solution to this, even if just for fun, you may have thought about building a convex hull to start off. Well, I thought of building one. But I couldn't find an easy algorithm that I could understand. Sure, there are plenty of them out there and I know how they work, but they all seemed a little convoluted. So I created my own algorithm only to find out it's a variation of the Graham Scan (you can see the one liner at the bottom of the article describing it under "Notes"). Still, the version described here is super simple to understand and I thought it deserves a mention.

A Convex Hull is essentially a list of points that completely contains all other points where all angles between any three points of this convex hull is convex (hence its name). You can think of it as an elastic band surrounding all points.

This version uses four incredibly easy to understand passes. Afterwards, I will explain how to optimize it (otherwise, there's no point to this article) as well as explain how you can combine the last three passes into one for a total of two passes overall. This algorithm runs in O(N log(N)) time because of the sorting required.

PASS 1

For this pass, we sort all our points according to the x value.

That's it. Just sort them from left to right.

PASS 2

You need to duplicate the sorted list created in pass 1.

Yeah, that's it. We'll call one list TOP and the other list BOTTOM.

PASS 3

Pass 3 and 4 are the same except the comparisons check opposite values.

For this pass, we build the top portion of the convex hull between the leftmost and rightmost points. So we scan three points (A,B,C) at a time making sure that the angle between those three points are convex. If they're not convex, we remove point B and then move A back one point and start scanning again from there. The reason we move back after removing B is because point A now has a new neighbour which means its angle has changed. So we need to check it again. And only move back if you can (ie. If A isn't already the leftmost point).

How do we check if three points are convex? Easy. We just check if the three points are clockwise. And we do that by using the cross product of two vectors, otherwise known as the determinant (in this particular case).

c = (B.x - A.x) * (C.y - A.y) - (C.x - A.x) * (B.y - A.y);

If c > 0 then it's counterclockwise and you need to remove B from the TOP list.

So that's all you have to do here is scan the whole list and remove B every time c is greater than zero (and then back up one point in the list before continuing to scan). Once you've scanned the whole list, you have the top part of the convex hull.

PASS 4

This pass is exactly the same as Pass 3 except you remove point B from the BOTTOM list if c < 0. That's it.

When you're done, merge your two lists together (making sure not to duplicate the leftmost and rightmost points since they appear in both lists) and you have your convex hull.


Optimizations

As mentioned earlier, you can combine passes 2-4 into a single pass by simply checking c for both lists at the same time. If it's convex, you include it in the proper list. Backtracking would take a little bit more work to do, but it's not that bad.

There are other optimizations you can do. Once you find the leftmost and rightmost points, call these P and Q respectively, you can create smaller top and bottom lists and eliminate a great deal of points right away. We'll call TOPY the maximum Y value from P and Q and BOTY is the lowest. For the top list, we do not include any points that are less than BOTY. And for the bottom list, we don't include any points that are above TOPY.

When we build the top list, we know that no points below P and Q will be part of this section of the convex hull. So there's no need to consider them. Same thing for the bottom list in that no point above both P and Q will be part of that section of the convex hull.

As far as scanning, I'm not sure if the following will help or not, but it's good information to have. When checking the points A, B and C, we know these are from left to right. (We assume we're checking the top list for this example.) So if the points are from left to right, there are two cases where we know for certain if it's concave or convex. If B is below both A and C on the Y axis, we know for certain that this is counterclockwise and hence concave. So if B is below A and B, we can remove it right away without using any cross product. This will actually be the case over 90% of the time from what I've seen. I haven't actually run tests, but the vast majority of the time, you will find B to be below both A and C.

The second case is if B is above both A and C. If so, then it's convex for certain and we can continue scanning the list.

The last case is if B is between A and C on the Y axis. In this case, you have to use the dot product or check if B is on the left side of the line between A and C. Either way, you need to use some multiplications.

That covers the Graham scan variant of convex hull construction.

Below, you will find screenshots of different aspects of the Graham scan.

This next pic shows the formation of the top list by not including any points below P and Q. As can be seen, you can eliminate a lot of possibilities right away. Unfortunately, this doesn't happen all the time as will be seen later for the bottom list.

(Click on picture for larger version)


Here, we see the result of ONLY checking if B is below A and C. No cross product is used. We can see on the left that there is a point that should not be there. But overall, it's very close to the actual convex hull.



And now we see the correct top part of the convex hull in yellow as well as the bottom list.



Just as was the case for the top list, we see how good a result only checking if B is above A and C for the bottom part of the convex hull. Some points should not be included, but it again demonstrates just how often this simple check eliminates candidate points.



And finally the convex hull.


What If... C++ Pointer Notation Was Different?P =/!= NP? Travelling Salesman Problem (Updated Nov 7, 2009)

Comments

Anonymous 1. November 2009, 17:34

gary writes:

I'm love algorithms. Most Math textbooks approach TSP and categorize TSP as an "optimization problem." This means that they'll use algorithms to build a valid model, and then they'll tweak that model until it is optimized (has a shortest route). There are a handful of algorithms that optimize a valid model. Constructing a valid model to begin with can be done any number of ways, and each algorithm produces valid models that are strikingly different from each other. The problem with TSP is that the "optimization" can generally only approximate optimization. It is only by pre-optimized brute force using hundreds of CPUs in parallel that we can prove a valid shortest route. Even this can only be done on a medium-sized input set (relative to, say, all the nodes and edges represented by all the cities and their respective distances in the world -- which would be a large set).

I am proposing an alternate set of algorithms and model construction called "model correction." Instead of starting with a valid model, we start with an invalid model and use unique algorithms to correct the model. (no details given here)

The difference between the two is not the approach -- both systems approach the same set of "shortest possible route(s)." Optimization gets us close within certain error bounds determined by the algorithms implemented. Correction proves the shortest route(s) spot-on without error bounds. Think of it as approaching a solution from opposite ends of the spectrum.

Does this prove P=NP? I'm not sure. I'm thinking the consensus will suggest that TSP has been miscategorized as NP when it should be P. If NP means "we think brute force is the only solution," and TSP will forever remain NP, well then, my version of model correction should raise some eyebrows. If any NP set of problems can be recategorized as P, it should raise some eyebrows. If my proofs have a glitch, I'll be the only one raising eyebrows.

Anonymous 1. November 2009, 17:35

Anonymous writes:

I'm love algorithms. LOL. Never claimed to be a great communicator.

Vorlath 2. November 2009, 06:00

I could have fixed that for you, but it's a great line. :wink:

About algorithms, I think you have a valid point about what you say. What really scares me is one particular statement of yours that bothers me (not the statement since I happen to agree with it, but rather the consequences of it worry me).

I'm thinking the consensus will suggest that TSP has been miscategorized as NP when it should be P.



Once you solve it in polynomial time, one would have a good argument that it's P.

Unfortunately, I don't know enough about the topic to know exactly what criteria is used to state if a problem is in the set of NP-complete problems.

I just hope an existing problem that is NP-complete, but that we've already solved in polynomial time hasn't gone unnoticed.

Vorlath 4. November 2009, 22:01

Can someone answer if a 2D TSP with triangle inequality (using Euclidean geometry) is still NP-complete for the decision version of the problem?

The answer is yes, correct?

And I understand that TSP in general (not the decision version) is NP-hard in that the verification also takes exponential time.

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies

December 2009
S M T W T F S
November 2009January 2010
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31