Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Vector;

public class Main {
    private static Vector<int[]> setpartition(int n) {
        Vector<int[]> subsetv = new Vector<int[]>();

        //set partition by setpart2 algorithm
        int[] c = new int[n + 1];
        int[] b = new int[n + 1];
        int r = 1;
        c[1] = 1;
        int j = 0;
        b[0] = 1;
        int n1 = n - 1;

        do {
            while (r < n1) {
                r = r + 1;
                c[r] = 1;
                j++;
                b[j] = r;
            }

            for (int i = 1; i <= n - j; ++i) {
                c[n] = i;
                int[] set = new int[n];
                for (int k = 0; k < n; ++k) {
                    set[k] = c[k + 1];
                }
                subsetv.add(set);
            }

            r = b[j];
            c[r]++;
            if (c[r] > r - j) {
                j--;
            }

        } while (r != 1);

        return subsetv;
    }
}