[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference rusure::math

Title:Mathematics at DEC
Moderator:RUSURE::EDP
Created:Mon Feb 03 1986
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2083
Total number of notes:14613

1365.0. "fun with permutations" by HANNAH::OSMAN (see HANNAH::IGLOO$:[OSMAN]ERIC.VT240) Fri Jan 04 1991 16:25

I needed a program that would generate all permutations of n objects, and
I came up with this one.  It's sort of interesting because it produces the
permuations in a strange order.  Anyone care to apply some math rigor to
explain why the program is guaranteed to produce every permutation exactly
once ?

Here it is:     /Eric

#define ndigs 5
main()
{
int choiceptr,permptr,value,weight,tryslot,perm[ndigs],fact,i;

fact = ndigs;

for (i=ndigs-1; i>1; i--) fact *= i;

for (permptr=0; permptr<fact; permptr++)
	{
	value = permptr;
	for (tryslot=0; tryslot<ndigs; tryslot++)
		perm[tryslot] = -1;
	for (weight=ndigs; weight>=1; weight--)
		{
		choiceptr = value % weight;
		value /= weight;
		tryslot = 0;
		while (choiceptr >= 0)
			if (perm[tryslot++] < 0) choiceptr--;
		perm[tryslot-1] = ndigs - weight;
		}
	for (i=0; i<ndigs; i++) printf ("%d",perm[i]);
	printf ("\n");
	}
}
T.RTitleUserPersonal
Name
DateLines
1365.1GUESS::DERAMODan D'EramoFri Jan 04 1991 17:016
        It looks like you are using an explicit bijective
        correspondence between {1,...,n!} and the permutation
        group on n objects.  Just loop from 1 to n! and print out
        the "decoding" of the loop index variable.
        
        Dan
1365.2HANNAH::OSMANsee HANNAH::IGLOO$:[OSMAN]ERIC.VT240Fri Jan 04 1991 17:135
Even if the decoding is unique, it doesn't prove that the permuations are
unique, since we must prove that whenever two decodings differ, their resultant
permuations differ also.

1365.3Another approachCIVAGE::LYNNLynn Yarbrough @WNP DTN 427-5663Fri Jan 04 1991 20:029
There are several nice algorithms to do this. One of the best is NEXPER in 
Nijenhuis & Wilf's *Combinatorial Algorithms*, which generates them one at
a time by interchanging two object indices, which has the effect of
minimizing the amount of object movement. I have a nice structured FORTRAN
version of this if you need it. 

No, it does not vectorize well!

Lynn 
1365.4GUESS::DERAMODan D'EramoFri Jan 04 1991 20:489
        The Knuth section on random number generation discusses
        encoding a permutation as a number from 1 to n!  It's
        kind of a base 2-3-4-...-n (or was it n-...-4-3-2?)
        notation.  It's not as useful to run through every
        permutation as the simpler "next permutation" generators
        are, but it gives you "random access" to the k-th
        permutation.
        
        Dan
1365.5YAP (Yet Another Permutation-algorithm)UTRUST::DEHARTOGmoduladaplisprologopsimulalgolMon Jan 07 1991 07:5420
Years ago I also needed the permutation of a given string of characters.
Here follows the code. For every string it reads (until EOF), it prints
all permutations starting with the string itself and ending with the
reversed string.

#include <stdio.h>
#define ML 100
int L; char A[ML], B[ML];
main(){
	while(gets(A)){
                for(L=0;L<ML;L++) B[L]='\0'; L=strlen(A); perm(A);
        }
}

perm(S) char *S; {
	int j;
        for(j=0;j<L;j++) if(!B[j]){
                B[j] = *S; if(S[1]) perm(S+1); else puts(B); B[j]='\0';
        }
}
1365.6CIVAGE::LYNNLynn Yarbrough @WNP DTN 427-5663Mon Jan 07 1991 14:163
[.5] This is the simple lexical-order recursion algorithm, which is very
clean but, I think, does more than the minimum data movement. For example,
when racheting from [15432] to [21345] everything moves.