delphi programming forums mysql charset mget recursive synonimos
free ventrilo servers hosting cs javascript delay python find in list
Back Forum New
abstract:

For instance combo's of size 3 would include "abc", "abd, " abe", abf", etc. Once the "ab?" series is done, we would generate the "ac?" series. Eventually, all the "a??" set would be done, and we would work on sets that started "bcd", "bce", etc. The last set would be "nop".
I'm not actually concerned about what order they are generated in, if that matters.
Output to a memo-field or list-box would be fine.
I hadn't expected this to be difficult, but I'm missing something, and I'm hoping someone here can help.


I need to figure out how to find all possible combinations of a set of items.
As an example, lets use a set of cards. One hand is 5 cards. I would like to generate every possible combination of 5 cards in a 52 card set.
An item can only be used once. (ie, when making a combination, there won't be any combo with two king of spades in it - that one card can only be used once in each combination.)
The number of items in the overall set (the deck of cards) and the number of items in each combination (each 5 card hand) are both variable.
On average, I'm guessinng I'll have 100-200 items in the overall set, and will be trying to make combinations in the 5-10 item range.
For simplplicity, lets say we're looking at characters in the "abcdefghijklmnop" range. So 16 items in our set.
The combinations could be any size, but all combos on any given run will be the same size.
For instance combo's of size 3 would include "abc", "abd, " abe", abf", etc. Once the "ab?" series is done, we would generate the "ac?" series. Eventually, all the "a??" set would be done, and we would work on sets that started "bcd", "bce", etc. The last set would be "nop".
I'm not actually concerned about what order they are generated in, if that matters.
Output to a memo-field or list-box would be fine.
I hadn't expected this to be difficult, but I'm missing something, and I'm hoping someone here can help.

TOP

[EDIT]
please don't [url=showthread.php?t=513197]Cross post[/url] another time.

TOP

I hope you are patient and have a fast computer.  For your 5 card example the number of possible combinations is 52*51*50*49*48 = 311,875,200.  In general to choose M items from a set of size N there are N!/(N-M)! possible combinations.
So for choosing 10 items out of a set of 100 there are 62,815,650,955,529,472,000 combinations.  Assuming your program generates 1 million combinations per second it is going to take a little under two million years to create the full set.   You are also going to have some difficulty finding a list-box that will hold that many items, especially as it will use up more computer memory than currently exists in the world.
Sorry to be such a party pooper, but what you want to do is not practical.
Dave

TOP

if he only want one combinations order, e.g. display abc and not abc, bca etc. then it would be enough with 200 days

TOP

abstract:

For instance combo's of size 3 would include "abc", "abd, " abe", abf", etc. Once the "ab?" series is done, we would generate the "ac?" series. Eventually, all the "a??" set would be done, and we would work on sets that started "bcd", "bce", etc. The last set would be "nop".
I'm not actually concerned about what order they are generated in, if that matters.
Output to a memo-field or list-box would be fine.
I hadn't expected this to be difficult, but I'm missing something, and I'm hoping someone here can help.



Originally Posted by MrFujin
if he only want one combinations order, e.g. display abc and not abc, bca etc. then it would be enough with 200 days
You are right - the original post was not clear on this point.
However even presenting the user with a listbox containing only 17,310,309,456,440 items may still be considered unusable by some UI design experts (assuming the user was willing to wait 200 days in the first place).
Dave

TOP

I appreciate the help and advice.
Once I had a set of ABC, I wouldn't want the CBA or BCA type of sets.  Much like a poker hand, order doesn't matter.  My end goal isn't really to display any of this.  It's probably worse, though.  I wanted to, for each combination, run an analysis.  Which would add more time, so obviously it isn't feasible the way I'd been trying to go about it.
Just out of curiosity, I'd still be interested in an algorithm to do the "create a list of all combinations" thing for smaller lists.  I never thought about that being difficult until I started trying to do it.  And with smaller lists, I can do it manually.  But I couldn't figure out how to program it.
Regardless, that wasn't an end-goal.
Maybe there is another way.  I'd thought building all possible sets, then looking for sets that meet certain criteria was the way to go, but you've convinced me that I need a new plan.
Lets say we have a list of items, A..Z, and each item has two attributes, 1..25.  (Real-world, I expect to have 100-200 items, and each item will have two attributes.  I believe their are between 20 and 30 possible attributes.)
What I want to accomplish is to find sets of items (usually 6 in a set, but that could vary) that have a combination of 12 (again, # could vary) attributes.  Also, in some situations, I might need certain attributes + one wildcard that could be essentially anything, so long as it doesn't duplicate one of the other attributes in that set.
For instance, lets say I wanted to find 3 items that had the attributes 2,3,5,7,8,9.
In a previous step, I've already trimmed a lot of items from my list, because they don't contain any of the needed attributes.  If they have one of the required attributes, and another attribute that isn't in the required list, I've kept that item available, as it might fit in with a wild-card.
My item list might be something like this.  (item name, and it's two attributes.)
A: (2, 9)
B: (3, 7)
C: (9, 11)
D: (1,11)
E: (5, 8)
F: (6, 3)
G: (4, 15)
H: (4, 7)
I: (3, 7)
J: (6, 3)
K: (2, 8)
etc.
A: (2, 9)
B: (3, 7)
E: (5, 8)
would be a valid set, with all the attributes I'm looking for.  2,3,5,7,8,9.
Ideally, order in the list also matters.  For instance, in that example, item B has attribs 3, 7.  Item I also has attribs 3 and 7.  In my final set, I'd prefer item B over item I since the B was listed first.  Essentially, as sets are created, those items will be removed from my list, and the items below them will move closer to the beginning.  New items added will be added at the bottom of the list.  Items which have been waiting the longest to be made part of a set (item B) should be favored over their exact counterpart (item I).
In some cases, I'll be looking for a set like "2,3,5,7,8,X".  X in that case is a "wildcard" which any attribute can fill.  However, it should not be a duplicate of any of the other attributes in the set.  (So in that example, X can be anything except 2,3,4,7,8.)
As I said before, my original thought was to generate a list of all possible combos, then look at each of those combos to see if it met the requirements for a set.  That doesn't appear to be feasible.  Maybe you can suggest a better way.
The way this works out now (with no program helping) is that a person is looking through trying to come up with a set.  As sets are found, the items in that set are removed from the item list, and we look for a new set.  Eventually, we don't have items with the right attribute to make a set, and things go on hold.  But new items come in, are added to the bottom of the list, and we have to look again to try and make a set.  The sets we are looking for also change periodically.  Some have wildcards, some don't.
The item list here is a subset of a bigger item list.  That list has many more attributes.  This subset is made by pulling items out of the master list that have at least one attribute that matches the set we are trying to build.  (If the set has no wildcard spot, then only items with two matching attributes will be used to build the sub-set.)

TOP

Do you need to find all possible combinations that meet your requirement, or just the first?
Here are some thoughts about how I would go about it, using your example data.  I will ignore wildcards, since they will complicate matters.
1) if two items have the same attributes then they are interchangeable as far as the solution is concerned, so I would create a dictionary mapping the pairs of attributes to a list of items that have those attributes, e.g.
(2, 9) -> [A],
(3, 7) -> [B, I],
etc.
2)  for the list of attributes I am looking for I would generate the permutations of pairs that can make up that list, e.g.
(2,3), (5,7), (8,9)
(2,5), (3,7), (8,9)
etc.
(This is similar to your original problem, but the size of the sets are now much smaller, so the number of permutations should be manageable.)
3) for each pair I can look up in the dictionary the list of objects that have those two attributes.
Read up on the branch of maths called Combinatorics.  A bit of googling should find a suitable algorithm for doing step 2.
Dave

