Algorithm A
Algorithm A (Multiply permutations in cycle form). This algorithm takes a
product of cycles, such as (6), and computes the resulting permutation in the
form of a product of disjoint cycles. For simplicity, the removal of singleton cycles
is not described here; that would be a fairly simple extension of the algorithm.
As this algorithm is performed, we successively “tag” the elements of the input
formula; that is, we mark somehow those symbols of the input formula that have
been processed.
A1. [First pass.] Tag all left parentheses, and replace each right parenthesis by
a tagged copy of the element that follows its matching left parenthesis. (See
the example in Table 1.)
A2. [Open.] Searching from left to right, find the first untagged element of the
input. (If all elements are tagged, the algorithm terminates.) Set START
equal to it; output a left parenthesis; output the element; and tag it.
A3. [See CURRENT.] Set CURRENT equal to the next element of the formula.
A4. [Scan formula.] Proceed to the right until either reaching the end of the
formula, or finding an element equal to CURRENT; in the latter case, tag it
and go back to step A3.
A5. [CURRENT = START?] If CURRENT != START, output CURRENT and go back to
step A4 starting again at the left of the formula (thereby continuing the
development of a cycle in the output).
A6. [Close.] (A complete cycle in the output has been found.) Output a right
parenthesis, and go back to step A2. |
Flow diagram
Data table
Java program
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created with IntelliJ IDEA.
* User: 1O1O
* Date: 12/18/13
* Time: 6:52 PM
* :)~
* Multiply permutations in cycle form:ALGORITHMS
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input;
String A1Input = "";
char START;
char CURRENT;
System.out.println("Please input the permutations:");
input = br.readLine();
String[] subInput = input.split("\\)");
int[] len = new int[subInput.length];
for(int i=0; i<subInput.length; i++){
len[i] = subInput[i].length()+1;
subInput[i]+=subInput[i].charAt(1);
A1Input+=subInput[i];
}
int[] tag = new int[A1Input.length()];
int index=0;
for(int k=0; k<len.length; k++){
index += len[k];
tag[index-1] = 1;
}
System.out.println();
System.out.println("After step A1, input permutations changed to:");
System.out.println(A1Input);
System.out.println();
System.out.println("The final result is:");
/*Kernel of the Algorithm*/
for(int i=0; i<A1Input.length(); i++){ /*A2*/
char character = A1Input.charAt(i);
if(character!='(' && tag[i]!=1){
START = character;
System.out.print('(');
System.out.print(character);
tag[i]=1;
int j = i;
do{
CURRENT = A1Input.charAt(++j); /*A3*/
do{
j++; /*A4*/
if(j == A1Input.length()){
break;
}
if(CURRENT == A1Input.charAt(j)){
tag[j] = 1;
break;
}
}while (true);
if(j == A1Input.length()){
break;
}
}while (true);
while (CURRENT != START){ /*A5*/
System.out.print(CURRENT);
j=0;
while (true){
do{
j++; /*A4*/
if(j == A1Input.length()){
break;
}
if(CURRENT == A1Input.charAt(j)){
tag[j] = 1;
break;
}
}while (true);
if(j == A1Input.length()){
break;
}
CURRENT = A1Input.charAt(++j); /*A3*/
}
}
System.out.print(')'); /*A6*/
}
}
}
}
OR
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created with IntelliJ IDEA.
* User: 1O1O
* Date: 12/18/13
* Time: 6:52 PM
* :)~
* Multiply permutations in cycle form:ALGORITHMS
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input;
String A1Input = "";
int[] tag = new int[27];
char START;
char CURRENT;
System.out.println("Please input the permutations:");
input = br.readLine();
String[] subInput = input.split("\\)");
for(int i=0; i<subInput.length; i++){
subInput[i]+=subInput[i].charAt(1);
A1Input+=subInput[i];
}
System.out.println();
System.out.println("After step A1, input permutations changed to:");
System.out.println(A1Input);
System.out.println();
System.out.println("The final result is:");
for(int i=0; i<A1Input.length(); i++){ /*A2*/
char character = A1Input.charAt(i);
if(character!='(' && tag[(int)character-96]!=1){
START = character;
System.out.print('(');
System.out.print(character);
tag[(int)character-96]=1;
int j = i;
do{
CURRENT = A1Input.charAt(++j); /*A3*/
do{
j++; /*A4*/
if(j == A1Input.length()){
break;
}
if(CURRENT == A1Input.charAt(j)){
break;
}
}while (true);
if(j == A1Input.length()){
break;
}
}while (true);
while (CURRENT != START){ /*A5*/
System.out.print(CURRENT);
tag[(int)CURRENT-96]=1;
j=0;
while (true){
do{
j++; /*A4*/
if(j == A1Input.length()){
break;
}
if(CURRENT == A1Input.charAt(j)){
break;
}
}while (true);
if(j == A1Input.length()){
break;
}
CURRENT = A1Input.charAt(++j); /*A3*/
}
}
System.out.print(')'); /*A6*/
}
}
}
}
Inputs & Outputs
Please input the permutations:
(acfg)(bcd)(aed)(fade)(bgfae)
After step A1, input permutations changed to:
(acfga(bcdb(aeda(fadef(bgfaeb
The final result is:
(adg)(ceb)(f)
Reference
<< The Art of Computer Programming: Fundamental Algorithms >> VOLUME 1, DONALD E. KNUTH