440 likes | 621 Views
CSCE 2100: Computing Foundations 1 Combinatorics. Tamara Schneider Summer 2013. Computer Science Approximations. Computer scientists “ cheat ” to convert powers of 2 into powers of 10. 2 10 = 1024 This is about 1000 (1K) 2 30 = (2 10 ) 3 = (1024) 3 This is about 1000 3 (1G)
E N D
CSCE 2100: Computing Foundations 1Combinatorics Tamara Schneider Summer 2013
Computer Science Approximations Computer scientists “cheat” to convert powers of 2 into powers of 10. 210 = 1024 This is about 1000 (1K)230 = (210)3 = (1024)3 This is about 10003 (1G) 4*230 = 4 * (1024)3 This is about 4*10003 (4G)
Counting Assignments • How many ways are there to paint a row of houses in any of colors? • Example: • 4 houses • 3 colors • How many “assignments” are there?
Painting Houses [1] We can paint the first house in 3 different ways
Painting Houses [2] For each choice of the first assignment, we can paint the second house in 3 different colors
Painting Houses [3] For each choice of the first 2 assignments, we can paint the third house in 3 different colors
Painting Houses [4] For each choice of the first 3 assignments, we can paint the fourth house in 3 different colors
Painting Houses [4] So how many ways can we paint these houses? • There are 3 different colors for the first house • For each of the possible assignments there are 3 choices for the second house: 3*3 • For each of the possible assignments there are 3 choices for the third house: 3*3*3 • For each of the possible assignments there are 3 choices for the fourth house: 3*3*3*3 = 34 = 81
Computer Memory Given 26 bits, how many different bit strings can be produced?Example: names for memory cells 26 different bits 2 possible assignments for each of them (0,1) 226 = 67,108,864 bit strings
Counting Permutations [1] • In how many different ways can we order objects in a line? • Each different way of ordering things is called a permutation of . • We use the notation for the number of permutations of things.
Counting Permutations [2] A single object can only be arranged in 1 order: 2 object can be arranged in 2 different ways. There are 2 choices to pick the first object. Then there is only 1 option for the second object. So how many permutations are there for 3 objects?
Counting Permutations [2] For 3 objects, there are 3 choices for the first object. Then, for each of the choices there are 2 choices for the second object. There is only 1 choice for the last object.
Counting Permutations [3] • In general, for n objects there are π(n) = n! permutations. • Examples • Lining up 10 horses in a row: π(10) = 10! • 15 people standing in a line: π(15) = 15!
Ordered Selections • counts the permutations of n elements • counts the number of ways we can select items from , such that order matters for the selected items • Example: In a horse race with 10 participating horses, how many different choices are there for the first three places (order matters)?
Horse Race • 10 options for winner • 9 options for second place • 8 options for third place
Selections without Replacement • General formula for π(n,m) • 1st choice: n • 2nd choice: n × (n-1) • 3rd choice: n × (n-1) × (n-2)... • mth choice: n × (n-1) × (n-2) × ... × (n-m+1) • π(n,m) = n!/(n-m)! • Horse race: π(10,3) = 10!/7! = 10 × 9 × 8 = 720
Unordered Selections [1] • Number of ways we can select items from , such that order does not matter for the selected items. Also called: “ choose ” • Horse race example: • We do not care about the order of the three winners All of these cases are different for ordered selections, but count as a single case for unordered selections.
Unordered Selections [2] All of these 6 different orders are considered distinct cases for ordered selections, but count as a single case for unordered selections. Ordered selection: Unordered selection:
Poker Hands • Each player receives 5 cards from a 52-card deck • How many different hands may we receive? • What do we need to calculate? • Ordered or unordered selection? Number of ordered selections: Number of permutations among 5 cards:Different hands: 311,875,200/120 = 2,598,960
Counting Combinations: “n choose m”[1] • π(n,m) = n!/(n-m)! ordered selections • Can be grouped π(m) = m! different ways
Counting Combinations: “n choose m”[2] Poker example:
Counting Combinations: “n choose m”[3] Inductive Definition Basis: There is only one way to pick 0 things. There is only way to pick n things.
Counting Combinations: “n choose m” [4] Inductive Definition Induction: Either do not pick the first element and and pick m things from the remaining n-1 elements, orpick the first element and then select m-1 things from the remaining n-1 elements.
Algorithm for “ choose ” [1] c = 1; for(inti=n; i>n-m; i--) c *= i; for(inti=2; i<=m; i++) c /= i;
Algorithm for “ choose ” [2] c = 1; for(inti=n; i>n-m; i--) c *= i; for(inti=2; i<=m; i++) c /= i; What is the running time of the algorithm? Do you see any potential problems for the execution of the algorithm? If so, how can we fix this issue?
Recursive Algorithm for “n choose m” int choose(int n, int m){ if(m<0|| m>n)return0;//error else if(m==0 || m==n)return1; //basis else//induction return choose(n-1, m-1) + choose(n-1, m); } Can you find an recurrence relation for the running time of this algorithm?
Counting Combinations: Pascal’s Triangle “n choose m” and Pascal’s Triangle “ choose ” can be found in row , entry Example: “3 choose 2” = 3 (row 4, entry 3) “4 choose 2” = 6 (row 5, entry 3) \
Binomial Coefficients • (x+y)(x+y)(x+y)(x+y) • 16 terms - Why? • x4 - There is only one possible order. • x3y - We have 4 choices for y. The order to select x does not matter. • x2y2 - (4 choose 2) = 6 options for x. The order of y does not matter. • xy3 - We have 4 choices for x. The order to select y does not matter. • y4 - There is only one possible order. • (x+y)4 = x4 + 4x3y + 6x2y2 + 4xy3 + y4
Ordering with Identical Items • Some items are identical • If items are distinguishable, order does matter! • Example: abenst (answer: absent)π(6) = 6! = 720 permutations • Example: eillttπ(6) = 6! = 720 permutations“l” counted double: 720/2 = 360 permutations“t” counted double: 360/2 = 180 permutations
Ordering with Identical Items – Formula If there are n items divided into k groups of sizes i1, i2,...,ik, items within a group are not distinguishable, but items from different groups are distinguishable, then the number of distinguishable orders of the n items is:
Distribution of Apples [1] Suppose we have 3 children and 4 apples. In how many ways can we distribute the apples?
Distribution of Apples [2] Unique distribution of 4 As and 2 *s
Distribution of Objects to Bins [1] • We are given mbins and n identical objects. • How many ways can the objects be put into the bins? • We have n Osfor the objects and (m-1) *s as separators between the bins. This gives us a string of length (n+m-1). • Now we can choose n out of the (n+m-1) places to hold the Os.
Distribution of Objects to Bins [2] • Example: Chuck-a-Luck: 3 dice numbered from 1 to 6. Players bet a dollar on a number. • Each player receives as many dollars as his or her number shows. How many different outcomes are there? • The dice are indistinguishable. The order does not matter. The numbers 1-6 act as “bins”. The dice are objects distributed to them.
Distinguishable Objects & Bins [1] • Distribute n objects into m bins. There are k different types of objects. • Let airepresent the members of class i. • Generate a string with • one aifor each member of class i • *’s for boundaries between bins • e.g. for k=3 , n=5 , m = 4 : a1a3*a1**a2a1 • So we have n objects with group sizes 3, 2 and 1, and one additional class of *s with m-1 members.
Distinguishable Objects & Bins [2] • Example: • 3 children • 3 apples, 2 pears, 1 banana • m = 3, n = 6, • k = 3 with i1 = 3, i2 = 2, i3 = 1
Counting Strategies: Sequence of Choices Number of 5-card poker hands that are one-pair. • Select rank of the pair (13 different ranks) • Select 3 ranks for remaining 3 cards (12 choose 3) • Select the suits for the two cards of the pair (4 choose 2) • Select suits of remaining cards (43 ways)
Counting Strategies: Difference of Counts [1] • Number of “straight flushes”: five cards of consecutive rank of the same suit. Since there are 13 different ranks, one of the first 9 ranks must be the starting point. • Select the lowest rank in the straight (9 choices) • Select the suit (4 choices)
Counting Strategies: Difference of Counts [2] • Number of “straights”: five cards of consecutive rank that do not have the same suit • Select the lowest rank in the straight (9 choices) • Select the suit of each of the 5 cards (45 choices) • Subtract the straight flushes
Counting Strategies: Sum of Subcases [1] • Toss a sequence of 10 coins. In how many cases will 8 or more be heads? • Exactly 8 heads (10 choose 8) • Exactly 9 heads (10 choose 9) • Exactly 10 heads (10 choose 10)
Counting Strategies: Sum of Subcases [2] • Alternate Solution Chuck-a-Luck (3 dies) • How many different outcomes are there for this game? • Number of outcomes with 3 different values (6 choose 3) • Number of outcomes with 2 of one value and one other value (6 choices for number that appears twice and 5 choices for number that appears once) • Same number for all dice: 6 different ways
Summary [1] • Permutations of n elements: π(n) = n! • Ordered selection (m out of n elements):π(n,m) = n!/(n-m)! • Unordered selection (“n choose m”):
Summary [2] • Ordering with identical items • Distribution of objects to bins • Distribution of distinguishable objects to bins