|
| 1 | +package com.jwetherell.algorithms.mathematics; |
| 2 | + |
| 3 | +import java.util.LinkedList; |
| 4 | + |
| 5 | +/** |
| 6 | + * In mathematics, the notion of permutation relates to the act of arranging all the members of a set into some sequence |
| 7 | + * or order, or if the set is already ordered, rearranging (reordering) its elements, a process called permuting. |
| 8 | + * <p> |
| 9 | + * http://en.wikipedia.org/wiki/Permutation |
| 10 | + * <br> |
| 11 | + * @author Justin Wetherell <phishman3579@gmail.com> |
| 12 | + * @author Lucjan Roslanowski <lucjanroslanowski@gmail.com> |
| 13 | + */ |
| 14 | +public class Permutations { |
| 15 | + |
| 16 | + /** |
| 17 | + * N! permutation of the characters in the string (in order) |
| 18 | + */ |
| 19 | + public static String[] permutations(String stringToGeneratePermutationsFrom) { |
| 20 | + final int size = numberOfPermutations(stringToGeneratePermutationsFrom.length()); |
| 21 | + final String[] list = new String[size]; |
| 22 | + final char[] prefix = new char[0]; |
| 23 | + final char[] chars = stringToGeneratePermutationsFrom.toCharArray(); |
| 24 | + permutations(list, 0, prefix, chars, 0, chars.length); |
| 25 | + return list; |
| 26 | + } |
| 27 | + |
| 28 | + private static final int numberOfPermutations(int N) { |
| 29 | + // factorial |
| 30 | + int result = N; |
| 31 | + while (N > 1) |
| 32 | + result *= --N; |
| 33 | + return result; |
| 34 | + } |
| 35 | + |
| 36 | + private static final int permutations(String[] list, int index, char[] prefix, char[] remaining, int prefixLength, int remainingLength) { |
| 37 | + final int N = remainingLength-prefixLength; |
| 38 | + if (N == 0) { |
| 39 | + list[index]=new String(prefix); |
| 40 | + index++; |
| 41 | + } else { |
| 42 | + for (int i=0; i<N; i++) { |
| 43 | + final char[] prefChars = new char[prefixLength+1]; |
| 44 | + System.arraycopy(prefix, 0, prefChars, 0, prefixLength); |
| 45 | + System.arraycopy(remaining, i, prefChars, prefixLength, 1); |
| 46 | + |
| 47 | + final char[] restChars = new char[N-1]; |
| 48 | + System.arraycopy(remaining, 0, restChars, 0, i); |
| 49 | + System.arraycopy(remaining, i+1, restChars, i, N-(i+1)); |
| 50 | + |
| 51 | + index = permutations(list, index, prefChars, restChars, remainingLength-(N-1), remainingLength); |
| 52 | + } |
| 53 | + } |
| 54 | + return index; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Permutations of numbers in an array using recursion |
| 59 | + * <br> |
| 60 | + * int numbers[] = {7,5,3}; |
| 61 | + * LinkedList<LinkedList<Integer>> result = getAllPermutations(numbers); |
| 62 | + */ |
| 63 | + public static final LinkedList<LinkedList<Integer>> getAllPermutations(final int[] numbers){ |
| 64 | + final LinkedList<LinkedList<Integer>> result = new LinkedList<LinkedList<Integer>>(); |
| 65 | + return getAllPermutations(numbers, result); |
| 66 | + } |
| 67 | + |
| 68 | + private static final LinkedList<LinkedList<Integer>> getAllPermutations(final int[] numbers, LinkedList<LinkedList<Integer>> result){ |
| 69 | + //numbers given in an array are also a permutation |
| 70 | + LinkedList<Integer> firstPermutation = new LinkedList<Integer>(); |
| 71 | + for(int el : numbers){ |
| 72 | + firstPermutation.add(new Integer(el)); |
| 73 | + } |
| 74 | + result.add(firstPermutation); |
| 75 | + //let's permute all elements in array starting from index 0 |
| 76 | + return permute(numbers, 0, result); |
| 77 | + } |
| 78 | + |
| 79 | + private static final LinkedList<LinkedList<Integer>> permute(final int[] numbers, int currentElementIndex, LinkedList<LinkedList<Integer>> result){ |
| 80 | + if(currentElementIndex == numbers.length - 1) |
| 81 | + return result; |
| 82 | + |
| 83 | + for(int i = currentElementIndex; i < numbers.length; ++i){ |
| 84 | + //swapping two elements |
| 85 | + int temp = numbers[i]; |
| 86 | + numbers[i] = numbers[currentElementIndex]; |
| 87 | + numbers[currentElementIndex] = temp; |
| 88 | + |
| 89 | + permute(numbers, currentElementIndex + 1,result); |
| 90 | + |
| 91 | + //all next permutation found |
| 92 | + if(i != currentElementIndex){ |
| 93 | + LinkedList<Integer> nextPermutation = new LinkedList<Integer>(); |
| 94 | + for(int j = 0; j < numbers.length; j++) |
| 95 | + nextPermutation.add(new Integer(numbers[j])); |
| 96 | + result.add(nextPermutation); |
| 97 | + } |
| 98 | + |
| 99 | + //swapping back two elements |
| 100 | + temp = numbers[i]; |
| 101 | + numbers[i] = numbers[currentElementIndex]; |
| 102 | + numbers[currentElementIndex] = temp; |
| 103 | + } |
| 104 | + return result; |
| 105 | + } |
| 106 | +} |
0 commit comments