0% found this document useful (0 votes)
65 views33 pages

Chap5 - Counting by Mapping

This document discusses counting by defining bijections between sets. It provides several examples of defining bijections to count objects: 1) A bijection is defined between subsets of a set S and binary strings to show that the power set of S has 2^n elements, where S has n elements. 2) A bijection is defined between configurations of placing 3 chess pieces on a board and sequences of row and column numbers to count the number of valid configurations. 3) Bijections are defined between selections of a dozen doughnuts and 16-bit strings with 4 ones to count the number of possible doughnut selections. 4) A bijection is defined between loops printing "hello world

Uploaded by

Dorian Grey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views33 pages

Chap5 - Counting by Mapping

This document discusses counting by defining bijections between sets. It provides several examples of defining bijections to count objects: 1) A bijection is defined between subsets of a set S and binary strings to show that the power set of S has 2^n elements, where S has n elements. 2) A bijection is defined between configurations of placing 3 chess pieces on a board and sequences of row and column numbers to count the number of valid configurations. 3) Bijections are defined between selections of a dozen doughnuts and 16-bit strings with 4 ones to count the number of possible doughnut selections. 4) A bijection is defined between loops printing "hello world

Uploaded by

Dorian Grey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Counting by Mapping

A B

… f …

Lecture 15: Nov 4 1


Plan

We will study how to define mappings to count.

There will be many examples shown.

• Bijection rule (this set of slides)

• Division rule (next set of slides)

• More mapping (next set of slides)

2
Counting Rule: Bijection

If f is a bijection from A to B,
then |A| = |B|

A B

… f …

To compute |A|, one strategy is to define a bijection of A and B,


where B is easier to count and we can compute |B| directly.
3
Power Set

How many subsets of a set S?

P(S) = the power set of S


= the set of all subsets of S

for S = {a, b, c},


P(S) = {, {a}, {b}, {c}, {a,b}, {a,c}, {b,c}, {a,b,c} }

Suppose S has n elements.

How large is the power set of S?

4
Bijection: Power Set and Binary Strings

S: {s1, s2, s3, s4, s5, … , sn}

We define a bijection f between subsets and binary strings

A: the set of all subsets of S


A B
… …
f B: the set of all n-bit strings

The mapping f :A->B is defined in the following way:

Given a subset T, we define f(T) as an n-bit string


with the i-th bit equal to 1 if and only if si is in T.

5
Bijection: Power Set and Binary Strings

S: {a,b,c}

P(S) 3-bit strings

, 000
{a}, 001
{b}, 010
{c}, 011
{a,b}, 100
{a,c}, 101
{b,c}, 110
{a,b,c} 111

6
Bijection: Power Set and Binary Strings

The mapping is defined in the following way:

subset: {s1, s3, s4, … , sn} A B


… …
string: 1 0 1 1 0 … 1 f

This mapping is a bijection, because

 two different subsets are mapped to two different strings (injection)


 each binary string is mapped by some subset (surjection).

Therefore, |A| = |B|, and |B| can be computed directly.

So, |P(S)| = |n-bit binary strings| = 2n.

7
Bijection: Full House

We have used this idea before when we counted poker hands.

There is a bijection between Full Houses and sequences specifying:


1. The value of the triple, which can be chosen in 13 ways.
2. The suits of the triple, which can be selected in (4 3) ways.
3. The value of the pair, which can be chosen in 12 ways.
4. The suits of the pair, which can be selected in (4 2) ways.

A: the set of full houses


A B
… …
B: the set of sequences which satisfy (1)-(4). f
8
A Chess Problem

In how many different ways can we place a pawn (p),


a knight (k), and a bishop (b) on a chessboard so that
no two pieces share a row or a column?

9
A Chess Problem

We define a mapping f between configurations to


sequences (r(p), c(p), r(k), c(k), r(b), c(b)),
where r(p), r(k), and r(b) are distinct rows,
and c(p), c(k), and c(b) are distinct columns.

A: the set of the configurations of the 3 pieces


A B
… …
f B: the set of the such sequences of 6 numbers

If we can define a bijection between A and B,


and also calculate |B|, then we can determine |A|.
10
A Chess Problem

We define a mapping f between configurations to


sequences (r(p), c(p), r(k), c(k), r(b), c(b)),
where r(p), r(k), and r(b) are distinct rows,
and c(p), c(k), and c(b) are distinct columns.

f (7,6,2,5,5,2)

