Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 2100: Computing Foundations 1 Combinatorics Tamara Schneider Summer 2013.

Similar presentations


Presentation on theme: "CSCE 2100: Computing Foundations 1 Combinatorics Tamara Schneider Summer 2013."— Presentation transcript:

1 CSCE 2100: Computing Foundations 1 Combinatorics Tamara Schneider Summer 2013

2 Computer Science Approximations 2 10 = 1024 This is about 1000 (1K) 2 30 = (2 10 ) 3 = (1024) 3 This is about 1000 3 (1G) 4*2 30 = 4 * (1024) 3 This is about 4*1000 3 (4G) Computer scientists “ cheat ” to convert powers of 2 into powers of 10. 2

3 Counting Assignments 3

4 Painting Houses [1] We can paint the first house in 3 different ways 4

5 Painting Houses [2] For each choice of the first assignment, we can paint the second house in 3 different colors 5

6 Painting Houses [3] For each choice of the first 2 assignments, we can paint the third house in 3 different colors 6

7 7 Painting Houses [4] For each choice of the first 3 assignments, we can paint the fourth house in 3 different colors

8 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 = 3 4 = 81 8

9 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) 2 26 = 67,108,864 bit strings 9

10 Counting Permutations [1] 10

11 Counting Permutations [2] So how many permutations are there for 3 objects? 11

12 Counting Permutations [2] 12

13 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! 13

14 Ordered Selections 14

15 Horse Race 15

16 Selections without Replacement General formula for π(n,m) – 1 st choice: n – 2 nd choice: n × (n-1) – 3 rd choice: n × (n-1) × (n-2)... – m th choice: n × (n-1) × (n-2) ×... × (n-m+1) π(n,m) = n!/(n-m)! Horse race: π(10,3) = 10!/7! = 10 × 9 × 8 = 720

17 Unordered Selections [1] ABC ACB BAC BCA CAB CBA All of these cases are different for ordered selections, but count as a single case for unordered selections. 17

18 Unordered Selections [2] ABC ACB BAC BCA CAB CBA All of these 6 different orders are considered distinct cases for ordered selections, but count as a single case for unordered selections. 18

19 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? 19

20 Counting Combinations: “ n choose m ” [1] π(n,m) = n!/(n-m)! ordered selections Can be grouped π(m) = m! different ways

21 Counting Combinations: “ n choose m ” [2] Poker example:

22 Counting Combinations: “ n choose m ” [3] Basis: There is only one way to pick 0 things. There is only way to pick n things. Inductive Definition

23 Counting Combinations: “ n choose m ” [4] Induction: Either do not pick the first element and and pick m things from the remaining n-1 elements, or pick the first element and then select m-1 things from the remaining n-1 elements. Inductive Definition

24 24 c = 1; for(int i=n; i>n-m; i--) c *= i; for(int i=2; i<=m; i++) c /= i;

25 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? 25 c = 1; for(int i=n; i>n-m; i--) c *= i; for(int i=2; i<=m; i++) c /= i;

26 Recursive Algorithm for “ n choose m ” Can you find an recurrence relation for the running time of this algorithm? 26 int choose(int n, int m){ if(m n)return 0; //error else if(m==0 || m==n)return 1; //basis else //induction return choose(n-1, m-1) + choose(n-1, m); }

27 Counting Combinations: Pascal’s Triangle “ n choose m ” and Pascal ’ s Triangle 1 11 121 1331 14641 \ 27

28 Binomial Coefficients 28 (x+y)(x+y)(x+y)(x+y) 16 terms - Why? – x 4 - There is only one possible order. – x 3 y - We have 4 choices for y. The order to select x does not matter. – x 2 y 2 - (4 choose 2) = 6 options for x. The order of y does not matter. – xy 3 - We have 4 choices for x. The order to select y does not matter. – y 4 - There is only one possible order. – (x+y) 4 = x 4 + 4x 3 y + 6x 2 y 2 + 4xy 3 + y 4

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

30 Ordering with Identical Items – Formula If there are n items divided into k groups of sizes i 1, i 2,...,i k, 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:

31 Suppose we have 3 children and 4 apples. In how many ways can we distribute the apples? Distribution of Apples [1] **AAAA*A*AAA*AA*AA*AAA*A*AAAA* A**AAAA*A*AAA*AA*AA*AAA* AA**AAAA*A*AAA*AA* AAA**AAAA*A* AAAA** 31

32 Distribution of Apples [2] **AAAA*A*AAA*AA*AA*AAA*A*AAAA* A**AAAA*A*AAA*AA*AA*AAA* AA**AAAA*A*AAA*AA* AAA**AAAA*A* AAAA** Unique distribution of 4 As and 2 *s 32

33 Distribution of Objects to Bins [1] We are given m bins and n identical objects. How many ways can the objects be put into the bins? We have n Os for 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. 33

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

35 Distinguishable Objects & Bins [1] Distribute n objects into m bins. There are k different types of objects. Let a i represent the members of class i. Generate a string with – one a i for each member of class i – *’s for boundaries between bins e.g. for k=3, n=5, m = 4 : a 1 a 3 *a 1 **a 2 a 1 So we have n objects with group sizes 3, 2 and 1, and one additional class of *s with m-1 members. 35

36 Distinguishable Objects & Bins [2] Example: – 3 children – 3 apples, 2 pears, 1 banana – m = 3, n = 6, – k = 3 with i 1 = 3, i 2 = 2, i 3 = 1

37 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 (4 3 ways) 37

38 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. 1. Select the lowest rank in the straight (9 choices) 2. Select the suit (4 choices) 38

39 Counting Strategies: Difference of Counts [2] Number of “ straights ” : five cards of consecutive rank that do not have the same suit 1. Select the lowest rank in the straight (9 choices) 2. Select the suit of each of the 5 cards (4 5 choices) 3. Subtract the straight flushes 39

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

41 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

42 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 ” ):

43 Summary [2] Ordering with identical items Distribution of objects to bins Distribution of distinguishable objects to bins


Download ppt "CSCE 2100: Computing Foundations 1 Combinatorics Tamara Schneider Summer 2013."

Similar presentations


Ads by Google