c# - Getting all possible combinations from a list of numbers -
i'm looking efficient way achieve this:
you have list of numbers 1.....n (typically: 1..5 or 1..7 or - reasonably small, can vary case case)
you need combinations of lengths numbers, e.g. combinations of 1 number ({1}, {2}, .... {n}), combinations of 2 distinct numbers ({1,2}, {1,3}, {1,4} ..... {n-1, n} ), combinations fo 3 of numbers ({1,2,3}, {1,2,4}) , forth
basically, within group, order irrelevant, {1,2,3} equivalent {1,3,2} - it's matter of getting groups of x numbers list
seems there ought simple algorithm - have searched in vain far. combinatorics , permutation algorithms seems a) take order account (e.g. 123 not equal 132), , seems operate on single string of characters or numbers....
anyone have great, nice'n'quick algorithm sleeve??
thanks!
just increment binary number , take elements corresponding bits set.
for instance, 00101101
mean take elements @ indexes 0, 2, 3, , 5. since list 1..n, element index + 1.
this generate in-order permuatations. in other words, {1, 2, 3}
generated. not {1, 3, 2}
or {2, 1, 3}
or {2, 3, 1}
, etc.
Comments
Post a Comment