edu.cs.ai.kreator.misc.util
Class Permutation

java.lang.Object
  extended by edu.cs.ai.kreator.misc.util.Permutation
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<Permutation>, java.util.Comparator<Permutation>, java.util.Iterator<Permutation>

public class Permutation
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable, java.util.Iterator<Permutation>, java.util.Comparator<Permutation>, java.lang.Comparable<Permutation>

This Class is designed to work with huge permutations. The webpages http://www.merriampark.com/perm.htm and http://www-sfb288.math.tu-berlin.de/~jtem/ did inspire me to some changes and improvements. You may download the SourceCode here: http://www.uwe-alex.de/Permutation/Permutation.java

The Class Permutation behave some like a not resizable ArrayList of int's. The size and get operations run in constant time, indexOf and toArray<\tt> run in linear time. The results are the integers of the current permutation not other permutations.

The class Permutation behave some like a ListTterator of a "List" of permutations. The methods next(O(2)),hasNext(O(N)),previous(O(2)), hasPrevious(O(N)) allows the programmer to traverse the (not realy existing) "list" of permutations in either direction, and obtain the permutation's current position in that "list". Other than a ListIterator this class does have a current element, its cursor position always lies at the element between the element that would be returned by a call to previous() and the element that would be returned by a call to next(). Special are the methods next(i)( O(N*(lg(N) ) and setPermutationNumber() and getPermutationNumber witch let you work with permutations like an ArrayList. The "list" of permutations with N elements does have 1*2* .. *N elements. This is usally more than any computers memory capacity. So these permutations are calculateted from the present permutation. The methods next and previous are running in constant average time.

This Class implements the Intefaces Comparator and Comparable (see the Java Collections Framework). The Permutations are compared like "Strings". A smaller Permutation does have a lower PermutationNumber.


toString  PermNum get(0) get(1) get(2) indexOf(1) hasNext
[0,1,2]   0       0      1      2      1          true
[0,2,1]   1       0      2      1      2          true
[1,0,2]   2       1      0      2      0          true
[1,2,0]   3       1      2      0      0          true
[2,0,1]   4       2      0      1      2          true
[2,1,0]   5       2      1      0      1          false


Example:

    public static void main(String[] args)
    {
      System.out.println("The Permutations of  Alex,Otto,Uwe :");
      System.out.println();
      String[] s={"Alex","Otto","Uwe"};
      Permutation p=new Permutation(s.length);
      for (int i=0;i<=6;i++)
      {
         for(int j=0;j<3;j++)
             System.out.print(s[p.get(j)]+",");
         System.out.println(" PermutationNumber: "+p.getPermutationNumber());
         p.next();
      }

      System.out.println();
      System.out.println("Select PermutationNumber 4");
      p.setPermutationNumber(4);

      for(int j=0;j<3;j++)
             System.out.print(s[p.get(j)]+",");
      System.out.println(" Number: "+p.getPermutationNumber());

      System.out.println();
      System.out.println("Select PermutationNumber 2");
      p.setPermutationNumber(2);

      for(int j=0;j<3;j++)
             System.out.print(s[p.get(j)]+",");
      System.out.println(" Number: "+p.getPermutationNumber());

      System.out.println();
      System.out.println("next()");
      p.next();

      for(int j=0;j<3;j++)
             System.out.print(s[p.get(j)]+",");
      System.out.println(" Number: "+p.getPermutationNumber());

    }

Output:

The Permutations of  Alex,Otto,Uwe :

Alex,Otto,Uwe, PermutationNumber: 0
Alex,Uwe,Otto, PermutationNumber: 1
Otto,Alex,Uwe, PermutationNumber: 2
Otto,Uwe,Alex, PermutationNumber: 3
Uwe,Alex,Otto, PermutationNumber: 4
Uwe,Otto,Alex, PermutationNumber: 5
Alex,Otto,Uwe, PermutationNumber: 0

Select PermutationNumber 4
Uwe,Alex,Otto, Number: 4

Select PermutationNumber 2
Otto,Alex,Uwe, Number: 2

