Re: Permutations.
- To: mathgroup at smc.vnet.net
- Subject: [mg14807] Re: [mg14771] Permutations.
- From: BobHanlon at aol.com
- Date: Wed, 18 Nov 1998 01:29:08 -0500
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 11/14/98 6:19:01 AM, awhopper at hermes.net.au writes:
>For the combinations of n objects taken k at a time, (where order counts
>and there is no duplication), the function KSubsets is the one to use.
>e.g.
> In[1]:= <<DiscreteMath`Combinatorica`
>
> In[2]:= Table[KSubsets[{a,b,c,d}, k], {k, 4}]
>
> Out[3]= {{{a}, {b}, {c}, {d}},
> {{a,b}, {a,c}, {b,c}, {b,d}, {c,d}},
> {{a,b,c}, {a,b,d}, {a,c,d}, {b,c,d}},
> {{a,b,c,d}}}
>
>But likewise in wanting to find all the permutation subsets (with no
>duplication and order not counting), of a numerical or symbolic list,
>there does not seem to be a function anywhere (including the packages),
>to achieve this goal.
>
>(By n!/(n-k)!, there will be be; 4, 12, 24, 24 permutations taken k =
>1, 2, 3, 4 at a time, for a 4 element list).
>
>The built-in function Permutations and also LexicographicPermutations
>(from Combinatorica) do not take a second argument, as KSubsets does,
>and so only the (n, k=n) permutations (24 in the example ) can be
>found.
>
>I would appreciate some assistance to find a way to generate all of the
>permutation subsets, in List, Table or Column form.
>
Alan,
One approach:
Needs["DiscreteMath`Combinatorica`"];
perm[theList_List, k_Integer?NonNegative] :=
Flatten[Map[Permutations, KSubsets[theList, k]], 1] /;
k <= Length[theList];
perm[theList_List] := Module[{nbr = Length[theList], k},
Table[Flatten[Map[Permutations, KSubsets[theList, k]], 1],
{k, 0, nbr}]];
Table[perm[{a,b,c,d}, k], {k, 0, 4}]//ColumnForm
{}
{{a}, {b}, {c}, {d}}
{{a, b}, {b, a}, {a, c}, {c, a}, {a, d}, {d, a}, {b, c}, {c, b},
{b, d}, {d, b}, {c, d}, {d, c}}
{{a, b, c}, {a, c, b}, {b, a, c}, {b, c, a}, {c, a, b}, {c, b, a},
{a, b, d}, {a, d, b}, {b, a, d}, {b, d, a}, {d, a, b}, {d, b, a},
{a, c, d}, {a, d, c}, {c, a, d}, {c, d, a}, {d, a, c}, {d, c, a},
{b, c, d}, {b, d, c}, {c, b, d}, {c, d, b}, {d, b, c}, {d, c, b}} {{a,
b, c, d}, {a, b, d, c}, {a, c, b, d}, {a, c, d, b},
{a, d, b, c}, {a, d, c, b}, {b, a, c, d}, {b, a, d, c},
{b, c, a, d}, {b, c, d, a}, {b, d, a, c}, {b, d, c, a},
{c, a, b, d}, {c, a, d, b}, {c, b, a, d}, {c, b, d, a},
{c, d, a, b}, {c, d, b, a}, {d, a, b, c}, {d, a, c, b},
{d, b, a, c}, {d, b, c, a}, {d, c, a, b}, {d, c, b, a}}
perm[{a,b,c,d}]//ColumnForm
{}
{{a}, {b}, {c}, {d}}
{{a, b}, {b, a}, {a, c}, {c, a}, {a, d}, {d, a}, {b, c}, {c, b},
{b, d}, {d, b}, {c, d}, {d, c}}
{{a, b, c}, {a, c, b}, {b, a, c}, {b, c, a}, {c, a, b}, {c, b, a},
{a, b, d}, {a, d, b}, {b, a, d}, {b, d, a}, {d, a, b}, {d, b, a},
{a, c, d}, {a, d, c}, {c, a, d}, {c, d, a}, {d, a, c}, {d, c, a},
{b, c, d}, {b, d, c}, {c, b, d}, {c, d, b}, {d, b, c}, {d, c, b}} {{a,
b, c, d}, {a, b, d, c}, {a, c, b, d}, {a, c, d, b},
{a, d, b, c}, {a, d, c, b}, {b, a, c, d}, {b, a, d, c},
{b, c, a, d}, {b, c, d, a}, {b, d, a, c}, {b, d, c, a},
{c, a, b, d}, {c, a, d, b}, {c, b, a, d}, {c, b, d, a},
{c, d, a, b}, {c, d, b, a}, {d, a, b, c}, {d, a, c, b},
{d, b, a, c}, {d, b, c, a}, {d, c, a, b}, {d, c, b, a}}
Bob Hanlon