0% found this document useful (0 votes)
0 views82 pages

CS112 Week5

The document discusses various combinatorial problems, including distributing coins among boys and girls, the Binomial Theorem, and the Inclusion-Exclusion Principle. It also covers generating functions and their applications in counting problems, such as selecting items and solving Diophantine equations. Additionally, it includes examples and exercises to illustrate these concepts.

Uploaded by

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

CS112 Week5

The document discusses various combinatorial problems, including distributing coins among boys and girls, the Binomial Theorem, and the Inclusion-Exclusion Principle. It also covers generating functions and their applications in counting problems, such as selecting items and solving Diophantine equations. Additionally, it includes examples and exercises to illustrate these concepts.

Uploaded by

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

Spring 2025

Prof. Dr. Hasan F. ATEŞ


Özyeğin University, İstanbul, Turkey
[email protected]
Distributing Money

Distribute n coins to k boys and l girls such that


each girl gets at least one coin, but boys may not
get any. In how many ways can we do this?

a.

b.

c.

d.
2
Distributing Money

Distribute n coins to k boys and l girls such that


each girl gets at least one coin, but boys may not
get any. In how many ways can we do this?

First take 1 coin from each boy.


Now, we have n + k coins. Use version 1 of the
distribution where each of the (k + l) kids gets at
least one coin.
So, the result is:
( 𝑛+ 𝑘 − 1
𝑘+ 𝑙 − 1 )
3
Chapter 3: roadmap

3.1 The Binomial Theorem


3.2 Distributing Presents
3.3 Anagrams
3.4 Distributing Money
3.5 Pascal’s Triangle
Pascal’s Triangle

(x + y)n
1
x+y
x2+2xy+y2
x3+3x2y+3xy2+y3
x4+4x3y+6x2y2+4xy3+y4

5
Pascal’s Triangle

Property :

6
Pascal’s Triangle – Python code

def triangleRow(n):
if n==0:
return [1]

xrow = triangleRow(n-1)

xl = [0]+xrow
xr = xrow+[0]
return [r+s for (r,s) in zip(xl,xr)]

def triangle(n):
tr = [triangleRow(i) for i in range(n+1)]
return tr

triangle(5)

7
Pascal’s Triangle

- + - … + (-1)n =0

- + - + - + =0

8
Pascal’s Triangle

12 = 1
12 + 12 = 2
12 + 22 + 12 = 6
12 + 32 + 32 + 12 = 20
12 + 42 + 62 + 42 + 12 = 70

9
Pascal’s Triangle

Property:

+ + +…+ =

What is the interpretation?


RHS: The number of ways to choose n things out of 2n.

LHS: Considering that: =


Divide the 2n things to n and n. Then select k things from
the first n and (n – k) from the second n. Add the number
of ways to do this for all k values from 0 to n.
10
Inclusion – Exclusion Principle

11
Musical Choices
In a class poll that all students participated, it turned out
that 15 students like pop music and 7 likes rock. 3
students like both. How many students are in this
class?
a. 25
b. 22
c. 18
d. 19
Inclusion-Exclusion Principle

|A⋃B| = ?
|A⋃B| = |A| + |B| - |A⋂B|
13
Inclusion-Exclusion

|A⋃B⋃C| = ?

14
Musical tastes
In a class of 40, 18 students have a picture of Beatles, 16 have a
picture of Rolling Stones, and 12 have a picture of Pink Floyd. There
are 7 who have pictures of both Beatles and Rolling Stones. 5 have
picture of both Beatles and Pink Floyd, and 3 have pictures of both
Rolling Stones and Pink Floyd. 2 Students have pictures of all three
groups. How many students in class do not have any pictures?

a. 9
b. 11
c. 7

15
Inclusion-Exclusion Principle
n n
|∪A i| = ∑ |A i| - ∑ |A i∩A j| + ∑ |A i∩A j∩A k| - …
i=1 i=1 1≤i<j≤n 1≤i<j<k≤n

+ (-1)n-1|A 1∩…∩A n|

