Chap5 - Counting by Mapping
Chap5 - Counting by Mapping
A B
… f …
2
Counting Rule: Bijection
If f is a bijection from A to B,
then |A| = |B|
A B
… f …
4
Bijection: Power Set and Binary Strings
5
Bijection: Power Set and Binary Strings
S: {a,b,c}
, 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
7
Bijection: Full House
9
A Chess Problem
f (7,6,2,5,5,2)
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|.
00 (none) 000000 00 00
Chocolate Lemon Sugar Glazed Plain
14
Counting Doughnut Selections
0011000000100100
00 1 1 000000 1 00 1 00
00 (none) 000000 00 00
Chocolate Lemon Sugar Glazed Plain
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
0c10l10s10g10p
0c10l10s10g10p
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”);
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”);
1 2 3 4 5 … n
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”);
1 2 3 4 5 … n
k j i
for i=1 to n do
for j=1 to i do
for k=1 to j do
printf(“hello world\n”);
1 2 3 4 5 … n
x1 x2 x3 x4
e.g. Suppose x1=3, x2=5, x3=2, x4=0,
Set xi=yi+1.
25
Counting Positive Integer Solutions
26
Choosing Non-Adjacent Books
27
Choosing Non-Adjacent Books
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.
A B
29
Choosing Non-Adjacent Books
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.
32
Exercises
for i=1 to n do
for j=1 to i-1 do
for k=1 to j-1 do
printf(“hello world\n”);
(Hint 2: inclusion-exclusion.)
33