Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating permutations with sub-linear memory

I am wondering if there is a sufficiently simple algorithm for generating permutations of N elements, say 1..N, which uses less than O(N) memory. It does not have to be compute n-th permutation, but it must be able to compute all permutations.

Of course, this algorithm should be a generator of some kind, or use some internal data structure which uses less than O(N) memory, since return the result as a vector of size N already violates the restriction on sub-linear memory.

like image 972
eold Avatar asked Nov 13 '22 14:11

eold


1 Answers

Let's assume that the random permutation is being generated one entry at a time. The state of the generator must encode the set of elements remaining (run it to completion) and so, since no possibility can be excluded, the generator state is at least n bits.

like image 95
not for show Avatar answered Dec 05 '22 04:12

not for show