3422

5
+20  Q:

## Generating permutations lazily

I'm looking for an algorithm to generate permutations of a set in such a way that I could make a lazy list of them in Clojure. i.e. I'd like to iterate over a list of permutations where each permutation is not calculated until I request it, and all of the permutations don't have to be stored in memory at once.

Alternatively I'm looking for an algorithm where given a certain set, it will return the "next" permutation of that set, in such a way that repeatedly calling the function on its own output will cycle through all permutations of the original set, in some order (what the order is doesn't matter).

Is there such an algorithm? Most of the permutation-generating algorithms I've seen tend to generate them all at once (usually recursively), which doesn't scale to very large sets. An implementation in Clojure (or another functional language) would be helpful but I can figure it out from pseudocode.

+3  A:

Hi,

You should check the Permutations article on wikipeda. Also, there is the concept of Factoradic numbers.

Anyway, the mathematical problem is quite hard.

In `C#` you can use an `iterator`, and stop the permutation algorithm using `yield`. The problem with this is that you cannot go back and forth, or use an `index`.

"Anyway, the mathematical problem is quite hard." No it's not :-)
Well, it is.. if you don't know about Factoradic numbers there is no way you could come up with a proper algorithm in an acceptable time. It's like trying to solve a 4th degree equation without knowing the method.
Oh sorry, I thought you were talking about the original problem. I still don't see why you need "Factoradic numbers" anyway... it's pretty simple to assign a number to each of the n! permutations of a given set, and to construct a permutation from a number. [Just some dynamic programming/counting..]
+39  A:

Yes, there is a "next permutation" algorithm, and it's quite simple too. The C++ standard template library (STL) even has a function called `next_permutation`.

The algorithm actually finds the next permutation -- the lexicographically next one. The idea is this: suppose you are given a sequence, say "32541". What is the next permutation?

If you think about it, you'll see that it is "34125". And your thoughts were probably something this: In "32541",

• there is no way to keep the "32" fixed and find a later permutation in the "541" part, because that permutation is already the last one for 5,4, and 1 -- it is sorted in decreasing order.
• So you'll have to change the "2" to something bigger -- in fact, to the smallest number bigger than it in the "541" part, namely 4.
• Now, once you've decided that the permutation will start as "34", the rest of the numbers should be in increasing order, so the answer is "34125".

The algorithm is to implement precisely that line of reasoning:

1. Find the longest "tail" that is ordered in decreasing order. (The "541" part.)
2. Change the number just before the tail (the "2") to the smallest number bigger than it in the tail (the 4).
3. Sort the tail in increasing order.

You can do (1.) efficiently by starting at the end and going backwards as long as the previous element is not smaller than the current element. You can do (2.) by just swapping the "4" with the '2", so you'll have "34521". Once you do this, you can avoid using a sorting algorithm for (3.), because the tail was, and is still (think about this), sorted in decreasing order, so it only needs to be reversed.

