2
$\begingroup$

How to generate a list of fixpoint free permutations of n elements in mathematica?

$\endgroup$
5
$\begingroup$

Here a brute force method.

n = 4;
perms = Permutations[Range[n]];
Pick[perms, Unitize[Min[Abs[# - Range[n]]] & /@ perms], 1]

{{2, 1, 4, 3}, {2, 3, 4, 1}, {2, 4, 1, 3}, {3, 1, 4, 2}, {3, 4, 1, 2}, {3, 4, 2, 1}, {4, 1, 2, 3}, {4, 3, 1, 2}, {4, 3, 2, 1}}

$\endgroup$
5
$\begingroup$
With[{n = 4},
  Select[Permutations[Range[n]], Length[PermutationSupport[#]] == n &]]

{{2, 1, 4, 3}, {2, 3, 4, 1}, {2, 4, 1, 3}, {3, 1, 4, 2}, {3, 4, 1, 2}, {3, 4, 2, 1}, {4, 1, 2, 3}, {4, 3, 1, 2}, {4, 3, 2, 1}}

The fraction of permutations satisfying this condition is $1/e$ as $n\to\infty$, so the above code is not very wasteful.

$\endgroup$
  • $\begingroup$ Thank you all very much for the hints. $\endgroup$ – Darwin1871 Apr 6 '19 at 10:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.