Puzzler: Permutations and Anagrams 
Author Message
 Puzzler: Permutations and Anagrams

The November 1995 issue of Dr. Dobbs has an article by Mani Iyer
entitled "Permutation Generation Using Matrices" (p. 133).
Unfortunately, the algorithm described in the article doesn't actually
work.  I mentioned this to Mani and he sent me a corrected version.  The
algorithm is not as fast (in APL, at least) as the algorithms described
here in June, and the result isn't in sorted order, and it doesn't work
for N{<=}2.  But the technique is intriguing because it builds a set of
square matrices and extracts permutations by reading off the rows and
columns of the matrices.  Half the permutations are found in the rows
and half in the columns!  An APL implementation is appended below.

   The article has a pointer to an interesting paper: "Permutation
Generation Methods", by Robert Sedgewick, in _Computing Surveys_, Vol.
9, No. 2, June 1977.  (Incidentally, Roger Hui's indexing algorithm is
not among those listed.)  The paper describes an algorithm, attributed
to D. N. Lehmer, for generating the i-th lexical pemutation of {iota}N
(function LEXPERM, below).  I had wondered how J's "atomic permutation"
function was able to generate the i-th lexical permutation so quickly.
It's simple enough to invert the algorithm to produce a function similar
to J's "atomic index" function (PERMNDX below).


Note:  These programs assume #IO is 1.
       For you IBMers, {commabar} is ,[#IO].

     {del} Z{<-}PERMDD3 N;I;S


[5]    Z{<-}1 2{rho}{iota}2
[6]    I{<-}2
[7]   L1:{->}(N<I{<-}I+1)/L2
[8]    Z{<-}I{slashbar}Z,I
[9]    Z{<-}((1{take}{rho}Z){rho}-{iota}I){rotate}Z
[10]   {->}L1
[11]  L2:S{<-}((1{take}{rho}Z){divide}N),N,N
[12]   Z{<-}Z{commabar}({rho}Z){rho}1 3 2{transpose}S{rho}Z


LEXPERM1 and PERMNDX1 are simple versions that process only a single
permutation.  LEXPERM and PERMNDX are more complicated parallel versions
that accept or return an array of permutation indices.  Some examples:

1 2 3 4 5 7 6

      P{<-}0 1000 5039 LEXPERM 7

0 1000 5039

     {del} Z{<-}J LEXPERM1 N;C;I

[3]    Z{<-},1
[4]    C{<-}1+({reverse}1+{iota}N-1){represent}J
[5]    I{<-}N
[6]   L1:{->}(0=I{<-}I-1)/0
[7]    Z{<-}C[I],Z+Z{>=}C[I]
[8]    {->}L1

     {del} Z{<-}PERMNDX1 P;N

[2]    N{<-}{rho}P{<-},P
[3]    Z{<-}{iota}0
[4]   L1:{->}(1{>=}{rho}P)/L2
[5]    Z{<-}Z,1{take}P
[6]    P{<-}1{drop}P-P>1{take}P
[7]    {->}L1
[8]   L2:Z{<-}({reverse}1+{iota}N-1){basevalue}Z-1

     {del} Z{<-}J LEXPERM N;C;I


[4]    Z{<-}(1,{times}/{rho}J){rho}1
[5]    C{<-}1+({reverse}1+{iota}N-1){represent},{transpose}J
[6]    I{<-}N
[7]   L1:{->}(0=I{<-}I-1)/L2
[8]    Z{<-}C[I;]{commabar}Z+Z{>=}({rho}Z){rho}C[I;]
[9]    {->}L1
[10]  L2:Z{<-}{transpose}((1{take}{rho}Z),{reverse}{rho}J){rho}Z

     {del} Z{<-}PERMNDX P;S

[4]    S{<-}{rho}P
[5]    P{<-}{transpose}(({times}/{neg}1{drop}S),{neg}1{take}1,S){rho}P
[6]    Z{<-}(0 1{times}{rho}P){rho}0
[7]   L1:{->}(1{>=}1{take}{rho}P)/L2
[8]    Z{<-}Z{commabar}P[1;]
[9]   P{<-}1 0{drop}P-P>({rho}P){rho}P[1;]
[10]   {->}L1
[11]  L2:Z{<-}({neg}1{drop}S){rho}({reverse}1+{iota}{neg}1+{neg}1{take}1,S){+

Sat, 23 May 1998 03:00:00 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Fwd: re: Puzzler: Permutations and Anagrams

2. Puzzler: Permutations and Anagrams

3. Puzzler: Permutations and Anagrams

4. Puzzler: function of permutation vector

5. all-permutations is faster than permutation

6. A permutation on permutations

7. Anagram Assist and TopDown vs. BottomUp

8. anagram

9. speedup of anagram finder

10. speedup of anagram finder

11. Anagrams in Prolog

12. Anagram


Powered by phpBB® Forum Software