L10-Counting by Mapping
L10-Counting by Mapping
A B
… f …
This
Lecture
We will study how to define mappings to
count.
• Bijection
rule
• Division
rule
• More
mapping
Counting Rule:
Bijection
If f is a bijection from A to B,
then |A| = |B|
A B
… f …
(7,6,2,5,5,2
)
Hint: define a A B
bijection! … …
f
Counting Doughnut
Selections
A ::= all selections of a dozen
doughnuts
B::= all 16-bit binary strings with exactly four
1’s.
Define a bijection between A and
B.
00110000001001
00
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
Counting Doughnut
Selections
A ::= doughnuts selections B::= all 16-bit strings with four
1’s.
12 0 0 0 0 00000000000011
Chocolate Lemon Sugar Glazed
11
Plain
2 3 0 3 4 00100011000100
Chocolate Lemon Sugar Glazed
00
Plain
2 0 6 2 00110000001001
2
Chocolate Lemon Sugar Glazed
00
Plain
0 0 0 0
12 11110000000000
Chocolate Lemon Sugar Glazed 00
13
Plain
Counting Doughnut
Selections
c chocolate, l lemon, s sugar, g glazed, p plain
maps to
0c10l10s10g1
0p
A ::= all selections of a dozen
doughnuts
B::= all 16-bit binary strings with exactly four
1’s.
This is a bijection because
that
noall
A ::= two adjacentof
selections books are selected?
6 non-adjacent books from 20
books
Hint: define a A B
bijection! … …
f
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:
❖ it is an injection
❖ it is a surjection
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14
1 0 0 1 1 0 0 0 1 0 0 1 0 0 1
1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 2
0 1 2 3 4 5 6 7 8 9 0
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14
0 1 1 0 0 0 1 0 0 1 0 0 1 0 1
1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 2
0 1 2 3 4 5 6 7 8 9 0
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14
1 0 0 1 1 0 0 0 1 0 0 1 0 1 0
1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 2
0 1 2 3 4 5 6 7 8 9 0
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”);
= 22
0
Counting Non-Negative Integer
Solutions
How many solutions are there to the equation
x1+x2+x3+x4=10,
separations.
Counting Non-Negative Integer
Solutions
How many solutions are there to the equation
x1+x2+x3+x4=10,
x4-1;
each yi >=0.
and
each yi>=0.
• Bijection
rule
• Division
rule
• More
mapping
Division
Rule
if function from A to B is k-to-1,
then
equivale
nt
Round
Table
A ::= all the permutations of the
people
B::= all possible seating arrangements at the round
table
as does
a2 a4 a3 a1 a13 a12 … a5
4 9
! !
So number of 4 element
subsets is
• Bijection
rule
• Division
rule
• More
mapping
Parenthe
sis
How many valid ways to add n pairs of
parentheses?
/\
/\ /\ /\/\ / \
/\/\/\, /\/ \, / \/\, / \, / \
The basic examples usually map a set into a properly defined binary
strings.