next()
Otto,Uwe,Alex, Number: 3


Example: Magic Square 4 x 4


  public static void main(String[] args)
  {
      // Demonstrates the use of the Permutation Class!

      Permutation p=new Permutation(16);
      Permutation q;
      do
      {
              q=(Permutation) p.clone();
              // Check the Lines
              while (p.get(0)+p.get(1)+p.get(2)+p.get(3)!=30) p.next(3);
              while (p.get(4)+p.get(5)+p.get(6)+p.get(7)!=30) p.next(7);
              while (p.get(8)+p.get(9)+p.get(10)+p.get(11)!=30) p.next(11);
              //if  (p.get(12)+p.get(13)+p.get(14)+p.get(15)!=30) p.next(15);

              // Check the Colums
              if   (p.get(0)+p.get(4)+p.get(8)+p.get(12)!=30) p.next(12);
              if   (p.get(1)+p.get(5)+p.get(9)+p.get(13)!=30) p.next(13);
              if   (p.get(2)+p.get(6)+p.get(10)+p.get(14)!=30) p.next(14);
              //if (p.get(3)+p.get(7)+p.get(11)+p.get(15)!=30) p.next(15);

              // Check the Diagonals
              if (p.get(3)+p.get(6)+p.get(9)+p.get(12)!=30) p.next(12);
              if (p.get(0)+p.get(5)+p.get(10)+p.get(15)!=30) p.next(15);

      }while(!p.equals(q)); // No errors found by checking all

      // Print the Square

      for(int i=0;i<16;i++)
      {
              int w=p.get(i)+1;
              if (w<10) System.out.print(" ");
              System.out.print(" "+w);
              if ((i+1)%4==0) System.out.println();
      }
  }



Output:

  1  2 15 16
 12 14  3  5
 13  7 10  4
  8 11  6  9

Example Magic Square 5 x 5


  public static void main(String[] args)
  {

      int N=25;
      Permutation p=new Permutation(N);
      Permutation q;
          for (int sn=1;sn<4;sn++)
          {
              do
          {
                q=(Permutation) p.clone();
                // Check the lines
                for(int i=20;i>=0;i-=5)
                {
                   if (p.nextGE(i+2,13-p.get(i)-p.get(i+1))<0)p.next(i+1);
                   if (p.nextGE(i+3,36-p.get(i)-p.get(i+1)-p.get(i+2))<0)p.next(i+2);
                   if (!p.nextEQ(i+4,60-p.get(i)-p.get(i+1)-p.get(i+2)-p.get(i+3),i+3))p.next(i+3);
                }
               // Check the Colums
                for(int i=4;i>=0;i--)
                {
                    if (p.nextGE(i+10,13-p.get(i)-p.get(i+5))<0)p.next(i+9);
                    if (p.nextGE(i+15,36-p.get(i)-p.get(i+5)-p.get(i+10))<0)p.next(i+14);
                    if (!p.nextEQ(i+20,60-p.get(i)-p.get(i+5)-p.get(i+10)-p.get(i+15),i+15))p.next(i+15);
                }
                // Check the Diagonals
                if (p.nextGE(12,13-p.get(4)-p.get(8))<0)p.next(11);
                if (p.nextGE(16,36-p.get(4)-p.get(8)-p.get(12))<0)p.next(15);
                if (!p.nextEQ(20,60-p.get(4)-p.get(8)-p.get(12)-p.get(16),16))p.next(16);

                if (p.nextGE(12,13-p.get(0)-p.get(6))<0)p.next(11);
                if (p.nextGE(18,36-p.get(0)-p.get(6)-p.get(12))<0)p.next(17);
                if (!p.nextEQ(24,60-p.get(0)-p.get(6)-p.get(12)-p.get(18),18))p.next(18);
          }while(!p.equals(q)); // No errors found by checking all
       // Print the Square


          System.out.println();System.out.println("Square No:"+sn);
          System.out.println("Pemutation Number: "+p.getPermutationNumber());
          System.out.println();

          for(int i=0;i<25;i++)
          {
                  int w=p.get(i)+1;
                  if (w<10) System.out.print(" ");
                  System.out.print(" "+w);
                  if ((i+1)%5==0) System.out.println();
          }
       }
  }



