Ok, I implemented this algorithm.
import com.google.common.collect.Lists; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class PermutationGenerator { private List<List<Integer>> result; private List<List<Integer>> data; public List<List<Integer>> permutate(List<List<Integer>> data) { this.data = data; this.result = Lists.newArrayList(); List<Integer> integers = new ArrayList<Integer>(Collections.nCopies(data.size(), 0)); foo(0, data.size() - 1, integers); return result; } private void foo(Integer index, Integer maxIndex, List<Integer> output) { List<Integer> list = data.get(index); for (int i = 0; i < list.size(); i++) { output.set(index, list.get(i)); if (index == maxIndex) { result.add(Lists.newArrayList(output)); } else { foo(index + 1, maxIndex, output); } } } }
Testing Class:
import com.google.common.collect.Lists; import org.junit.Test; import java.util.Arrays; import java.util.List; public class PermutationGeneratorTest { @Test public void test() throws Exception {
Output:
[1, 4, 6] [1, 4, 7] [1, 4, 8] [1, 4, 9] [1, 5, 6] [1, 5, 7] [1, 5, 8] [1, 5, 9] [2, 4, 6] [2, 4, 7] [2, 4, 8] [2, 4, 9] [2, 5, 6] [2, 5, 7] [2, 5, 8] [2, 5, 9] [3, 4, 6] [3, 4, 7] [3, 4, 8] [3, 4, 9] [3, 5, 6] [3, 5, 7] [3, 5, 8] [3, 5, 9] TOTAL: 24