250 likes | 401 Views
Combinatorics. University of Akron Programming Team 9 /23/2011. Permutations. Ways of ordering a set of items. - OR -. Counting Permutations. Depends on the size of the set S of items. |S| = 1 1 Permutation |S| = 2 2 Permutations |S| = 3 ?.
E N D
Combinatorics University of Akron Programming Team 9/23/2011
Permutations • Ways of ordering a set of items. - OR -
Counting Permutations • Depends on the size of the set S of items. • |S| = 1 1 Permutation • |S| = 2 2 Permutations • |S| = 3 ?
Counting Permutations • |S| = 3 6 Permutations
Counting Permutations • |S| = 3 • Any of the three items can go in the 1st spot. • Any of the remaining two items can go in the 2nd spot • Any of the remaining one items can go into the 3rd spot. • 3 options * 2 options * 1 option • 6 total options
Counting Permutations • In general, there are |S|! (factorial) permutations. • Knowing how quickly factorials grow lets us know whether enumerating all the permutations of a set is reasonable within the confines of a programming competition.
Generating Permutations Bottom Up • If you knew all the permutations of the set {A, B, C}, could you utilize that to quickly generate the permutations of {A, B, C, D}? • Permutations of {A, B, C} • (A, B, C) • (A, C, B) • (B, A, C) • (B, C, A) • (C, A, B) • (C, B, A)
Generating Permutations Bottom Up • Easier example: If we know all the permutations of {A}, can we generate all the permutations of {A, B}? • Permutations of {A} • (A) • Let’s add B to the existing permutation. Two options: • Add B to the right of A • (A, B) • Add B to the left of A • (B, A)
Generating Permutations Bottom Up • Permutations of {A, B} • (A, B) • (B, A) • Let’s generate the permutations of {A, B, C} • Using (A, B) as a starting point • Add C to the right: (A, B, C) • Add C in the middle: (A, C, B) • Add C on the left: (C, A, B) • Using (B, A) as a starting point • Add C to the right: (B, A, C) • Add C to the middle: (B, C, A) • Add C to the left: (C, B, A)
Generating Permutations Bottom Up • BottomUpPermutations(List list) • List<List> results • Add 1st element of list (as a new list) to results • for(i = 2 to |list|) { • List<List> nextLengthResults • for(List permutation in results) { • Add the ithelemnt of list to each position in permutation } • results = nextLengthResults } • return results
Generating Permutations Bottom Up – CODE! public static <T> List<List<T>> BottomUp(List<T> items) { List<List<T>> results = new ArrayList<List<T>>(); List<T> initial = new ArrayList<T>(); initial.add(items.get(0)); results.add(initial); for(inti = 1; i < items.size(); i++) { List<List<T>> nextLengthResults = new ArrayList<List<T>>(); for(List<T> permutation: results) { for(int j = 0; j <= permutation.size(); j++) { // Add the ith item to the jth position & add that to the nextLengthResults ArrayList<T> tempPerm = new ArrayList<T>(permutation); tempPerm.add(j, items.get(i)); nextLengthResults.add(tempPerm); } } results = nextLengthResults; } return results; }
Generating Permutations Special Orderings • Minimum Change • Each consecutive permutation differs by only one swap of two items. • (1 2 3) (1 3 2) (3 1 2) (3 2 1) (2 3 1) (2 1 3) • Lexicographic order • Consider the input list to be in “alphabetic order.” Then the lexicographic order gives all permutations in combined alphabetic order • Input list: (A B C) • (A B C) • (A C B) • (B A C) • (B C A) • (C A B) • (C B A)
Subsets • Pick as many or few items from this set as you’d like:
Counting Subsets • Depends on the size of the set S of items. • |S| = 0 1 Subset • |S| = 1 2 Subsets • |S| = 2 4 Permutations • |S| = 3 ?
Counting Subsets • |S| = 3 8 Subsets
Counting Subsets • |S| = 3 • Item 1 can either be part of the subset or not. • 2 options • Item 2 can either be part of the subset or not. • 2 options * 2 options = 4 options • Item 3 can either be part of the subset or not. • 4 options * 2 options = 8 options
Counting Subsets • In general, there are 2|S| subsets (exponential). • Knowing how quickly exponentials grow lets us know whether enumerating all the subsets of a set is reasonable within the confines of a programming competition.
Generating Subsets Bottom Up • BottomUpSubsets(List list) • If list has 0 elements • return {Ø} • results := new List<List> • head := first element of the list • headlessList:= list with head removed • for(List subset in BottomUpSubsets(headlessList)) { • Add subset to results • Add subset + head to results } • return results
Generating Subsets Bottom Up – CODE! public static <T> List<Set<T>> bottomUp(Set<T> originalSet) { List<Set<T>> result = new ArrayList<Set<T>>(); if(originalSet.size() == 0) { result.add(new HashSet<T>()); return result; } List<T> list = new ArrayList<T>(originalSet); T first = list.get(0`); Set<T> remainder = new HashSet<T>(list.subList(1, list.size())); for (Set<T> without : bottomUp(remainder)) { Set<T> with = new HashSet<T>(without); with.add(first); result.add(without); result.add(with); } return result; }
Generating Subsets Another Approach • Can take advantage of bit representations of integers. • Consider S = (A, B, C), using ints in [0, 2|S|-1] • 0 000 _ _ _ • 1 001 _ _ C • 2 010 _ B _ • 3 011 _ B C • 4 100 A _ _ • 5 101 A _ C • 6 110 A B _ • 7 111 A B C
Counting Topics • Binomial coefficients • n choose k • “k member committee from n people” • Alternate notation nCk • There are n! / (n-k)!k!) ways. • nCk = (n-1)C(k-1) + (n-1)C(k) • Ex: “Num paths from (0, 0) to (10, 10) in plane only making steps in the positive directions.” • Pascals Triangle relationship • Coefficients on (a+b)n
Counting Topics • Stirling numbers • First kind – permutations on n with exactly k cycles. • Second kind – ways to partition a set of n objects into k groups. • Catalan numbers • Number of ways to balance n sets of parentheses • Cn = 1/(n+1) * (2nCn) • Eulerian Numbers • Number of permutations of length n with k ascending sequences. • Solving recurrence relations for closed form solutions.
Other Combinatorics Problems • Permutations with duplicate elements • (A, A, B) • (A, A, B) (A, B, A) (B, A, A) • “Strings” of length n on string s • Length 2 over “ab” • “aa”, “ab”, “ba”, “bb”
Images • http://www.flickr.com/photos/jay-em-tee/3492378275/ • http://www.flickr.com/photos/mtrichardson/4626549119/ • http://www.flickr.com/photos/mdpettitt/2680399319/ • http://www.flickr.com/photos/gee01/2190903226/ • http://www.flickr.com/photos/eiriknewth/382976575/ • http://www.flickr.com/photos/atkinson000/5315913793/ • http://www.flickr.com/photos/beaugiles/5374266252/