Newsgroups: comp.ai.neural-nets
From: Leon@lfheller.demon.co.uk (Leon Heller)
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!gatech!howland.reston.ans.net!news.sprintlink.net!peernews.demon.co.uk!lfheller.demon.co.uk!Leon
Subject: Re: Generating permutations
References: <3iu55r$65v@news.imssys.com>
Organization: Home
Reply-To: Leon@lfheller.demon.co.uk
X-Newsreader: Demon Internet Simple News v1.27
Lines: 35
X-Posting-Host: office.demon.net
Date: Wed, 1 Mar 1995 19:14:53 +0000
Message-ID: <794085293snz@lfheller.demon.co.uk>
Sender: usenet@demon.co.uk

In article <3iu55r$65v@news.imssys.com>
           jimk01@imssys.imssys.com "Jim Kennedy" writes:

> OK, this might be a weird question for this group, but *somebody* will
> know the answer to it!  What are some good algorithms for generating
> all the permutations of a set of items?  Given there are n! possible
> orders for n items, I would like to be able to create a matrix, or
> at least know how to create it, containing all possible orders of 
> some variables: it would be most cool if rows next to one other were
> similar, for instance, if ABCD was next to ABDC...  
> 
> It seems like we can do it in our heads-- how do you get a computer
> to do it?

I used to have a very neat program for this, translated from a Fortran
program I found somewhere. Unfortunately, I seem to have lost it.
Knuth, in his Fundamental Algorithms book, gives a couple of algorithms.

The simpler one is:

For each permutation a1a2...an-1 on (n-1) elements, form n
others by inserting the number n in all possible places, obtaining
n a1 a2 ... an-1, a1 n a2 ... an-1, ..., a1 a2 ... n an-1,
a1 a2 ... an-1 n.

the 1, 2, ... n-1, n are subscripts.

The algorithm is obviously recursive.

Leon
-- 
Leon Heller                       | "Do not adjust your mind, there is
G1HSM                             |  a fault in reality": on a wall
Email: leon@lfheller.demon.co.uk  |  many years ago in Oxford.
Phone: +44 (01734) 266679         |
