Guest User

Untitled

a guest
Jul 22nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. //allPossibleItems is an AL of all items
  2.  
  3. //this is called with generatePerm(null, new ArrayList<Item>);
  4.  
  5. private void generatePerm(Item i, ArrayList<Item> a) {
  6. if(i != null) { a.add(i); }
  7. if (a.size() == DESIRED_SIZE){
  8. permutations.add(a);
  9. return;
  10. }
  11. for(int j = 0; j < allPossibleItems.size(); j ++) {
  12. if(allPossibleItems.get(j) != i)
  13. generatePerm(allPossibleItems.get(j), a);
  14. }
  15. }
  16.  
  17. public <E> List<List<E>> generatePerm(List<E> original) {
  18. if (original.size() == 0) {
  19. List<List<E>> result = new ArrayList<List<E>>();
  20. result.add(new ArrayList<E>());
  21. return result;
  22. }
  23. E firstElement = original.remove(0);
  24. List<List<E>> returnValue = new ArrayList<List<E>>();
  25. List<List<E>> permutations = generatePerm(original);
  26. for (List<E> smallerPermutated : permutations) {
  27. for (int index=0; index <= smallerPermutated.size(); index++) {
  28. List<E> temp = new ArrayList<E>(smallerPermutated);
  29. temp.add(index, firstElement);
  30. returnValue.add(temp);
  31. }
  32. }
  33. return returnValue;
  34. }
  35.  
  36. //allPossibleItems is an AL of all items
  37.  
  38. //this is called with generatePerm(null, new ArrayList<Item>);
  39.  
  40. private void generatePerm(Item i, ArrayList<Item> a) {
  41. if(i != null) { a.add(i); }
  42. if (a.size() == DESIRED_SIZE){
  43. permutations.add(a);
  44. return;
  45. }
  46. for(int j = 0; j < allPossibleItems.size(); j ++) {
  47. if(!a.contains(allPossibleItems.get(j))){
  48. ArrayList<Item> b = clone(a);
  49. generatePerm(allPossibleItems.get(j), b);
  50. }
  51. }
  52. }
  53.  
  54. private List generatePerm(List a, int depth) {
  55. // this is the method definition you want
  56. // to generate all permutations, you need cycle thru all elements in your list and for each element
  57. // add that element to each member of generatePerm(a, depth - 1);
  58. // if you want combinations, you need to remove the element and then call
  59. /// generateCombinations with the remaining list
  60. }
  61.  
  62. public static ArrayList recurse_nums(Set<Integer> currentnums, String currentstring, ArrayList list_of_permutes){
  63. if(currentnums.size()==1){
  64. int elem = currentnums.iterator().next();
  65. list_of_permutes.add(currentstring + Integer.toString(elem));
  66. return list_of_permutes;
  67. }
  68. for(int a:currentnums){
  69. String newstring = currentstring + a;
  70. Set<Integer> newnums = new HashSet<>();
  71. newnums.addAll(currentnums);
  72. newnums.remove(a);
  73. recurse_nums(newnums, newstring,list_of_permutes);
  74. }
  75. return list_of_permutes;
  76. }
  77.  
  78. public static ArrayList permute_array(int[] arr){
  79. Set<Integer> currentnums = new HashSet<>();
  80. for (int i = 0; i < arr.length; i++) {currentnums.add(arr[i]);}
  81. ArrayList permutations = new ArrayList();
  82. recurse_nums(currentnums,"",permutations);
  83. return permutations;
  84. }
Add Comment
Please, Sign In to add comment