perlfaq4 asks and answers the question How do I permute N elements of a list?. So Tom Christiansen and Nat Torkington apparently feel that this question is frequently asked. And in my experience, it is frequently asked. I'm sure that folks come into the comp.lang.perl.misc newsgroup fairly often asking how to do this.

My question is: Why? Who needs to do this, and what for?

I ask because I've just written a long article about how to generate permutations efficiently (the FAQ code is not very good) but I just can't come up with a plausible example of why anyone would care. I know that people do sometimes care, but I don't know why.

I'd most prefer to hear from folks who have actually needed to do this, but speculations would be interesting also.

Thanks.

--
Mark Dominus
Perl Paraphernalia

  • Comment on Who Needs to Find the Permutations of a List?

Replies are listed 'Best First'.
Re: Who Needs to Find the Permutations of a List?
by jlongino (Parson) on Oct 07, 2001 at 04:01 UTC
    Reading your post I happen to remember replying to the node Sorting characters within a string. Apparently geneticists do alot of this stuff. I seem to remember another post in Meditations along a similar line, although I couldn't find it as readily.

    "Make everything as simple as possible, but not simpler." -- Albert Einstein

        For some reason robin doesn't bring his grate brane here very often, so I passed the question on. Verbatim:

        The most obvious use of an exhaustive permutation generator is brute force search. Examples aren't difficult to come up with - see for example Jason Brazile's 1998 TPJ article Iterating Over Permutations which begins "My nephew asked for help with a homework problem" and proceeds to show how a permutation generator can be used to brute force a solution to the problem.

        The ideal motivating problem would be one that is:
        a) Susceptible to brute force search over permutations
        b) Not obviously solvable by a more efficient method
        c) As natural (uncontrived) as possible.

        There is an extensive directory of NP-complete problems which satisfy (b) and (c), which would seem to be a good place to start looking.

        For example, the minimum linear arrangement problem obviously satsfies condition (a).

        More useful, perhaps, is the problem of "Sequencing with Release Times and Deadlines" which any programmer who has to manage her own time will be informally familiar with! That can be solved by trying all the possible ordering of tasks (i.e. all the permutations of the task list) until an ordering is found which works (or not).

Re: Who Needs to Find the Permutations of a List?
by Masem (Monsignor) on Oct 07, 2001 at 04:59 UTC
    How about for generating quizzes, where you want to select M (<= N) distinct questions from a set of N questions, mixing the order to prevent 'cheating' or similar. Permutating the list beforehand simply allows one to pop or shift off new questions instead of continuing to do a loop on a random number until you reach one that hasn't been done.

    Also, not just only for genetic simulations, but for other cases where order may or may not matter (say, travelling salesmen approach with genetic algorithsm), permutations are easier to start with then trying to build up a 'random' list .

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

Re: Who Needs to Find the Permutations of a List?
by Dr. Mu (Hermit) on Oct 07, 2001 at 07:31 UTC
    If you're using a genetic algorithm, say, to solve a travelling salesman problem, you need to be able to enumerate the permutations of a list of destinations, representing each as a unique string. These strings are the "chromosomes" which undergo reproduction and selection, the goal being to find the trajectory that minimizes the overall distance travelled in visiting all the destinations.
Re: Who Needs to Find the Permutations of a List?
by tommyw (Hermit) on Oct 07, 2001 at 04:59 UTC

    I've had to write code which generates programmes of study for students (although I did it in C, not perl), and it relates to power sets, not permuations. But they're similar problems. (Power sets, if you aren't aware, are all the possible subsets of a set. So there are n! permutations of a list, but 2n power sets.)

    Since I was working for a modular course, the students frequently had to take 2 out of <these 3 modules>, and 2 out of <these other 4 modules> etc, etc.

    The brute force and ignorance method (and oh boy, was there ever a lot of ignorance. Unfortunately, the force wasn't quite as brute as the algorithm demanded...) was to generate the power set of the target list, merge it with the student's existing program and then delete those results which didn't meet requirements (or were more complicated than necessary).

(jeffa) Re: Who Needs to Find the Permutations of a List?
by jeffa (Bishop) on Oct 07, 2001 at 22:49 UTC
    The problem that brought me to PerlMonks in the first place was to provide a list of possible corrected sentances from a sentance that contained zero or more misspellings (user input to a search script). I thought drop down boxes would suffice, but others thought, well, otherwise . . . they wanted a list of sentances that could be clicked to resubmit the form (they also wanted drag and drop shopping carts in a virtual 3-d world - but that's another story).

    Turned out, permutations was the trick . . . here is the original question (asked by proxy - eduardo):

    permutations?

    jeffa

Re: Who Needs to Find the Permutations of a List?
by buckaduck (Chaplain) on Oct 09, 2001 at 16:08 UTC
    A scientific problem we worked on here recently:

    For a given mass (as detected via mass spectrometry), determine the combinations of atomic weights that could possibly combine into a fragment of that mass.

    One way to solve this problem is by permuting large lists of atomic weights, for those atoms which are considered likely candidates under the circumstances.

    buckaduck