1

I have to obtain all possibile combination of this kind of matrix:

String[][] matrix = {
   {"AA-123", "AA-124", "AA-125", "AA-126"},
   {"BB-12", "BB-13"},
   {"CC-1"},
};

After all, that is the final implementation. It's in Java, but the language may be irrilevant:

long nComb = 1;
for (int iMatr = 0; iMatr < matrix.length; iMatr++)
   nComb *= matrix[iMatr].length;

for (int iComb = 0; iComb < nComb; iComb++) {
   System.out.print("|");

   long nSec = 1;
   for (int iSec = 0; iSec < matrix.length; iSec++) {
       String[] sec = matrix[iSec];

       for (int iAtom = 0; iAtom < sec.length; iAtom++) {

           if (iAtom == ((iComb / nSec) % sec.length))
               System.out.print(1);
           else
               System.out.print(0);
       }

       nSec *= sec.length;
       System.out.print("|");
   }

   System.out.println();
}

I have to apply my logic on the if that it prints 1 or 0. I need to know what is the current element (index) of the combination of the array. The expected result:

|1000|10|1|
|0100|10|1|
|0010|10|1|
|0001|10|1|
|1000|01|1|
|0100|01|1|
|0010|01|1|
|0001|01|1|

Regards.

Edit:

I find a possible answer using another variable in the array iteration: nSec. It product increse by the lenght of the array over iterations, reaching at the last iteration the value of nComb.

Yohji
  • 170
  • 8
  • 1
    your question is not really clear – Zardaloop Jun 18 '14 at 08:50
  • I need for an algorithm to operate over the combination of this matrix. – Yohji Jun 18 '14 at 08:51
  • you really need to explain what you need in a more clear way. Your code doesnt mak much sense. hwo car the digit goes?is it only AA-BB ...ZZ or it could be BA, ZD etc... – Zardaloop Jun 18 '14 at 08:56
  • The element (the String in this case) is atomic. I need to combine elements. Printing of 1 or 0 can be replaced by the printing of elements themself. – Yohji Jun 18 '14 at 09:06
  • Do you mean permutation of the rows of the matrix maybe? – Eypros Jun 18 '14 at 09:06
  • I'm think it is a combination, not a permutation. Maybe I'm in wrong. http://en.wikipedia.org/wiki/Combination vs http://en.wikipedia.org/wiki/Permutation – Yohji Jun 18 '14 at 09:11
  • what is the input ? is an array of arrays ? and what is the exact patter of output? – Zardaloop Jun 18 '14 at 09:21
  • 1
    Same algorithm: http://stackoverflow.com/a/9591777/1654265 – Andrea Ligios Jun 18 '14 at 12:34
  • @Andrea Ligios: I post a comment to solution about the overflowing of the dividend. – Yohji Jun 18 '14 at 12:49
  • @Yohji your comment there was quickly flagged and deleted :| – Andrea Ligios Jun 20 '14 at 09:26
  • @Andrea Ligios : I removed it by myself. I assume that the numeric overflow is unavoidable, but it happens only with VERY LARGE size of the matrix. The use of a 64bit long variable contributes to reduce this risk. – Yohji Jun 20 '14 at 10:10

1 Answers1

2

I believe what you're after here is called the cartesianProduct of the multiple collections and this is already supported by a number of collection libraries in Java.

Personally I'd recommend using Guava (https://code.google.com/p/guava-libraries/) which will allow you to define your problem as follows (conversion between Array and Set I will leave out as an exercise :):

import com.google.common.collect.Sets;
import java.util.List;
import java.util.Set;

public class CartesianProduct {

    public static void main(String[] args) {
        Set<List<String>> merged = Sets.cartesianProduct(
                Sets.newHashSet("AA-123", "AA-124", "AA-125", "AA-126"),
                Sets.newHashSet("BB-12", "BB-13"),
                Sets.newHashSet("CC-1")
        );
        System.out.println("Size: " + merged.size());
        System.out.println("Content: " + merged);
    }

}

By executing this code you will get the following result:

Size: 8
Content: [
   [AA-125, BB-13, CC-1], 
   [AA-125, BB-12, CC-1], 
   [AA-124, BB-13, CC-1], 
   [AA-124, BB-12, CC-1], 
   [AA-123, BB-13, CC-1], 
   [AA-123, BB-12, CC-1], 
   [AA-126, BB-13, CC-1], 
   [AA-126, BB-12, CC-1]
]

Then further you can process, sort and format output the way you require (and when printing it another guava class Joiner might come handy).

Norbert Radyk
  • 2,608
  • 20
  • 24
  • 1
    I guess you are right, but he wants the POSITIONs of the elements, not their VALUEs. – Andrea Ligios Jun 18 '14 at 09:39
  • @Andrea Ligios: Quoting Yohji: 'I need to combine elements. Printing of 1 or 0 can be replaced by the printing of elements themself.'. This is printing the elements. However if you wanted to print the position only, then you can do a product on value-position pairs (or even convert values to their respective positions and then do the cartesian product). – Norbert Radyk Jun 18 '14 at 09:45