TOP

I only need to find the first set of combinations that let me create a set.  At that point, those are going to get pulled out and we'll start looking for another set.  Repeat until no sets are left.  Repeat again when we get new items to add.
It occurs to me that I can make the number of items smaller by eliminating duplicates.
The items B and I, in my example, had the same attributes.  So item I can simply be eliminated based on that, since B will do anything that I will do.
I'm very short on time now, but will be back tomorrow.

TOP

This seems like a variation on exact cover, which can be solved using, among others, Knuth's DLX.
I can also think of a dozen dozen ways to achieve speedup by doing set (not list, that's the wrong data structure) manipulation using bitwise operations. Think hash tables, Bloom filters and Zobrist hashing.

TOP

abstract:

For instance combo's of size 3 would include "abc", "abd, " abe", abf", etc. Once the "ab?" series is done, we would generate the "ac?" series. Eventually, all the "a??" set would be done, and we would work on sets that started "bcd", "bce", etc. The last set would be "nop".
I'm not actually concerned about what order they are generated in, if that matters.
Output to a memo-field or list-box would be fine.
I hadn't expected this to be difficult, but I'm missing something, and I'm hoping someone here can help.



Originally Posted by jafet
This seems like a variation on exact cover, which can be solved using, among others, Knuth's DLX.
I can also think of a dozen dozen ways to achieve speedup by doing set (not list, that's the wrong data structure) manipulation using bitwise operations. Think hash tables, Bloom filters and Zobrist hashing.
I'm looking into the Exact Cover thing to see if that works.
On the rest of it, you lost me. :^)
I'm probably in over my head here, but I'm stubborn.

TOP

Back Forum