Is there any way to do permutation of more than 11 components?

杀马特。学长 韩版系。学妹 提交于 2020-01-06 19:46:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!