Algorithm: Multiply permutations in cycle form

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值