This is a bijection, because:


(2,5)
 no two configs map to same sequence (injection)
 every such sequence is mapped (surjection)
(5,2)
So, to count the number of chess configurations,
(7,6) it is equivalent to count the number of such sequences.
11
A Chess Problem

We define a mapping between configurations to


sequences (r(p), c(p), r(k), c(k), r(b), c(b)),
where r(p), r(k), and r(b) are distinct rows,
and c(p), c(k), and c(b) are distinct columns.

Using the generalized product rule,


there are 8 choices of r(p) and c(p),
there are 7 choices of r(k) and c(k),
there are 6 choices of r(b) and c(b).

Thus, total number of configurations


= (8x7x6)2 = 112896.
(7,6,2,5,5,2) 12
Counting Rule: Bijection

If f is a bijection from A to B,
then |A| = |B|

Steps:
1) Come up with B.
A B 2) Come up with f.
… …
f 3) Show f is a bijection.
4) Compute |B|.

Usually the first two steps are more difficult.

Now we see some more interesting examples. 13


Counting Doughnut Selections

There are five kinds of doughnuts.

How many different ways to select a dozen doughnuts?

00 (none) 000000 00 00
Chocolate Lemon Sugar Glazed Plain

A ::= all selections of a dozen doughnuts

Hint: define a bijection to some bit strings!

14
Counting Doughnut Selections

A ::= all selections of a dozen doughnuts

B::= all 16-bit binary strings with exactly four 1’s.

Define a bijection f between A and B.

0011000000100100
00 1 1 000000 1 00 1 00
00 (none) 000000 00 00
Chocolate Lemon Sugar Glazed Plain

Each doughnut is represented by a 0,


and four 1’s are used to separate five types of doughnuts. 15
Counting Doughnut Selections

A ::= doughnuts selections B::= all 16-bit strings with four 1’s.

12 0 0 0 0 0000000000001111
Chocolate Lemon Sugar Glazed Plain

2 3 0 3 4 0010001100010000
Chocolate Lemon Sugar Glazed Plain

2 0 6 2 2 0011000000100100
Chocolate Lemon Sugar Glazed Plain

0 0 0 0 12
Chocolate Lemon Sugar Glazed Plain
1111000000000000

16
Counting Doughnut Selections

c chocolate, l lemon, s sugar, g glazed, p plain maps to

0c10l10s10g10p

A ::= all selections of a dozen doughnuts

B::= all 16-bit binary strings with exactly four 1’s.

This is a bijection because

•Two doughnut selections map to two different strings. (injection)


(think of a doughnut selection as a sequence of 5 numbers (c,l,s,g,p),
consider the first number which is different in the two sequences).

•Every string in B corresponds to a valid doughnut selection. (surjection)


17
Counting Doughnut Selections

c chocolate, l lemon, s sugar, g glazed, p plain maps to

0c10l10s10g10p

A ::= all selections of a dozen doughnuts

B::= all 16-bit binary strings with exactly four 1’s.

A B

18
Counting Loops

for i=1 to n do
for j=1 to i do
for k=1 to j do
printf(“hello world\n”);

How many “hello world” will this program print?

A ::= all (i,j,k) triple

B::= all strings with n-1 zeroes and 3 ones.

19
Counting Loops

for i=1 to n do
for j=1 to i do
for k=1 to j do
printf(“hello world\n”);

There are n possible values for the i,j,k.

1 2 3 4 5 … n

Imagine there are n-1 separators for the n values.

If i=4, j=2, k=2, then there are two zeroes in 2 and one zero in 4.
20
Counting Loops

for i=1 to n do
for j=1 to i do
for k=1 to j do
printf(“hello world\n”);

There are n possible values for the i,j,k.

1 2 3 4 5 … n

k j i

In general, the position of the first zero corresponds to the value of k.


The position of the second zero corresponds to the value of j.
The position of the third zero corresponds to the value of i.
21
Counting Loops

for i=1 to n do
for j=1 to i do
for k=1 to j do
printf(“hello world\n”);

There are n possible values for the i,j,k.

1 2 3 4 5 … n

This is a bijection (verify this!) between the values for i,j,k


and the set of strings with n-1 ones and 3 zeros.

So, the program prints “hello world” exactly times. 22


Counting Non-Negative Integer Solutions

How many solutions are there to the equation x1+x2+x3+x4=10,


where x1,x2,x3,x4 are nonnegative integers?

