csce 2100: computing foundations 1 combinatorics

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

Upload: whitby

Post on 24-Feb-2016

52 views

Category:

Documents


0 download

DESCRIPTION

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) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSCE 2100: Computing Foundations 1 Combinatorics

CSCE 2100: Computing Foundations 1

Combinatorics

Tamara SchneiderSummer 2013

Page 2: CSCE 2100: Computing Foundations 1 Combinatorics

2

Computer Science Approximations

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)

Computer scientists “cheat” to convert powers of 2 into powers of 10.

Page 3: CSCE 2100: Computing Foundations 1 Combinatorics

3

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?

Page 4: CSCE 2100: Computing Foundations 1 Combinatorics

4

Painting Houses [1]

We can paint the first house in 3 different ways

Page 5: CSCE 2100: Computing Foundations 1 Combinatorics

5

Painting Houses [2]

For each choice of the first assignment, we can paint the second house in 3 different colors

Page 6: CSCE 2100: Computing Foundations 1 Combinatorics

6

Painting Houses [3]

For each choice of the first 2 assignments, we can paint the third house in 3 different colors

Page 7: CSCE 2100: Computing Foundations 1 Combinatorics

7

Painting Houses [4]

For each choice of the first 3 assignments, we can paint the fourth house in 3 different colors

Page 8: CSCE 2100: Computing Foundations 1 Combinatorics

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 = 34 = 81

Page 9: CSCE 2100: Computing Foundations 1 Combinatorics

9

Computer MemoryGiven 26 bits, how many different bit strings can be produced?

Example: names for memory cells

26 different bits2 possible assignments for each of them (0,1)226 = 67,108,864 bit strings

Page 10: CSCE 2100: Computing Foundations 1 Combinatorics

10

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.

Page 11: CSCE 2100: Computing Foundations 1 Combinatorics

11

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?

Page 12: CSCE 2100: Computing Foundations 1 Combinatorics

12

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.

Page 13: CSCE 2100: Computing Foundations 1 Combinatorics

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!

Page 14: CSCE 2100: Computing Foundations 1 Combinatorics

14

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

Page 15: CSCE 2100: Computing Foundations 1 Combinatorics

15

Horse Race• 10 options for winner• 9 options for second place• 8 options for third place

Page 16: CSCE 2100: Computing Foundations 1 Combinatorics

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

Page 17: CSCE 2100: Computing Foundations 1 Combinatorics

17

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

A B CA C BB A CB C AC A BC B A

All of these cases are different for ordered selections, but count as a single case for unordered selections.

Page 18: CSCE 2100: Computing Foundations 1 Combinatorics

18

Unordered Selections [2]

A B CA C BB A CB C AC A BC B A

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:

Page 19: CSCE 2100: Computing Foundations 1 Combinatorics

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?Number of ordered selections: Number of permutations among 5 cards: Different hands: 311,875,200/120 = 2,598,960

Page 20: CSCE 2100: Computing Foundations 1 Combinatorics

Counting Combinations: “n choose m” [1]

• π(n,m) = n!/(n-m)! ordered selections

• Can be grouped π(m) = m! different ways

Page 21: CSCE 2100: Computing Foundations 1 Combinatorics

Counting Combinations: “n choose m” [2]

Poker example:

Page 22: CSCE 2100: Computing Foundations 1 Combinatorics

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

Page 23: CSCE 2100: Computing Foundations 1 Combinatorics

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

Page 24: CSCE 2100: Computing Foundations 1 Combinatorics

24

Algorithm for “ choose ” [1]

c = 1;for(int i=n; i>n-m; i--) c *= i;

for(int i=2; i<=m; i++) c /= i;

Page 25: CSCE 2100: Computing Foundations 1 Combinatorics

25

Algorithm for “ choose ” [2]

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?

c = 1;for(int i=n; i>n-m; i--) c *= i;

for(int i=2; i<=m; i++) c /= i;

Page 26: CSCE 2100: Computing Foundations 1 Combinatorics

26

Recursive Algorithm for “n choose m”

Can you find an recurrence relation for the running time of this algorithm?𝑇 (𝑛 ,0)=𝑇 (𝑛 ,𝑛)=𝑎𝑇 (𝑛 ,𝑚)=𝑇 (𝑛−1 ,𝑚−1)+𝑇 (𝑛−1 ,𝑚)+𝑏

int choose(int n, int m){ if(m<0|| 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);}

Page 27: CSCE 2100: Computing Foundations 1 Combinatorics

27

Counting Combinations: Pascal’s Triangle“n choose m” and Pascal’s Triangle

11 1

1 2 11 3 3 1

1 4 6 4 1

“ choose ” can be found in row , entry

Example: “3 choose 2” = 3 (row 4, entry 3) “4 choose 2” = 6 (row 5, entry 3)

Page 28: CSCE 2100: Computing Foundations 1 Combinatorics

28

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

Page 29: CSCE 2100: Computing Foundations 1 Combinatorics

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

Page 30: CSCE 2100: Computing Foundations 1 Combinatorics

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:

Page 31: CSCE 2100: Computing Foundations 1 Combinatorics

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**AAA A*A*AA A*AA*A A*AAA*

AA**AA AA*A*A AA*AA*

AAA**A AAA*A*

AAAA**

Page 32: CSCE 2100: Computing Foundations 1 Combinatorics

32

Distribution of Apples [2]**AAAA *A*AAA *AA*AA *AAA*A *AAAA*A**AAA A*A*AA A*AA*A A*AAA*AA**AA AA*A*A AA*AA*AAA**A AAA*A*AAAA**

Unique distribution of 4 As and 2 *s

Page 33: CSCE 2100: Computing Foundations 1 Combinatorics

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.

Page 34: CSCE 2100: Computing Foundations 1 Combinatorics

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.

Page 35: CSCE 2100: Computing Foundations 1 Combinatorics

35

Distinguishable Objects & Bins [1]• Distribute n objects into m bins. There are k different types of

objects. • Let ai represent the members of class i.

• Generate a string with– one ai for 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.

Page 36: CSCE 2100: Computing Foundations 1 Combinatorics

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

Page 37: CSCE 2100: Computing Foundations 1 Combinatorics

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 (43 ways)

Page 38: CSCE 2100: Computing Foundations 1 Combinatorics

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)

Page 39: CSCE 2100: Computing Foundations 1 Combinatorics

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 (45 choices)

3. Subtract the straight flushes

Page 40: CSCE 2100: Computing Foundations 1 Combinatorics

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)

Page 41: CSCE 2100: Computing Foundations 1 Combinatorics

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

Page 42: CSCE 2100: Computing Foundations 1 Combinatorics

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

Page 43: CSCE 2100: Computing Foundations 1 Combinatorics

Summary [2]• Ordering with identical items

• Distribution of objects to bins

• Distribution of distinguishable objects to bins