4

Here is code from Simon Woods' answer for getting all possible weak (equal ranks allowed) orderings for $N=3$ objects:

 ClearAll[f]; SetAttributes[f, Orderless];
 ReplaceList[f[a, b, c], f[a___, b___, c___] :> {{a}, {b}, {c}}] //
 DeleteCases[#, {}, -1] & // Union // Column

It gives $13$ such orderings:

{{a, b, c}}
{{a}, {b, c}}
{{b}, {a, c}}
{{c}, {a, b}}
{{a, b}, {c}}
{{a, c}, {b}}
{{b, c}, {a}}
{{a}, {b}, {c}}
{{a}, {c}, {b}}
{{b}, {a}, {c}}
{{b}, {c}, {a}}
{{c}, {a}, {b}}
{{c}, {b}, {a}}

How can I modify this code for the case when not more than $2$ subsets are allowed? The desired output is:

{{a, b, c}}
{{a}, {b, c}}
{{b}, {a, c}}
{{c}, {a, b}}
{{a, b}, {c}}
{{a, c}, {b}}
{{b, c}, {a}}

I am trying to find way for doing such reductions in general $N$ and for any number of subsets-restriction.

aeiklmkv
  • 177
  • 6
  • 2
    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 Nov 26 '13 at 14:35

2 Answers2

5

Here's how you can generalize the code for any $N$:

ClearAll@weakOrderings
weakOrderings[list_, n_Integer] := 
    Block[{f, x = Table[Unique["x"], {n}]},
        SetAttributes[f, Orderless];
        With[{lhs = f @@ (Pattern[#, BlankNullSequence[]] & /@ x), rhs = List /@ x},
            ReplaceList[f @@ list, lhs :> rhs] // DeleteCases[#, {}, -1] & // Union // Column
        ]
    ]

You can verify that it gives you the expected results:

enter image description here

rm -rf
  • 88,781
  • 21
  • 293
  • 472
  • Hm... No output gives. Sorry, I am null in Mathematica. Just copy-pasted your code and used Ctrl+Enter. – aeiklmkv Nov 26 '13 at 14:12
  • @aeiklmkv I showed how to call the function in the screenshot... – rm -rf Nov 26 '13 at 14:14
  • @rm-rf Now I understand. It works. – aeiklmkv Nov 26 '13 at 14:24
  • 3
    @YvesKlett This is not my homework. I need it for statistical analysis of some experimental data. I am not lazy, just a beginner in Mathematica. – aeiklmkv Nov 26 '13 at 14:25
  • @aeiklmkv o.k. - you can motivate possible helpers by showing what you tried by yourself, where exactly you are stuck etc. since the problem is not an entirely trivial one (and this answer is a beginners nightmare as well, the frog layed on thick) :D – Yves Klett Nov 26 '13 at 14:27
  • @YvesKlett I was afraid to clutter up the message. My attempts were completely unsuccessful (I tried to delete needless orderings using multiple cycles), especially for the general case. I will try to consider your recommendation in the future. – aeiklmkv Nov 26 '13 at 14:31
  • @aeiklmkv well then: welcome to the party! – Yves Klett Nov 26 '13 at 14:39
  • 2
    @aeiklmkv Please consider that there are quite a few students around trying to suck other user's time to get their homework done without effort. Try to differentiate your questions from theirs – Dr. belisarius Nov 26 '13 at 14:42
3
Needs["Combinatorica`"]
f[l_List, n_Integer] := Flatten[Table[Union@Map[Sort, 
     Flatten[KSetPartitions[#, i] & /@ Permutations[l], 1], {2}], {i, n}], 1]

f[{a, b, c}, 2] // Column
(*
{{a,b,c}}
{{a},{b,c}}
{{b},{a,c}}
{{c},{a,b}}
{{a,b},{c}}
{{a,c},{b}}
{{b,c},{a}}
*)
f[{a, b, c}, 3] // Column
(*
{{a,b,c}}
{{a},{b,c}}
{{b},{a,c}}
{{c},{a,b}}
{{a,b},{c}}
{{a,c},{b}}
{{b,c},{a}}
{{a},{b},{c}}
{{a},{c},{b}}
{{b},{a},{c}}
{{b},{c},{a}}
{{c},{a},{b}}
{{c},{b},{a}}
*)
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453