2

I would like MMA to only calculate some of the permutations of a set. I am currently doing something along the lines of

Permutations[Flatten[{Array[1 &, #], Array[0 &, # (# - 1)]}, 1]] &@3

and using Take and Drop before performing further calculation on smaller set.

For $n=3$ as above, this is fine, but since hte above calculation gives ${n^2}\choose{n}$ different permutations, asking MMA to calculate anything above $n=6$ results in SystemException["MemoryAllocationFailure"].

I don't know the algorithm used to calculate Permutations, but is there any way of calculating and returning only permutations between x and y?

martin
  • 8,678
  • 4
  • 23
  • 70
  • 1
    the Combinatorica package has a function called NextPermutation – vapor Mar 18 '17 at 07:10
  • @happyfish ...only problem is, I dont know what previous permutation was! - should work for x==1, but not for say, $1000$ permutations around x==n^2. – martin Mar 18 '17 at 07:13
  • I thought you can iterate over x, but never mind, you can try UnrankPermutation, it looks more suitable here, – vapor Mar 18 '17 at 07:15
  • @happyfish unsure how to impliment. Trying UnrankPermutation[3, Flatten[{Array[1 &, #], Array[0 &, # (# - 1)]}, 1]] &@3 but nothing much happening – martin Mar 18 '17 at 07:17
  • things like UnrankPermutation[#, {0, 0, 1}] & /@ Range[2, 4], but it will be a problem if you worry about duplicates, since the algorithm used for this function and NextPermutation does not remove duplicates – vapor Mar 18 '17 at 07:24
  • @happyfish, yes, this seems to be the problem - so for $n=7$, I'm looking at $49!$ permutations instead of ${n^2}\choose{n}$ - quite a big difference! – martin Mar 18 '17 at 07:28
  • 1
    I will try to write an algorithm – vapor Mar 18 '17 at 07:30
  • @happy fish - thanks :) – martin Mar 18 '17 at 07:30
  • @happyfish I think this question could be considered a duplicate of (1283). If it is closed as such I think it should be merged to move your answer to that Q&A. What do you think of this? martin, same question for you? – Mr.Wizard Mar 18 '17 at 19:57
  • @Mr.Wizard I'm not sure it is since the answer is specific to binary permutations. I have reworded the title to reflect that. In principle though, I have no objection to your proposal if it is closed. – martin Mar 18 '17 at 23:26
  • @Mr.Wizard No problem. Is there anything I need to do? – vapor Mar 19 '17 at 07:31
  • 1
    martin, I missed the fact that this was specific to binary permutations. Thank you for editing the title. @happyfish No, nothing you need to do; thanks for asking. – Mr.Wizard Mar 19 '17 at 08:00

1 Answers1

3

This function will only work for binary permutations

permutationChunk[total_, select_, start_, end_] := 
 ReplacePart[ConstantArray[0, total], Thread[# -> 1]] & /@ 
  Subsets[Range[total], {select}, {start, end}]

total is the length of the list, select is the number of 1s. You will have to know the total number of permutations, and then input start and end to get specific range of permutations.

For example,

Sort@permutationChunk[9, 3, 1, Binomial[9, 3]] == 
   Sort@Permutations[
     Flatten[{Array[1 &, #], Array[0 &, # (# - 1)]}, 1]] &@3
(*True*)
vapor
  • 7,911
  • 2
  • 22
  • 55