title: Efficient Cartesian Product algorithm
body: <p>Can somebody please demonstrate for me a more efficient Cartesian product algorithm than the one I am using currently (assuming there is one).  I've looked around SO and googled a bit but can't see anything obvious so I could be missing something.</p>
  
  <pre><code>foreach (int i in is) {
     foreach (int j in js) {
        //Pair i and j
     }
  }
  </code></pre>
  
  <p>This is a highly simplified version of what I do in my code.  The two integers are lookup keys which are used to retrieve one/more objects and all the objects from the two lookups are paired together into new objects.</p>
  
  <p>This small block of code in a much larger more complex system becomes a major performance bottleneck as the dataset it's operating over scales.  Some of this could likely be mitigated by improving the data structures used to store the objects and the lookups involved but the main issue I feel is still the computation of the Cartesian product itself.</p>
  
  <p><strong>Edit</strong> </p>
  
  <p>So some more background on my specific usage of the algorithm to see if there may be any tricks that I can use in response to Marc's comment.  The overall system is a SPARQL query engine which processes SPARQL queries over sets of Graph data, SPARQL is a pattern based language so each query consists of a series of patterns which are matched against the Graph(s).  In the case where two subsequent patterns have no common variables (they are disjoint) it is necessary to compute the Cartesian product of the solutions produced by the two patterns to get the set of possible solutions for the overall query.  There may be any number of patterns and I may have to compute Cartesian products multiple times which can lead to a fairly exponential expansion in possible solutions if the query is composed of a series of disjoint patterns.</p>
  
  <p>Somehow from the existing answers I doubt whether there any tricks that could apply</p>
  
  <p><strong>Update</strong></p>
  
  <p>So I thought I would post an update on what I implemented in order to minimise the need to do Cartesian products and thus optimise the query engine generally.  Note that it's not always possible to completely eliminate the need for products but it's nearly always possible to optimise so the size of the two sets being joined is much smaller.</p>
  
  <p>Since each BGP (Basic Graph Pattern) which is a set of Triple Patterns is executed as a block (in essence) the engine is free to reorder patterns within a BGP for optimal performance.  For example consider the following BGP:</p>
  
  <pre><code>?a :someProperty ?b .
  ?c :anotherProperty ?d .
  ?b a :Class .
  </code></pre>
  
  <p>Executed as is the query requires a cartesian product since the results of the first pattern are disjoint from the second pattern so the results of the first two patterns is a cartesian product of their individual results.  This result will contain far more results than we actually need since the third pattern restricts the possible results of the first pattern but we don't apply this restriction till afterwards.  But if we reorder like so:</p>
  
  <pre><code>?b a :Class .
  ?a :someProperty ?b .
  ?c :anotherProperty ?d .
  </code></pre>
  
  <p>We'll still need a cartesian product to get the final results since the 2nd and 3rd patterns are still disjoint but by reordering we restrict the size of the results of the 2nd pattern meaning the size of our cartesian product will be much smaller.</p>
  
  <p>There are some various other optimisations we make but I'm not going to post them here as it starts to get into fairly detailed discussion of SPARQL engine internals.  If anyone is interested in further details just leave a comment or send me a tweet @RobVesse</p>