0

Is there anyway to do a Cartesian product on 15 integer sets of size 5 ? It seems like the library is throwing an exception error: cartesian product too big !

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
  • Can you show us what method you're referring to? – arshajii Jan 18 '13 at 00:16
  • Thanks for the quick response. Yes it is Sets.cartesianproduct(). – user1988908 Jan 18 '13 at 00:38
  • It's not integer overflow, however, the size of the product I described is 5^15. I am trying to find a way to get around this. In fact, since AxBxC = (AxB)xC, I wanna be able to remove some of the sets after each product (according to the problem structure)... – user1988908 Jan 18 '13 at 00:53
  • 2
    Yes, it's integer overflow; `Sets.cartesianProduct` rejects products that would have size greater than `Integer.MAX_VALUE`. – Louis Wasserman Jan 18 '13 at 02:07
  • I see. Is there anything to go around it ? Potentially I can write my own function, but I guess this one is more efficient in terms of memory. – user1988908 Jan 18 '13 at 02:14
  • 5
    What would you even do with a Cartesian product that big, is the question? If you can filter some of the sets down, then do that first. – Louis Wasserman Jan 18 '13 at 03:20

1 Answers1

1

The error message explains the issue:

IllegalArgumentException: Cartesian product too large; must have size at most Integer.MAX_VALUE

The Cartesian product of 15 5-length sets produces 5^15 different results, or 30,517,578,125. Because a Set cannot have a size() larger than Integer.MAX_VALUE it's not possible to (correctly) return such a large set. As Louis Wasserman points out, it's somewhat unlikely you really want to operate on such a large number of results, since constructing 30 billion lists is expensive no matter how you do it. Your best bet may be to re-examine the problem and see if there's some solution that avoids needing to construct so many results.

If you don't actually need Set semantics you can of course construct all the Cartesian sets one by one simply enough with nested for loops (as described in the documentation):

for (B b0 : sets.get(0)) {
  for (B b1 : sets.get(1)) {
    ...
    ImmutableList<B> tuple = ImmutableList.of(b0, b1, ...);
    // operate on tuple
  }
}

But of course don't try to store them all in a single collection - you'll get an error as you exceed the maximum size of the collection (or your heap space).

dimo414
  • 47,227
  • 18
  • 148
  • 244