Emre Sermutlu CENG 124 Discrete Structures
Emre Sermutlu CENG 124 Discrete Structures
CENG 124
Discrete Structures
Exercise 1-1: How many integers in A = {1, 2, . . . , 300} are divisible by 3 but not
divisible by 7?
Exercise 1-2: Let Σ = {a, b, c, d, e, f, g} How many words of 5 letters can we make
a) With repetition?
b) Without repetitions?
Exercise 1-3: Let |S| = n, |T | = m. How many functions are there from S to T ? How
many are 1 − 1?
Permutation: A permutation of a finite set is an ordered list of its elements, with each
element occurring once.
n!
P (n, r) =
(n − r)!
Exercise 1-4: There are 15 girls and 18 boys in a class of 33. In how many different
ways can we choose:
a) 5 students?
b) 2 boys and 3 girls?
Exercise 1-5: In how many ways can we order a group of 7 people such that
a) A and B are next to each other?
b) A and B are not next to each other?
c) A and B are next to each other but A and C are not?
Exercise 1-6: For 5 digit integers from 10000 to 99999, how many are palindromes and
what is their sum?
Answer: 900, 49 500 000
3
Exercise 1-7: How many 11-letter words can be made from the letters of the word
ABRACADABRA?
Answer: 83160
Exercise 1-8: How many n−bit binary sequences contain exactly k zeros?
Exercise 1-9: How many paths are there from one end of the chessboard to the other,
if we can only move right and up?
Binomial Theorem: For x, y ∈ R and n ∈ N, we have:
n
X
n n r n−r
(x + y) = xy
r=0
r
n+1 n n
We can prove this using induction and = + , but we can also prove
r r−1 r
it by counting.
n
X n
Exercise 1-10: Prove that = 2n . (Three different ways)
i=0
i
Exercise 1-11: We will select 4 distinct numbers from the list {−5, −4, −3, −2, −1, 1, 2, 3, 4}.
In how many ways can we do this if their product is positive?
4 4 5 5
Answer: + + = 66
4 2 2 4
Exercise 1-12: In how many different ways can we express 72 as a sum of 2’s and 3’s if
a) Order is not important?
b) Order is important?
Solution:
a) Possible values for number of 2’s: {0, 3, 6, 9, . . . , 36}. Possible values for number of 3’s:
{24, 22, 20, . . . , 0}. So there are clearly 13 possibilities.
b) If we use n 2’s and m 3’s, and order is important, it means we are permuting n + m
objects. But n of these and m of these can be permuted among themselves, so there are
(n + m)!
n! m!
different permutations (for this case). Therefore the answer is:
35! 34! 33! 25!
1+ + + + ··· + +1
2! 33! 4! 30! 6! 27! 22! 3!
12
X (36 − k)!
=
k=0
(2k)! (36 − 3k)!
Exercise 1-13: 6 students will sit at a round table. Anıl, Sümeyye and Tahsin are in
section 1 and Bora, İpek and Efnan are in section 2. They will sit such that nobody is
next to someone in his/her own section. In how many different ways can they do that?
Solution: 2! · 3! = 12
4
Week 2– Counting - II
Objects in Boxes: If we want to place n identical objects into k distinct boxes, there
are
n+k−1
n
different ways. We can prove this by considering permutations of n objects and k − 1
dividers.
Exercise 2-1: How many numbers between 1 and 1000 have the property that their
digits sum to 5?
7
Answer: Place 5 marbles into 3 boxes,
5
Exercise 2-2: We will select 8 pizzas from 4 available types. In how many ways can we
make this choice?
Exercise 2-4: a) How many ways are there to put 14 objects in 3 boxes with at least 8
objects in one box?
b) How many ways are there to put 14 objects in 3 boxes with no more than 7 objects in
one box?
c) How many numbers between 0 and 999 have the sum of their digits equal to 20?
Answer: 84, 36, 36
Exercise 2-5: You can buy a pizza with 5 toppings. There are 8 possibilities for toppings:
Pepperoni, ham, chicken, tuna, mushroom, shrimp, onion, olives.
a) How many different orders are possible if you buy a single pizza?
b) How many different orders are possible if you buy 3 pizzas?
8
Solution: a) = 56
5
56 + 3 − 1 58
b) Distribute 3 orders to 56 pizza types: = = 30856
3 3
563
Note: The answer = 29269.33 is close, but obviously wrong. It is not even an integer!
3!
The error is that, if two among the three pizzas are the same, we are not counting this
choice 6 times. We count it only 3 times.
5
Exercise 2-6: Find the number of solutions to x1 + x2 + · · · + x13 6 10 where
each xi is integer and xi > 0
10 + 14 − 1 23!
Answer: =
10 10! 13!
Exercise 2-8: How many numbers in the set {10000, 10001, 10002, . . . , 99999} have sum
of digits equal to 20?
Solution: Distribute 20 balls to 5 containers randomly. Then subtract the cases where
one gets 10 or more. Then add cases where two gets 10.
24 5 14 5
− +
20 1 10 2
But this number includes the cases where the first digit is zero. So we have to find all
such distributions for 4 containers and subtract:
24 5 14 5 23 4 13 4
− + − − + = 4998
20 1 10 2 20 1 10 2
An easier alternative would be to put one ball to the first container, then distribute
remaining 19 randomly. This method results in:
23 4 13 14 4
− − + = 4998
19 1 9 10 1
Exercise 2-9: Find the number of different integers n such that 1000 6 n 6 9999 and
the sum of digits of n is 9.
(For example 3033, 6012, 1223 etc.)
11 12 11
Answer: = − = 165
8 9 9
Exercise 2-10: There are 7 departments in a faculty. We will choose 40 students from
the faculty, such that there will be at least 3 and at most 20 students from any department.
In how many different ways can we do this?
Solution: Give 3 to each. Distribute remaining 19 to 7 randomly, using 19 balls and 6
lines:
25!
= 177 100
19! 6!
Now subtract those cases where one gets 19 (total 22) or 18 (total 21):
6
Exercise 2-11: Consider all 4 digit numbers made from digits {1, 2, 3, 4, 5, 6, 7}.
How many have nondecreasing digits?
(1234, 1222, 4444, 5667 are possible, 1231, 7512, 3776, 5553 are NOT)
Solution: We can choose
and order:
7
4 distinct numbers in = 35 different ways. (Example: 1246)
4
7
3 distinct numbers in 3 = 105 different ways. (Example: 1244,1224,1124)
3
7
2 distinct numbers in 3 = 63 different ways. (Example: 1112, 1122,1222)
2
(6 + 5 − 1)!
= 210
6! 4!
Exercise 2-12: We have 5 Red, 5 White and 5 Blue chips. We will arrange them in a line,
but we want all reds to be on the left of all blues. For example, RRWWRWRRWBBBWBB
is acceptable, RRWBWRWRRWBBWBB is not.
How many arrangements are there?
Solution: Place 5 Reds and 5 Blue’s. Then, fill the 11 spaces with Whites randomly.
This is like distributing 5 identical objects to 11 distinct boxes.
11 + 5 − 1 15!
= = 3003
5 5! 10!
Exercise 2-13: We will distribute 40 identical balls to 10 different children such that
each child gets at least 2 and at most 20 balls. In how many different ways can we do
this?
Solution: First, give 2 balls to each of the 10 children. Distribute the remaining 20 balls
to 10 children randomly. This can be done in
20 + 10 − 1 29
=
20 20
different ways.
Among these distributions, there are 10 cases where one child gets all 20 (total number
of 22) balls and 10 · 9 = 90 cases where one child gets 19 (total number of 21) balls. So
we have to subtract these.
29
− 10 − 90 = 10 014 905
20
7
Exercise 2-14: Find the number of integer solutions of the system of inequalities
x1 + x2 + x3 + x4 + x5 + x6 + x7 6 25
15 6 x1 + x2 + x3 + x4 + x5 + x6 + x7
0 6 xi
8
Ordered Partitions: If a set has n elements and if n1 + n2 + · · · + nk = n, then there
are
n!
n1 !n2 ! · · · nk !
ordered partitions, where the ith partition has ni elements.
Exercise 2-15: There are 30 students in a classroom. The instructor will form 4 groups
of sizes 4, 7, 9 and 10, and give them homeworks from logic, sets, divisibility and counting.
In how many ways can this be done?
n!
Multinomial Theorem: The expression is called a multinomial coefficient
n1 !n2 ! · · · nk !
n
and written as .
n1 n2 · · · nk
For x1 , x2 , . . . , xk ∈ R and n ∈ N we have
X n
(x1 + x2 + · · · + xk ) = n
xn1 1 xn2 2 · · · xnk k
x +···+x =n
n1 n2 · · · nk
1 k
9
10
Week 3– Logic
P Q P ∧Q P Q P ∨Q
P ¬P 1 1 1 1 1 1
1 0 1 0 0 1 0 1
0 1 0 1 0 0 1 1
0 0 0 0 0 0
Logically Equivalent Statements: If two statements are both true or both false, we
say they are logically equivalent. For example, ¬(P ∧Q) is logically equivalent to ¬P ∨¬Q
and ¬(P ∨ Q) is logically equivalent to ¬P ∧ ¬Q.
P Q P →Q P Q P ⇐⇒ Q
1 1 1 1 1 1
1 0 0 1 0 0
0 1 1 0 1 0
0 0 1 0 0 1
11
The Laws of Logic
• ¬¬p ⇐⇒ p
• ¬(p ∨ q) ⇐⇒ ¬p ∧ ¬q
¬(p ∧ q) ⇐⇒ ¬p ∨ ¬q
• p ∨ q ⇐⇒ q ∨ p, p ∧ q ⇐⇒ q ∧ p,
• p ∨ (q ∨ r) ⇐⇒ (p ∨ q) ∨ r, p ∧ (q ∧ r) ⇐⇒ (p ∧ q) ∧ r
• p ∨ (q ∧ r) ⇐⇒ (p ∨ q) ∧ (p ∨ r), p ∧ (q ∨ r) ⇐⇒ (p ∧ q) ∨ (p ∧ r)
• p ∨ p ⇐⇒ p, p ∧ p ⇐⇒ p
• p ∨ ¬p ⇐⇒ Tautology, p ∧ ¬p ⇐⇒ Contradiction
Universal and Existential Quantifiers: ∀xP (x) means the statement P (x) is correct
for all x. ∃xP (x) means there exists at least one x for which the statement P (x) is correct.
12
Exercise 3-10: Let P be the statement n is a multiple of 4 and Q be n2 is a multiple
of 4. Which of the following are true?
a) P → Q
b) Q → P
c) P ⇐⇒ Q
d) ¬P → ¬Q
e) ¬Q → ¬P
Answer: a), e) are true, the others are false .
Exercise 3-12: Convert into logical notation: If he listens Shakira, he feels good. If
Fenerbahçe wins, he feels good. He feels good now, therefore either he is listening to
Shakira, or Fenerbahçe won today.
Is this correct?
Answer: [(p ⇒ r) ∧ (q ⇒ r)] ⇒ [r ⇒ (p ∨ q)], No
13
Fifth and seventh columns are identical.
Alternatively, we can show equivalence as:
p → (q ∨ r) ⇐⇒ ¬p ∨ (q ∨ r)
⇐⇒ (¬p ∨ q) ∨ r
⇐⇒ ¬(p ∧ ¬q) ∨ r
⇐⇒ (p ∧ ¬q) → r
(p ∨ q) → (q → q) ⇐⇒ (p ∨ q) → (¬q ∨ q)
⇐⇒ (p ∨ q) → 1
⇐⇒ ¬(p ∨ q) ∨ 1
⇐⇒ 1
Exercise 3-15: For the following statements the universe is real numbers. Are they
correct? Explain.
a) ∀x∃y(x2 + y 2 = 1)
b) ∃x∀y(x2 + y 2 = 1)
c) ∃x∃y(x2 + y 2 = 1)
Solution:
a) NO.
If x = 2 there is no y ∈ R such that x2 + y 2 = 1.
b) NO.
There is no x ∈ R such that x2 + 52 = 1.
c) YES
x = 0, y = 1 is one of the possible choices satisfying x2 + y 2 = 1.
14
Exercise 3-16: Express the following in logical notation. Is the reasoning correct?
Exercise 3-17: A student says If I miss the bus and there is an exam in the morning,
then I am in trouble. Express this in logical notation. Find the contrapositive of this
statement.
Solution:
P : I miss the bus
Q : There is an exam in the morning
R : I am in trouble
The statement is equivalent to:
(P ∧ Q) → R
Its contrapositive is:
¬R → (¬P ∨ ¬Q)
In other words, If I am not in trouble, then either I didn’t miss the bus or I did not have
an exam in the morning.
Exercise 3-18: Let m, n be positive integers. Are the following statements true or false?
(No explanation necessary)
a) ∀n ∃m [n = m2 ]
b) ∀n ∃m [m = n2 ]
c) ∃n ∃m [n = m2 ]
d) ∃n ∀m [m = n2 ]
15
Exercise 3-19: Show that [(p ∧ ¬q) ∧ r] → [(p ∧ r) ∨ q] is a tautology or provide a
counterexample.
Solution:
p q r p ∧ ¬q ∧ r (p ∧ r) ∨ q S
1 1 1 0 1 1
1 1 0 0 1 1
1 0 1 1 1 1
1 0 0 0 1 1
0 1 1 0 0 1
0 1 0 0 1 1
0 0 1 0 0 1
0 0 0 0 0 1
The statement is correct for all possible truth values of p, q, r. Therefore it is a tautology.
Exercise 3-20: Show that (p ∧ ¬q) ∧ [p → (q → r)] → ¬r is a tautology or provide a
counterexample.
Solution: We can solve this question with a truth table. Or, alternatively, we can do
this: If the implication is wrong, first part must be true, so p = 1, q = 0. The second part
must be false, so r = 1. This is the counterexample we want: (1, 0, 1).
The statement is NOT a tautology.
Exercise 3-21: Let A = {1, 3, 5, . . .} denote the set of all positive odd integers and
B = {2, 4, 6, . . .} the set of all positive even integers.
Are the following statements true or false? Explain!
Solution:
• TRUE. (Given any odd number, we can find a larger even number)
16
Exercise 3-22: Suppose that everyone who is young and healthy is happy. Are the
following claims about three different people correct? Explain.
a) A is not happy. Therefore A is not young and A is not healthy.
b) B is happy. Therefore B is young.
c) C is not happy and C is young. Therefore C is not healthy.
Solution:
a) FALSE. A is not young OR A is not healthy.
b) FALSE. B may be old.
c) TRUE. We can see this by taking the contrapositive of the given statement.
Not happy ⇒ not young OR not healthy.
Exercise 3-23: Elements in a set A may or may not have the properties P, Q, R, S. (For
example, A may be a set of integers and P denote divisible by 4 etc.) Are the following
logical inferences correct? In other words, does the conclusion follow from assumptions?
Explain your reasons.
a) If an element has property P , then it has properties Q and R. The element x has
property Q. Therefore x also has property R.
b) If an element has properties P and Q, then it has property S. The element y has
property S. Therefore y also has properties P and Q.
c) If an element has properties P, Q and R, then it has property S. The element z has
properties P, Q and S. Therefore z also has property R.
d) If an element has properties P and R, then it has properties Q and S. The element
w has property Q but it does not have the property S. Therefore w does not have the
property P or w does not have the property R.
Solution:
d. TRUE. ¬(P ∧ R) ≡ ¬P ∨ ¬R
17
Exercise 3-24: Are the following pairs of compound statements logically equivalent or
not? Explain.
a) ∀x (P (x) ∧ Q(x)) and (∀x P (x)) ∧ (∀x Q(x))
b) ∃x (P (x) ∧ Q(x)) and (∃x P (x)) ∧ (∃x Q(x))
Solution:
a) YES they are logically equivalent.
The first statement says that both P (x) and Q(x) are correct for all elements in the set.
The second statement says exactly the same thing.
b) NO they are not logically equivalent. The first statement says there is an element x for
which both P (x) and Q(x) are true. The second statement says there are two elements,
x1 , x2 such that P (x1 ) is true and Q(x2 ) is true. We do not know if x1 and x2 are the
same or not.
a. At least one of them has passed both PHYS and MATH courses.
b. At least one of them has passed PHYS course and at least one of them has passed
MATH course.
It is clear that these two statements are not equivalent. 1) may be FALSE while 2) is
TRUE.
Exercise 3-25: Are the following pairs of compound statements logically equivalent or
not? Explain.
a) ∀x (P (x) ∨ Q(x)) and (∀x P (x)) ∨ (∀x Q(x))
b) ∃x (P (x) ∨ Q(x)) and (∃x P (x)) ∨ (∃x Q(x))
Solution:
a) NO they are not logically equivalent. The first statement says P (x) or Q(x) is true for
all elements in the set. The second statement says P (x) is true for all elements or Q(x)
is true for all elements.
a. All of them have passed at least one of the two courses: PHYS or MATH.
b. All of them have passed PHYS course or all of them have passed MATH course.
It is clear that these two statements are not equivalent. 1) may be TRUE while 2) is
FALSE.
b) YES they are logically equivalent.
The first statement says that P (x) or Q(x) is correct for some element in the set. The
second statement says exactly the same thing.
18
Week 4– Induction
n
X n(n + 1)
Exercise 4-1: Show that i=
i=1
2
n
X n(n + 1)(2n + 1)
Exercise 4-2: Show that i2 =
i=1
6
Exercise 4-3: Show that a set with n elements has 2n subsets using induction.
n
X n n−i i
n
Exercise 4-5: Show that (x + y) = x y
i=1
i
19
Exercise 4-7: Show that the sum of the interior angles of a polygon of n sides is π(n−2).
(Hint: Start with the triangle and use induction)
= 8 · 73p + 73 · 92k+1
Therefore 8k+3 + 92k+3 = 73q for some integer q and the claim is correct by mathematical
induction.
4 + 10 + 16 + · · · + (6k + 4) = (k + 1)(3k + 4)
Exercise 4-16: Show that 23 9 · 23n−1 + 102n−1 for all positive integers n.
20
Solution: For n = 1, 9 · 22 + 101 = 46, 23 46.
Assume the claim is correct for n = k. Then 9 · 23k−1 + 102k−1 = 23p for some integer p.
For n = k + 1:
9 · 23(k+1)−1 + 102(k+1)−1 =
9 · 8 · 23k−1 + 100 · 102k−1
8 · (9 · 23k−1 + 102k−1 ) + 92 · 102k−1
=
8 · 23p + 4 · 23 · 102k−1
=
=
23q
for some integer q. Therefore by mathematical induction, 23 9 · 23k−1 + 102k−1
(1 + x)k > 1 + kx
21
Exercise 4-19: Show that
1
1 · 2 + 2 · 3 + · · · + n · (n + 1) = n(n + 1)(n + 2)
3
for all n ∈ Z+ .
1
Solution: For n = 1 we have 1 · 2 = 2 = 3
· 1 · 2 · 3 or 2 = 2 so the statement is correct
for n = 1.
Exercise 4-20: Prove that 4 17n − 13n for all n > 1.
Solution: Let’s use mathematical induction. The statement is correct for n = 1. Suppose
it is correct for n = k, in other words
4 17k − 13k
Now consider n = k + 1
Therefore
4 4 · 17k + 13 · 17k − 13 · 13k
In other words
4 17k+1 − 13k+1
22
Week 5– Prime Numbers, Integers
Definition: If n ∈ Z+ , n > 1 and n has exactly two divisors, 1 and n, it is called a prime
number.
Theorem: (Euclid) There are infinitely many primes.
Exercise 5-1: Write an algorithm that makes a list of prime numbers up to a given
number.
The Division Algorithm: Let n ∈ Z and m ∈ Z+ . There are unique integers q and r
such that
n = mq + r 06r<m
Definition: Let a, b be integers, not both zero. A positive integer c is called a greatest
common divisor of a and b if
1) c | a and c | b
2) If d is another common divisor of a and b, then d | c.
23
Example: m = 49, n = 19
49 − 2 · 19 = 11
19 − 1 · 11 = 8
11 − 1 · 8 = 3
8−2·3 = 2
3−1·2 = 1
We can see that gcd(49, 19) = 1 which means they are relatively prime. Now reversing
this process we obtain:
1 = 3−1·2
= 3 − 1 · (8 − 2 · 3)
= −1 · 8 + 3 · 3
= −1 · 8 + 3 · (11 − 1 · 8)
= 3 · 11 − 4 · 8
= 3 · 11 − 4 · (19 − 1 · 11)
= −4 · 19 + 7 · 11
= −4 · 19 + 7 · (49 − 2 · 19)
= 7 · 49 − 18 · 19
Thus we have found two numbers (x, y) = (7, −18) such that 49x + 19y = 1.
Exercise 5-3: Find c = gcd(111, 81). Then find x, y ∈ Z such that 111x + 81y = c.
Answer: − 8 · 111 + 11 · 81 = 3
Exercise 5-5: a) Find the greatest common divisor of 612 and 221.
b) Find two integers x, y such that 612x + 221y = c, where c = gcd(612, 221).
Solution:
a)
612 − 2 · 221 = 170
221 − 1 · 170 = 51
170 − 3 · 51 = 17
51 − 3 · 17 = 0
gcd(612, 221) = 17
24
b)
17 = 170 − 3 · 51
= 170 − 3 · (221 − 170)
= −3 · 221 + 4 · 170
= −3 · 221 + 4 · (612 − 2 · 221)
17 = 4 · 612 − 11 · 221
Exercise 5-8: There are 100 prisoners in a prison, each in a separate cell. Each pressing
of a button outside opens the door if it is locked and locks the door if it is open. On
the day of a festival, the first guard presses each button to free the prisoners. Then,
the second one presses {2, 4, 6, . . .}, the third one {3, 6, 9, . . .} etc. until the 100th guard
presses number 100. Which prisoners are free?
Exercise 5-10: Find smallest integer m such that 2m is a square and 3m is a cube.
Answer: 72
Exercise 5-11: (3, 5, 7) is a prime triple. Find another one if there is. Prove that there’s
no other if there is not.
Answer: mod 3
Exercise 5-12: You have two containers, one is a 3 liter and the other is a 5 liter
container. You have unlimited amount of water, and can transfer between the containers.
Can you obtain amounts of 1,2,4 liters? What if we begin with 3 and 6?
Exercise 5-13: Find c = gcd(128, 38). Then find x, y ∈ Z such that 128x + 38y = c.
Answer: − 8 · 128 + 27 · 38 = 2
Exercise 5-14: Determine the smallest square number that is divisible by 8!.
Solution: 8! = 27 · 32 · 5 · 7 ⇒ n = 2 · 5 · 7 · 8! = (24 · 3 · 5 · 7)2 = 16802
25
Exercise 5-15: Find the number of divisors of 51975.
Solution: 51975 = 33 · 52 · 7 · 11
It has 4 · 3 · 2 · 2 = 48 divisors.
Exercise 5-16: Are the following statements true or false? Here, x, y, z are integers.
Explain.
a) ∀x ∃y ∃z (x = 3y + 8z)
b) ∀x ∃y ∃z (x = 2y + 8z)
Solution: a) We can express the number 1 in terms of 3 and 8:
1=3·3−1·8
x = 3 · 3x − 1 · 8x ⇒ TRUE
b) We can not express an odd integer in terms of 2 and 8. For example, if x = 5, there
is no y, z, such that 5 = 2y + 8z ⇒ FALSE.
Exercise 5-17: How many zeroes are there at the end of 128! ? Explain.
Solution: For a zero, we need 10 = 5 · 2 as a factor. There are more 2’s than 5’s, in any
factorial, so we have to count 5’s. Don’t forget that 25 and multiples contain two, 125
contain three 5’s.
128 128 128
+ + = 31
5 25 125
64 − 1 · 59 = 5
59 − 11 · 5 = 4
5−1·4 = 1
4−4·1 = 0
1 = 5−1·4
= −1 · 59 + 12 · 5
= 12 · 64 − 13 · 59
So p = 12 and q = −13.
26
Week 6– Relations, Functions
R = {(s, t) | s ∈ S, t ∈ T and . . .}
Exercise 6-1: Let S = {1, 2, 3, 4, 5}. Find a relation R on S that is symmetric, transitive,
not reflexive and not anti-symmetric.
Answer: {(1, 2), (2, 1), (1, 1), (2, 2)}
[s] = {t ∈ S | t ∼ s}
27
Exercise 6-3: For the following questions, determine whether the given relation is re-
flexive, symmetric, transitive, anti-symmetric:
a) The relation R is defined on the set of lines on the plane as L1 RL2 if L1 ⊥L2 .
Answer: Symmetric
c) The relation R is defined on the set of people as p1 Rp2 if p1 and p2 have a common
parent.
Answer: Reflexive and symmetric.
d) R ⊆ Z × Z, mRn if m = −n.
Answer: Symmetric.
e) R ⊆ R × R, xRy if |x − y| < 1.
Answer: Reflexive and symmetric.
g) R is a relation on N, (m, n) ∈ R if m | n.
Answer: Reflexive, antisymmetric, transitive.
Exercise 6-4: A relation R is defined on the Cartesian plane as (x, y)R(z, w) if xw = yz.
Is this an equivalence relation?
Answer: Yes
28
Exercise 6-6: Consider the set S = {1, 2, 3, . . . , 20} on the number line. Define a re-
lation R on S as follows: two numbers x, y are related if |x − y| 6 2. For example,
(2, 3) ∈ R, (2, 4) ∈ R but (2, 5) ∈
/ R. Is this relation reflexive? Is it symmetric? Is it
antisymmetric? Is it transitive?
Exercise 6-7: Let S be the set of real numbers [−1, 1]. Consider the relation R on S
defined by:
(x, y) ∈ R if x2 + y 2 = 1
Exercise 6-8: Let S be the set of integers {2, 3, 4, 6, 8, 12, 16, 24, 48, 72, 96}. Consider
the relation R on S defined by:
(m, n) ∈ R if m | n
29
b) 2 | 4 but 4 - 2 ⇒ this relation is NOT Symmetric.
c) If m | n and n | m at the same time, then m = n ⇒ this relation is Antisymmetric.
d) If k | m and m | n then obviously k | n ⇒ this relation is Transitive.
Exercise 6-9: Consider the students in our University. Define a relation between them
as follows: If their birthdays (just the day and month) is 5 days or less apart, they are
related.
For example, suppose you were born on December 3. Then you will be related to
people born between November 28 and December 8 (inclusive). You will NOT be related
to someone born on January 10, or December 9.
a) Is this relation Reflexive?
b) Is this relation Symmetric?
c) Is this relation Anti - Symmetric?
d) Is this relation Transitive?
e) Is this relation an Equivalence relation?
Solution: a) YES, everybody is born on the same date with himself/herself.
b) YES, if x is related to y, obviously, y is related to x.
c) NO (for the same reason)
d) NO, if x is related to y and y is related to z, x is not necessarily related to z. For
example, their birthdays may be on 5th, 8th and 11th of the month.
e) NO, not transitive.
FUNCTIONS
A function f : A −→ B gives one and only one output from B for each input from A.
The set A is called domain of f .
Composition: Function composition is defined as (f ◦ g)(x) = f (g(x)).
In general f ◦ g 6= g ◦ f but (f ◦ g) ◦ h = f ◦ (g ◦ h). (Function composition is associative
but it is not commutative)
One–to–one Functions: If f (x1 ) = f (x2 ) ⇒ x1 = x2 then f is one-to one.
Onto Functions: If there exists an x ∈ A for all y ∈ B such that f (x) = y then f is
onto.
30
Exercise 6-11: Let f : B → C, g : A → B, f ◦ g : A → C. Either show that the
following propositions are true, or give counterexamples:
a) If f ◦ g is one-to-one, then, f is one-to-one.
b) If f ◦ g is one-to-one, then, g is one-to-one.
c) If f ◦ g is onto, then, f is onto.
d) If f ◦ g is onto, then, g is onto.
Theorem: Let f : A → B where A, B are finite sets with |A| = |B|. Then, the following
are equivalent:
a) f is one-to-one.
b) f is onto.
c) f is invertible.
Exercise 6-12: Let f : [0, 2] → [−4, 0], f (x) = x2 − 4x. Find the inverse of f .
√
−1
Answer: f (x) = 2 − 4 + x
Exercise 6-13: For the following questions, describe a bijection (one–to–one correspon-
dence) between the two given sets:
a) 40–bit sequences containing exactly 23 zeros and possible paths from (0, 0) to (17, 23)
consisting of right and up steps of length 1.
b) n–bit sequences and all subsets of a set containing n elements.
c) 20–bit sequences containing exactly 4 one’s and different ways of distributing 16 mar-
bles to 5 different kids.
d) 110–bit sequences with exactly 10 ones and solutions over N to the equation x1 + x2 +
· · · + x10 6 100
Exercise 6-15: Same question, this time for |A| = m and |B| = n (Number of elements).
31
Exercise 6-16: Let A = {1, 2, 3, . . . , 10} and B = {1, 2, 3, . . . , 12}. How many one-to-
one functions are there from A to B such that f (1) = 1?
11!
Answer: 1 · 11 · 10 · 9 · · · 3 =
2!
Exercise 6-17: Let A = {1, 2, 3, . . . , 10} and B = {1, 2, 3, . . . , 12}. How many functions
are there from A to B such that f (1) is even?
9
Answer: 6 · 12
50!
b) = 50 · 49 · · · 18
(50 − 33)!
c) 103 5030
Exercise 6-20: a) Find a function f : Z → Z such that f is one-to-one but not onto.
b) Find a function g : Z → Z such that g is onto but not one-to-one.
Solution: Many different solutions are possible. An example is:
a) f (n) = 2n
(
n/2 if n is even
b) g(n) =
(n + 1)/2 if n is odd
32
Week 7– Pigeonhole Principle
If a finite set S is partitioned into k sets, then at least one of them has
d|S|/ke
or more elements.
Example: A sack contains 50 marbles of 4 different colors. There must be at least 13
marbles of the same color.
Exercise 7-1: How many times must we roll a single die to get the same score n times?
Answer: 6(n − 1) + 1 = 6n − 5
Exercise 7-2: Let A be a 10–element subset of {1, 2, 3, . . . , 50}. Show that A has two
different 4–element subsets the sums of whose elements are equal.
10
Answer: Consider
4
Exercise 7-3: Show that in any set of 301 integers there must be a pair (m, n) such that
300|m − n.
Answer: Use pigeonhole principle
Exercise 7-4: We select 5 points from (the interior of) an equilateral triangle with side
length 1. Show that there are at least two whose distance is less than 1/2.
Exercise 7-5: Show that if any 14 integers are selected from the set S = {1, 2, 3, . . . 25}
there are at least two whose sum is 26.
Exercise 7-6: Assume a country has a population of 74 million. There are 100 cities
and 20 football teams in this country. Everyone lives in a city and supports one team.
We can certainly find 2 or 3 people who have the same birthday, live in the same city and
support the same team.
What is the maximum number of such people that we can always find?
Solution: There are 74 million people and 366 × 100 × 20 = 736 000 categories. If we
distribute all people there will be
74 000 000
= 101.09
736 000
people per category on average.
d101.09e = 102
By pigeonhole principle, at least one category will certainly contain 102 people.
Exercise 7-7: There are 4000 students in a university. Can we say with certainty that
there are 12 students with the same birthday? Explain.
33
Solution: NO.
366 × 11 = 4026 > 4000. Therefore it is possible to distribute 4000 people to 366 groups
such that each group contains 11 or fewer people.
Exercise 7-8: There are n > 2 students in a classroom. Prove that there will always be
two students who have the same number of friends in the class.
Note: The relation is a friend of is symmetric. In other words, if x is a friend of y
then y is a friend of x.
Exercise 7-9: A very large number of students take an examination. Their grades are
integers in the range [0, 100]. We randomly select 2015 groups of students. Each group
consists of 5 students. Show that we can always find 5 groups with exactly the same
average grade.
Solution: Note that the object of interest is the group, not the student. The possible
values for group average are:
There are 501 different possibilities. It is easier to see this if we look at the sum of group’s
grades, where the possibilities are:
This makes 501 holes and 2015 pigeons. Therefore there are
2015
= d4.02e = 5
501
Exercise 7-10: We choose n different 3-digit numbers randomly. We claim with certainty
that there are at least two among them such that their difference is a 3 digit number with
identical digits. (Such as 111,222, etc)
What can you say about n?
Solution: Consider all these n numbers modulo 111. Clearly, there are 111 categories:
{0, 1, 2, . . . , 110}. Therefore if
n > 112
there are at least two numbers in the same category.
These two numbers are different, so their difference cannot be 0. Therefore it must be
a positive multiple of 111.
Exercise 7-11: We randomly place 13 squares of side length 1 inside a circle of radius
2. Show that at least two of the squares intersect.
Solution: A circle of radius 2 has area 4π = 12.56 < 13. These thirteen unit squares
have a total area of 13. Therefore at least two squares must intersect.
34
Week 8– Inclusion and Exclusion
The Principle of Inclusion and Exclusion: Consider a set S with |S| = N and con-
ditions c1 , c2 , . . . , cm on the elements of S. The number of elements of S that satisfy none
of the conditions ci is given by:
N (c1 c2 . . . cm ) = N
−[N (c1 ) + · · · + N (cm )]
+[N (c1 c2 ) + · · · + N (cm−1 cm )]
−[N (c1 c2 c3 ) + · · · ]
..
.
m
(−1) N (c1 c2 . . . cm )
Proof: Consider an element that satisfies r of these conditions. Then, we are adding it
r r r r
r
1−r+ − + · · · (−1) = 1 + (−1)
2 3 r
Exercise 8-1: Determine the number of positive integers n where 1 6 n 6 100 and n is
not divisible by 2, 3 or 5.
Answer: 100 − 50 − 33 − 20 + 16 + 10 + 6 − 3 = 26
Exercise 8-2: Find the number of integers in {1, 2, . . . , 1000} that are divisible by 6,7
or 9.
|D6 ∪ D7 ∪ D9 | = |D6 | + |D7 | + |D9 | − |D6 ∩ D7 | − |D6 ∩ D9 | − |D7 ∩ D9 | + |D6 ∩ D7 ∩ D9 |
= |D6 | + |D7 | + |D9 | − |D42 | − |D18 | − |D63 | + |D126 |
Exercise 8-3: In how many ways can we divide 12 students into 3 sets with one set
having 2 and others having 5 students?
Answer: 8316
35
Exercise 8-4: In how many different ways can we factorize 156009, where each factor is
greater than 1 and order is not important?(Hint: 156009 = 3 · 7 · 17 · 19 · 23)
Answer: 51 = 15 + 25 + 10 + 1
Exercise 8-5: In how many ways can three x’s, three y’s and three z’s be arranged so
that no consecutive triplet of the same letter appears?
9! 7! 5!
Answer: −3 + 3 − 3! = 1314
3! 3! 3! 3! 3! 3!
Exercise 8-6: How many numbers from the set S = {1000, 1001, . . . , 9999} have at least
one digit 0, at least one 8 and at least one 9?
4 3 3 4 4 3 4
Answer: 9000 − 9 − 8 · 9 − 8 · 9 + 8 + 8 + 7 · 8 − 7 = 150
Exercise 8-7: In how many different ways can we distribute 15 identical marbles to 5
different children such that each child has at least one and at most 4 marbles?
14 5 10 5 6
Answer: − + = 101
10 1 6 2 2
Exercise 8-8: On a graph, we will connect 5 distinct vertices with 4 edges such that no
vertex will be isolated. In how many different ways can we do this?
10 5 6 5 3 5 1 5 0 5 0
Answer: 2 − 2 + 2 − 2 + 2 − 2 = 768
1 2 3 4 5
Exercise 8-9: Six married couples will be seated at a circular table. In how many ways
can we do this such that no wife sits next to her husband?
Answer: 12 771 840
Derangements
In a derangement, nothing is in its natural place.
Exercise 8-10: Find the number of permutations of n such that no number is in its
correct place for n = 2, 3, 4, 5.
Answer: 1, 2, 9, 44
36
Exercise 8-11: A secretary mails 10 letters without checking the envelopes for correct
address. What is the probability that all letters are sent to wrong locations?
10 10 10 1
Answer: 10! − 9! + 8! + · · · + 0! /10! ≈
1 2 10 e
9 9 9 9
9! − 9 · 8! + · 7! − · 6! + · · · + · 1! − · 0!
2 3 8 9
If we divide this by the total number of distributions 9!, we obtain
1 1 1 1 1
1−1+ − + ··· + − ≈
2! 3! 8! 9! e
1
The case where at least one gets correct paper has probability ≈ 1 − e
Exercise 8-13: How many numbers from the set S = {1, 2, . . . , 1000} are divisible by at
least one of the numbers 3, 5 or 11?
Answer: 333 + 200 + 90 − 66 − 30 − 18 + 6 = 515
Exercise 8-14: Consider all strings of 4 letters made of letters {v, w, x, y, z}. Some
examples are xyvw, xxyy, zyvz. How many of them contain at least one x, at least one y
and at least one z?
4 4 4 4
Answer: 5 − 3 · 4 + 3 · 3 − 2 = 84
Exercise 8-15: Turkish alphabet has 29 letters, {A, B, C, . . . , Z}. Clearly, there are 29!
permutations of this set. How many of these permutations do not contain any of the
words AYŞE, MERT, NUR or CANSU?
Solution: The number of permutations is:
37
Exercise 8-16: 8 students, Nilay, Özgür, Arda, Cansu, Kübra, Merve, Mert and Gizem
will sit around a circular table.
38
Week 9– Stirling Numbers
Exercise 9-1: We will form 3 teams from a set of 5 students. In how many different
ways can we do this? (Students are distinct, teams are not)
m\n 1 2 3 4 5 6
1 1
2 1 1
3 1 3 1
4 1 7 6 1
5 1 15 25 10 1
6 1 31 90 65 15 1
If we construct this table’s a few rows carefully, we will notice that
Exercise 9-3: In how many ways can we distribute 7 distinct problems to 4 distinct
students?
4 7 4 7 4 7 4 7
Answer: 4 − 3 + 2 − 1 = 8400
4 3 2 1
39
Exercise 9-4: There are 9 students in a class, Ayça, Batuhan, Çag̃atay, Damla, Elif,
Turaç, Ug̃ur, Volkan, Zehra. We will form 4 project groups from these students. Each
group may contain at least 1 and at most 6 students. In how many different ways can we
do this if
a) Projects are identical?
b) Projects are distinct?
Solution: We can think of this problem as the number of onto functions from a 9 element
set to a 4 element set.
Extend the Stirling table up to 9th level using the formula
S(m, n) = S(m − 1, n − 1) + nS(m − 1, n):
m\n 1 2 3 4 5 6
1 1
2 1 1
3 1 3 1
4 1 7 6 1
5 1 15 25 10 1
6 1 31 90 65 15 1
7 1 63 301 350 140 21 1
8 1 127 966 1701 1050 · · ·
9 1 255 3025 7770 · · ·
So:
a) 7770
b) 4! 7770 = 186 480
Second Method:
We can distribute students to subsets as follows:
8 6
1+2+3+3: 9 /2 = 2520 ways
2 3
9 7 5
2+2+2+3: /3! = 1260 ways
2 2 2
9 5
1+1+3+4: = 1260 ways
4 3
9 5 3
1+2+2+4: /2 = 1890 ways
4 2 2
9 4
1+1+2+5: = 756 ways
5 2
9
1+1+1+6: = 84 ways
6
Total is: 2520 + 1260 + 1260 + 1890 + 756 + 84 = 7770 different ways.
40
Exercise 9-5: 6 students, A, B, C, D, E, F go to a cafe where there are 3 empty tables.
They take seats such that no table is left empty. In how many different ways can they do
this? (Tables are identical)
Solution:
6 3 6 3 6
3 − 2 + 1
1 2
= 90
3!
Exercise 9-6: 5 students, A, B, C, D, E go to a cafe where there are 3 empty tables. One
is near the cashier, the second is in the middle and the third is near the window. They
take seats such that no table is left empty. In how many different ways can they do this?
Solution:
35 − 3 · 25 + 3 · 15 = 150
5 3
5 2 2
+ = 25
3 2!
25 · 3! = 150
Exercise 9-7: How many positive integers n less than 900 satisfy gcd(n, 900) = 1?
Solution:
900 900 900 900 900 900 900
900 − − − + + + −
2 3 5 2·3 2·5 3·5 2·3·5
= 240
41
Exercise 9-8: How many words of seven letters composed of the set {a, b, c, d, e} contain
all five symbols?
Solution:
7 5 7 5 7 5 7 5 7
5 − 4 + 3 − 2 + 1
1 2 3 4
= 16800
OR
5 7!
{a, b, c, d, e} + two identical letters, for example a, a : = 4200
1 3!
5 7!
{a, b, c, d, e} + two distinct letters, for example a, b : = 12600
2 2!2!
Exercise 9-10: We will form 3 teams from 10 distinct workers. In how many different
ways can we do this? (Each team contains at least one worker)
Solution: Using Stirling numbers on the table:
42
Exercise 9-11: 6 people on an air plane have identical suitcases. On exit, they take
one randomly. Aslı and Kardelen make a bet on how many of them will get their own
suitcases.
• Kardelen thinks that, exactly one person will get his/her own suitcase.
The number of different cases where Kardelen wins is 6 times the derangements of 5:
" #
5 5 5 5 5
6 · 5! − · 4! + · 3! − · 2! + · 1! − · 0! = 6 · 44 = 264
1 2 3 4 5
265 264
Probabilities are: = 0.3681 for Aslı and = 0.3667 for Kardelen.
6! 6!
Exercise 9-12: There are 8 chairs in a classroom. 6 students attend a lecture. After the
break, the same 6 students come to the same class and choose chairs randomly.
How many arrangements are there where exactly 2 students sit on their previous chairs?
6
Solution: Choose two students in = 15 different ways. The rest is an inclusion-
2
exclusion problem. We have 4 items and 6 containers. Each container can contain at
most 1 item. Item 1 can NOT be placed in container 1 etc.
4 4 4 4
6·5·4·3− 5·4·3+ 4·3− 3+ = 181
1 2 3 4
Exercise 9-13: An online dating service will match the women A,B,C with the men
U,V,W,X,Y,Z.
Women’s constraints are:
43
Men have no constraints. How many matchings are possible?
Solution: We will use inclusion-exclusion to count the distributions as follows:
6 · 5 · 4 − 3 · 5 · 4 + 3 · 4 − 1 = 71
Exercise 9-14: An online dating service will match the women A,B,C with the men
U,V,W,X,Y,Z.
Women’s constraints are:
6 · 5 · 4 − 5 · 4 − 2 · 5 · 4 − 3 · 5 · 4 + 4 + 2 · 4 + 4 · 4 − 1 = 27
5 · 4 · 3 − 4 · 3 − 2 · 4 · 3 + 3 = 27
44
Week 10– Generating Functions
∞
X 1
(−1)n xn = 1 − x + x2 − x3 + · · · =
n=0
1+x
∞
X 1
x2n = 1 + x2 + x4 + x6 + · · · =
n=0
1 − x2
∞
X 1
(n + 1)xn = 1 + 2x + 3x2 + · · · =
n=0
(1 − x)2
∞
1 X 1
(n + 2)(n + 1)xn = 1 + 3x + 6x2 + · · · =
2 n=0
(1 − x)3
Exercise 10-2: In how many ways can we distribute 10 identical balls to three different
children A, B, C such that A gets 1 or 2, B gets 3 or more, C gets an even number of
balls?
Answer: 7, The problems are equivalent
45
Exercise 10-3: In how many ways can we distribute 12 oranges to A, B, C such that A
gets at least 4, B gets at least 2 and C gets between 2 and 5?
Answer: 14
x
Exercise 10-5: Determine the coefficient of x10 in the expansion of
(1 + x2 )(1 − x)
Exercise 10-6: In how many ways can we fill a bag with n fruits subject to the following
constraints?
Apples 6 6 4 4 2 2 0 0
Bananas 0 0 0 0 0 5 5 5
Oranges 0 1 3 2 4 0 2 1
Pears 1 0 0 1 1 0 0 1
1 1 − x5
1 1
Answer: 2 5
(1 + x) = 2
= 1 + 2x + 3x2 + · · ·
1−x 1−x 1−x (1 − x)
So the answer is simply n + 1.
Exercise 10-7: In how many different ways can we distribute n apples to A, B, C such
that A gets 0 or 1, B gets 0 or 2, C gets a multiple of 4?
Answer: 1. The result is independent of n
46
Exercise 10-8: In how many different ways can we distribute 75 pieces of chocolate to
5 different children such that each child gets at least 6 and at most 24?
Solution: The generating function is:
(1 − x19 )5
= x30
(1 − x)5
∞
30 19 38 57 76 95
X n+4
= x (1 − 5x + 10x − 10x + 5x − x ) xn
n=0
n
49 30 11
Coefficient of x75 is: −5 + 10 = 78151
45 26 7
Second Method: Give 6 to each, distribute remaining 45 balls randomly. Subtract cases
where one child gets 19 or more. Add cases where two children gets 19 or more, to obtain:
49 30 11
−5 + 10 = 78151
45 26 7
Exercise 10-9: We are distributing 33 pieces of identical bonibons to Nilay, Ahmet Can,
Mehmet, Cihan and Büşra with the following constraints:
1 1 − x6 1
= (1 + x2 )x7 (1 + x)
1 − x 6 1 − x 1 − x4
x7 (1 + x)(1 + x2 )
=
(1 − x)(1 + x2 )(1 − x)(1 + x)
x7
=
(1 − x)2
= x7 (1 + 2x + 3x2 + 4x3 + · · · )
Coefficient of x33 is 33 − 6 = 27. Therefore there are 27 different ways.
47
Exercise 10-10: We will buy a total of 100 identical laptop computers from 5 distinct
stores A, B, C, D, E such that
x 1 − x11 x7 1
= 2
(1 + x)
1−x 1−x 1−x 1−x
x8 − x19
=
(1 − x)4
∞
8
X
19n+3 n
= (x − x ) x
n=0
n
Coefficient of x10 :
10 1
− =9
9 0
48
Exercise 10-12: We will order 6 pizzas. There are 3 types available: Vegetarian, pep-
peroni and margarita. We want at most 3 vegetarian, at most 4 margarita and at least 2
pepperoni. In how many different ways can we order?
Solution:
(1 + x + x2 + x3 ) (1 + x + x2 + x3 + x4 ) (x2 + x3 + · · · )
1 − x4 1 − x5 x2
=
1−x 1−x 1−x
x2 − x6 − x7 + x11
=
(1 − x)3
∞
2 6 7 11
X n+2
= (x − x − x + x ) xn
n=0
n
Coefficient of x6 :
6 2
− = 14
4 0
• Sergen gets 0 or 2.
• Utku gets 0 or 1.
• Yılmaz gets 0 or 1.
1 1 1 1 − x3
· · · · (1 + x2 ) · (1 + x) · (1 + x)
1 − x2 1 − x3 1 − x4 1 − x
After cancellations, we obtain:
∞
1 X n+2 n
= x
(1 − x)3 n=0
n
49
Exercise 10-14: How many integer solution are there for the equation
x1 + x2 + x3 + x4 = 21
• 0 6 x1
• 3 6 x2 6 8
• 0 6 x3 6 4
• 7 6 x4
1 x3 (1 − x6 ) (1 − x5 ) x7
=
1−x 1−x 1−x 1−x
x10 (1 − x5 − x6 + x11 )
=
(1 − x)4
∞
10 15 16 21
X n+3 n
= x −x −x +x x
n=0
n
50
Week 11– Exponential Generating Functions
Exercise 11-1: In how many ways can four of the letters of the word ENGINE be
arranged? (Here, order is important)
Answer: 102
We can obtain the same result by finding the coefficient of x4 /4! in the expansion of:
2
x2
1+x+ (1 + x)2
2!
Exercise 11-2: A ship carries flags of the colors red, white, blue, black. Twelve of these
are placed on a vertical pole.
a) How many such signals use an even number of blue flags and an odd number of black
flags?
11
Answer: 4
b) How many of the signals have at least three white flags or no white flags at all?
12 11 10
Answer: 4 − 12 · 3 − 66 · 3
Exercise 11-4: A set has 10 elements. We will partition it into 4 subsets such that each
subset will have an odd number of elements. In how many different ways can we do this?
4
1 ex − e−x
Answer: 5440, Use generating function:
4! 2
51
Exercise 11-5: There are 32 houses on a street. We will paint each house with one of
the six colors: White, blue, red, yellow, green or orange. For each color, the number of
houses painted must be even.
In how many different ways can we do that?
Solution: The exponential generating function is:
6 6
x2 x4 ex + e−x
1+ + + ··· =
2! 4! 2
x32 1 32
Coefficient of is: 6 + 6 · 432 + 15 · 232
32! 32
Exercise 11-6: Consider all words of length 10 made of the letters in the set {a, b, c, d, e, f, g}.
How many contain at least one a, at least one b and at least two c?
Solution: Define conditions as:
= N − N (P ) − N (Q) − N (R)
+N (P Q) + N (P R) + N (QR)
−N (P QR)
2 2 4
x2 x3 x3 x2 x3
x
x+ + + ··· + + ··· 1+x+ + + ···
2! 3! 2! 3! 2! 3!
x10
The coefficient of is:
10!
52
Exercise 11-7: We will form a 7-letter sequence using the letters A, B, C. Each letter
must appear an odd number of times or zero times. How many possible sequences are
there?
Solution: Order is important ⇒ we have to use exponential generating functions.
3
ex − e−x
(1 + x + x3 + x5 · · · )3 = 1+
2
x7
In the Taylor series expansion of this function, the coefficient of the term is:
7!
1 9 + 37
9 + 9 + 6 · 27 − 6 · 27 + 37 + 37 =
8 4
Exercise 11-8: We will form 16 letter sequences using the letters {x, y, z} with the
following conditions:
x16
Coefficient of the term in the series expansion is:
16!
1 16 16 16 · 15 14 16 · 15 · 14 13
15
3 + 1 − 2 − 16 · 2 − ·2 − ·2 = 17 951 649
2 2! 3!
53
Exercise 11-9: We will form 16 letter sequences using the letters {x, y, z} with the
following conditions:
x16
Coefficient of the term in the series expansion is:
16!
1 16
3 − 1 − 216 − 16 · 215 = 21 228 448
2
54
Week 12– Computational Complexity
Growth of Functions: Given two increasing functions, how can we tell which one is
increasing faster?
Big-Oh: Let f (n), g(n) be two functions. We say f (n) = O(g(n)) (f is Big-Oh of g) if
0 6 f (n) 6 cg(n)
for all n > n0 . In other words, f (n) = O(g(n)) if g(n) times a constant is greater than
f (n) for sufficiently large n values.
for all n > n0 . In other words, if f (n) = O(g(n)) and g(n) = O(f (n)) then we say
f (n) = Θ(g(n)).
b) Θ(n3 )
c) Θ(n!)
55
Exercise 12-7: Rank the following functions according to their growth rates:
√ √
n, n!, 2n , n2 , n3 , log n, n log n, n, n n, 3n , nn , 1
Exercise 12-8: Rank the following functions according to their growth rates:
n
2
√ 2 3/2 3
f (n) = n + 2n n, g(n) = n log n − n log n, h(n) = 1 + 2n +
2
Solution: f = O(g), g = O(h). In other words for sufficiently large n we have f < g < h.
Exercise 12-9: Rank the following functions according to their growth rates:
√
n + n2 , n! + en , n n, 4n , (n + 1)n
Solution: √
n n < n + n2 < 4n < n! < (n + 1)n
Exercise 12-10: Rank the following functions according to their growth rates:
Solution:
log(n2 ) < n log n < e(n+2) < nn < (2n)!
56
For the following questions, write an algorithm in pseudo-code. Then,
find the asymptotic complexity of the algorithm:
Exercise 12-13: Sort N numbers by finding the smallest in the list and placing it as the
first:
Solution:
For i = 1 to N
index = i
For j = i + 1 to N
If A(j) < A(index)
index = j
EndIf
EndFor
Swap(A(i), A(index))
EndFor
57
Exercise 12-14: Given an array of n elements, check whether all elements are distinct.
Return False if any two are the same.
Solution:
INPUT: Vector A
n = size(A)
For i = 1 to n − 1
For j = i + 1 to n
If A(i) == A(j)
Return False
EndIf
EndFor
EndFor
Return True
Exercise 12-15: Find the time the following algorithm takes based on input size n. In
other words, find Θ class. You may assume n is a power of 2 for simplicity.
Solution:
INPUT n
S = 0; k = 1
While k 6 n
For j = 1 to n
S =S+1
EndFor
k =2∗k
EndWhile
Answer: Θ(n log2 n)
Exercise 12-16: Find the time the following algorithm takes based on input size n. In
other words, find Θ class. You may assume n is a power of 2 for simplicity.
Solution:
INPUT n
S = 0; k = 1
While k 6 n
For j = 1 to k
S =S+1
EndFor
k =2∗k
EndWhile
Answer: Θ(n)
58
Exercise 12-17: The following program checks if all elements of the given vector are
distinct. It returns False if any two are the same. What is the asymptotic growth rate?
(Consider worst case)
Solution:
INPUT: Vector A
n = size(A)
For i = 1 to n − 1
For j = i + 1 to n
If A(i) == A(j)
Return False
EndIf
EndFor
EndFor
Return True
(n − 1)n
Solution: (n − 1) + (n − 2) + (n − 3) + · · · + 2 + 1 = = Θ(n2 )
2
Exercise 12-18: Find the time the following algorithm takes based on input size n. In
other words, find its Θ class.
Solution:
INPUT n
Sum = 0
For i = 1 to n
For j = 1 to i2
Sum = Sum + j
EndFor
EndFor
n(n + 1)(2n + 1) 2n3 + 3n2 + n
Solution: 12 + 22 + 32 + · · · + n2 = =
6 6
Exercise 12-19: Find a function g(n) such that f (n) = O(g(n)) and g(n) = O(h(n))
but f (n) 6= Θ(g(n)) and g(n) 6= Θ(h(n)).
In other words, g(n) should increase faster that f (n) but slower than h(n). For
example: f (n) = n2 , h(n) = n4 ⇒ g(n) = n3
a) f (n) = ln n, h(n) = n
b) f (n) = 2n , h(n) = nn
c) f (n) = n2 + n, h(n) = n2 + en
Solution: Many answers are possible, some examples are:
√
a) n
b) n! or 3n or n2n
c) n3 or n4 or 2n
59
Exercise 12-20: We know that Fonk1(n) is an algorithm of complexity Θ(n) and
Fonk2(n) is an algorithm of complexity Θ(n2 ). Find the complexity of the following
algorithm:
INPUT n
For i = 1 to 3n
Fonk1(i)
Fonk2(i)
EndFor
For j = 1 to n
Fonk1(n)
EndFor
Solution:
2 2 2
1 + 2 + · · · + 3n + 1 + 2 + · · · + (3n) + n + · · · + n = Θ(n2 ) + Θ(n3 ) + Θ(n2 )
= Θ(n3 )
60
Week 13– Homogeneous Recurrence Relations
We say that a sequence is defined recursively if some finite set of values, usually the
first few are specified, and the remaining values are defined in terms of the previous ones.
For example, the factorial function defined as: Factorial(0) = 1,
Factorial(n) = Factorial(n − 1) is recursive.
Fibonacci sequence is another example.
Exercise 13-1: Solve the first order recurrence relation an+1 = 2an , a0 = 7.
n
Answer: 7 · 2
Exercise 13-5: How can we obtain the general formula for Fibonacci sequence, given
that Fn = Fn−1 + Fn−2 ?
" √ !n √ !n #
1 1+ 5 1− 5
Answer: √ −
5 2 2
n n n−1
Assume Fn = x . In that case,
√ x =x + xn−2 and x2 − x − 1 = 0. The solution of
1± 5
this equation gives x1,2 = , therefore the general solution is Fn = c1 xn1 + c2 xn2 . We
2
can determine c1 and c2 from the initial conditions F1 = 1, F2 = 1.
61
Exercise 13-7: Solve an+2 = an+1 + an , a0 = 0, a1 = 4.
Exercise 13-8: (Repeated Root) Solve the recurrence relation an+2 = 4an+1 −4an , where
a0 = 1, a1 = 3
n n−1
Answer: an = 2 + n2
Exercise 13-9: (Repeated Root) Solve the recurrence relation 9an+2 = 42an+1 − 49an ,
where a1 = 21, a2 = 98
n
7
Answer: an = 9n
3
Complex Roots
For the following questions, remember the formulas:
p y
x + iy = reiθ , where r = x2 + y 2 , and θ = arctan
x
Exercise 13-11: Solve the recurrence relation an+2 = 2an+1 − 2an , where a0 = 1, a1 = 2
√ n
nπ nπ
Answer: an = ( 2) cos + sin
4 4
Exercise 13-12: Solve the recurrence relation an+2 = 3an+1 − 3an , where a0 = 2, a1 = 3
√ n
nπ
Answer: an = 2( 3) cos
6
n
n 1
Answer: an = 3(−4) + 6
2
Exercise 13-14: Solve the recurrence relation an+2 −2an+1 +an = 18, a0 = 7, a1 = 8
for n > 0.
Solution: x2 − 2x + 1 = 0 ⇒ x = 1 (Double Root)
62
ahn = c1 1n + c2 n1n = c1 + c2 n
apn = A n2 1n = A n2
an = c1 + c2 n + 9n2
n=0 ⇒ c1 = 7,
n=1 ⇒ c2 = −8
an = 7 − 8n + 9n2
= 3n (9n − 4)
√
n 2 1 3
b) an = kr ⇒ r +r+1=0 ⇒ r=− ± i
2 2
√ !n √ !n
1 3 1 3
an = k1 − + i + k2 − − i
2 2 2 2
√ √ √
k1 + k2 = 4, k1 − k2 = 2 3i ⇒ k1 = 2 + 3i, k2 = 2 − 3i
√ 2πni √ −2πni
an = (2 + 3i) e 3 + (2 − 3i) e 3
√
2πn 2πn
= 4 cos − 2 3 sin
3 3
Exercise 13-16: Solve the recursion relation an = 10an−1 − 18an−2 with the initial
conditions
a0 = 4, a1 = 20.
Solution: x2 − 10x + 18 = 0
63
√
x=5± 7
√ √
an = c1 (5 + 7)n + c2 (5 − 7)n
n = 0 ⇒ 4 = c1 + c2
√ √
n = 1 ⇒ 20 = c1 (5 + 7) + c2 (5 − 7)
⇒ c1 = c2 = 2
h √ √ i
an = 2 (5 + 7)n + (5 − 7)n
Exercise 13-17: Solve the recursion relation 2an+2 − 8an+1 + 7an = 0 with the initial
conditions
a0 = 16, a1 = 32.
Solution: 2x2 − 8x + 7 = 0
√
2
x=2±
2
√ !n √ !n
2 2
an = c 1 2+ + c2 2−
2 2
n = 0 ⇒ 16 = c1 + c2
√ ! √ !
2 2
n = 1 ⇒ 32 = c1 2+ + c2 2 −
2 2
⇒ c1 = c2 = 8
" √ !n √ !n #
2 2
an = 8 2+ + 2−
2 2
64
Week 14– Recurrence Relations and Algorithms
Exercise 14-1: Let Σ = {0, 1, 2}. Let an denote the number of n-bit strings that contain
an even number of zeros. Clearly, a0 = 1, a1 = 2, a2 = 5. What is an in general?
We can obtain an n-bit sequence with an even number of zeros as follows: Take an
(n − 1)–bit sequence with an even number of zeros, add 1 or 2. Or, take an (n − 1)–bit
sequence with an odd number of zeros and add 0. This means:
3n + 1
an = 2an−1 + (3n−1 − an−1 ) ⇒ an =
2
Exercise 14-2: Let Σ = {a, b}. How many words of length n are there that do not
contain aa?
Let’s call this number sn . Clearly, s0 = 1, s1 = 2, s2 = 3. The last two letters of our
word must be ab or bb or ba. To obtain an n letter word that does not contain aa, we can
add b to an n − 1 letter word, or we can add ba to an n − 2 letter word. So
sn = sn−1 + sn−2
√ √ !n √ √ !n
5+3 5 1+ 5 5−3 5 1− 5
sn = +
10 2 10 2
Exercise 14-3: In how many different ways can we express n as a sum of 1’s and 2’s,
where order is important?
√ √ !n √ √ !n
5+ 5 1+ 5 5− 5 1− 5
Answer: an = an−1 + an−2 , an = +
10 2 10 2
Exercise 14-5: For n > 1 let an be the number of ternary strings (containing 0,1,2) of
length n that do not contain 11 or 22. Find and solve a recurrence relation for an . (Hint:
Consider those ending in 00 separately)
√ n+1 √ n+1
1
Answer: an = 2an−1 + an−2 , an = (1 + 2) + (1 − 2)
2
Exercise 14-6: There are n houses around a circular street. We will color them using 5
distinct colors such that any two adjacent houses will have different colors. In how many
different ways can we do this? (The recurrence relation is correct for n > 2)
n n
Answer: an+2 = 3an+1 + 4an , an = 4 + 4(−1)
Exercise 14-7: Let A = {0, 01, 011, 111}. For n > 1, find the number of binary strings
of length n made from elements of A.
65
√
4 n 3 2nπ 3 2nπ
Answer: an = an−1 + an−2 + 2an−2 , an = 2 + cos + sin
7 7 3 21 3
Exercise 14-8: Let an be the number of regions in the plane obtained by drawing n
lines. For example, a1 = 2, a2 = 4, a3 = 7.
(Lines are not parallel, three lines do not intersect at a point)
a) Find a recurrence relation for an .
b) Find an .
Solution:
a) an = an−1 + n
b) x − 1 = 0 ⇒ ahn = c
apn = An2 + Bn
1 1
⇒ A= , B=
2 2
n2 + n n2 + n
apn = ⇒ an = +c
2 2
a1 = 2 ⇒ c=1
n2 + n + 2
an =
2
Exercise 14-9: In how many different ways can we express 100 as a sum of 5’s and 10’s
if order is important?
Solution: an = an−5 + an−10
x10 − x5 − 1 = 0
√
1 ± 5
x5 =
2
√ √ !n/5 √ √ !n/5
5+ 5 1+ 5 5− 5 1− 5
a5 = 1, a10 = 2 ⇒ an = +
10 2 10 2
a100 = 10946
66
Or, alternatively, divide by 5 and transform the problem to obtaining 20 using 1’s and
2’s.
Exercise 14-10: The number of laptops manufactured in a factory was 10000 in the first
year, 16000 in the second year, and the average of previous two years after that. How
many laptops were produced on the nth year?
an−1 + an−2
Solution: an =
2
2x2 − x − 1 = 0
1
x = 1 or x = −
2
n
n 1
an = c1 (1) + c2 −
2
c2
10000 = c1 −
2
c2
16000 = c1 +
4
n
1
an = 14000 + 8000 −
2
Exercise 14-11: An algorithm makes T (n) operations to solve a problem of input size
n. Find an explicit formula for T (n) if
a) T (n) = T (n/2)
b) T (n) = 2T (n/2)
c) T (n) = aT (n/b)
(Hint consider the transformation n = 2m )
Nonhomogeneous Recurrence Relations
Consider the second order nonhomogeneous relation
• Solve the homogeneous version of the equation an + pan−1 + qan−2 = 0. Call the
solution ahn .
67
Exercise 14-12: Solve the recurrence relation an+2 − an+1 − 6an = 14 · 5n , a0 = 5, a1 =
10 for n > 0.
7 n 13 n n
Answer: an = (−2) + 3 +5
5 5
Exercise 14-13: Solve the recurrence relation an+2 −5an+1 +4an = 6·4n , a0 = 2, a1 = 6
for n > 0.
2 n 4 1 n
Answer: an = 4 + + n4
3 3 2
Exercise 14-14: Solve the recurrence relation an+2 − 16an+1 + 64an = 2 · 8n+2 ,
a0 = 1, a1 = 12 for n > 0.
1
Answer: an = 1 − n + n 8n 2
2
Exercise 14-16: Solve the recurrence relation an+2 − 4an+1 + 3an = −200, a0 =
3000, a1 = 3300 for n > 0.
n
Answer: an = 100 · 3 + 2900 + 100n
68
Exercise 14-17: Solve the recurrence relation
3
c1 + c2 + 2 = 3, 3c1 + c2 + 8 = 14 ⇒ c1 = 3, c2 = −2
2
n
n 3
an = 3 · 3 − 2 + 2 · 4n
2
16 n 2
A(n + 2)2n+2 − 4An2n = ·2 ⇒ A=
3 3
4 2 7
c1 + c2 = 3, 2c1 − 2c2 + = −2 ⇒ c1 = , c2 =
3 3 3
7 2
an = (−2)n + (n + 1) 2n
3 3
69
Exercise 14-19: Solve the recurrence relation
ahn = c1 + c2 2n apn = A n
c1 + c2 = 4, c1 + 2c2 = 7 ⇒ c1 = 1, c2 = 3
an = 1 + 5n + 3 · 2n
3
A(n + 2)2 7n+2 − 14A(n + 1)2 7n+1 + 49An2 7n = −42 · 7n ⇒ A=−
7
c1 = 1, −3 + 7 + 7c2 = 11 ⇒ c2 = 1
3 2 n
an = 1 + n − n 7
7
n
n 1
Answer: an = 4(−6) + 9
3
70