Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All valid combinations of points, in the most (speed) effective way

I know there are quite some questions out there on generating combinations of elements, but I think this one has a certain twist to be worth a new question:

For a pet proejct of mine I've to pre-compute a lot of state to improve the runtime behavior of the application later. One of the steps I struggle with is this:

Given N tuples of two integers (lets call them points from here on, although they aren't in my use case. They roughly are X/Y related, though) I need to compute all valid combinations for a given rule.

The rule might be something like

  • "Every point included excludes every other point with the same X coordinate"
  • "Every point included excludes every other point with an odd X coordinate"

I hope and expect that this fact leads to an improvement in the selection process, but my math skills are just being resurrected as I type and I'm unable to come up with an elegant algorithm.

  • The set of points (N) starts small, but outgrows 64 soon (for the "use long as bitmask" solutions)
  • I'm doing this in C#, but solutions in any language should be fine if it explains the underlying idea

Thanks.


Update in response to Vlad's answer:

Maybe my idea to generalize the question was a bad one. My rules above were invented on the fly and just placeholders. One realistic rule would look like this:

  • "Every point included excludes every other point in the triagle above the chosen point"

By that rule and by choosing (2,1) I'd exclude

  • (2,2) - directly above
  • (1,3) (2,3) (3,3) - next line
  • and so on

So the rules are fixed, not general. They are unfortunately more complex than the X/Y samples I initially gave.

like image 950
Benjamin Podszun Avatar asked Mar 02 '10 21:03

Benjamin Podszun


People also ask

How do I find the most possible combinations?

The formula for combinations is nCr = n! / r! * (n - r)!, where n represents the number of items, and r represents the number of items being chosen at a time.

How many combinations of 4 numbers are there?

There are 10,000 possible combinations that the digits 0-9 can be arranged into to form a four-digit code.

How many combinations of 5 items are there without repeating?

Note that your choice of 5 objects can take any order whatsoever, because your choice each time can be any of the remaining objects. So we say that there are 5 factorial = 5! = 5x4x3x2x1 = 120 ways to arrange five objects.

What is the number of combinations of 5 objects taken all at once?

Thus, for 5 objects there are 5! = 120 arrangements.) For combinations, k objects are selected from a set of n objects to produce subsets without ordering.


1 Answers

How about "the x coordinate of every point included is the exact sum of some subset of the y coordinates of the other included points". If you can come up with a fast algorithm for that simply-stated constraint problem then you will become very famous indeed.

My point being that the problem as stated is so vague as to admit NP-complete or NP-hard problems. Constraint optimization problems are incredibly hard; if you cannot put extremely tight bounds on the problem then it very rapidly becomes not analyzable by machines in polynomial time.

like image 74
Eric Lippert Avatar answered Oct 25 '22 09:10

Eric Lippert