16
Inclusion-Exclusion is a consequence
There are 21 people in a group who went to eat together. Each of them eats at
least one dish. 9 eat chicken, 10 eat spinach, 7 eat yogurt, and 5 eat all
selections, i.e., chicken, spinach and yogurt. How many people eat at least two
dishes?

spinach (10) chicken (9)

5 yogurt (7) 1

21 People
1 3
5 5
2 1
4
min: 11 people
max: 16 people
17
Pigeonholes

18
Counting hairs!
Can you find two persons in Istanbul who have the same number of
strands of hair on their heads? (If it helps, it is known that people have
less than about 500,000 strands of hair.)

YES (TRUE) NO (FALSE)

19
Pigeonhole principle:

Given n boxes and m >


n objects placed in
them, at least one box
must contain more than
one object
Too many pigeons:
Covering the chessboard:
x Can we cover the modified
board using 1x2 tiles as shown
below (i.e., each tile covers
two squares)?

x
YES (TRUE) NO (FALSE) ?
Covering the chessboard:
x Can we cover the new
modified board using 1x2 tiles
as shown below (i.e., each tile
covers two squares)?

x
YES!
Letterbox principle:

If the average number


of letters per box is x,
then some box will
have at least x letters
(similarly, some box
has at most x)
Great Theoretical Ideas In Computer Science
V. Adamchik
D. Sleator
Carnegie Mellon
University
Generating Functions I

1 2 3
X + X + X
( + + )( + )=?
Plan

• Counting with generating functions


• Solving the Diophantine equations (a polynomial equation

in two or more unknowns such that only the integer

solutions are searched)


• Proving combinatorial identities

Applied Combinatorics, by Alan Tucker

Generatingfunctionology, by Herbert Wilf


Problem
Ula is allowed to choose two
items from a tray containing
an apple, an orange, a pear, a
banana, and a plum. In how
many ways can she choose?

Ans: 10 2
There is a correspondence between paths

in a choice tree and the cross terms of

the product of polynomials!


Counting with Generating Functions

(0 apple + 1 apple)(0 orange + 1 orange)

(0 pear + 1 pear)(0 banana + 1 banana)

(0 plum + 1 plum)

5
(1 + x)(1 + x)(1 + x)(1 + x)(1 + x) = (1 + x)

2
Take the coefficient of x .
Recall: The Binomial Theorem

5
(x + y) = (x + y)(x + y)(x + y)(x + y)(x + y)

= (xx+xy+yx+yy)(x + y)(x + y)(x + y)

= (xxx+xyx+yxx+yyx+xxy+xyy+yxy+yyy)(x + y)(x + y)

= (xxxx+xyxx+yxxx+yyxx+xxyx+xyyx+yxyx+yyyx

+xxxy+xyxy+yxxy+yyxy+xxyy+xyyy+yxyy+yyyy)(x + y)

= xxxxx+xyxxx+yxxxx+yyxxx+xxyxx+xyyxx+yxyxx+yyyxx

+xxxyx+xyxyx+yxxyx+yyxyx+xxyyx+xyyyx+yxyyx+yyyyx

+xxxxy+xyxxy+yxxxy+yyxxy+xxyxy+xyyxy+yxyxy+yyyxy

+xxxyy+xyxyy+yxxyy+yyxyy+xxyyy+xyyyy+yxyyy+yyyyy
Each of the resulting terms has 5 letters (x’s or y’s).
2 3
e.g., the term x y can be obtained in 10 ways (2 select 5)
j k
In how many different ways can we get x y ?
30
Recall: The Binomial Theorem

5 5 4 3 2 2 3 4 5
(x + y) = x + x y+ x y + x y + xy + y

selecting 3 y’s
same as selecting
out of 5 factors,
3 x’s, i.e., 2 y’s
that is, (x + y)’s
out of 5 factors,

that is, (x + y)’s

© M. Reha Civanlar 31
Problem

Ula is allowed to choose two items from a tray containing

TWO apples, an orange, a pear, a banana, and a

