# Help with Permutations

I am currently trying to generate a list of permutations of length 2 of elements of a list of strings. For example:

Permutations[{"s1", "s2", "s3", "s4"}, {2}]
{{"s1", "s2"}, {"s1", "s3"}, {"s1", "s4"}, {"s2", "s1"}, {"s2", "s3"}, {"s2", "s4"}, {"s3", "s1"}, {"s3", "s2"}, {"s3", "s4"}, {"s4", "s1"}, {"s4", "s2"}, {"s4", "s3"}}


which is quite correct.

Now, my problem starts when attempting the same operation for a really big list of strings imported from a tsv file - we are talking of a bit more than 10000 strings. All sort of characters can be expected in strings.

This is the code that I am using:

t1 = Import["/home/bla/work/parsed_names.tsv"]
t2 = Table[t1[[i]][], {i, 1, Length[t1]}]
Permutations[t2, {2}]


After evaluation of the last line, I get no output. If I evaluate it twice, all the symbols defined previously (t1, t2) become undefined, and Permutations finally give this output:

Permutations[t2, {2}]
Permutations::normal: Nonatomic expression expected at position 1 in Permutations[t2,{2}]. >>
Permutations[t2, {2}]


Which is the output for an undefined value of t2.

I tried to debug the problem: the import operations seems to go fine, each element of t2 is a proper string:

Length[t2]
10525

Tally[Table[Head[t2[[i]]], {i, 1, Length[t2]}]]
{{String, 10525}}


I tried running Trace to understand what is going on, but Mathematica still kept quiet during the first evaluation. The second evaluation again forces all the symbols to be cleared, and the output of Trace is unhelpful:

{Permutations[t2,{2}], {Message[Permutations::normal,1,Permutations[t2,{2}]],
{MakeBoxes[Permutations::normal: Nonatomic expression expected at position 1 in Permutations[t2,{2}]. >>,StandardForm],
RowBox[{StyleBox[RowBox[{Permutations,::,normal}],MessageName],: ,
"Nonatomic expression expected at position \!$$1$$ in \!$$Permutations[\(\(t2, \(\({2}$$\)\)\)]\). \!$$\*ButtonBox[\">>\", ButtonStyle->\"Link\", ButtonFrame->None, ButtonData:>\"paclet:ref/message/General/normal\", ButtonNote -> \"Permutations::normal\"]$$"}]},Null},Permutations[t2,{2}]}


Is there something obviously wrong that I am doing? If not, how can I debug this problem further?

• Can you upload the tsv file compressed? – M.R. Aug 29 '12 at 19:52
• If you simply type, "t1" and hit shift + enter, what does mms show? – DavidC Aug 29 '12 at 19:58
• @Mike: unfortunately I cannot – paolo Aug 29 '12 at 20:00
• Look at how it scales: ListLogLogPlot@Table[{i, Length[Permutations[Range[i], {2}]]}, {i,10^Range[1, 3., .2]}] gives this. It's probably related (but I don't see exactly what is happening) – acl Aug 29 '12 at 20:01
• @paolo, keep an eye on the In[] values for your inputs. If it resets to In you know the Kernel has quit and restarted. – Simon Woods Aug 29 '12 at 20:13

## 1 Answer

Perhaps I misunderstood, but look at how many elements there should have been in your list:

ListLogLogPlot@Table[{i, Length[Permutations[Range[i], {2}]]}, {i,10^Range[1, 3., .2]}] It sounds like your kernel quits when you try to calculate the permutations.

• Unfortunately, I believe you may be correct. The fact that the symbols gets undefined at the second evaluation could point towards a kernel restart. – paolo Aug 29 '12 at 20:12
• @paolo If memory serves me right, then the number of permutations you are trying to produce is Binomial[10000,2] which is roughly 5*10^7, not counting ordering. It's not very suprising that Mathematica is not happy finding all of them. Just crashing the Kernel is kindof rude though... – sebhofer Aug 30 '12 at 22:56