Presentation is loading. Please wait.

Presentation is loading. Please wait.

Combinatorics University of Akron Programming Team 9/23/2011.

Similar presentations


Presentation on theme: "Combinatorics University of Akron Programming Team 9/23/2011."— Presentation transcript:

1 Combinatorics University of Akron Programming Team 9/23/2011

2 Permutations Ways of ordering a set of items. - OR -

3 Counting Permutations Depends on the size of the set S of items. ◦ |S| = 1  1 Permutation ◦ |S| = 2  2 Permutations ◦ |S| = 3  ?

4 Counting Permutations |S| = 3  6 Permutations

5 Counting Permutations |S| = 3 ◦ Any of the three items can go in the 1 st spot.  Any of the remaining two items can go in the 2 nd spot  Any of the remaining one items can go into the 3 rd spot. ◦ 3 options * 2 options * 1 option  6 total options

6 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. Countms / perm in 15s 6! = 72020.83 7! = 5,0402.976 8! = 40,3200.372 9! = 362,8800.0413 10! = 3,628,8000.00413 11! = 39,916,8000.0003757

7 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)

8 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)

9 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)

10 Generating Permutations Bottom Up BottomUpPermutations(List list) ◦ List results ◦ Add 1 st element of list (as a new list) to results ◦ for(i = 2 to |list|) {  List nextLengthResults  for(List permutation in results) {  Add the i th elemnt of list to each position in permutation }  results = nextLengthResults } ◦ return results

11 Generating Permutations Bottom Up – CODE! public static List > BottomUp(List items) { List > results = new ArrayList >(); List initial = new ArrayList (); initial.add(items.get(0)); results.add(initial); for(int i = 1; i < items.size(); i++) { List > nextLengthResults = new ArrayList >(); for(List permutation: results) { for(int j = 0; j <= permutation.size(); j++) { // Add the i th item to the j th position & add that to the nextLengthResults ArrayList tempPerm = new ArrayList (permutation); tempPerm.add(j, items.get(i)); nextLengthResults.add(tempPerm); } results = nextLengthResults; } return results; }

12 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)

13 Subsets Pick as many or few items from this set as you’d like:

14 Subsets

15 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  ?

16 Counting Subsets |S| = 3  8 Subsets

17 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

18 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. Countms / perm in 15 s 2 6 = 64234.375 2 10 = 1,02414.648 2 14 = 163840.9155 2 18 = 2621440.05722 2 22 = 41943040.003576 2 26 = 671088640.0002235

19 Generating Subsets Bottom Up BottomUpSubsets(List list) ◦ If list has 0 elements  return {Ø} ◦ results := new 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

20 Generating Subsets Bottom Up – CODE! public static List > bottomUp(Set originalSet) { List > result = new ArrayList >(); if(originalSet.size() == 0) { result.add(new HashSet ()); return result; } List list = new ArrayList (originalSet); T first = list.get(0`); Set remainder = new HashSet (list.subList(1, list.size())); for (Set without : bottomUp(remainder)) { Set with = new HashSet (without); with.add(first); result.add(without); result.add(with); } return result; }

21 Generating Subsets Another Approach Can take advantage of bit representations of integers. Consider S = (A, B, C), using ints in [0, 2 |S| -1] ◦ 0000_ _ _ ◦ 1001_ _ C ◦ 2010_ B _ ◦ 3011_ B C ◦ 4100A _ _ ◦ 5101A _ C ◦ 6110A B _ ◦ 7111A B C

22 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

23 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.

24 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”

25 Images http://www.flickr.com/photos/jay-em-tee/3492378275/ http://www.flickr.com/photos/mtrichardson/46265491 19/ http://www.flickr.com/photos/mtrichardson/46265491 19/ 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/531591379 3/ http://www.flickr.com/photos/atkinson000/531591379 3/ http://www.flickr.com/photos/beaugiles/5374266252/


Download ppt "Combinatorics University of Akron Programming Team 9/23/2011."

Similar presentations


Ads by Google