I didn't analyze your algorithm, but I don't think it finds all of the possibilities. For example, your output is missing {{2, 3, 1}, {4, 5, 1}, {6, 3, 2}, {10, 11, 1}}.
As I've said, the following condition must also be met:
All the second elements in a specific sub-list should be different.
In the above result shown by you, 3 appears twice in the 2nd positions of the sub-lists, so this shouldn't be a desired solution in my case, and I removed it from the results:
lst=Select[ sub, (#[[All,1]]//Total)==n && (#[[All,2]]//Length)==(Union[ #[[All,2]] ]//Length) & ]
I came up with an algorithm that is faster and that finds more results. It uses IntegerPartitions instead of Subsets. However it typically takes over a minute when length of pk is 30.
Does your approach also rely on dynamic programming (DP) technology?
I wonder if you can show me the algorithm?
Here's another algorithm I've noticed for this problem:
(* 2.2 The practical code
We here present a light code for the algorithm, which is written in language “Mathematica”
because of its easily available and amicable windows. *)
BeginPackage["DiscreteMath‘SubsetSum‘"]
SubsetSum::usage = "Find all solutions for subset sum problem."
Begin["‘Private‘"]
SubsetSum[t_, W_List]:=
Module[{T,K,A,len,F},
T={t};
For[i=1,i<=Length[W],i++,B=T-W[[i]];T=Join[T,B]];
K=Flatten[Position[T,0]];
If[Length[K]>0,
For[i=1,i<=Length[K],i++,
A=IntegerDigits[2(K[[i]]-1),2];
len=Length[A];F={};
For[j=1,j<=len,j++,If[A[[j]]==1,F=Append[F,W[[len-j]]]]];
Print[F]
],
Print["Fail"]
]
]
End[]
EndPackage[]
How long will your pk be? Do you need to find absolutely all solutions?
I'm solving a problem related to number theory and group theory, and of course I want the algorithm to deal with extremely long lists as efficiently as possible, so that I can make a larger comparison database for others to study and use.