Message Boards Message Boards

3 Replies
0 Total Likes
View groups...
Share this post:

Permutations with conditions: element a to appear before c

Posted 9 years ago


i need to calculate something in Mathematica; We have the list

A = {a,b,c,d,e} 

I need to find all permutations which will contain exactly 3 elements from A in which:

  1. element b appears only once (or does not appear at all)
  2. if a and c appear, a will always appear before c

So far i have:

Select[Tuples[A, 3], OrderedQ]

But it seems i'm missing quite a lot .. Any help will be appreciated. Thanks!

POSTED BY: Lara Craft
3 Replies
Posted 7 years ago

I believe this works too

Clear[a, b, c, d, e]; Sort /@ 
 Select[Tuples[{a, b, c, d, e}, {3}], Count[#, b] <= 1 &]
POSTED BY: Paul Cleary
Posted 7 years ago

A couple of ways come to mind. Both delete the cases that are not wanted from the output of the permutation generator.

aa = {a, b, c, d, e};
p1 = DeleteCases[
  Tuples[{aa, aa, aa}], {___, c, ___, a, ___} | {___, b, ___, b, ___}];
bb = {a, a, a, b, c, c, c, d, d, d, e, e, e};
p2 = DeleteCases[Permutations[bb, {3}], {___, c, ___, a, ___}];

p1 and p2 are the same list.

POSTED BY: Larry Morris

There might be a clever way to do this, but let's focus on defining functions that test your two properties

testA[list_] := Count[list, b]<2

testB[list_] := If[MemberQ[list, a] && MemberQ[list, b], 
                   First@FirstPosition[list, a] < First@FirstPosition[list, b], 

You can use these functions with Select

POSTED BY: Sean Clarke
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract