问题
I am trying to generate all the sequences of a given variable with some precedence constraint. For example, if we have five objects, [1 2 3 4 5]
, then there are 5! = 125
ways to permute those. However, if we impose some precedence constraints like:
- a) The sequence should always start from 1.
- b) After 1 only 2 or 3 can be attached.
- c) 4 can come after 5 or 3
- d) 5 can come after 4 or 2
only these possibilities remain:
1 2 3 5 4,
1 2 3 4 5,
1 2 5 4 3,
1 2 5 3 4,
1 3 4 5 2,
1 3 4 2 5,
1 3 2 5 4,
1 3 2 4 5,
Like for six Objects the codes with constraints is:-
allCombs = perms(1:objects);
constraintsFor1 = union(findKAfterNRows(allCombs,2,1),findKAfterNRows(allCombs,3,1));
constraintsFor1 = intersect(constraintsFor1,find(allCombs(:,1)==1));
constraintsFor4 = union(findKAfterNRows( allCombs, 4,5 ),findKAfterNRows( allCombs, 4,3 ));
constraintsFor5 = union(findKAfterNRows( allCombs, 5,2 ),findKAfterNRows( allCombs, 5,4 ));
constraintsFor6 = union(findKAfterNRows( allCombs, 6,4 ),findKAfterNRows( allCombs, 6,5 ));
resultRows = intersect(constraintsFor1,constraintsFor4);
resultRows = intersect(resultRows,constraintsFor5);
resultRows = intersect(resultRows,constraintsFor6);
output = allCombs(resultRows,:);
来源:https://stackoverflow.com/questions/37242533/is-there-any-way-to-do-permutation-of-more-than-11-components