import java.util.*; import java.awt.*; import java.applet.*; import java.lang.Math.*; import Reversal; // Unsigned permutation only. Constructors get signed permutation and unsign it public class Permutation { private int[] m_original_permutation; private int[] m_permutation; private int[] m_reversed_permutation; Permutation(int[] permutation) throws Exception { m_original_permutation = permutation; expend(); } // Create a random permutation Permutation(int n) throws Exception { m_original_permutation = new int[n]; Random rnd = new Random(); for(int i=0 ;i 0) { // i=1(second), val=3 -- need 3,4 new_permutation[2*i + 1] = 2 * permutation[i] - 1; new_permutation[2*i + 2] = 2 * permutation[i]; } else { new_permutation[2*i + 1] = 2 * (-permutation[i]); new_permutation[2*i + 2] = 2 * (-permutation[i]) - 1; } } return new_permutation; } public int[] reverse_permutation(int[] permutation) throws Exception { int[] reversed = new int[permutation.length]; for(int i=0; i= permutation.length) ) return false; } for(int i=0; i start) || (start > end) || (end >= m_permutation.length) || ((start % 2) == 0) || ((end % 2) == 1)) // Invalid reversal throw new Exception("Permutation::reversal - Invalid reversal"); // Handle the original permutation int orig_start_index = start / 2; // 1->0,3->1 , ... int orig_end_index = (end-1) / 2; // 2->0,4->1 , ... for(int i=orig_start_index, j=orig_end_index; i<=j; i++, j--) { if(i == j) m_original_permutation[i] *= (-1); else { temp = m_original_permutation[i]; m_original_permutation[i] = (-1) * m_original_permutation[j]; m_original_permutation[j] = (-1) * temp; } } for(int i=start, j=end; i= m_permutation.length)) throw new Exception("Permutation::get - asked for invalid index"); return this.m_permutation[i]; } public int getSize() { return this.m_permutation.length; } int[] getPositions(int i, int j) throws Exception { if( (i >= m_permutation.length) || (j >= m_permutation.length)) throw new Exception("Permutation::getRange - invalid arguments, out of range"); int[] result = new int[2]; result[0] = getPosition(i); result[1] = getPosition(j); if(result[0] > result[1]) { int temp = result[0]; result[0] = result[1]; result[1] = temp; } return result; } int[] getIndexes(int pos1, int pos2) throws Exception { if( (pos1 >= m_permutation.length) || (pos2 >= m_permutation.length)) throw new Exception("Permutation::getRange - invalid arguments, out of range"); int[] result = new int[2]; result[0] = getIndex(pos1); result[1] = getIndex(pos2); if(result[0] > result[1]) { int temp = result[0]; result[0] = result[1]; result[1] = temp; } return result; } public int getPosition(int index) throws Exception { return m_reversed_permutation[index]; } public int getIndex(int position) throws Exception { return m_permutation[position]; } public int getBreakpointsNumber() { int result = 0; for(int i=0; i<=this.m_permutation.length-2; i++) if(java.lang.Math.abs(m_permutation[i] - m_permutation[i+1]) > 1) result++; return result; } public int getCyclesNumber() throws Exception { int result; int size = getSize(); boolean[] check_array = new boolean[size]; int checked = 0; int next_index = 0; int first_index = 0; boolean even = true; int cycle_size; for(result=0; checked return 2 // 4 --> return 5 else return((4*(index/2))+1-index); } public String origToString() { return makeString(m_original_permutation); } public String toString() { return makeString(m_permutation); } private String makeString(int[] permutation) { String result = new String(""); for(int i=0; i