COMPSCI 102
Discrete Mathematics for
Computer Science
Counting III
Lecture 8
X 1 + X 2 + X 3
How many ways to rearrange the letters in the word “SYSTEMS” ?
SYSTEMS
7 places to put the Y,
6 places to put the T,
5 places to put the E,
4 places to put the M, and the S’s are forced
7 X 6 X 5 X 4 = 840
SYSTEMS
Let’s pretend that the S’s are distinct:
S
1
Y S
2
TEM S
3
There are 7! permutations of S
1
Y S
2
TEM S
3
But when we stop pretending we see that we have counted each arrangement of
SYSTEMS 3! times, once for each of 3! rearrangements of S
1
S
2
S
3
7!
3!
= 840
Arrange n symbols: r r
2 of type 2, …, r k
1 of type 1, of type k n r
1 n-r
1 r
2
… n - r
1
- r
2 r k
- … - r k-1
= n!
(n-r
1
)!r
1
!
(n-r
1
)!
(n-r
1
-r
2
)!r
2
!
… n!
= r
1
!r
2
! … r k
!
CARNEGIEMELLON
14!
2!3!2!
= 3,632,428,800
Remember:
The number of ways to arrange n symbols with r
1 of type 1, r
2
2, …, r k of type of type k is: n!
r
1
!r
2
! … r k
!
5 distinct pirates want to divide
20 identical, indivisible bars of gold. How many different ways can they divide up the loot?
Sequences with 20 G’s and 4 /’s
GG / G // GGGGGGGGGGGGGGGGG / represents the following division among the pirates: 2, 1, 0, 17, 0
In general, the ith pirate gets the number of G’s after the (i-1) st / and before the i th /
This gives a correspondence (bijection) between divisions of the gold and sequences with 20 G’s and 4 / ’s
How many different ways to divide up the loot?
Sequences with 20 G’s and 4 /’s
24
4
How many different ways can n distinct pirates divide k identical, indivisible bars of gold?
n + k - 1 n - 1
= n + k - 1 k
How many integer solutions to the following equations?
x
1
+ x
2 x
1
, x
2
, x
+ x
3
3
, x
4
+ x
4
, x
5
+ x
5
≥ 0
= 20
Think of x k are being the number of gold bars that are allotted to pirate k
24
4
How many integer solutions to the following equations?
x
1
+ x x
1
, x
2
2
+ x
, x
3
3
+ … + x n
, …, x n
≥ 0
= k n + k - 1 n - 1
= n + k - 1 k
Identical/Distinct Dice
Suppose that we roll seven dice
How many different outcomes are there, if order matters?
6 7
What if order doesn’t matter?
(E.g., Yahtzee)
12
7
(Corresponds to 6 pirates and 7 bars of gold)
Multisets
A multiset is a set of elements, each of which has a multiplicity
The size of the multiset is the sum of the multiplicities of all the elements
Example:
{X, Y, Z} with m(X)=0 m(Y)=3, m(Z)=2
Unary visualization: {Y, Y, Y, Z, Z}
Counting Multisets
There number of ways to choose a multiset of size k from n types of elements is: n + k - 1 n - 1
= n + k - 1 k
(
+
Polynomials Express
Choices and Outcomes
Products of Sum = Sums of Products
+ +
+
) (
+
) =
+ + +
t 1 b 1 b 2 t 2 b 3 t 1 t 2 t 1 t 2 b 1 t 1 b 1 t 2 b 2 t 1 b 2 t 2 b 3 t 1 b 3 t 2
(b 1 +b 2 +b 3 )(t 1 +t 2 ) = b 1 t 1 + b 1 t 2 + b 2 t 1 + b 2 t 2 + b 3 t 1 + b 3 t 2
There is a correspondence between paths in a choice tree and the cross terms of the product of polynomials!
Choice Tree for Terms of (1+X) 3
1 X
1 X
1 X
1 X 1 X 1 X 1 X
1 X X X 2 X X 2 X 2
Combine like terms to get 1 + 3X + 3X 2 + X 3
X 3
What is a Closed Form
Expression For c k
?
(1+X) n = c
0
+ c
1
X + c
2
X 2 + … + c n
X n
(1+X)(1+X)(1+X)(1+X)…(1+X)
After multiplying things out, but before combining like terms, we get 2 n cross terms, each corresponding to a path in the choice tree c k
, the coefficient of X k , is the number of paths with exactly k X’s c k
= n k
The Binomial Formula
(1+X) n = n
0
X 0 + n
1
X 1 +…+ n n
X n binomial expression
Binomial Coefficients
The Binomial Formula
(1+X) 0 =
(1+X) 1 =
(1+X) 2 =
1
1 + 1 X
1 + 2 X + 1 X 2
(1+X) 3 = 1 + 3 X + 3 X 2 + 1 X 3
(1+X) 4 = 1 + 4 X + 6 X 2 + 4 X 3 + 1 X 4
What is the coefficient of EMSTY in the expansion of
(E + M + S + T + Y) 5
?
5!
What is the coefficient of
EMS 3 TY in the expansion of
(E + M + S + T + Y) 7
?
The number of ways to rearrange the letters in the word
SYSTEMS
What is the coefficient of BA 3 N 2 in the expansion of
(B + A + N) 6
?
The number of ways to rearrange the letters in the word
BANANA
What is the coefficient of (X
1 r1 X
2 r2 …X k rk ) in the expansion of
(X
1
+X
2
+X
3
+…+X k
) n ?
n!
r
1
!r
2
!...r
k
!
There is much, much more to be said about how polynomials encode counting questions!
Power Series Representation
(1+X) n = n
k = 0
“Product form” or
“Generating form”
=
k = 0 n k n k
X k
X k
For k>n, n k
= 0
“Power Series” or “Taylor Series” Expansion
By playing these two representations against each other we obtain a new representation of a previous insight:
(1+X) n = n
k = 0 n k
X k
Let x = 1, 2 n = n
k = 0 n k
The number of subsets of an n-element set
By varying x, we can discover new identities:
(1+X) n = n
k = 0 n k
X k
Let x = -1, 0 = n
k = 0 n k
(-1) k
Equivalently, n
k odd n k
= n
k even n k
The number of subsets with even size is the same as the number of subsets with odd size
(1+X) n = n
k = 0 n k
X k
Proofs that work by manipulating algebraic forms are called
“algebraic” arguments. Proofs that build a bijection are called
“combinatorial” arguments
n
k odd n k
= n
k even n k
Let O n be the set of binary strings of length n with an odd number of ones.
Let E n be the set of binary strings of length n with an even number of ones.
We gave an algebraic proof that
O n
= E n
A Combinatorial Proof
Let O n be the set of binary strings of length n with an odd number of ones
Let E n be the set of binary strings of length n with an even number of ones
A combinatorial proof must construct a bijection between O n and E n
An Attempt at a Bijection
Let f n be the function that takes an n -bit string and flips all its bits f n is clearly a one-toone and onto function for odd n . E.g. in f
7 we have:
0010011 1101100
1001101 0110010
...but do even n work?
In f
6 we have
110011 001100
101010 010101
Uh oh. Complementing maps evens to evens!
A Correspondence That
Works for all n
Let f n be the function that takes an n-bit string and flips only the first bit. For example,
0010011 1010011
1001101 0001101
110011 010011
101010 001010
(1+X) n = n
k = 0 n k
X k
The binomial coefficients have so many representations that many fundamental mathematical identities emerge…
The Binomial Formula
(1+X) 0 =
(1+X) 1 =
1
1 + 1 X
(1+X) 2 = 1 + 2 X + 1 X 2
(1+X) 3 = 1 + 3 X + 3 X 2 + 1 X 3
(1+X) 4 = 1 + 4 X + 6 X 2 + 4 X 3 + 1 X 4
Pascal’s Triangle: k th row are coefficients of (1+X) k
Inductive definition of k th entry of n th row:
Pascal(n,0) = Pascal (n,n) = 1;
Pascal(n,k) = Pascal(n-1,k-1) + Pascal(n-1,k)
“Pascal’s Triangle”
0
0
= 1
2
0
= 1
1
0
= 1
2
1
= 2
1
1
= 1
2
2
= 1
3
0
= 1
3
1
= 3
3
2
= 3
• Al-Karaji, Baghdad 953-1029
• Chu Shin-Chieh 1303
• Blaise Pascal 1654
3
3
= 1
Pascal’s Triangle
1
1
1
“It is extraordinary how fertile in properties the triangle is.
1
1
3
2
3
1
1
1 4 6 4 1
Everyone can try his hand”
1 5 10 10 5 1
1 6 15 20 15 6 1
Summing the Rows
2 n = n
k = 0 n k
1
1 + 1
1 + 2 + 1
1 + 3 + 3 + 1
1 + 4 + 6 + 4 + 1
1 + 5 + 10 + 10 + 5 + 1
1 + 6 + 15 + 20 + 15 + 6 + 1
= 1
= 2
= 4
= 8
= 16
= 32
= 64
Odds and Evens
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 + 15 + 15 + 1 = 6 + 20 + 6
Summing on 1 st Avenue
1
1 1
1 2 1 n
i = 1 i
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
= n
i = 1 i
1
= n+1
2
Summing on k th Avenue
1
1 1
1 2 1 n
i = k
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1 i k
= n+1 k+1
Fibonacci Numbers
1
1
1
1
1 2 1
3 3
= 2
= 3
1
= 5
= 8
= 13
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
Sums of Squares
1
1 1
1
2
2
2
1
2
1
2
3
2
3
2
1
2
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
Al-Karaji Squares
1
1 1
1 2 +2 1
1 3 +2 3 1
1 4 +2 6 4 1
1 5 +2 10 10 5 1
1 6 +2 15 20 15 6 1
= 1
= 4
= 9
= 16
= 25
= 36
Pascal Mod 2
All these properties can be proved inductively and algebraically. We will give combinatorial proofs using the
Manhattan block walking representation of binomial coefficients
How many shortest routes from A to B?
A
B
10
5
Manhattan j th street
4
3
2
1
0 0
1
2
3 k th avenue
4
There are j+k k shortest routes from (0,0) to (j,k)
Level n
4
3
2
1
0
Manhattan
0
1
2
3 k th avenue
4
There are n k shortest routes from (0,0) to (n-k,k)
Level n
4
3
2
1
0
Manhattan
0
1
2
3 k th avenue
4
There are n k shortest routes from (0,0) to level n and k th avenue
Level n
4
3
2
1
1
1
6
0
1
5
1
0
1
1 1
2
3 3
1
1
4 6
10 10
15 20
4
15
2
1
5
3 k th avenue
1
6
4
1
Level n
4
3
2
1
1
1
6
0
1
5
1
1
1 1
2
3 3
1
4
15
6
+
20
4
15
1
5 k th avenue
1
6
1 n k
= n-1 k-1
+ n-1 k
Level n
4
3
2
1
0
0
1
2
3 k th avenue
4 n
k = 0 n k
2
=
2n n
Level n
4
3
2
1
0
0
1
2
3 k th avenue
4 n
i = k i k
= n+1 k+1
Vector Programs
Let’s define a (parallel) programming language called VECTOR that operates on possibly infinite vectors of numbers.
Each variable V !
can be thought of as:
< * , * , * , * , *, *, … >
Vector Programs
Let k stand for a scalar constant
<k> will stand for the vector <k,0,0,0,…>
<0> = <0,0,0,0,…>
<1> = <1,0,0,0,…>
V !
+ T !
means to add the vectors position-wise
<4,2,3,…> + <5,1,1,….> = <9,3,4,…>
Vector Programs
RIGHT(V !
) means to shift every number in V !
one position to the right and to place a 0 in position 0
RIGHT( <1,2,3, …> ) = <0,1,2,3,…>
Vector Programs
Example:
V !
:= <6>;
V !
:= RIGHT(V !
) + <42>;
V !
:= RIGHT(V !
) + <2>;
V !
:= RIGHT(V !
) + <13>;
Store:
V !
= <6,0,0,0,…>
V !
= <42,6,0,0,…>
V !
= <2,42,6,0,…>
V !
= <13,2,42,6,…>
V !
= < 13, 2, 42, 6, 0, 0, 0, … >
Vector Programs
Example:
V !
:= <1>;
Loop n times
V !
:= V !
+ RIGHT(V !
);
Store:
V !
= <1,0,0,0,…>
V !
= <1,1,0,0,…>
V
V !
!
= <1,2,1,0,…>
= <1,3,3,1,…>
V !
= n th row of Pascal’s triangle
X 1 + X 2 +
Vector programs can be implemented by polynomials!
X 3
Programs Polynomials
The vector V !
= < a
0
, a
1
, a
2
, . . . > will be represented by the polynomial:
P
V
=
i = 0 a i
X i
Formal Power Series
The vector V !
= < a
0
, a
1
, a
2
, . . . > will be represented by the formal power series :
P
V
=
i = 0 a i
X i
V !
= < a
0
, a
1
, a
2
, . . . >
P
V
=
i = 0 a i
X i
<0> is represented by
<k> is represented by
V ! + T !
is represented by
RIGHT(V !
) is represented by
0 k
(P
V
+ P
T
)
(P
V
X)
Vector Programs
Example:
V !
:= <1>; P
V
:= 1;
Loop n times
V !
:= V !
+ RIGHT(V !
);
P
V
:= P
V
+ P
V !
= n th row of Pascal’s triangle
V
X;
Vector Programs
Example:
V !
:= <1>; P
V
:= 1;
Loop n times
V !
:= V !
+ RIGHT(V !
);
P
V
:= P
V
(1+X);
V !
= n th row of Pascal’s triangle
Vector Programs
Example:
V !
:= <1>;
Loop n times
V !
:= V !
+ RIGHT(V !
);
P
V
= (1+ X)
V !
= n th row of Pascal’s triangle n
Here’s What
You Need to
Know…
• Polynomials count
• Binomial formula
• Combinatorial proofs of binomial identities
• Vector programs