combinatorics university of akron programming team 9/23/2011
TRANSCRIPT
![Page 1: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/1.jpg)
Combinatorics
University of Akron Programming Team
9/23/2011
![Page 2: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/2.jpg)
PermutationsWays of ordering a set of items.
- OR -
![Page 3: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/3.jpg)
Counting PermutationsDepends on the size of the set S
of items.◦|S| = 1 1 Permutation◦|S| = 2 2 Permutations◦|S| = 3 ?
![Page 4: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/4.jpg)
Counting Permutations
|S| = 3 6 Permutations
![Page 5: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/5.jpg)
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
![Page 6: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/6.jpg)
Counting PermutationsIn 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.
Count ms / perm in 15s
6! = 720 20.83
7! = 5,040 2.976
8! = 40,320 0.372
9! = 362,880 0.0413
10! = 3,628,800
0.00413
11! = 39,916,800
0.0003757
![Page 7: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/7.jpg)
Generating PermutationsBottom 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)
![Page 8: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/8.jpg)
Generating PermutationsBottom 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)
![Page 9: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/9.jpg)
Generating PermutationsBottom 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)
![Page 10: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/10.jpg)
Generating PermutationsBottom 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 ith elemnt of list to each position in permutation}
results = nextLengthResults }
◦return results
![Page 11: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/11.jpg)
Generating PermutationsBottom 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(int i = 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 nextLengthResultsArrayList<T> tempPerm = new ArrayList<T>(permutation);tempPerm.add(j, items.get(i));nextLengthResults.add(tempPerm);}}
results = nextLengthResults;}
return results;}
![Page 12: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/12.jpg)
Generating PermutationsSpecial 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)
![Page 13: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/13.jpg)
SubsetsPick as many or few items from
this set as you’d like:
![Page 14: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/14.jpg)
Subsets
![Page 15: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/15.jpg)
Counting SubsetsDepends on the size of the set S
of items.◦|S| = 0 1 Subset◦|S| = 1 2 Subsets◦|S| = 2 4 Permutations◦|S| = 3 ?
![Page 16: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/16.jpg)
Counting Subsets
|S| = 3 8 Subsets
![Page 17: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/17.jpg)
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
![Page 18: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/18.jpg)
Counting SubsetsIn 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.
Count ms / perm in 15 s
26 = 64 234.375
210 = 1,024 14.648
214 = 16384 0.9155
218 = 262144 0.05722
222 = 4194304 0.003576
226 = 67108864 0.0002235
![Page 19: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/19.jpg)
Generating SubsetsBottom 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
![Page 20: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/20.jpg)
Generating SubsetsBottom 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;}
![Page 21: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/21.jpg)
Generating SubsetsAnother 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
![Page 22: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/22.jpg)
Counting TopicsBinomial 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
![Page 23: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/23.jpg)
Counting TopicsStirling 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.
![Page 24: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/24.jpg)
Other Combinatorics ProblemsPermutations 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”
![Page 25: Combinatorics University of Akron Programming Team 9/23/2011](https://reader036.vdocuments.mx/reader036/viewer/2022062322/5697c02c1a28abf838cd9278/html5/thumbnails/25.jpg)
Imageshttp://www.flickr.com/photos/jay-
em-tee/3492378275/http://www.flickr.com/photos/mtri
chardson/4626549119/http://www.flickr.com/photos/md
pettitt/2680399319/http://www.flickr.com/photos/gee
01/2190903226/http://www.flickr.com/photos/eirik
newth/382976575/http://www.flickr.com/photos/atki
nson000/5315913793/http://www.flickr.com/photos/bea
ugiles/5374266252/