Output:


Square No:1
Pemutation Number: 12310598036244579013654

  1  2 13 24 25
  3 22 19  6 15
 23 16 10 11  5
 21  7  9 20  8
 17 18 14  4 12

Square No:2
Pemutation Number: 12310603413326743456918

  1  2 13 24 25
  3 23 16  8 15
 21 19 10  6  9
 22  4 14 20  5
 18 17 12  7 11

Square No:3
Pemutation Number: 12310604397993244628398

  1  2 13 24 25
  3 23 19  4 16
 21 15 10 12  7
 22  8  9 20  6
 18 17 14  5 11





Author:
Uwe Alex, Ulmenweg 22, 68167 Mannheim, Germany http://www.uwe-alex.de
See Also:
Serialized Form

Field Summary
static long serialVersionUID
           
 
Constructor Summary
Permutation(int n)
          Constructs a Permutation with a size n.
Permutation(int[] A)
          Creates a Permutation with a copy of A as Permutation.
Permutation(Permutation p)
          Creates a clone of p
 
Method Summary
 java.lang.Object clone()
          Returns a copy of this Permutation instance.
 int compare(Permutation p1, Permutation p2)
          Compares its two arguments for order.
 int compareTo(Permutation p)
          Compares this object with the specified object for order.
 boolean equals(java.lang.Object o)
          Compares the specified object with this permutation for equality.
 void first()
          Change this to the first (lowest) possible permutation .
 int get(int index)
          Returns the int at the specified position in this permutation.
 java.math.BigInteger getPermutationNumber()
          Returns the number of the permutation.
 boolean hasNext()
          Returns true if this permutation is not the last traversing the "list" of permutations in the forward direction.
 boolean hasPrevious()
          Returns true if this permutation is not the first traversing the "list" of permutations in the backward direction.
 int indexOf(int elem)
          Searches for the first occurence of the given argument.
 Permutation inverse()
          Returns the inverse permutation of this.
 boolean isDerangement()
          Returns whether this is a valid derangement (permutation without fixed point) of the indices 0..N-1
 boolean isEven()
          Returns true if this is a even permutation.
 boolean isFirst()
          Returns true if this equals 0,1,2,...N-2,N-1, false if not
 boolean isLast()
          Returns true if this permutation is the last traversing the "list" of permutations in the forward direction.
 boolean isOdd()
          Returns true if this is a odd permutation.
 void last()
          Change this to the last (highest) possible permutation .
 Permutation next()
          Change the the permutation to the next permutation in sorting order.
 java.lang.Object next(int index)
          Change to the smallest permutation bigger than this with a change at position index.
 boolean nextEQ(int index, int value, int save)
           
 int nextGE(int index, int value)
          Change the value in index to the value or higher without changes at positions lower than index.
 int numTranspos()
          Each permutation can be written as a product of transpositions.
 java.lang.Object previous()
          Change the the permutation to the previous permutation in sorting order.
 void remove()
          Not supported.
 void setPermutationNumber(java.math.BigInteger Number)
          Creates the permutation with PermutationNumber Number.
 void setPermutationNumber(long l)
          Creates the permutation with number BigInteger(l.toString()).
 void setPermutationNumber(java.lang.String s)
          Creates the permutation with number BigInteger(s).
 void shuffle()
          Randomly permutes the array of this permutation using a default source of randomness.
 int size()
          Returns the number of integers in this permutation.
 void swap(int i, int j)
          Swaps the elements at the specified positions in this permutation.
 int[] toArray()
           
 java.lang.String toString()
          Returns a string representation of this permutation.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

public static final long serialVersionUID
See Also:
Constant Field Values
Constructor Detail

Permutation

public Permutation(int n)
Constructs a Permutation with a size n. Initialisize Permutation with the Numbers: 0,1,2, ... , N-2, N-1
Example:

       Permutation p=new Permutation(4); // 0,1,2,3

