Computer Science 15-100 (Sections T & U), Spring 2008
Class Notes: Solving Combinatorial Problems Using Arrays and Combinatorial
Iterators
Logistics
Topic Outline:
| File | Permutations.java | Combinations.java | BaseNCounting.java | PowerSet.java |
| Meaning | A permutation is an arrangement where order matters. | A combination is an arrangement where order does not matter. | This is a counter, or an odometer, where the digits are in baseN (0,1,...,n-1). | A"power set" of a set is the set of all the possible subsets of that set. |
| Sample Code |
Permutation p = new Permutation(4,2); while (p.hasNext()) { int[] a = p.next(); System.out.println(Arrays.toString(a)); } |
Combination c = new Combination(4,2); while (c.hasNext()) { int[] a = c.next(); System.out.println (Arrays.toString(a)); } |
BaseNCounter bnc = new
BaseNCounter(4,2); while (bnc.hasNext()) { int[] a = bnc.next(); System.out.println(Arrays.toString(a)); } |
PowerSet ps = new PowerSet(4); while (ps.hasNext()) { int[] a = ps.next(); System.out.println(Arrays.toString(a)); } |
| Sample Output |
[0, 1] [0, 2] [0, 3] [1, 0] [1, 2] [1, 3] [2, 0] [2, 1] [2, 3] [3, 0] [3, 1] [3, 2] |
[0, 1] [0, 2] [0, 3] [1, 2] [1, 3] [2, 3] |
[0, 0] [0, 1] [0, 2] [0, 3] [1, 0] [1, 1] [1, 2] [1, 3] [2, 0] [2, 1] [2, 2] [2, 3] [3, 0] [3, 1] [3, 2] [3, 3] |
[] [0] [1] [0, 1] [2] [0, 2] [1, 2] [0, 1, 2] [3] [0, 3] [1, 3] [0, 1, 3] [2, 3] [0, 2, 3] [1, 2, 3] [0, 1, 2, 3] |
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem