Howdy Luke,
In one of my other posts I gave a link to an explanation of permutations
and combinations. Simply put, the permutations are all the possible
arrangements of a set and the combinations are all the <unique> arrangements.
Let's consider N = 4 players (A, B, C and D) and two 2-player teams.
A pair of nested loops (which check that the same player isn't being added
twice) can be used.
for i = A to D
for j = A to D
if i <> j then
'Team 1 is player(i) & player (j) - Team 2 is the others.
next
next
This will give us Team 1's with:
{A, B}, {A, C}, {A, D}
{B, A}, {B, C}, {B, D}
{C, A}, {C, B}, {C, D}
{D, A}, {D, B}, {D, C}
For each i, it will pair up with (N - 1) j's
Now if you look at that you'll notice {A, B} and {B, A}. These are in fact
the same team. And this duplication occurs for each team. The reason this
happens is that the j loop looks 'backwards' as well as forwards. So if i is
on B, j will consider A. But the i loop has already dealt with all the A's.
So this nested loop method generates all the <possible> arrangements. This
doesn't look too serious with just 2 loops as it's only doubling the number of
teams.
Let's look at the maths, though.
For every i, there are (N - 1) j's
That's N x (N - 1) = 4 x 3 = 12
If we add another loop (to make 3-player teams from a set of 4), it's
going to be
N x (N - 1) x (N - 2) = 4 x 3 x 2 = 24
Let's do the 3-player teams by hand and only show the unique teams.
{A, B, C}, {A, B, D}, {A, C, D}
{B, C, D}
That's it. Just the 4. So the nested loop method above generates 24
permutations but there are in fact only 4 unique combinations.
When you get to the 16 player level, the number of possible arrangements
is <huge> - 16 factorial / 8 factorial (16! / 8!) = 16 x 15 x 14 x ... 10 x 9
= about half a billion.
The number of combinations, however, is a mere 12870.
These can be generated by a minor but important change to the nested for
loops. Remember that the problem was j looking backwards? Here's the correct
form where it only looks forwards:
for i = A to D - 1
for j = i + 1 to D
'Team A is player(i) & player (j) - Team B is the others.
next
next
[This is the same form as the loop in Selection Sort.]
This now gives us the following
{A, B}, {A, C}, {A, D}
{X, X}, {B, C}, {B, D}
{X, X}, {X, X}, {C, D}
{X, X}, {X, X}, {X, X}
where (X, X) is an arrangement that has been skipped.
In practice, nested loops are rarely used - due to the team housekeeping
that must be done, plus the fact that each loop being hard-coded makes it
non-scalable.
The solution is recursion whereby you assign each player to a team and
then generate all the combinations of the <succeeding> players in a team one
smaller.
So, with the 3-player teams:
Generate all the 3-player teams from {A, B, C, D)
Put player A into the team.
Generate all the 2-player teams from {B, C, D)
| Add player B into the team.
| Generate all the 1-player teams from {C, D)
| | Add player C into the team.
| | Output team. {A, B, C}
| | Remove C
| | Add player D into the team.
| | Output team. {A, B, D}
| | Remove D
| Remove B
| Add player C into the team.
| Generate all the 1-player teams from {D)
| | Add player D into the team.
| | Output team. {A, C, D}
| | Remove D
| Remove C
Remove A
Put player B into the team.
Generate all the 2-player teams from {C, D)
| Add player C into the team.
| Generate all the 1-player teams from {D)
| | Add player D into the team.
| | Output team. {B, C, D}
| | Remove D
| Remove C
Remove B
I don't know if you've seen the solution (ie. a zipped VB Solution) that I
posted, but it shows the above in action. Perhaps that's going too far, lol
and you have enough to chew on here! ;-))
[Oops - I've just discovered that I <didn't post> the solution!! LOL - I'd
better do that now.]
Regards,
Fergus
Permutations and Combinations - the maths of.
http://engineering.uow.edu.au/Courses/Stats/File2417.html