Heap's algorithmHeap'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.[2] The sequence of permutations of n objects generated by Heap's algorithm is the beginning of the sequence of permutations of n+1 objects. So there is one infinite sequence of permutations generated by Heap's algorithm (sequence A280318 in the OEIS). Details of the algorithmFor a collection 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. Described recursively as a decrease and conquer method, Heap's algorithm operates at each step on the initial elements of the collection. Initially and thereafter . Each step generates the permutations that end with the same final elements. It does this by calling itself once with the element unaltered and then times with the () element exchanged for each of the initial elements. The recursive calls modify the initial elements and a rule is needed at each iteration to select which will be exchanged with the last. Heap's method says that this choice can be made by the parity of the number of elements operated on at this step. If is even, then the final element is iteratively exchanged with each element index. If is odd, the final element is always exchanged with the first. // Output the k! permutations of A in which the first k elements are permuted in all ways.
// To get all permutations of A, use k := length of A.
//
// If, k > length of A, will try to access A out of bounds.
// If k <= 0 there will be no output (empty array has no permutations)
procedure permutations(k : integer, A : array of any):
if k = 1 then
output(A)
else
// permutations with last element fixed
permutations(k - 1, A)
// permutations with last element swapped out
for i := 0; i < k-1; i += 1 do
if k is even then
swap(A[i], A[k-1])
else
swap(A[0], A[k-1])
end if
permutations(k - 1, A)
end for
end if
One can also write the algorithm in a non-recursive format.[3] procedure permutations(n : integer, A : array of any):
// c is an encoding of the stack state.
// c[k] encodes the for-loop counter for when permutations(k - 1, A) is called
c : array of int
for i := 0; i < n; i += 1 do
c[i] := 0
end for
output(A)
// i acts similarly to a stack pointer
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)
// Swap has occurred ending the while-loop. Simulate the increment of the while-loop counter
c[i] += 1
// Simulate recursive call reaching the base case by bringing the pointer to the base case analog in the array
i := 1
else
// Calling permutations(i+1, A) has ended as the while-loop terminated. Reset the state and simulate popping the stack by incrementing the pointer.
c[i] := 0
i += 1
end if
end while
ProofIn this proof, we'll use the below implementation as Heap's algorithm as it makes the analysis easier, and certain patterns can be easily illustrated. While it is not optimal (it does not minimize moves, which is described in the section below), the implementation is correct and will produce all permutations. // Output the k! permutations of A in which the first k elements are permuted in all ways.
// To get all permutations of A, use k := length of A.
//
// If, k > length of A, will try to access A out of bounds.
// If k <= 0 there will be no output (empty array has no permutations)
procedure permutations(k : integer, A : array of any):
if k = 1 then
output(A)
else
for i := 0; i < k; i += 1 do
permutations(k - 1, A)
if k is even then
swap(A[i], A[k-1])
else
swap(A[0], A[k-1])
end if
end for
end if
Claim: If array A has length n, then Base: If array A has length 1, then Induction: If the claim is true for arrays of length l ≥ 1, then we show that the claim is true for arrays of length l+1 (together with the base case this proves that the claim is true for arrays of all lengths). Since the claim depends on whether l is odd or even, we prove each case separately. If l is odd, then, by the induction hypothesis, for an array A of length l, 1,2,3,4 ... original array 1,2,3,4 ... 1st iteration (permute subarray) 4,2,3,1 ... 1st iteration (swap 1st element into last position) 4,2,3,1 ... 2nd iteration (permute subarray) 4,1,3,2 ... 2nd iteration (swap 2nd element into last position) 4,1,3,2 ... 3rd iteration (permute subarray) 4,1,2,3 ... 3rd iteration (swap 3rd element into last position) 4,1,2,3 ... 4th iteration (permute subarray) 4,1,2,3 ... 4th iteration (swap 4th element into last position) The altered array is a rotated version of the original If l is even, then, by the induction hypothesis, for an array A of length l, 1,2,3,4,5 ... original array 4,1,2,3,5 ... 1st iteration (permute subarray, which rotates it) 5,1,2,3,4 ... 1st iteration (swap) 3,5,1,2,4 ... 2nd iteration (permute subarray, which rotates it) 4,5,1,2,3 ... 2nd iteration (swap) 2,4,5,1,3 ... 3rd iteration (permute subarray, which rotates it) 3,4,5,1,2 ... 3rd iteration (swap) 1,3,4,5,2 ... 4th iteration (permute subarray, which rotates it) 2,3,4,5,1 ... 4th iteration (swap) 5,2,3,4,1 ... 5th iteration (permute subarray, which rotates it) 1,2,3,4,5 ... 5th iteration (swap) The final state of the array is in the same order as the original The induction proof for the claim is now complete, which will now lead to why Heap's Algorithm creates all permutations of array A. Once again we will prove by induction the correctness of Heap's Algorithm. Basis: Heap's Algorithm trivially permutes an array A of size 1 as outputting A is the one and only permutation of A. Induction: Assume Heap's Algorithm permutes an array of size i. Using the results from the previous proof, every element of A will be in the "buffer" once when the first i elements are permuted. Because permutations of an array can be made by altering some array A through the removal of an element x from A then tacking on x to each permutation of the altered array, it follows that Heap's Algorithm permutes an array of size , for the "buffer" in essence holds the removed element, being tacked onto the permutations of the subarray of size i. Because each iteration of Heap's Algorithm has a different element of A occupying the buffer when the subarray is permuted, every permutation is generated as each element of A has a chance to be tacked onto the permutations of the array A without the buffer element. Frequent mis-implementationsIt is tempting to simplify the recursive version given above by reducing the instances of recursive calls. For example, as: procedure permutations(k : integer, A : array of any):
if k = 1 then
output(A)
else
// Recursively call once for each k
for i := 0; i < k; i += 1 do
permutations(k - 1, A)
// swap choice dependent on parity of k (even or odd)
if k is even then
// no-op when i == k-1
swap(A[i], A[k-1])
else
// XXX incorrect additional swap when i==k-1
swap(A[0], A[k-1])
end if
end for
end if
This implementation will succeed in producing all permutations but does not minimize movement. As the recursive call-stacks unwind, it results in additional swaps at each level. Half of these will be no-ops of and where but when is odd, it results in additional swaps of the with the element.
These additional swaps significantly alter the order of the prefix elements. The additional swaps can be avoided by either adding an additional recursive call before the loop and looping times (as above) or looping times and checking that is less than as in: procedure permutations(k : integer, A : array of any):
if k = 1 then
output(A)
else
// Recursively call once for each k
for i := 0; i < k; i += 1 do
permutations(k - 1, A)
// avoid swap when i==k-1
if (i < k - 1)
// swap choice dependent on parity of k
if k is even then
swap(A[i], A[k-1])
else
swap(A[0], A[k-1])
end if
end if
end for
end if
The choice is primarily aesthetic but the latter results in checking the value of twice as often. See alsoReferences
|
Portal di Ensiklopedia Dunia