The C++ code does precisely this (look at the source in `/usr/include/c++/4.0.0/bits/stl_algo.h` on your system, or see this article); it should be simple to translate it to your language: [Read "BidirectionalIterator" as "pointer", if you're unfamiliar with C++ iterators. The code returns `false` if there is no next permutation, i.e. we are already in decreasing order.]

``````template <class BidirectionalIterator>
bool next_permutation(BidirectionalIterator first,
BidirectionalIterator last) {
if (first == last) return false;
BidirectionalIterator i = first;
++i;
if (i == last) return false;
i = last;
--i;
for(;;) {
BidirectionalIterator ii = i--;
if (*i <*ii) {
BidirectionalIterator j = last;
while (!(*i <*--j));
iter_swap(i, j);
reverse(ii, last);
return true;
}
if (i == first) {
reverse(first, last);
return false;
}
}
}
``````

It might seem that it can take O(n) time per permutation, but if you think about it more carefully, you can prove that it takes only O(n log n) time for all permutations in total, so only O(1) -- constant time -- per permutation.

The good thing is that the algorithm works even when you have a sequence with repeated elements: with, say, "232254421", it would find the tail as "54421", swap the "2" and "4" (so "232454221"), reverse the rest, giving "232412245", which is the next permutation.

This will work, assuming you have a total order on the elements.
If you start with a set, you can arbitrarily define a total order on the elements; map the elements to distinct numbers. :-)
This algorithm works very well. Thanks.
You're welcome; I enjoyed revisiting it and thinking about the details too :-)
whee! neat. +1 -- I'm not an algorithms expert, but I've seen most of the basic ones and it's been a while since I've seen an algorithm that was both new to me, and easy to understand.
This answer just doesn't get enough upvotes, but I can only upvote it once... :-)
Very nice, I've implemented it in F#: http://projecteulerfun.blogspot.com/2010/05/problem-24-what-is-millionth.html
And here is a further developed version which is generic, takes a function for defining a total ordering on the elements, and is wrapped by a immutable functional sequence expression: http://stackoverflow.com/questions/286427/calculating-permutations-in-f/3180680#3180680
Probably a stupid question, but if at (1) you look for the longest tail of decreasing order, and in (3) you make the tail increasing order, isn't the longest tail afterwards always of length 1?(Probably a stupid question, but let's put it on account of sleep deprivation)
@Masse: Not exactly... roughly, you can go from 1 to a larger number. Using the example: Start with 32541. The tail is 541. After doing the necessary steps, the next permutation is 34125. Now the tail is just 5. Incrementing 3412 using the 5 and swapping, the next permutation is 34152. Now the tail is 52, of length 2. Then it becomes 34215 (tail length 1), 34251 (tail length 2), 34512 (length 1), 34521 (length 3), 35124 (length 1), etc. You are right that the tail is small most of the time, which is why the algorithm has good performance over multiple calls.
+1  A:

the permutations function in clojure.contrib.lazy_seqs already claims to do just this.

Thanks, I wasn't aware of it. It claims to be lazy, but sadly it performs very poorly and overflows the stack easily.
+12  A:

Assuming that we're talking about lexicographic order over the values being permuted, there are two general approaches that you can use:

1. transform one permutation of the elements to the next permutation (as ShreevatsaR posted), or
2. directly compute the `n`th permutation, while counting `n` from 0 upward.

For those (like me ;-) who don't speak c++ as natives, approach 1 can be implemented from the following pseudo-code, assuming zero-based indexing of an array with index zero on the "left" (substituting some other structure, such as a list, is "left as an exercise" ;-):

``````1. scan the array from right-to-left (indices descending from N-1 to 0)
1.1. if the current element is less than its right-hand neighbor,
call the current element the pivot,
and stop scanning
1.2. if the left end is reached without finding a pivot,
reverse the array and return
(the permutation was the lexicographically last, so its time to start over)
2. scan the array from right-to-left again,
to find the rightmost element larger than the pivot
(call that one the successor)
3. swap the pivot and the successor
4. reverse the portion of the array to the right of where the pivot was found
5. return
``````

Here's an example starting with a current permutation of CADB:

``````1. scanning from the right finds A as the pivot in position 1
2. scanning again finds B as the successor in position 3
3. swapping pivot and successor gives CBDA
4. reversing everything following position 1 (i.e. positions 2..3) gives CBAD
``````

For the second approach (direct computation of the `n`th permutation), remember that there are `N!` permutations of `N` elements. Therefore, if you are permuting `N` elements, the first `(N-1)!` permutations must begin with the smallest element, the next `(N-1)!` permutations must begin with the second smallest, and so on. This leads to the following recursive approach (again in pseudo-code, numbering the permutations and positions from 0):

``````To find permutation x of array A, where A has N elements:
0. if A has one element, return it
1. set p to ( x / (N-1)! ) mod N
2. the desired permutation will be A[p] followed by
permutation ( x mod (N-1)! )
of the elements remaining in A after position p is removed
``````

So, for example, the 13th permutation of ABCD is found as follows:

``````perm 13 of ABCD: {p = (13 / 3!) mod 4 = (13 / 6) mod 4 = 2; ABCD[2] = C}
C followed by perm 1 of ABD {because 13 mod 3! = 13 mod 6 = 1}
perm 1 of ABD: {p = (1 / 2!) mod 3 = (1 / 2) mod 2 = 0; ABD[0] = A}
A followed by perm 1 of BD {because 1 mod 2! = 1 mod 2 = 1}
perm 1 of BD: {p = (1 / 1!) mod 2 = (1 / 1) mod 2 = 1; BD[1] = D}
D followed by perm 0 of B {because 1 mod 1! = 1 mod 1 = 0}
B (because there's only one element)
DB