Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating all possible combinations

I'm writing some code and ended up with this problem. I have N products and I have to form all possible combinations of these products, form a product catalog and find some properties, like price. In order to do this I have to form the catalog of products from the given products (exhaustively, but no duplicates allowed). Is there a standardized algorithm for doing this? Note that the catalogs can contain any positive number of products.

like image 456
Malice Avatar asked Jul 26 '26 16:07

Malice


1 Answers

Combinations can be represented by a bit-vector. If a bit is set, the element is present in the combination.

So you simply have to enumarte all numbers from 1 to 2^N-1 (from 0000001, last element present till 1111111, all elements present), and will represent a possible combination.

like image 187
Karoly Horvath Avatar answered Jul 28 '26 07:07

Karoly Horvath