4
$\begingroup$

I am able to do a permutation counr for the set {a, a, b} which gives me 3 groups. I am happy with that result. However, if the set is {a, a, b, c, c, d}, Mathematica gives me all the possible groups, but it is difficult to count the number of groups. Can Mathematica find the total number of groups? If so, which function to do I use?

$\endgroup$
2
  • 1
    $\begingroup$ I must be missing the question. But isn't the length of the list the same as number of groups? Length@Permutations[{a, a, b, c, c, d}] gives 180. Or you looking for some other number? $\endgroup$
    – Nasser
    Commented Dec 26, 2013 at 6:22
  • 1
    $\begingroup$ @Nasser I agree that is unclear how the word 'group' is to be interpreted. The first answer counted permutations. I merely presented another approach for that objective. OP will presumably clarify. $\endgroup$
    – ubpdqn
    Commented Dec 26, 2013 at 13:12

3 Answers 3

8
$\begingroup$

You can count the permutations using Multinomial:

cp[u_List]:=Multinomial@@(Tally[u][[All,2]])

Testing:

test={{a,a,b},{a,a,b,c,c,d},{a,b,d}}
cp/@test

yields:

{3, 180, 6}

$\endgroup$
6
$\begingroup$

Length@Permutations[...] certainly suffices but for larger multisets I would just define a function that does not find all the permutations and instead uses the explicit formula if the number of permutations is all that is of interest.

PermutationCount[list_List] := 
 With[{len = Length@list, dups = Length /@ Gather[list]}, 
  len!/Times @@ Factorial /@ dups]
PermutationCount[{a, a, b}]
(* 3 *)
PermutationCount[{a, a, b, c, c, d}]
(* 180 *)
PermutationCount[{a, b, c}]
(* 6 *)
$\endgroup$
2
$\begingroup$

In the case that you want partial permutations (k-permutations) you will need something more than Multinomial. For that I suggest this function:

kP =
  SeriesCoefficient[
    #2! Product[Sum[\[FormalX]^r/r!, {r, 0, n}], {n, #}],
    {\[FormalX], 0, #2}
  ] &

Which can be formatted in the FrontEnd like this:

enter image description here

Example of use:

lst = {"a", "a", "b", "c", "c", "d"};

kP[Last /@ Tally[lst], 4]
102

Confirmation:

Permutations[lst, {4}] // Length
102

It can handle large sets:

SeedRandom[1];
lst = RandomInteger[500, 2500];

kP[Last /@ Tally[lst], 75]
14234989704140257941300312514179784876937595388745791760303140394396200500917930350000721682369301114491055047302455106937589189933917947645440365920659637314061106826442544855655885539058290509408064000
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.