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

Combinatorics University of Akron Programming Team 9/23/2011

Embed Size (px)

Citation preview

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

Combinatorics

University of Akron Programming Team

9/23/2011

Page 2: Combinatorics University of Akron Programming Team 9/23/2011

PermutationsWays of ordering a set of items.

- OR -

Page 3: Combinatorics University of Akron Programming Team 9/23/2011

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

Counting Permutations

|S| = 3 6 Permutations

Page 5: Combinatorics University of Akron Programming Team 9/23/2011

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

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

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

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

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

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

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

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

SubsetsPick as many or few items from

this set as you’d like:

Page 14: Combinatorics University of Akron Programming Team 9/23/2011

Subsets

Page 15: Combinatorics University of Akron Programming Team 9/23/2011

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

Counting Subsets

|S| = 3 8 Subsets

Page 17: Combinatorics University of Akron Programming Team 9/23/2011

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

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

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

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

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

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

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

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”