Parameters:
n - the length of the array.
Throws:
java.lang.IllegalArgumentException - if n is lower then 1.

Permutation

public Permutation(Permutation p)
Creates a clone of p


Permutation

public Permutation(int[] A)
Creates a Permutation with a copy of A as Permutation. The Values of A have to contain all values from 0 to A.length-1

Method Detail

clone

public java.lang.Object clone()
Returns a copy of this Permutation instance. (The elements themselves are copied to.)

Overrides:
clone in class java.lang.Object
Returns:
a clone of this Permutation instance.

indexOf

public int indexOf(int elem)
Searches for the first occurence of the given argument.

Parameters:
elem - an int.
Returns:
the index of the first occurrence of the argument in this permutation; returns -1 if the object is not found.
See Also:
Object.equals(Object)

get

public int get(int index)
Returns the int at the specified position in this permutation.

Parameters:
index - index of int to return.
Returns:
the position of the specified value in this permutation or -1 if not found.
Throws:
java.lang.IndexOutOfBoundsException - if index is out of range (index < 0 || index >= size()).

swap

public void swap(int i,
                 int j)
Swaps the elements at the specified positions in this permutation. (If the specified positions are equal, invoking this method leaves the permutation unchanged.)

Parameters:
i - the index of one element to be swapped.
j - the index of the other element to be swapped.
Throws:
java.lang.IndexOutOfBoundsException - if either i or j is out of range (i < 0 || i >= permutation.size() || j < 0 || j >= permutation.size()).

shuffle

public void shuffle()
Randomly permutes the array of this permutation using a default source of randomness. All permutations occur with approximately equal likelihood.

The hedge "approximately" is used in the foregoing description because default source of randomenss is only approximately an unbiased source of independently chosen bits. If it were a perfect source of randomly chosen bits, then the algorithm would choose permutations with perfect uniformity.

This implementation traverses the permutation array forward, from the first element up to the second last, repeatedly swapping a randomly selected element into the "current position". Elements are randomly selected from the portion of the permutation array that runs from the current position to the last element, inclusive.

This method runs in linear time.


size

public int size()
Returns the number of integers in this permutation.
            Example:

                Permutation p=new Permutation(4);
                int a=p.size();  //a=4
    

Returns:
the number of elements in this permutation.

hasNext