plum. In how many ways can she choose?

The two apples are identical.

a) 15

b) 11

c) 13

d) 9
no apple

5 choices

4x3/2 = 6 choices
Total: 11 choices
Counting with Generating
Functions

(0 apple + 1 apple + 2 apple)

(0 orange + 1 orange)(0 pear + 1 pear)

(0 banana + 1 banana)(0 plum + 1 plum)


2
In this notation, apple stand for choosing 2 apples, and + stands

for an exclusive or.

2
Take the coefficient of x .
11
The function f(x) that has a polynomial

expansion

n
f(x)=a0 + a1 x + …+ an x

is the generating function (GF) for the

sequence

a0, a1, …, an
24
If the polynomial (1+x+x )

is the generating polynomial

for ak, what is the combinatorial meaning of ak


k
(the coefficient of x )?

The number of ways to select k

objects from 4 types with at most 2 of each type.


The pizza party…

We want to order 8 pizzas for a party. The shop only has

2 sausage, 3 cheese, and 4 mushroom pizzas left.

What is the number of possible orders?

a) 3
b) 5
c) 6
d) 9
Counting with Generating
Functions

(1+x+x2) (1+x+x2+x3) (1+x+x2+x3+x4)


sausage cheese mushroom

= 1+3x+6x2+9x3+11x4+11x5+9x6+6x7+3x8+x9

The coefficient by x8 shows that there are only 3


orders for 8 pizzas.
Note, we solve the whole parameterized family
of problems!!!
We want to order 8 pizzas for a party. The shop

only has 2 sausage, 3 cheese, and 4

mushroom pizzas left. What the number of

possible orders? Mushroom pizzas come in

multiples of two.
Counting with Generating
Functions

(1+x+x2) (1+x+x2+x3) (1+x2+x4)


sausage cheese mushroom

= 1+2x+4x2+5x3+6x4+6x5+5x6+4x7+2x8+x9

The coefficient by x8 shows that there are only 2


possible orders.
Diophontine Equations
Problem

Find the number of ways to select R balls from a

pile of 2 blue, 2 red and 2 green balls

R 23
Coefficient by x in (1+x+x )
R 23
Coefficient by x in (1+x+x )

23
(1+x+x ) =

0 1 2 0 1 2 0 1 2
(x +x +x )(x +x +x )(x +x +x )

all of the terms are of the form:

e e e e e e
1 2 3
X X X X 1 2 3
Find the number of ways to
select R balls from a pile of 2
blue, 2 red and 2 green balls

e e e
1 2
X X X 3

e1 + e2 + e3 = R

The original question


0 ≤ek ≤2
is equivalent to
finding the number of integer
solutions for this constrained
Exercise

Find the number of integer solutions to

x1+x2+x3+x4=21

0 ≤ xk ≤7

Take the coefficient by x21 in


(1+x+x2+x3+x4+x5+x6+x7)4
Exercise

Find the number of integer solutions to

x1+x2+x3+x4 = 21

0 < xk< 7

It is possible to formulate this as it is, i.e. take the coefficient of


21
x in:
2 64
(x+x +…+x ) .

But, let’s look into another way to handle the inequality

constraints...
x1+x2+x3+x4 = 21
0 < xk< 7

Observe

0 < xk < 7

-1 < xk-1 < 6

0 ≤xk-1 ≤5

Then

(x1-1)+(x2-1)+(x3-1)+(x4-1) = 21-4

or

y1 + y2 + y3 + y4 = 17
x1+x2+x3+x4=21
0<xk<7

The problem is reduced to solving

y1 + y2 + y3 + y4 = 17, 0≤yk≤5

17
Solution: take the coefficient by y in
2 3 4 54
(1+y+y +y +y +y )

which is 20
Another Problem

x1+x2+x3=9

1≤x1≤2

2≤x2≤4

1≤x3≤4
Solution

x1+x2+x3=9
2
1≤x1≤2 (x+x )
2 3 4
2≤x2≤4 (x +x +x )
2 3 4
1≤x3≤4 (x+x +x +x )

