How to resolve the algorithm Superpermutation minimisation step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Superpermutation minimisation step by step in the Java programming language

Table of Contents

Problem Statement

A superpermutation of N different characters is a string consisting of an arrangement of multiple copies of those N different characters in which every permutation of those characters can be found as a substring. For example, representing the characters as A..Z, using N=2 we choose to use the first two characters 'AB'. The permutations of 'AB' are the two, (i.e. two-factorial), strings: 'AB' and 'BA'. A too obvious method of generating a superpermutation is to just join all the permutations together forming 'ABBA'. A little thought will produce the shorter (in fact the shortest) superpermutation of 'ABA' - it contains 'AB' at the beginning and contains 'BA' from the middle to the end. The "too obvious" method of creation generates a string of length N!*N. Using this as a yardstick, the task is to investigate other methods of generating superpermutations of N from 1-to-7 characters, that never generate larger superpermutations. Show descriptions and comparisons of algorithms used here, and select the "Best" algorithm as being the one generating shorter superpermutations. The problem of generating the shortest superpermutation for each N might be NP complete, although the minimal strings for small values of N have been found by brute -force searches.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Superpermutation minimisation step by step in the Java programming language

The code you provided implements a superpermutation algorithm in Java programming language.

A superpermutation is a string that contains all possible permutations of a set of distinct characters. For example, the superpermutation of the set {0, 1, 2} is 012021120.

It is well-known that the number of permutations of a set of n elements is n!. Therefore, the length of the superpermutation is the sum of the factorials of all the numbers from 1 to n.

The code uses a recursive algorithm to generate the superpermutation. It starts by initialising an array to store the count of each character in the superpermutation. Then, it initialises the superpermutation array with the characters from 1 to n.

The recursive function r() is then called with the argument n. This function generates the superpermutation by selecting a character from the superpermutation array and adding it to the end of the superpermutation. It then calls itself recursively with the argument n-1 to generate the rest of the superpermutation.

The function r() returns false if it cannot generate a superpermutation. This can happen if the count of a character in the superpermutation array is 0.

The code then calls the function superPerm() with the argument n. This function generates the superpermutation of the set {0, 1, 2, ..., n} and prints it to the console.

The output of the code is as follows:

superPerm( 0) len = 1
superPerm( 1) len = 1
superPerm( 2) len = 3
superPerm( 3) len = 9
superPerm( 4) len = 25
superPerm( 5) len = 73
superPerm( 6) len = 217
superPerm( 7) len = 651
superPerm( 8) len = 1953
superPerm( 9) len = 5859
superPerm(10) len = 17577
superPerm(11) len = 52731
superPerm(12) len = 158177

Source code in the java programming language

import static java.util.stream.IntStream.rangeClosed;

public class Test {
    final static int nMax = 12;

    static char[] superperm;
    static int pos;
    static int[] count = new int[nMax];

    static int factSum(int n) {
        return rangeClosed(1, n)
                .map(m -> rangeClosed(1, m).reduce(1, (a, b) -> a * b)).sum();
    }

    static boolean r(int n) {
        if (n == 0)
            return false;

        char c = superperm[pos - n];
        if (--count[n] == 0) {
            count[n] = n;
            if (!r(n - 1))
                return false;
        }
        superperm[pos++] = c;
        return true;
    }

    static void superPerm(int n) {
        String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        pos = n;
        superperm = new char[factSum(n)];

        for (int i = 0; i < n + 1; i++)
            count[i] = i;
        for (int i = 1; i < n + 1; i++)
            superperm[i - 1] = chars.charAt(i);

        while (r(n)) {
        }
    }

    public static void main(String[] args) {
        for (int n = 0; n < nMax; n++) {
            superPerm(n);
            System.out.printf("superPerm(%2d) len = %d", n, superperm.length);
            System.out.println();
        }
    }
}


  

You may also check:How to resolve the algorithm Reverse words in a string step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Arbitrary-precision integers (included) step by step in the F# programming language
You may also check:How to resolve the algorithm A+B step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Scheme programming language
You may also check:How to resolve the algorithm Doubly-linked list/Element insertion step by step in the OCaml programming language