# Generating permutations of at most 'n' elements, and where a specific subset of elements always appears

I often run into a situation where I'd like to generate a set of permutations with at most $n$ elements (here $n = 5$):

Permutations[{"t1","t2","t3","t4","t5","t6","t7","t8","t9","t10","t11","t12","t13"}, 5]


I'd like to also specify that a specific subset of elements always appears in each subset (without generating the entire list of permutations and scanning through it, or scanning through the permutations in lexicographic order: Generating a permutation of elements in chunks). For example, could one generate a list of all length $n = 5$ permutations for the above example where the subset of elements {"t2","t5","t7"} always appears (in any order)?

Is there a (fast) way to ask Mathematica to do this? One solution would be to ask for all length $q = 2$ subsets of {"t1","t2","t3","t4","t5","t6","t7","t8","t9","t10","t11","t12","t13"}, concatenate these subsets with the list {"t2","t5","t7"}, generate the permutations for each subset, then concatenate each list of permutations. However, is there maybe a nicer solution?

• I think the only way to get the result is to pick the lists you want from the all Permutations[list,5]. It will be so easy this way. Something like this: Cases[allPermutations, x_ /; SubsetQ[x, {"t2","t5","t7"}]] – Algohi Dec 27 '14 at 2:59
• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Dr. belisarius Dec 27 '14 at 22:18

l = {"t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9", "t10", "t11", "t12", "t13"};
lAlways = {"t2", "t5", "t7"};

f[n_Integer, l_List, always_List] :=
Module[{lVar = Complement[l, always], toSel = n - Length@always},
Flatten[Permutations /@ (Join[always, #] & /@ Subsets[lVar, {toSel}]), 1]]
f[5, l, lAlways] // Length

(*5400*)

f[5, l, lAlways][[;; 5]] // Column


Similar to Algohi's proposal but without SubsetQ so that it runs also in version 8.

list = {"t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9", "t10",
"t11", "t12", "t13"};

list1 = {"t2", "t5", "t7"};

t = Permutations[list, {5}];
Length[t]
(* 154440  *)

t1= Select[t, Intersection[#, list1] == list1 &];

Length[t1 ]
(* 5400 *)

t1//Short


$\{\{\text{t1},\text{t2},\text{t3},\text{t5},\text{t7}\},\{\text{t1},\text{t2},\text{t3},\text{t7},\text{t5}\},\{\text{t1},\text{t2},\text{t4},\text{t5},\text{t7}\},\{\text{t1},\text{t2},\text{t4},\text{t7},\text{t5}\},\{\text{t1},\text{t2},\text{t5},\text{t3},\text{t7}\},\langle\langle 5391\rangle\rangle ,\{\text{t13},\text{t12},\text{t5},\text{t2},\text{t7}\},\{\text{t13},\text{t12},\text{t5},\text{t7},\text{t2}\},\{\text{t13},\text{t12},\text{t7},\text{t2},\text{t5}\},\{\text{t13},\text{t12},\text{t7},\text{t5},\text{t2}\}\}$

Best regards,
Wolfgang

• The OP specifically stated "(without generating the entire list of permutations and scanning through it)". I believe this solution is doing exactly that :) – Dr. belisarius Dec 30 '14 at 14:23
• @belisarius: you are right, I neglected the condition "without generating the entire list ...", but I simply wanted to make Algohi's proposal running in V8. BTW you didn't object to his comment. BTW2: your solution shows that Algohi was wrong in stating that his is the "only way". – Dr. Wolfgang Hintze Dec 30 '14 at 22:23
• I've never seen a problem with only one possible solution in Mathematica :) – Dr. belisarius Dec 30 '14 at 22:42
• @belisarius: that's what I was saying. – Dr. Wolfgang Hintze Jan 1 '15 at 12:16