Discrete Mathematics: Swearword
Discrete Mathematics: Swearword
ALEXANDER BORISOVICH
Swearword
the 5 black vertices in the picture can be covered by two blue faces:
one of dimension 2 (on top), the other of dimension 1 (the vertical blue
line). The Boolean function, equal to 1 at the vertices of the top blue
face only, is simply q. The Boolean function, equal to 1 at the vertices
of the vertical blue edge, is p pq ^ r. Therefore q _ p p ^ rq (in general,
the disjunction of the conjunction monomials representing the faces) is
an economical Boolean expression for our function.
1
r
0
0 1 p
Figure 1
Note that the white vertices (defining the function spp, q, rq) can
be covered by two green edges, leading to the expression
s ” p q ^ rq _ pp ^ qq
and consequently
s” pp q ^ rq _ pp ^ qqq.
S
bulb
battery
element
R
Figure 2
p NOT
p
AND
OR s AND
q r NOT
Figure 3
p NAND
NOR
q NOR s
NOR
r
Figure 4
p
OR
AND Sum
q
AND NOT
Carry
Figure 5. Half-adder
S2 S
r
q S1 half−adder 2
p half−adder 1 C2 OR
C
C1
Figure 6. Full-adder
8 ALEXANDER BORISOVICH
p s0
0
q half−adder c
0
0 s1
p1 full−adder c
1
q
1
s2
p2 full−adder c2
q
2
s3
full−adder
2. Set theory
1. Operations with sets. By a set one means a collection of
objects of any nature. The notion is so basic that it cannot be defined
formally, simply because there are no more basic terms to rely on. The
best one can do is to say that a set S is well-defined if for any object
x in the Universe it is known whether x is an element of S (x P S) or
not (x R S). One says that T is a subset of S, T Ă S, if x P T ñ x P S.
Two sets are called equal, T “ S, if T Ă S and S Ă T , i.e. if they
consist of the same elements.
Warnings. (a) A set can be described by listing its elements, e.g.
S “ t2, 3, 5, 7u is the set of all one-digit primes. However, lists with
repeated objects, strictly speaking, don’t describe sets, since elements
of a set cannot be “repeated”: they are either there or not.
(b) One can consider the set t3, t3uu, consisting of two elements:
the integer 3 P Z, and the subset t3u Ă Z consisting of one integer,
3. Yet, the statement t3u P Z is false, since the elements of Z :“
t0, ˘1, ˘2, . . . u are numbers, not sets.
One defines the complement of a set, and the intersection, union,
difference, and symmetric difference of two sets A and B by
px P Ac q ô px R Aq
x P pA X Bq ô px P Aq ^ px P Bq
x P pA Y Bq ô px P Aq _ px P Bq
x P pB ´ Aq ô px P Bq ^ px P Aq
x P pA△Bq ô px P Aq ‘ px P Bq
Exercise. Prove the following properties of the operations:
pAc qc “ A, A△B “pA ´ Bq Y pB ´ Aq
pA X Bq “ Ac X B c ,
c
pA Y Bqc “ Ac X B c
A X pB Y Cq “pA X Bq Y pA X Cq
A Y pB X Cq “pA Y Bq X pA Y Cq
Example: Venn diagrams. Let us prove a ridiculous identity about
sets, e.g. that for any three sets A, B, C we have:
pA Y B Y Cq ´ pA△Bq “ pA X Bq Y pC ´ pA Y Bqq.
We can use the definitions of the operations and translate this into a
Boolean identity with three propositions — a : x P A, b : x P B, and
c : x P C:
pa _ b _ cq ^ pa ‘ bq ” pa ^ bq _ pc ^ pa _ bqq,
DISCRETE MATHEMATICS 11
where the left (or right) side of ” expresses the fact that x is an element
of the set written on the left (resp. right) side of “. Now we can try
to check the equivalence using truth tables. Consider, however, the
example shown on Figure 8, where A, B, and C are three discs on the
plane.
A
A
C C
B B
6. Cardinality. How to find out whether two finite sets have the
same number of elements? One way is to count their elements. In fact,
counting is the process of establishing a one-to-one correspondence with
some reference set. E.g. there are 5 business days in a week because I
can establish a one-to-one correspondence between Mo, T u, W e, T h, F r
and the fingers of my right hand. Abstractly speaking, a counting
number is the class of all those finite sets which can be put into one-
to-one correspondence with each other. By the number of elements in
a finite set we simply mean the class to which this set belongs.
The notion of cardinality extends this idea beyond finite sets. Two
sets are said to have the same cardinality (written as |X| “ |Y |, or
X Y ), if there exists a one-to-one correspondence between them. One
should check at this point that this is an equivalence relation. There-
fore all sets are partitioned into equivalence classes according to their
cardinalities.
Cardinalities of finite sets are usually denoted by non-negative inte-
gers 0, 1, 2, . . . , n, . . . , where 0 stands for the class of the empty set H.
These cardinalities (i.e. the classes of equivalence of finite sets) form a
set denoted in the book by N. It itself is infinite. Its subset t1, 2, 3, . . . u
(denoted in the book Z` ) is also infinite, and seems smaller. However
|N| “ |Z` |, since the function n ÞÑ n ` 1 from N to Z` establishes a
one-to-one correspondence.6 Sets with the same cardinality as Z` (or
N) are called countable.
Exercises. (a) Prove that the set Z of all integers (positive and
negative) is countable.
(b) Prove that the union of countably many countable sets is count-
able. (Hint: Draw the 2-dimensional table whose rows contain the lists
of elements of the given sets, and number all the items in the table by
scanning it diagonally and skipping repeated items.)
(c) Prove that the set Q of all rational numbers is countable. (Hint:
Write elements of Q as fractions m{n.)
(d) Prove that the real number line R has the same cardinality as
any segment pa, bq :“ tx P R|a ă x ă bu with a ă b. (Hint: Use
stereographic projection.)
(e) The same for pa, bq and pa, bs or ra, bs, obtained from pa, bq by
adding one of the endpoints or both. (Hint: Find a common countable
subset in pa, bq and pa, bs.)
(f) Prove that the unit interval r0, 1s and the unit square r0, 1s ˆ
r0, 1s have the same cardinality. (Hint: Encode a pair x, y of decimal
6By
the way, the official definition of an infinite set is that its cardinality should
not change when a new element is adjoined.
DISCRETE MATHEMATICS 17
3. Iterative processes
1. Induction. Consider an infinite sequence of propositions ppnq,
n “ 0, 1, 2, . . . , and suppose that the following two theorems are proved:
1˝ pp0q (the base of induction)
.
2˝ @n P N, ppnq Ñ ppn ` 1q (the step of induction).
5. Combinatorics
1. The Pigeonhole Principle says that if n ` 1 items are placed
into n compartments, then at least one of the compartments contains
more than one item. Scientifically speaking, this means that no func-
tion f : X Ñ Y is injective if |Y | ă |X| ă 8. A proof, if desired at all,
can be done by induction on the cardinality n “ |Y |. The principle is
clear when Y “ H (for there are no functions from X ‰ H to H), and
for n ą 0, if |f ´1 py0 q| ď 1, the problem is reduced to the case n ´ 1 for
the function from pX ´ f ´1 py0 qq Ñ pY ´ ty0 uq.
Exercises. (a) Show that in any group of people, there are at least
two who have the same number of friends within the group.
(b) Prove that the decimal (or binary) expansion of any fraction
m{n repeats (starting from some place) and has a period fewer than
n places. (Hint: The process of long division of m by n (let n ą m)
retraces itself as soon as the remainder repeats.)
2. Permutations, arrangements, combinations. Invertible
functions from a set to itself are called permutations. When the set
is finite (e.g. S “ t1, 2, . . . nu), the total number of such permutations
equals n! :“ n ¨ pn ´ 1q! “ n ¨ ppn ´ 1q ¨ ¨ ¨ 2 ¨ 1q. Indeed, after making
one of the n choices for f p1q, there remains n ´ 1 choices for f p2q, after
which, there remains n ´ 2 choices for f p3q, etc.9
Exercises. (a) Show that the number An,r of ordered r-tuples of el-
ements of t1, . . . , nu (they are called arrangements, or r-permutations)
is equal to n!{pn ´ rq! “ n ¨ pn ´ 1q ¨ ¨ ¨ pn ´ r ` 1q (assuming that
0 ď r ď n, of course). ` ˘
(b) Show that the number, denoted Cn,r or nr (“n choose r”),
of unordered r-tuples of elements from t1, . . . , nu (they are called r-
combinations, but are, simply speaking, r-element subsets) is equal to
n!{pn ´ rq!r!
(c) Prove that for prime p and 0 ă r ă p, the integer p!{r!pp ´ rq! is
divisible by p.
(d) Find how many times a prime p occurs in the prime factorization
of n! (Answer: tn{pu ` tn{p2 u ` ¨ ¨ ¨ where txu is the integer part, or
floor function, assigning to x P R the greatest integer not exceeding x.)
(e) An expression xr11 ¨ ¨ ¨ xrnn , where each ri is a non-negative integer,
is called a monomial in n variables px1 , . . . , xn q of total degree r “
r1 ` ¨ ¨ ¨ ` rn . Prove that the total number of monomials in n variables
9One can organize this
argument as formal induction by showing that the number
of permutations, ppnq, satisfies ppnq “ n ¨ ppn ´ 1q for all n ą 0. Can you explain,
though, why pp0q “ 1 (the base of induction)?
DISCRETE MATHEMATICS 29
` ˘
of total degree r is equal to n`r´1
r
. (Hint: Placing r ´ 1 crosses “ˆ”
in the string of n ` r ´ 1 blank spaces divides the spaces remaining
blank into r, possibly empty, groups.)
(f) An (unordered) collection of possibly repeated letters taken from
a certain alphabet is called a multiset. How many multisets of size r
can be made of n letters? What if the order of the letters also matters?
3. The binomial formula. Multiplying out n copies of the factor
px ` yq, we find
px ` yqn “px ` yqpx ` yq ¨ ¨ ¨ px ` yq
ˆ ˙ ˆ ˙ ˆ ˙ n ˆ ˙
n n 0 n n´1 n 0 n ÿ n n´k k
“ x y ` x y ` ¨¨¨` xy “ x y .
0 1 n k“0
k
But some integers are divisible by both, say, p and q, and so we have
excluded them twice. Therefore we should include them once (and the
same for the multiples of qr and rp). Thus, it seems now that the
answer for ϕpnq should be
ˆ ˙
1 1 1 1 1 1
nˆ 1´ ´ ´ ` ` ` .
p q r pq qr rp
However, some integers are divisible by all the three primes. Each of
them was excluded 3 times and then included 3 times. Excluding them
once again, we obtain
ˆ ˙
1 1 1 1 1 1 1
ϕpnq “ n ˆ 1 ´ ´ ´ ` ` ` ´
p q r pq qr rp pqr
ˆ ˙ˆ ˙ˆ ˙
1 1 1
“nˆ 1´ 1´ 1´
p q r
in agreement with the general number-theoretic formula.
Example: Stirling’s partition numbers (also known as “Stirling’s
numbers of the second kind”, and denoted Sn,r ) count the number
of partitions of the set t1, 2, . . . , nu into r non-empty subsets (in other
words, the number of equivalence relations with r equivalence classes
on a set of n elements). The number of such partitions, for which tnu is
one of the subsets, coincides with the number Sn´1,r´1 of partitions of
t1, . . . , n ´ 1u into r ´ 1 non-empty subsets. Partitions not containing
tnu as one of the subsets are obtained by dividing t1, . . . , n ´ 1u into r
non-empty subsets, and then adjoining n to one of them. The number
of such partitions is rSn´1,r . Thus, Stirling’s partition numbers satisfy
the recursion relation somewhat resembling Pascal’s:
Sn,r “ Sn´1,r´1 ` r Sn´1,r .
Yet, no simple closed formula for Sn,r is known.
Exercises. (a) Show that Sn,1 “ Sn,n “ 1, for all n ě 1, and
compute a few rows of “Stirling’s triangle”.
(b) Prove that Sn,2 “ 2n ´ 1 for all n ě 1.
32 ALEXANDER BORISOVICH
6. Probability
1. Kolmogorov’s axioms. One often encounters experiments (e.g.
rolling dice) which, even when repeated under seemingly identical con-
ditions, can lead to different outcomes. Applying the idea of probability
to such situations, one assumes that for each outcome there exists a
real number p between 0 and 1 such that the fraction of the total num-
ber of experiments which lead to this outcome tends to p when the
experiment is repeated indefinitely. While applications of probabil-
ity theory to real life situations rely on this (unverifiable) assumption,
the purely mathematical formalism of probability theory can be put on
solid logical foundations. This was done in 1933 by Andrey Nikolaevich
Kolmogorov, who proposed the following axiomatic approach.
Let S be the sample space, i.e. the set of all possible outcomes
of a given experiment. By an event one means a subset of S which
belongs to a certain collection of subsets which form a so-called σ-
algebra. To explain what it is, let us recall that all subsets of S form a
Boolean algebra with respect to the operations of complement, union,
and intersection of sets. A σ-algebra must form a Boolean subalgebra,
i.e. be closed with respect to those operations: together with each
subset A, to contain its complement Ac “ S ´ A, and together with
any finite collection of subsets, to contain their intersection and their
union. However, for the purposes of most applications of probability
theory one needs to require more: that for any countable collection of
events, their union and intersection are events too. Thus, by definition,
a σ-algebra of events must be some collection of subsets in S which
includes H, S, and is closed with respect to passing to complements,
as well as taking countable unions and intersections.
Given such a σ-algebra, Kolmogorov’s axioms require that there be
defined a function, which to each event A (i.e. a subset of S which
belongs to the given σ-algebra) associates a real number P pAq (inter-
preted as probability of the event A) in such a way that the following
properties are satisfied:
(1) 0 ď P pAq ď 1 for all events A
(2) P pHq “ 0 and P pSq “ 1
(3) For any countable collection of pairwise disjoint10 events
˜ ¸
ď8 ÿ
8
P Ai “ P pAi q.
i“1 i“1
Exercises. (a) Assuming that the die is fair, compute: Epxq; Epx2 q;
Eppx ´ Epxqq2 q.
(b) If px, yq is the outcome of a fair pair of dice, compute Epx ` yq.
(c) Let f, g : S Ñ R be two random variables. Prove that Epf `gq “
Epf q ` Epgq, and Epkf q “ kEpf q for any k P R. (These properties
establish linearity of the mean value.)
(d) Give an example showing that generally speaking, the median
value does not possess such a linearity property.
(e) Does Epf gq always coincide with Epf qEpgq?
Example: Gambler’s ruin. A gambler’s strategy is successful if the
expected dollar amount of his gain is positive. Here is a gambling game:
The gambler wins $1 each time a fair coin comes up heads, and loses
$1 each time it comes up tails. Initially he has $n, and quits when he
loses all, or when he reaches his goal of having $m (where m ě n).
Let pn be the probability that starting with $n the gambler loses (and
respectively the probability that he reaches his goal be 1 ´ pn ). We
want to determine pn (which should depend on m too).
After the first toss, the gambler has either $pn ` 1q or $pn ´ 1q with
probability 1{2 each, after which his chances of losing become pn`1 and
pn´1 respectively. Therefore pn “ pn`1 {2 ` pn´1 {2, and we get the 2nd
order linear recursion relation:
pn`1 “ 2pn ´ pn´1 .
Its characteristic polynomial λ2 ´ 2λ ` 1 “ pλ ´ 1q2 has a double root
λ “ 1. The general solution in this case has the form pn “ A`Bn where
A and B are arbitrary constants. To find them, we note that p0 “ 1
and pm “ 0, because in either case the gambler quits right away, thus
having no chance to win in the former case, and to lose in the latter.
n
From these we find A “ 1 and B “ ´1{m so that pn “ 1 ´ m . When
m ą 2n, the gambler is more likely to lose, and when m ă 2n he is
more likely to win. The expected value E of the dollar amount at the
end of the game gives a quantitative measure of his average success:
´ n¯ n
E :“ pn $0 ` p1 ´ pn q$m “ 1 ´ $0 ` $m “ $n.
m m
Thus as one might expect, on average the gambler is left with just as
much as he invested into the gambling, neither gaining nor losing.
4. Conditional probability. We talk of chances when there is
no certainty. But if new information arrives, chances can change. For
example, the probability of the event A that the sum x ` y on two
dice is at least 10, equals 1{6 (check this); but if the first die shows 5
(condition C), then the chances for x ` y ě 10 improve to 1{3. Thus,
DISCRETE MATHEMATICS 35
Indeed,
P pC X Xi q P pC|Xi qP pXi q
P pXi |Cq “ “ ,
P pCq P pCq
where, since C is the disjoint union of all C X Xj ,
ÿ ÿ
P pCq “ P pC X Xj q “ P pC|Xj qP pXj q.
j j
behind A? So, when Monty Hall opens door B, it is still true that
P pA|B opensq “ P pAq “ 1{3, and hence P pC|B opensq “ 2{3.
ϕpNq ź ˆ 1
˙
P px is relatively prime to Nq :“ “ 1´ .
N prime p|N
p
Figure 9
By definition, a graph consists of several vertices connected by several
edges, and is called connected if for any two vertices one can find a chain
of edges connecting them.
Exercises. (a) Show that if a connected graph has an Euler circuit
(i.e. a closed chain of edges containing every edge exactly once), then
the numbers of edges attached to each vertex11 are all even.
(b) For each “vertex” of Königsberg, compute the number (it is called
the degree of the vertex) of the edges attached to it, and derive that
this town does not have Euler circuits.
(c) Conversely, prove that if all vertices of a connected graph have
even degrees, then Euler circuits exist. (Hint: Show that whenever
Euler enters a vertex, he can leave it along a yet unwalked edge, until
he comes to his starting point. If more than one of such walks are
needed to include all edges, argue that the union of these walks can be
walked differently as a single circuit.)
(d) A closed circuit in a graph is called Hamiltonian, if it visits
each vertex exactly once. Show that every Hamiltonian graph with n
vertices is obtained from the regular n-gon by adding extra edges.
Remark. We should have said “is isomorphic to the graph, obtained
...” Two graphs are called isomorphic, if there exist a bijection between
the sets of their vertices, and a bijection between the sets of their edges,
such that the corresponding edges connect corresponding vertices.
11To
be accurate, if an edge is a loop, meaning that it connects a vertex to itself
(which is allowed), then the edge should be considered attached twice to this vertex.
DISCRETE MATHEMATICS 39
i j
aik p (N−1)
kj
Figure 10
Proof: Induction on N. Base: P p0q is the identity matrix A0 .
Step: For N ą 0, any N-edge-long walk starting at the vertex j and
terminating at the vertex i (Figure 10, right) can come from any of the
vertices k, and consist of one of the aik edges leading from vertex k to
vertex i, preceded by any of the pkj pN ´1q walks of length N ´1 leading
to vertex k from vertex j. The possibilities of passing
ř through different
vertices k are mutually exclusive, hence pij pNq “ k aik ¨ pkj pN ´ 1q.
Miraculously, this is the formula for the elements of the matrix product:
P pNq “ A ¨ P pN ´ 1q. By the induction hypothesis, P pN ´ 1q “ AN ´1 ,
and therefore P pNq “ AN . Q.E.D.
Exercises. (a) Write down the adjacency matrix of the graph on
Figure 10, left, and compute the numbers of walks of length 4 between
its vertices.
(b) Formulate and prove the analogue of the above theorem in the
case of undirected graphs (as in Euler’s problem). (Hint: The matrix
A in this case will be symmetric.)
(c) Use (b) and Linear Algebra to compute the total number of
closed walks of length N in the complete graph with n vertices. (In
this graph, every two distinct vertices are connected by an undirected
edge.) (Answer: pn ´ 1qN ` pn ´ 1qp´1qN .)
40 ALEXANDER BORISOVICH
H H H
H C C C H
H H C H H
Figure 11
root + level 0
. / 1
_ c d e 2
a b 3
Figure 12
Exercises. (a) Prove that if a full binary tree has k internal vertices
(i.e. not leaves), then it has k ` 1 terminal vertices (i.e. leaves). (Hint:
Show that |E| “ 2 ˆ k.)
(b) Prove that the number t of terminal vertices of any binary tree
of height h does not exceed 2h . (Hint: Complete such a tree to the full
binary tree of height h with exactly 2h leaves on the bottom level, as
in the example of Figure 12, right.)
5. Kruskal’s and Prim’s algorithms. Suppose the edges of a con-
nected graph are labeled by positive weights wpeq (e.g. the cost of trans-
portation of something between cities represented by the vertices),
ř and
one needs to find a spanning tree T of minimal total weight ePT wpeq.
Kruskal’s algorithm proposes to build the tree, TK , by adding edges
one at a time in the order of increasing weight, but skipping an edge
if it creates a circuit with the previously added ones. Prim’s algorithm
proposes to grow the tree, TP , from a root vertex, by adding one vertex
at a time and always picking the “cheepest” one among the available
choices. Figure 13, where the weights a ă b ă ¨ ¨ ¨ ă k are ordered al-
phabetically, shows the order in which the edges of the tree are added
42 ALEXANDER BORISOVICH
d 4 5 5
e a
1
j 14
h g
5 f
k 3
i 2
c b
3 1 4 2 2 3
Figure 13
Proof. (i) To have the total weight less than that of TK , a tree T
must replace edges ei of TK of weights wpe1 q ă ¨ ¨ ¨ ă wpem q ă . . .
with some edges e1i of weights wpe11 q ă ¨ ¨ ¨ ă wpe1m q ă . . . such that
wpe1m q ă wpem q for at least one m. But all edges with wpeq ă wpem q,
together with their vertices, form a subgraph where the edges of TK
with wpeq ă wpem q form a spanning forest. Therefore replacing only
m ´ 1 edges e1 , . . . , em´1 of this forest with m edges e11 , . . . , e1m will
create a circuit in T . This contradiction shows that TK is minimal.
Remark. Note that TK depends not on the weights wpeq, but only
on the total ordering of the edges by their weights. Thus, if some
weights are equal, to run the algorithm, one can perturb them a little
to remove the coincidence and yet not destroy their ordering relative to
other weights. The tree TK may depend on the perturbation, but the
total “unperturbed” weight of TK will be the same. Now we will prove
that TK coincides with TP constructed using the same edge ordering.
Figure 14
Theorem. Dijkstra’s algorithm works correctly.
Proof. We show by induction that dpvactive q is the shortest distance
from vactive to the root. Base: When the root is active, this is true.
Step: On a path from vactive P U to the root, let euv be an edge con-
necting u P U with v R U. By the induction hypothesis, the minimal
distance from (previously active) v to the root equals dpvq. Therefore
the length of the path ě dpvq ` wpeuv q ě dpuq ą dpvactive q since vactive
provides the minimum of d on U.