public class PermutationGenerator
extends java.lang.Object
Using this one can start with a fixed array or List and modify it in place, generating all permutations of the elements. Note if the two elements to be interchanged are the same, nextIndex() can just be called again.
This class has static methods giving both an Iterator and an (in place) ArrayIncrementor.
This uses the Johnson-Trotter algorithm: start with the indentity permutation and with the direction of each integer left. If there is a mobile integer k, find the largest one, swap it with the neighbor it points to, and reverse the arrows of all integers larger than k. Mobile means pointing to a smaller integer.
Constructor and Description |
---|
PermutationGenerator(int n) |
Modifier and Type | Method and Description |
---|---|
static ArrayIncrementor |
arrayIncrementor(int[] arr)
This increments arr, applying the next transposition that results
in a different array.
|
static java.util.Iterator |
iterator(int n)
This iterator iterates all permutations on the set 0, ..., n-1.
|
static ArrayIncrementor |
listIncrementor(java.util.List lst)
This increments arr, applying the next transposition that results
in a different array.
|
static void |
main(java.lang.String[] args) |
int |
nextIndex() |
void |
reset() |
public void reset()
public int nextIndex()
public static java.util.Iterator iterator(int n)
public static ArrayIncrementor arrayIncrementor(int[] arr)
public static ArrayIncrementor listIncrementor(java.util.List lst)
public static void main(java.lang.String[] args)
Copyright 2003 Ralph Freese. All Rights Reserved.