package math.statistics;

/*************************************************************************
 *  Compilation:  javac Combinations.java
 *  Execution:    java Combinations N
 *  
 *  Enumerates all subsets of N elements using recursion.
 *  Uses some String library functions.
 *
 *  Both functions (comb1 and comb2) print them in alphabetical
 *  order; comb2 does not include the empty subset.
 *
 *  % java Combinations 3
 *  
 *  a
 *  ab
 *  abc
 *  ac
 *  b
 *  bc
 *  c
 *
 *  a
 *  ab
 *  abc
 *  ac
 *  b
 *  bc
 *  c
 *
 *  Remark: this is, perhaps, easier by counting from 0 to 2^N - 1 by 1
 *  and looking at the bit representation of the counter. However, this
 *  recursive approach generalizes easily, e.g., if you want to print
 *  out all combinations of size k.
 *  
 *  
 *  
 *  Enumerates all subsets of size k on N elements in lexicographic order.
 *  Two different solutions. Uses some String library functions. 
 *
 *  % java CombinationsK 5 3
 *  abc
 *  abd
 *  abe
 *  acd
 *  ace
 *  ade
 *  bcd
 *  bce
 *  bde
 *  cde
 *
 *************************************************************************/

public class Combinations {

    // print all subsets of the characters in s
    public static void comb1(String s) { comb1("", s); }

    // print all subsets of the remaining elements, with given prefix 
    private static void comb1(String prefix, String s) {
        if (s.length() > 0) {
            System.out.println(prefix + s.charAt(0));
            comb1(prefix + s.charAt(0), s.substring(1));
            comb1(prefix,               s.substring(1));
        }
    }  

    // alternate implementation
    public static void comb2(String s) { comb2("", s); }
    private static void comb2(String prefix, String s) {
        System.out.println(prefix);
        for (int i = 0; i < s.length(); i++)
            comb2(prefix + s.charAt(i), s.substring(i + 1));
    }  
    
// with k
    // print all subsets that take k of the remaining elements, with given prefix 
    public  static void comb1(String s, int k) { comb1(s, "", k); }
    private static void comb1(String s, String prefix, int k) {
        if (s.length() < k) return;
        else if (k == 0) System.out.println(prefix);
        else {
            comb1(s.substring(1), prefix + s.charAt(0), k-1);
            comb1(s.substring(1), prefix, k);
        }
    }  

    // print all subsets that take k of the remaining elements, with given prefix 
    public  static void comb2(String s, int k) { comb2(s, "", k); }
    private static void comb2(String s, String prefix, int k) {
        if (k == 0) System.out.println(prefix);
        else {
            for (int i = 0; i < s.length(); i++)
                comb2(s.substring(i + 1), prefix + s.charAt(i), k-1);
        }
    }  

    private static void showCombination(int[] s) {
        for (int i = 0; i < s.length; i++)
            System.out.print(s[i] + " ");
        System.out.println();
    }
    
    /**
     * Enumerates all subsets of size k on N elements.
     *
     * k=5, n=3:
     * 0 1 2 
     * 0 1 3 
     * 0 1 4 
     * 0 2 3 
     * 0 2 4 
     * 0 3 4 
     * 1 2 3 
     * 1 2 4 
     * 1 3 4 
     * 2 3 4 
     * @param s array
     * @param position
     * @param nextInt
     * @param k
     * @param N
     */
    public static void generate(int[] s, int position, int nextInt, int k, int N) {
        if (position == k) {
            showCombination(s);
            return;
        }
        for (int i = nextInt; i < N; i++) {
            s[position] = i;
            generate(s, position + 1, i + 1, k, N);
        }
    }  


    // read in N from command line, and print all subsets among N elements
    // read in N and k from command line, and print all subsets of size k from N elements
    public static void main(String[] args) {
//       int n = Integer.parseInt(args[0]);
      int n = 4;
       String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
       String elements = alphabet.substring(0, n);

       // using first implementation
       comb1(elements);
       System.out.println();

       // using second implementation
       comb2(elements);
       System.out.println();
       

//       int k = Integer.parseInt(args[1]);
       int k = 2;

       comb1(elements, k);
       System.out.println();
       comb2(elements, k);
       
       

       int[] s = new int[k];
       generate(s, 0, 0, k, n);
    }

}