9
Take the coefficient by x in the product of generating

functions.
Two observations

1. A generating function approach is


designed to model a selection of all
possible numbers of objects.

2. It can be used not only for counting but


for solving the linear Diophantine
equations
Polynomials in Python
Polynomial functions may be represented as lists
of their coefficients, starting from the constant
coefficient:
P(x) = c0 + c1x + c2x2+…+ cnxn -> [c0, c1, c2,…, cn]
Python Numpy library has Polynomial functions:
import numpy as np

ppar = [4, 3, -2, 10]


p = np.poly1d(ppar)
print(p)

print(p(3))
print(np.polyval(ppar, 3))
Multiplying Polynomials in
Python
The product of f(z) = f0 + f1z + f2z + … +
2

fkzk and g(z) = g0 + g1z + g2z2 + … + gmzm


looks like this:
f(z) * g(z) = (f0 + f1z + f2z2 + … + fkzk ) * (g0 +
g1z + g2z2 + … + gmzm )
= f0 g0 + ( f0 g1 + f1 g0) z + ( f0 g2 + f1 g1 + f2
g0) z2 + …
Multiplying Polynomials in Python
import numpy as np

poly1 = np.poly1d([4, 3, -2, 10])


poly2 = np.poly1d([1, 2, 1])
poly3 = np.polymul(poly1, poly2)

print(poly3)

5 4 3 2
4 x + 11 x + 8 x + 9 x + 18 x + 10
The pizza party

Admin pulls a few strings…

and a large sausage pizza kitchen is built next to

OzU.

Now we can order as many sausage pizzas as we

like…
The pizza party.

Unfortunately, still only 3 cheese and 4 mushroom

pizzas are available. Mushroom pizzas come in

multiples of two. What is the number of

possible orders for 8 pizzas now?


Counting with Generating
Functions
2 3 2 4 2 3
(1+x+x +x ) (1+x +x ) (1+x+x +x + …)

cheese mushroom sausage

There is a problem (…) with the


above
expression.
Counting with Generating
Functions
2 3 2 4 2 3
(1+x+x +x ) (1+x +x ) (1+x+x +x + …)=

cheese mushroom sausage

2 3 4 5 6 7 2 3
(1+x+2x +2x +2x +2x +x +x ) (1+x+x +x + …)

2 3 4
=1+2x+4x +6x +8x +…
Generators: Infinite Lists in Python
def natural_numbers(n):
i = 0
while i<n:
yield i
i += 1

natural_no = list(natural_numbers(10))
natural_no

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

k = natural_numbers(20)
next(k)
>0
next(k)
>1
Counting with Generating
Functions
2 3 2 4 2 3
(1+x+x +x ) (1+x +x ) (1+x+x +x + …)

cheese mushroom sausage

Can’t we do this in Python with the lazy evaluation?


Counting with Generating Functions
(1+x+x2+x3) (1+x2+x4)
(1+x+x2+x3+ …)
cheese
def ones(n): mushroom sausage
for i in range(n):
yield 1

poly1 = np.poly1d([1,1,1,1])
poly2 = np.poly1d([1,0,1,0,1])

np.flip(np.polymul(np.polymul(poly1, poly2), list(ones(9))))


[8]
 12

np.flip(np.polymul(np.polymul([1,1,1,1],[1,0,1,0,1]),
list(ones(9))))[8]
 12
The power series

2
a0 + a1 x + a2 x +…

is the generating function for the infinite

sequence

a0, a1, a2, …,

a
k 0
k x k
A famous generating function from

calculus:

 k
x
e 
x

k 0 k !
We can use the generating

function technique to solve almost all the

mathematical problems you have met in your

life so far.
Exercise

Count the number of N-letter combinations of

MATH in which M and A appear with as many

repetitions as we like and T and H may appear

only once.

N
Coefficient by x in

2 2 2
(1+x+x +…) (1+x)
In Python:

