Preface
Heap's algorithm generates all possible permutations of N objects. It was first proposed by B. R. Heap in 1963.[1] The algorithm minimizes movement: it generates each permutation from the previous one by interchanging a single pair of elements; the other N−2 elements are not disturbed. In a 1977 review of permutation-generating algorithms, Robert Sedgewick concluded that it was at that time the most effective algorithm for generating permutations by computer.
Details of the algorithm
Suppose we have a permutation containing N different elements. Heap found a systematic method for choosing at each step a pair of elements to switch, in order to produce every possible permutation of these elements exactly once. Let us describe Heap's method in a recursive way. First we set a counter i to 0. Now we perform the following steps repeatedly until i is equal to N. We use the algorithm to generate the (N − 1)! permutations of the first N − 1 elements, adjoining the last element to each of these. This generates all of the permutations that end with the last element. Then if N is odd, we switch the first element and the last one, while if N is even we can switch the i th element and the last one (there is no difference between N even and odd in the first iteration). We add one to the counter i and repeat. In each iteration, the algorithm will produce all of the permutations that end with the element that has just been moved to the "last" position. The following pseudocode outputs all permutations of a data array of length N.
- Recursive Pseudo Code
- procedure generate(n : integer, A : array of any):
- if n = 1 then
- output(A)
- else
- i := 0
- while True do
- generate(n - 1, A)
- if i = (n - 1) then
- break
- end if
- if n is even then
- swap(A[i], A[n-1])
- else
- swap(A[0], A[n-1])
- end if
- i := i + 1
- end while
- end if
- procedure generate(n : integer, A : array of any):
- c : array of int
- for i := 0; i < n; i += 1 do
- c[i] := 0
- end for
- output(A)
- i := 1
- while i < n do
- if c[i] < i then
- if i is even then
- swap(A[0], A[i])
- else
- swap(A[c[i]], A[i])
- end if
- output(A)
- c[i] += 1
- i := 1
- else
- c[i] := 0
- i += 1
- end if
- end while
- package medium;
- import java.util.ArrayList;
- import java.util.List;
- public class permutations {
- /**
- * Given a collection of distinct numbers, return all possible permutations.
- * For example,
- * [1,2,3] have the following permutations:
- *
- * [[1,2,3],
- * [1,3,2],
- * [2,1,3],
- * [2,3,1],
- * [3,1,2],
- * [3,2,1]]
- *
- * @see
- * https://en.wikipedia.org/wiki/Heap%27s_algorithm
- *
- * @param nums
- * Numbers with distinct values to do permutation
- *
- * @return
- */
- public static List
- > permute(
- List
- > permList = new ArrayList
- >();
- generate(nums.length, nums, permList);
- return permList;
- }
- public static void generate(int n, int[] nums, List
- > permList)
- {
- if(n==1)
- {
- List
list = new ArrayList (); - for(int v:nums) list.add(v);
- permList.add(list);
- }
- else
- {
- int i = 0;
- while(true)
- {
- generate(n-1, nums, permList);
- if(i==(n-1)) break;
- if(n%2==0) // even
- {
- swap(nums, i, n-1);
- }
- else
- {
- swap(nums, 0, n-1);
- }
- i++;
- }
- }
- }
- public static void swap(int[] nums, int i, int j)
- {
- int t = nums[i]; nums[i]=nums[j]; nums[j]=t;
- }
- }
Supplement
* leetcode - Permutation
沒有留言:
張貼留言