public boolean hasNext()
Returns true if this permutation is not the last traversing the "list" of permutations in the forward direction. (In other words, returns true>/tt> if this permutation is not [N-1,N-2,...,1,0]

Specified by:
hasNext in interface java.util.Iterator<Permutation>
Returns:
true if next has more elements when traversing the "list" of permutations in the forward direction.

isLast

public boolean isLast()
Returns true if this permutation is the last traversing the "list" of permutations in the forward direction. (In other words, returns true>/tt> if this permutation is [N-1,N-2,...,1,0]

Returns:
true if next has no more elements when traversing the "list" of permutations in the forward direction.

hasPrevious

public boolean hasPrevious()
Returns true if this permutation is not the first traversing the "list" of permutations in the backward direction. (In other words, returns true>/tt> if this permutation is not [0,1,2,...,N-2,N,1]

Returns:
true if next has more elements when traversing the "list" of permutations in the backward direction.

isFirst

public boolean isFirst()
Returns true if this equals 0,1,2,...N-2,N-1, false if not

Returns:
true if this equals 0,1,2,...N-2,N-1, false if not

isEven

public boolean isEven()
Returns true if this is a even permutation. A even permutation can be build in a even number of transpositions starting with the identity.

Returns:
true if this is a even permutation, false if not.

isOdd

public boolean isOdd()
Returns true if this is a odd permutation. A even permutation can be build in a odd number of transpositions starting with the identity. The result is calculated by isOdd is !isEven.

Returns:
true if this is a odd permutation, false if not.

numTranspos

public int numTranspos()
Each permutation can be written as a product of transpositions. Each cycle in the decomposition gives rise to an independant product of transposition. The oermutation [2,3,...,n,1} can be written as the composition of n-1 transpositions: (1,n)(1,n-1)...(1,2). This method returns the minimal total number of required transpositions.

Returns:
an int value

isDerangement

public boolean isDerangement()
Returns whether this is a valid derangement (permutation without fixed point) of the indices 0..N-1. *

Returns:
true if this is a valid derangement, false if not.

toArray

public int[] toArray()

toString

public java.lang.String toString()
Returns a string representation of this permutation. The string representation consists of a list of the permutations elements enclosed in square brackets ("[]").

This implementation creates an empty string buffer, appends a left square bracket, and iterates over the permutation appending the string representation of each element in turn. After appending each element , the string ", " is appended. Finally the last element and a right bracket is appended. A string is obtained from the string buffer, and returned.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of this collection.

compare

public int compare(Permutation p1,
                   Permutation p2)
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor does ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor does ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementer does ensure that compare(x, y)==0 implies that sgn(compare(x, z))==sgn(compare(y, z)) for all z.

(compare(x, y)==0) == (x.equals(y)). "Note: this comparator is consistent with equals."

Specified by:
compare in interface java.util.Comparator<Permutation>
Parameters:
o1 - the first object to be compared.
o2 - the second object to be compared.
Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
Throws:
java.lang.ClassCastException - if the arguments' types prevent them from being compared by this Comparator.

compareTo

public int compareTo(Permutation p)
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

The implementor does ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception iff y.compareTo(x) throws an exception.)

The implementor does also ensure that the relation is transitive: (x.compareTo(y)>0 && y.compareTo(z)>0) implies x.compareTo(z)>0.

Finally, the implementer does ensure that x.compareTo(y)==0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.

(x.compareTo(y)==0) == (x.equals(y)). "Note: this class has a natural ordering that is consistent with equals."

Specified by:
compareTo in interface java.lang.Comparable<Permutation>
Parameters:
o - the Object to be compared.
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
Throws:
java.lang.ClassCastException - if the specified object's type prevents it from being compared to this Object.

equals

public boolean equals(java.lang.Object o)
Compares the specified object with this permutation for equality. Returns true if and only if the specified object is also a permutation, both permutations have the same size, and all corresponding pairs of elements in the two permutations are equal. In other words, two permutations are defined to be equal if they contain the same elements in the same order.

This implementation first checks if the specified object is this list. If so, it returns true; if not, it checks if the specified object is a permutation. If not, it returns false; if so, the integer arrays are compared

Specified by:
equals in interface java.util.Comparator<Permutation>
Overrides:
equals in class java.lang.Object
Parameters:
o - the object to be compared for equality with this permutation.
Returns:
true if the specified object is equal to this permutation.

inverse

public Permutation inverse()
Returns the inverse permutation of this.

Returns:
the inverse permutation of this.

first

public void first()
Change this to the first (lowest) possible permutation .
             Examples:

             before            after first()
             [0,3,1,2]         [0,1,2,3]
             [0,1]             [0,1]
             [2,1,0]           [0,1,2]

     
The numbers from 0 to N-1 are storted in ascending numerical order.


last

public void last()
Change this to the last (highest) possible permutation .
             Examples:

             before            after
             [0,3,1,2]         [3,2,1,0]
             [0,1]             [1,0]
             [2,1,0]           [2,1,0]

     
The numbers from 0 to N-1 are stored in descending numerical order in the permutation array.


next

public Permutation next()
Change the the permutation to the next permutation in sorting order. This method may be called repeatedly to iterate through the permutatons, or intermixed with calls to previous to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.) if isLast()==false then next will increment the PermutationNumber; if isLast()==true then next() will perform like first().

Example:

     public static void main(String[] args)
     {
         Permutation p=new Permutation(7);
         p.shuffle();
         for (int i=0;i<12;i++)
         {
             System.out.println(p+" = PermutationNumber : "+p.getPermutationNumber());
             p.next();
         }
             System.out.println(p+" = PermutationNumber : "+p.getPermutationNumber());
     }

Output:

[5,1,2,6,0,4,3] = PermutationNumber : 3763
[5,1,2,6,3,0,4] = PermutationNumber : 3764
[5,1,2,6,3,4,0] = PermutationNumber : 3765
[5,1,2,6,4,0,3] = PermutationNumber : 3766
[5,1,2,6,4,3,0] = PermutationNumber : 3767
[5,1,3,0,2,4,6] = PermutationNumber : 3768
[5,1,3,0,2,6,4] = PermutationNumber : 3769
[5,1,3,0,4,2,6] = PermutationNumber : 3770
[5,1,3,0,4,6,2] = PermutationNumber : 3771
[5,1,3,0,6,2,4] = PermutationNumber : 3772
[5,1,3,0,6,4,2] = PermutationNumber : 3773
[5,1,3,2,0,4,6] = PermutationNumber : 3774
[5,1,3,2,0,6,4] = PermutationNumber : 3775

     

Specified by:
next in interface java.util.Iterator<Permutation>

previous

public java.lang.Object previous()
Change the the permutation to the previous permutation in sorting order. This method may be called repeatedly to iterate through the permutatons, or intermixed with calls to next to go forth and back. (Note that alternating calls to next and previous will return the same element repeatedly.) if isFirst()==false then previous will decrement the PermutationNumber; if IsFirst()==true then previous will perform like last().

Example:

     public static void main(String[] args)
     {
         Permutation p=new Permutation(5);
         p.setPermutationNumber(10);
         for (int i=10;i>-3;i--)
         {
             System.out.println(p+" = PermutationNumber : "+p.getPermutationNumber());
             p.previous();
         }
         System.out.println(p+" = PermutationNumber : "+p.getPermutationNumber());
     }

Output:

[0,2,4,1,3] = PermutationNumber : 10
[0,2,3,4,1] = PermutationNumber : 9
[0,2,3,1,4] = PermutationNumber : 8
[0,2,1,4,3] = PermutationNumber : 7
[0,2,1,3,4] = PermutationNumber : 6
[0,1,4,3,2] = PermutationNumber : 5
[0,1,4,2,3] = PermutationNumber : 4
[0,1,3,4,2] = PermutationNumber : 3
[0,1,3,2,4] = PermutationNumber : 2
[0,1,2,4,3] = PermutationNumber : 1
[0,1,2,3,4] = PermutationNumber : 0
[4,3,2,1,0] = PermutationNumber : 119
[4,3,2,0,1] = PermutationNumber : 118
[4,3,1,2,0] = PermutationNumber : 117

 


next

public java.lang.Object next(int index)
Change to the smallest permutation bigger than this with a change at position index.

Example:

     public static void main(String[] args)
     {
         Permutation p=new Permutation(9);
         p.shuffle();
         for (int i=0;i<12;i++)
         {
             System.out.println(p+" = PermutationNumber : "+p.getPermutationNumber());
             p.next(5);
         }
             System.out.println(p+" = PermutationNumber : "+p.getPermutationNumber());
     }

Output:

[8,0,3,4,5,6,7,2,1] = PermutationNumber : 324305
[8,0,3,4,5,7,1,2,6] = PermutationNumber : 324306
[8,0,3,4,6,1,2,5,7] = PermutationNumber : 324312
[8,0,3,4,6,2,1,5,7] = PermutationNumber : 324318
[8,0,3,4,6,5,1,2,7] = PermutationNumber : 324324
[8,0,3,4,6,7,1,2,5] = PermutationNumber : 324330
[8,0,3,4,7,1,2,5,6] = PermutationNumber : 324336
[8,0,3,4,7,2,1,5,6] = PermutationNumber : 324342
[8,0,3,4,7,5,1,2,6] = PermutationNumber : 324348
[8,0,3,4,7,6,1,2,5] = PermutationNumber : 324354
[8,0,3,5,1,2,4,6,7] = PermutationNumber : 324360
[8,0,3,5,1,4,2,6,7] = PermutationNumber : 324366
[8,0,3,5,1,6,2,4,7] = PermutationNumber : 324372

    


nextGE

public int nextGE(int index,
                  int value)
Change the value in index to the value or higher without changes at positions lower than index. Work as if as many next() are made as necessary to change the value at position index to value. If the value at position index is already greater-equal value nothing is changed.
Example:

   public static void main(String[] args)
   {
     // Demonstrates the use of the Permutation Class!
      Permutation p=new Permutation(10);
      for(int i=0;i<10;i++)
      {
              p.shuffle();
              System.out.println(p+" Number : "+p.getPermutationNumber());
              p.next(6);
              System.out.println(p+" Number : "+p.getPermutationNumber());
              System.out.println();
      }
  }

Output:

[4,5,9,8,7,2,0,1,3,6] Number : 1653048
[4,5,9,8,7,2,1,0,3,6] Number : 1653054

[5,8,6,9,3,7,0,4,2,1] Number : 2126621
[5,8,6,9,3,7,1,0,2,4] Number : 2126622

[9,7,0,5,2,4,8,3,1,6] Number : 3551228
[9,7,0,5,2,6,1,3,4,8] Number : 3551232

[7,9,5,0,4,8,3,2,1,6] Number : 2888390
[7,9,5,0,4,8,6,1,2,3] Number : 2888394

[6,7,1,8,2,4,3,5,9,0] Number : 2428017
[6,7,1,8,2,4,5,0,3,9] Number : 2428020

[5,3,7,9,0,2,1,4,8,6] Number : 1964905
[5,3,7,9,0,2,4,1,6,8] Number : 1964910

Returns:
1 if the value at position is now value, 0 if the value at position is now higher as value, -1 if the value at position is still lower as value

nextEQ

public boolean nextEQ(int index,
                      int value,
                      int save)

remove

public void remove()
Not supported.

Specified by:
remove in interface java.util.Iterator<Permutation>
Throws:
java.lang.UnsupportedOperationException

getPermutationNumber

public java.math.BigInteger getPermutationNumber()
Returns the number of the permutation. The first permutation [0,1,2,...N-2,N-1] has the number 0, the last permutation [N-1,N-2,...,0] has the number N!-1 .
        Example

            Permutation   PermutationNumber
              0,1,2          0
              0,2,1          1
              1,0,2          2
              ...            ...

              0,1,2,3        0
              0,1,3,2        1
              0,2,1,3        2
              0,2,3,1        3
              ...            ...

    

Returns:
the index of this in the "list" of permutations.

setPermutationNumber

public void setPermutationNumber(java.lang.String s)
Creates the permutation with number BigInteger(s). Realised as setPermutationNumber(new BigInteger(s))
Example:

   public static void main(String[] args)
   {
     // Demonstrates the use of the Permutation Class!
      Permutation p=new Permutation(20);
      p.setPermutationNumber("1234567890123456789");
      System.out.println(p+" Number : "+p.getPermutationNumber());
      p.previous();
      System.out.println(p+" Number : "+p.getPermutationNumber());
  }

Output:

[10,2,16,18,17,5,3,12,13,9,1,8,6,15,14,7,19,4,11,0] Number : 1234567890123456789
[10,2,16,18,17,5,3,12,13,9,1,8,6,15,14,7,19,4,0,11] Number : 1234567890123456788

       


setPermutationNumber

public void setPermutationNumber(long l)
Creates the permutation with number BigInteger(l.toString()). See getPermutationNumber. Realised as setPermutationNumber(new BigInteger(l.toString()))


setPermutationNumber

public void setPermutationNumber(java.math.BigInteger Number)
Creates the permutation with PermutationNumber Number. See getPermutationNumber.

              PermutationNumber   Permutation
              0                   0,1,2
              1                   0,2,1
              2                   1,0,2
              ...                 ...

              0                   0,1,2,3
              1                   0,1,3,2
              2                   0,2,1,3
              3                   0,2,3,1
              ...                 ...

        setPermutationNumber("0")                   is the same as
        setPermutationNumber(0)                     is the same as
        setPermutationNumber(BigInteger.ZERO)       is the same as
        setPermutationNumber(new BigInteger("0"))   is the same as
        first()