CS112 Week5
CS112 Week5
a.
b.
c.
d.
2
Distributing Money
(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:
+ + +…+ =
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?
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.)
19
Pigeonhole principle:
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:
1 2 3
X + X + X
( + + )( + )=?
Plan
Ans: 10 2
There is a correspondence between paths
(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)
= (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,
© M. Reha Civanlar 31
Problem
a) 15
b) 11
c) 13
d) 9
no apple
5 choices
4x3/2 = 6 choices
Total: 11 choices
Counting with Generating
Functions
2
Take the coefficient of x .
11
The function f(x) that has a polynomial
expansion
n
f(x)=a0 + a1 x + …+ an x
sequence
a0, a1, …, an
24
If the polynomial (1+x+x )
a) 3
b) 5
c) 6
d) 9
Counting with Generating
Functions
= 1+3x+6x2+9x3+11x4+11x5+9x6+6x7+3x8+x9
multiples of two.
Counting with Generating
Functions
= 1+2x+4x2+5x3+6x4+6x5+5x6+4x7+2x8+x9
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 )
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
x1+x2+x3+x4=21
0 ≤ xk ≤7
x1+x2+x3+x4 = 21
0 < xk< 7
constraints...
x1+x2+x3+x4 = 21
0 < xk< 7
Observe
0 < xk < 7
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
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
print(p(3))
print(np.polyval(ppar, 3))
Multiplying Polynomials in
Python
The product of f(z) = f0 + f1z + f2z + … +
2
print(poly3)
5 4 3 2
4 x + 11 x + 8 x + 9 x + 18 x + 10
The pizza party
OzU.
like…
The pizza party.
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 + …)
poly1 = np.poly1d([1,1,1,1])
poly2 = np.poly1d([1,0,1,0,1])
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 +…
sequence
a
k 0
k x k
A famous generating function from
calculus:
k
x
e
x
k 0 k !
We can use the generating
life so far.
Exercise
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
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
1
P(X) =
1-X
Formal Power Series
P(X) = aiXi
i=0
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 = ½ !!!
P(X) = aiXi
i=0
A(X) = a0 + a1 X + a2 X2 + …
B(X) = b0 + b1 X + b2 X2 + …
A(X) = a0 X0 + a1 X1 + a2 X2 + …
multiplying by X
X * A(X) = 0 X0 + a0 X1 + a1 X2 + a2 X3 + …
Loop n times
V := V + SHIFT(V); PV := PV(1+X);