Let\'s say I have 2 groups of numbers:
{1, 2, 3},
{4, 5}
I\'d like to create an algorithm (in Java) that outputs the following 6 combinations:<
One possible approach (not necessarily the most efficient) might be to take a divide and conquer approach. It's relatively simple to find all the permutations of two groups (the dumbest way is just nested for loops). Let's say you write a function called permute
and it does permute(A,B)
where A (e.g. {(1), (2), (3)}) and B (e.g. {(4), (5)} are groups of numbers and it returns you all the permutations of A & B as a single group (e.g {(1,4), (1,5), (2,4), (2,5), (3,4), (3,5)}).
So when you have N groups instead of 2, the easiest thing to do is just pick off small portions of the problem. Let's say you have groups A, B and C. Rather than worrying about them all separately, you can think of it as something like:
permute(permute(A,B),C)
Find all the permutations of A and B first. Once you have that result, find all the permutations of that result with C. And four groups A, B, C, D might look like:
permute(permute(permute(A,B),C),D)
And so on. At each step along the way, you take the current permutation result and permute it with the next group in the list of groups that you got as input. You're only ever combining two groups at a time, so the algorithm doesn't have to change depending on the number of groups you get as input.
When you're doing recursion, there are a few major questions you need to answer:
Can you recursively break down the problem into smaller, more solvable problems? I think the examples above prove that you can.
What is the base case? What is the solution that will cause the recursion to stop and unwind? It should generally be something really simple that your recursion can work down towards. In this case, it probably comes down to something like permute(A,{})
where {} is the empty set.
What is the recursive case? How will you break off a chunk of the problem and recurse on a smaller subset of the problem? I think the initial explanation gives you one way of potentially doing this. Just break off one group at a time and permute it with your ever-growing result.
There are certainly other solutions to this problem, this is just the first that popped into my head. As N gets bigger and bigger, this algorithm will get prohibitively slow since it's not very efficient.
So even if you don't use this solution, I hope it gets you on the right track!