np.flip((np.poly1d(list(ones(N+1)))**2)*(np.poly1d([1,1])**2))[N]
n
What is the coefficient of X in the expansion

of:

2 3 4 k
(1+X+X +X +X +....) ?

A solution to:

e1 + e2 + . . . + ek = n

ei≥ 0

In Python: np.flip(np.poly1d(list(ones(n+1)))**k)[n]
Recall: Distributing Money

Example: 10 coins, 4 kids (some kids get no coin case):

number of coins

=
number of kids

The number of ways to distribute n identical coins to k kids, when some kids may get none, is:

67
n
What is the coefficient of X in the expansion

of:

2 3 4 k
(1+X+X +X +X +....)
Recall the Geometric Series?
Xn – 1
1 + X1 + X2 + X3 + … + Xn-2 +
Xn-1 = X-1
1 - Xn
=
1-X
Y = 1 + X1 + X2 + X3 + … + Xn-2 + Xn-1

XY = X(1 + X1 + X2 + X3 + … + Xn-2 +
3 Xn-1)
= X + X + X + … + Xn-2 + Xn-1 + Xn
1 2

Y – XY = 1 – Xn

1 - Xn Xn – 1
Y= =
1-X X-1
1
1+X +X +X +…+X +…
1 2 3 n

= 1-X

the Infinite Geometric Series

Holds when |X| < 1


(lim xn → 0)
n→∞

Also makes sense if we view


the infinite sum on the left as
a formal power series
P(X) = 1 + X1 + X2 + X3 + … + Xn
2 + …
-X * P(X) = - X 1
- X - X3 - … - Xn - Xn+1 -

(1- X) P(X) = 1

1
 P(X) =
1-X
Formal Power Series


P(X) =  aiXi
i=0

There are no worries about convergence issues.

This is a purely syntactic object. The variable x is


only a place holder.
On convergence…

S= 1-1+1-1+1-…
S = (1 – 1) + (1 – 1) + (1 - … =
S = 1 + (- 1 + 1) 0 + (- 1 + 1) +… =
1 !!!
1 - S = 1 – (1 - 1 + 1 - 1 + 1… )
S
= 1 – 1 + 1 – 1 + 1 - 1… =
S
2 S = 1 ⇒ S = ½ !!!

so, we must be careful with infinite


sums of numbers!
Formal Power Series


P(X) =  aiXi
i=0

Also, you may think of it as the infinite list of symbol


V = [ a0, a1, a2, ..., an, … ]

But, as you will see, thinking of as a


“polynomial” is very natural.
Operations

A(X) = a0 + a1 X + a2 X2 + …
B(X) = b0 + b1 X + b2 X2 + …

adding them together

(A+B)(X) = (a0+b0) + (a1+b1) X + (a2+b2) X2 + …

like adding two lists (vectors) position-wise

[4,2,3,…] + [5,1,1,….] = [9,3,4,…]


Operations

A(X) = a0 X0 + a1 X1 + a2 X2 + …

multiplying by X

X * A(X) = 0 X0 + a0 X1 + a1 X2 + a2 X3 + …

like shifting the vector entries to the right


SHIFT[4,2,3,…] = [0,4,2,3,…]
Example
Example: Store:
V := [1,0,0,…]; V = [1,0,0,0,…]
V = [1,1,0,0,…]
Loop n times V = [1,2,1,0,…]
V := V + SHIFT(V); V = [1,3,3,1,…]

V = nth row of Pascal’s triangle


Example
Example:
V := [1,0,0,…]; PV := 1;

Loop n times
V := V + SHIFT(V); PV := PV(1+X);

V = nth row of Pascal’s triangle


Example
Example:
V := [1,0,0,…];

Loop n times PV = (1+ X)n


V := V + SHIFT(V);

V = nth row of Pascal’s triangle

As expected, the coefficients of PV give


the nth row of Pascal’s triangle
To summarize…
Given a sequence V = [ a0, a1, a2, ..., an, … ]

we can associate a formal power series with it:



P(X) =  aiXi
i=0

This is the “generating function” for V

You might also like