It is just like buying 10 doughnuts from 4 types.

A ::= all non-negative integer solutions x1+x2+x3+x4=10

B::= all 13-bit binary strings with exactly three 1’s.

Think of there are 10 points to be distributed into 4 variables.

x1 x2 x3 x4
e.g. Suppose x1=3, x2=5, x3=2, x4=0,

0 0 0 0 0 0 0 0 0 0 the 3 ones are used for separations.


23
Counting Non-Negative Integer Solutions

How many solutions are there to the equation x1+x2+x3+x4=10,


where x1,x2,x3,x4 are nonnegative integers?

It is just like buying 10 doughnuts from 4 types.

A ::= all non-negative integer solutions x1+x2+x3+x4=10

B::= all 13-bit binary strings with exactly three 1’s.

It is not difficult to verify that this is a bijection,


by the same argument as in doughnut selections.

So, the are exactly integer solutions.


24
Counting Positive Integer Solutions

How many integer solutions to x1+x2+x3+x4=14 if each xi>=1?

Set xi=yi+1.

Consider the equation y1+y2+y3+y4=10 where each yi >=0.

There is a bijection between the solutions for y and

the solutions for x.

Therefore we can apply the previous result,

and conclude that the answer is

25
Counting Positive Integer Solutions

How many integer solutions to x1+x2+x3+x4<=10 if each xi>=0?

Consider the equation y1+y2+y3+y4+y5=10 where each yi>=0.

There is a bijection between the solutions for y and

the solutions for x.

Therefore we can apply the previous result,

and conclude that the answer is

because we need 4 ones and 10 zeroes.

26
Choosing Non-Adjacent Books

There are 20 books arranged in a row on a shelf.

How many ways to choose 6 of these books so that

no two adjacent books are selected?

A ::= all selections of 6 non-adjacent books from 20 books

B::= all 15-bit binary strings with exactly six 1’s.

27
Choosing Non-Adjacent Books

A ::= all selections of 6 non-adjacent books from 20 books

B::= all 15-bit binary strings with exactly six 1’s.

Map each zero to a non-chosen book, each of the first five 1’s to a chosen
book followed by a non-chosen book, and the last 1 to a chosen book.

This is a bijection, because:


Two different selections are mapped to two different strings (injection)
(consider the first position that is different in the two selections)
 Every string in B corresponds to a valid selection (surjection) 28
Choosing Non-Adjacent Books

A ::= all selections of 6 non-adjacent books from 20 books

B::= all 15-bit binary strings with exactly six 1’s.

A B
29
Choosing Non-Adjacent Books

A ::= all selections of 6 non-adjacent books from 20 books

B::= the set of integer solutions to x1+x2+x3+x4+x5+x6<=20


where x1>=1 and x2,x3,x4,x5,x6>=2.

Here, each xi represents to pick the xi book after the previous chosen book.

e.g. this configuration corresponds to x1=1, x2=4, x3=2, x4=5, x5=4, x6=4.

It is not difficult to check that this is a bijection.


30
Choosing Non-Adjacent Books

A ::= all selections of 6 non-adjacent books from 20 books

B::= the set of integer solutions to x1+x2+x3+x4+x5+x6<=20


where x1>=1 and x2,x3,x4,x5,x6>=2.

From slide 24 we learned that |B|=|C| for

C::= the set of integer solutions to x1+x2+x3+x4+x5+x6<=9


where x1,x2,x3,x4,x5,x6>=0.

From slide 26 we learned that |C|=|D| for

D::= the set of integer solutions to x1+x2+x3+x4+x5+x6+x7=9


where x1,x2,x3,x4,x5,x6,x7>=0.

From slide 24 we learned that |D|= 31


Exercises

What is the number of non-negative integer solutions to x1+x2+…+xm=n?

What is the number of integer solutions to x1+x2+…+xm=n


where x1>=a1, x2>=a2, …, and xm>=am?

What is the number of integer solutions to x1+x2+…+xm<n


where x1>=a1, x2>=a2, …, and xm>=am?

32
Exercises

How many “hello world” will this program print?

for i=1 to n do
for j=1 to i-1 do
for k=1 to j-1 do
printf(“hello world\n”);

What is the number of non-negative integer solutions to x1+x2=n


where x1<a1 and x2<a2? (Go to tutorial!)

(Hint 1: count the complement.)

(Hint 2: inclusion-exclusion.)

33

You might also like