MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: All permutations of a sequence

  • To: mathgroup at smc.vnet.net
  • Subject: [mg76216] Re: All permutations of a sequence
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Fri, 18 May 2007 06:02:59 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <f2h8t7$v4$1@smc.vnet.net>

Virgil Stokes wrote:
> I would like to get all unique permutations of a sequence (e.g. 
> {1,2,3,4,5,6,7,8,9,10,11,12,13}) with the constraint that all sequences 
> that have an opposite ordering are not included (e.g. {13, 12, 
> 11,10,9,8,6,5,4,3,2,1}) would not be included.
> 
> Are there some Mathematica commands that can be used to efficiently 
> generate these permutations?
> 
> --V. Stokes

Hi Virgil,

Assuming that I have correctly understood your request, the following 
code should do what you are looking for. (Note that the function is 
recursive. You may have to adjust the system parameter $RecursionLimit 
to a higher value than the default of 256. Also, if the list is large, 
this approach can become quickly inefficient).

In[1]:=
mySearch[l_List] :=
   Module[{e = First[l], r = Rest[l]},
     Sow[e]; mySearch[DeleteCases[r,
        x_ /; x == Reverse[e]]]; ] /; Length[l] > 1

lst = {1, 2, 3, 4, 2};

<< "DiscreteMath`Combinatorica`"

p = DistinctPermutations[lst];
q = Reap[mySearch[p]][[2,1]];

{Length[p], Length[q], Sort[p] == Sort[Join[q, Reverse /@ q]]}

Out[6]=
{60, 30, True}

Regards,
Jean-Marc


  • Prev by Date: Re: Guessing "exact" values
  • Next by Date: A harmless and amusing bug
  • Previous by thread: Re: All permutations of a sequence
  • Next by thread: Re: All permutations of a sequence