Professional Documents
Culture Documents
Discrete Structures in
Mathematics and Computer Science
Coursebook
Department of Mathematics
and
School of Computer Science
Contents
i
ii CONTENTS
5 Graphs 69
5.1 Introduction to graphs . . . . . . . . . . . 69
5.2 Basic terminology . . . . . . . . . . . . . . 76
5.3 Representing graphs . . . . . . . . . . . . 86
5.4 Connectivity, paths and circuits . . . . . . 89
5.5 Isomorphism of graphs . . . . . . . . . . . 94
5.6 Euler circuits and walks . . . . . . . . . . 98
5.7 Hamilton paths and cycles . . . . . . . . . 103
6 Enumeration 107
6.1 Introduction . . . . . . . . . . . . . . . . . 107
6.2 Arrangement problems . . . . . . . . . . . 107
6.3 Selections . . . . . . . . . . . . . . . . . . 109
6.4 Selections with repetitions allowed . . . . 111
6.5 Some properties of binomial coefficients . 113
6.6 The inclusion/exclusion principle . . . . . 114
6.7 The pigeonhole principle . . . . . . . . . . 116
6.8 Trickier pigeonhole applications . . . . . . 118
8 Codes 129
8.1 Prefix codes and optimal binary trees . . 129
8.2 Introduction to error-correcting codes . . 133
8.3 RSA public-key cryptosystem . . . . . . . 137
Logic and Proofs
3. “ 2 + 2 = 4.”
4. “ 2 + 3 = 7.”
1. “ 2x = 100”
Notation
1
2 TOPIC 1. LOGIC AND PROOFS
Connectives
(p ∧ q) → r
Truth
Truth tables
Using these rules, we can build up the truth table for any
compound proposition.
4 TOPIC 1. LOGIC AND PROOFS
Example 1.5 For example, here are the truth tables for
p → (q ∨ ¬r) and (p → q) ∨ (q → r).
p q r ¬r q ∨ ¬r p → (q ∨ ¬r)
0 0 0 1 1 1
0 0 1 0 0 1
0 1 0 1 1 1
0 1 1 0 1 1
1 0 0 1 1 1
1 0 1 0 0 0
1 1 0 1 1 1
1 1 1 0 1 1
p q r (p → q) ∨ (q → r)
0 0 0 0 1 0 1 0 1 0
0 0 1 0 1 0 1 0 1 1
0 1 0 0 1 1 1 1 0 0
0 1 1 0 1 1 1 1 1 1
1 0 0 1 0 0 1 0 1 0
1 0 1 1 0 0 1 0 1 1
1 1 0 1 1 1 1 1 0 0
1 1 1 1 1 1 1 1 1 1
The second style becomes more useful as the propositions
get more complicated. The column between the vertical
lines gives the truth value of the whole proposition.
Other connectives
1. p ∨ ¬p is a tautology.
2. p → p is a tautology.
3. p ∧ ¬p is a contradiction.
4. ¬(p → q) ∧ ¬p is a contradiction.
5. p → (q → r) is contingent.
6. p ∧ (q → p) is contingent.
p → q ⇔ ¬p ∨ q
⇔ q ∨ ¬p
⇔ ¬¬q ∨ ¬p
⇔ ¬q → ¬p
1. p ∧ ¬q
2. (p → q)
3. (p ∨ ¬q) ∧ (¬p ∨ r)
Logical implication
Definitions
4. An integer n is even if n ∈ A.
Theorems
Proofs
Direct proofs
We will also use the fact that every integer is either even
or odd but not both. The following theorem is proved
using a direct proof.
Law of syllogism
p: x2 − 1 = 0
r: (x + 1)(x − 1) = 0
s: x + 1 = 0 or x − 1 = 0
q: x = −1 or x = 1.
Proof by contradiction
Solution. (This proof is due to the great Euclid, who lived around
300 BC in ancient Greece.)
Assume, for a contradiction, that there are finitely many primes.
Write them as {p1 , p2 , ...pn } for some natural number n. Consider
the number q = p1 p2 ...pn +1. By its construction, q is not divisible
by any of the primes p1 , p2 , ...pn . Hence, q must itself be prime
or it must be divisible by another prime that is not in the set
{p1 , p2 , ...pn }. In either case, we find a prime that is not in our
original set. Hence we achieve our contradiction.
Proof by cases
Counterexamples
1. Tell the reader from the start what the general ap-
proach of the proof will be.
Direct proof
Proof by contraposition
Proof by contradiction
Proof by cases
To Prove: (p ∨ q) → r
Method: Prove p → r, then prove q → r
Start Cues: “We prove by cases . . . ”
Case start: “Case 1 (2, . . . )”, “In the first case”
Case end: “. . . finishes Case 1”, “So we are done in the
first case”
End Cues: “So in either case . . . ”
1. n is even.
2. n is prime
3. n = 3
4. n > 512
5. 1 + 2 + · · · + n = n(n + 1)/2
1. n = x
2. 1 + 2 + · · · + n = n(n + 1)/2 for all n
A statement like p(n) that includes the variable n is not
a proposition, since it cannot be said to be true or false
until we know what n is. There are two ways we can turn
it into a proposition. The first is to substitute in some
particular value of n, to get propositions like p(4) and
p(17). The other way is to quantify the predicate. To do
this we use one of the two quantifiers ∀ and ∃. The first
of these, ∀, is called the universal quantifier. It means
“For all ”, or “For every ”. So, for example, the
statement ∀n p(n) means “For every n, n is even”. The
second quantifier, ∃, is called the existential quantifier.
It means “For some ”, or “There exists such that”.
For example, ∃n p(n) means “There is some n such that n
is even”. So in this case ∀n p(n) is false, whereas ∃n p(n)
is true. Sometimes we make the domain of definition
more explicit: for example, we could have written these
examples as ∀n ∈ N (p(n)) and ∃n ∈ N (p(n)).
If we quantify a predicate p to get a proposition, the truth
value of a resulting proposition depends on the truth val-
ues of the various propositions p(d) for the elements d of
the domain of definition. To be precise, ∀x p(x) is true if
p(d) is true for every element d of the domain of defini-
tion of p. On the other hand, ∃x p(x) is true if p(d) is true
for at least one element d of the domain of definition.
Example 1.44
Proof:
Some implications
Example 1.45
1. ∀x∀y p(x, y) ⇔ ∀y∀x p(x, y).
2. ∃x∃y p(x, y) ⇔ ∃y∃x p(x, y).
Proof:
1. Let D and E be non-empty sets, and let p be a
predicate with domain of definition D × E. Then
∀x∀y p(x, y) is true if and only if p(x, y) is true
for every (x, y) ∈ D × E, and the same holds for
∀y∀x p(x, y). So ∀x∀y p(x, y) and ∀y∀x p(x, y) al-
ways have the same truth value.
2. Exercise.
Negating quantifiers
Proof:
1. Let D be a non-empty set and let p be a predicate
with domain of definition D. Suppose ¬∀x p(x) is
true. Then ∀x p(x) is false, in other words it is not
the case that p(x) is true for every x ∈ D. So there
must be some x ∈ D for which p(x) is false. Then
¬p(x) is true, so ∃x ¬p(x) is true.
On the other hand, if ¬∀x p(x) is false, then ∀x p(x)
is true, so p(x) is true for every x ∈ D. Thus ¬p(x)
is false for every x ∈ D, in other words there is no
x ∈ D for which ¬p(x) is true, so ∃x ¬p(x) is false.
2. Exercise.
Solution.
1. ∃x ∈ Z, ∀y ∈ Z, xy ̸= 1.
2. ∀x ∈ R, ∀y ∈ Q, xy ⩽ 1.
Integers and Divisibility
2.1 Definitions
Factors
Prime numbers
Rational numbers
2 −2 11
Example 2.1 5, 0, −3 (= 3 ), and 123 are rational
numbers.
k
Every integer k is a rational number (because k = 1 ).
21
22 TOPIC 2. INTEGERS AND DIVISIBILITY
Commutative laws:
k+m= m+k k·m= m·k
Identity laws:
k+0= k k·1= k
Distributive law:
k · (m + n) = (k · m) + (k · n)
Other properties:
k + (−k) = 0 k·0= 0
Transitivity
Anti-symmetry
n|n [reflexive]
If k | m and m | n then k | n [transitive]
If m | n and n | m then m = n [anti-symmetric]
Further exercises
a | 0 for all a ∈ Z.
If a | b and b | a then a = ±b.
If a | b and a | c then a | (b + c).
2.3. FINDING ALL FACTORS OF A POSITIVE INTEGER 23
If a | b then ac | bc.
1. Initialise k = 1;
Algorithm correctness
1. Initialise m = n;
Proof of correctness
m = 90 [initialisation];
Output k = 2, and replace m by 90
2 = 45;
Output k = 3, and replace m by 45
3 = 15;
Output k = 3, and replace m by 15
3 = 5;
Output k = 5, and stop [since k = 5 = m].
m = 63756 [initialisation];
Output k = 2, and replace m by 63756
2 = 31878;
Output k = 2, and replace m by 31878
2 = 15939;
Output k = 3, and replace m by 15939
3 = 5313;
Output k = 3, and replace m by 5313
3 = 1771;
Output k = 7, and replace m by 1771
7 = 253;
Output k = 11, and replace m by 253
11 = 23;
Output k = 23, and stop [since k = 23 = m].
Euclidean algorithm
5 = 25 − 1 · 20
= 25 − 1 · (120 − 4 · 25)
= 5 · 25 − 1 · 120.
Binary arithmetic
Integer congruence
(a − b) − (q − s)m = r − t.
a ≡ a (mod m) [reflexive]
Congruence classes
1. a + c ≡ b + d (mod m).
2. ac ≡ bd (mod m).
32 TOPIC 2. INTEGERS AND DIVISIBILITY
Induction and Recursion
3.1 Induction
Mathematical induction
P (n) → P (n + 1)
33
34 TOPIC 3. INDUCTION AND RECURSION
Important remark
Summation example
Solution.
Let P (n) denote the proposition that the sum of the first n odd
positive integers is n2 .
36 TOPIC 3. INDUCTION AND RECURSION
Basis step: P (0) is the claim that the sum of the first zero odd
positive integers is 02 = 0. This is true.
Some students may be more comfortable starting with n = 1
in this case. P (1) states that the sum of the first odd positive
integer is 12 . This is true since the sum of the first odd
positive integer is 1.
Inductive step: Show that P (n) → P (n + 1) is true ∀n ∈ Z+ .
Suppose that P (n) is true for a positive integer n; that is
1 + 3 + 5 + · · · + (2n − 1) = n2
(Note that the nth odd positive integer is (2n − 1), since
this integer is obtained by adding 2 a total number of n − 1
times to 1). We must show that P (n + 1) is true, assuming
that P (n) is true. Note that P (n + 1) is the statement that
1 + 3 + 5 + · · · + (2n − 1) + (2n + 1) = (n + 1)2
So, assuming that P (n) is true, it follows that
1 + 3 + 5 + · · · + (2n − 1) + (2n + 1)
= [1 + 3 + 5 + · · · + (2n − 1)] + (2n + 1)
= n2 + (2n + 1)
= (n + 1)2
This shows that P (n + 1) follows from P (n). Note that we
used the inductive hypothesis P (n) in the second equality to
replace the sum of the first n odd positive integers by n2 .
Since P (1) is true and the implication P (n) → P (n + 1)
is true ∀n ∈ Z+ , the principle of mathematical induction
shows that P (n) is true for all positive integers n.
1 + 2 + 22 + · · · + 2n = 2n+1 − 1
Inequality example
Divisibility examples
Factorial example
Geometric examples
Solution.
While G do B
1. Set q = 0 and r = n
2. While (r ≥ m) do
q =q+1
r =r−m
f (0) = 3,
f (n + 1) = 2f (n) + 3.
fn = fn−1 + fn−2
Initial conditions
Compound interest
Non-consecutive 0’s
Codewords
a0 = C0 , a1 = C1 , . . . , ak−1 = Ck−1 .
if and only if
Characteristic equation
Solution.
Step 1 Determine the characteristic equation by substituting
an = rn and factoring out rn
rn+2 + 2rn+1 − 3rn = 0
r2 + 2r − 3 = 0
(r + 3)(r − 1) = 0
Take r1 = −3 and r2 = 1.
Step 2 The general solution is
an = β1 (−3)n + β2 (1)n
Step 3 Using the initial values a0 = 1 and a1 = −1 we obtain
a0 = 1 = β1 + β2 (n = 0)
a1 = −1 = β2 − 3β1 (n = 1)
and this gives us β1 = β2 = 12 .
Thus an = 12 + 21 (−3)n , n ≥ 0 is the unique solution to
the given recurrence relation.
an = c1 an−1 + c2 an−2
an + 2an−1 + an−2 = 0
Cartesian product
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
a a
b b
c c
d d
e e
f f
g g
h h
47
48 TOPIC 4. RELATIONS AND FUNCTIONS
Power Set
P(A) =
4.2 Relations
(x, y) ∈ R ⇔ x < y.
Properties of relations
x≡y (mod m) ⇔ m | x − y,
is an equivalence relation.
x ∼f y ⇔ f (x) = f (y)
[x] = { y ∈ A : x ∼ y }
Solution. [1] = {1, −1}, [14] = {14, −14}, and [0] = {0}.
1. x ∼ y.
2. [x] = [y].
3. [x] ∩ [y] ̸= ∅.
1. Xi ∩ Xj = ∅ when 1 ⩽ i < j ⩽ t;
2. X1 ∪ X2 ∪ · · · ∪ Xt = X.
Solution. [1] = {1, −1}, [14] = {14, −14}, and [0] = {0}.
The graph representing an equivalence relation on a finite
set is a union of complete graphs (with loops at every
vertex).
Introduction
Poset
Notation
Comparable
Example 4.25 In the poset (P, |), are the integers 3 and
9 comparable? Are 5 and 7 comparable?
Lexicographic order
(a1 , a2 ) ≺ (b1 , b2 ),
.. .. .. .. .. .. .. .. ..
. . . . . . . . .
...
(1,7) (2,7) (3,7) (4,7) (5,7) (6,7) (7,7) (8,7) (9,7)
...
(1,6) (2,6) (3,6) (4,6) (5,6) (6,6) (7,6) (8,6) (9,6)
...
(1,5) (2,5) (3,5) (4,5) (5,5) (6,5) (7,5) (8,5) (9,5)
...
(1,4) (2,4) (3,4) (4,4) (5,4) (6,4) (7,4) (8,4) (9,4)
...
(1,3) (2,3) (3,3) (4,3) (5,3) (6,3) (7,3) (8,3) (9,3)
...
(1,2) (2,2) (3,2) (4,2) (5,2) (6,2) (7,2) (8,2) (9,2)
...
(1,1) (2,1) (3,1) (4,1) (5,1) (6,1) (7,1) (8,1) (9,1)
Figure 4.1:
(a1 , a2 , . . . , an ) ≺ (b1 , b2 , . . . , bn )
set of integers.)
(a1 , a2 , . . . , at ) ≺ (b1 , b2 , . . . , bt ), or
(a1 , a2 , . . . , at ) = (b1 , b2 , . . . bt ) and m < n,
Directed graph
Redundant edges
4 4 4
3 3 3
2 2 2
1 1 1
Figure 4.2:
Hasse diagrams
b c d d e
a a b
(a) (b)
d d
c b c
a b a
(c) (d)
Figure 4.3:
4.4. PARTIAL ORDERINGS 61
h
j
g f
d e
b c
Figure 4.1
62 TOPIC 4. RELATIONS AND FUNCTIONS
4.5 Functions
A f B
x f (x)
A f B
Figure 4.2
Example 4.45
f g
C
A B
x g(f (x))
f (x)
g◦f
Figure 4.3
Composition is associative
Figure 4.4
So f is a bijection, as required.
Preimages
f ← (y) = {x : f (x) = y}
f ← (B) = {x : f (x) ∈ B, B ⊆ T }
Examples of graphs
Note that all the roads are two-way roads (can drive from
Auckland to Hamilton or from Hamilton to Auckland
along the same road).
69
70 TOPIC 5. GRAPHS
11111111111001111111111111
00000000000 011
00000000000 Auckland
00000000000
11111111111
00000000000
11111111111 0
00000000000
11111111111
101
00000000000
11111111111
00000000000
11111111111 011111111111
00000000000
11111111111
00000000000
11111111111
1111111111111
00 11
0
00000000000 0011
00000000000
Hamilton
0
00000000000
11111111111 Tauranga
00000000000
11111111111
00000000000
11111111111
11
0
00000000000
11111111111
0
00000000000
11111111111
00000000000
11111111111
00000000000001111111111111
101
00000000000
11111111111 010
00000000000
11111111111
Marton
1010
1010
001111111111111 0011
Palmerston North
10
00000000000
Napier
1011111111111
00000000000
11111111111
1011111111111
00000000000
10
00000000000
001111111111111 1011111111111
Wellington 00000000000
10
00000000000
Multigraphs
11111111111001111111111111
00000000000 00000000000
Auckland
00000000000
11111111111
00000000000
11111111111
a
00000000000
11111111111
00000000000
11111111111 b
00000000000
11111111111 00000000000
11111111111
1111111111111
00000000000 00000000000
11111111111
d c
00
00000000000
11111111111
Hamilton
00000000000
11111111111 0011
Tauranga
00000000000
11111111111
00000000000
11111111111 00000000000
11111111111
0000000000011111111111
00000000000 e
11111111111 00000000000
11111111111
g f
00000000000001111111111111
11111111111 00000000000
10 Marton
1010
10
00111011111111111 0011
Palmerston North
10
00000000000 Napier
1011111111111
00000000000
11111111111
1011111111111
00000000000
10
00000000000
0011111111111111011111111111
00000000000
10
00000000000
Wellington
11111111111000
111
00000000000 00000000000
11111111111
Auckland
00000000000
11111111111
00000000000
11111111111
a
00000000000
11111111111
00000000000
11111111111 b
00000000000
11111111111 00000000000
11111111111
00000000000
11111111111
111
000 00000000000
11111111111 111
000
00000000000
11111111111 00000000000
11111111111
y Hamilton d c Tauranga
00000000000
11111111111
00000000000
11111111111 00000000000
11111111111
00000000000
11111111111 e
00000000000
11111111111 00000000000
11111111111
g f
00000000000
11111111111
111
000 00000000000
11111111111
000000000001011111111111
11111111111 00000000000
Marton
1010
1010
Palmerston North
111
000 1011111111111
00000000000
11111111111 111
000
Napier
1011111111111
00000000000
10
00000000000
1011111111111
00000000000
11111111111
1011111111111
00000000000
111
000 10
00000000000
Wellington
Directed graphs
000
111
11111111111
00000000000
000
11100000000000
11111111111
00000000000
11111111111
00000000000
11111111111 00000000000
11111111111
Auckland
00000000000
11111111111
00000000000
11111111111
0
1
0
1 00000000000
11111111111
00000000000
11111111111
000
111
0
1
00000000000
11111111111
00
11
00
11
000
111
00000000000
11111111111
000
111 00000000000
11111111111
00
11
000
111
Hamilton
00000000000
11111111111
00 11111111111
00
11
11
00000000000
11111111111 00000000000
00
11 Tauranga
0 11111111111
00000000000
1
00000000000
11111111111
0
1 00000000000
11111111111
0
1
0
1
00000000000
11111111111
00
11
00
11
0
1
000
11100000000000
11111111111
0
100
11
00
11
00000000000111
11111111111
00
11
00000000000000
11111111111
Marton
11
00 11
00
00
11
00
11
111
000
0000
1111
11111111111
00000000000
00
11
00 111
000
111
000
Palmerston North
1011111111111
00000000000
11111111111
10 11
00
11
00
11
11
00000000000
00
11
00
11
Napier
00
11 0
1
00000000000
11111111111
0000 00
1111
00 11
11
00
00
11
00000000000
11111111111
000
111 10
00000000000
11111111111
Wellington
Directed multigraphs
Directed pseudograph
11001100
11111111111
00000000000
00000000000
11111111111
00000000000
11111111111
Auckland
00000000000
11111111111
00000000000
11111111111 00
11
00000000000
11111111111
00
11
00000000000
11111111111
00
11
00
11
0
1
0
1
00000000000
11111111111
00000000000
11111111111
00
11
00
11
00
11
0
1
00000000000
11111111111 00
11
00000000000
11111111111
0011111111111
11
00
11
0000000000011
0
1
0 11111111111 00
1
0
0Hamilton
1 00000000000
0
1
1
0
1 Tauranga
00000000000
11111111111
00000000000
11111111111
00
11
00
11
00000000000
11111111111
00
11
00
11
0
1
0
1
000000000001100
11111111111 Marton
1
0 1
0
0
1 0
1
0011110011111111111
10 1
00000000000
0
1
0 11
00
11
00
Palmerston North
1011111111111
00000000000
11111111111
0
1
0
1
1011111111111
00000000000
0
1
0
1
Napier
00 1
11
0
1 00000000000
0
0
1
11100011111111111
00000000000
11111111111
0
1
110000000000000
10
Wellington
E = {(0, 1), (0, 2), (0, 3), (2, 2), (2, 4), (3, 4), (3, 5), (4, 0),
(5, 6), (5, 8), (6, 8), (7, 9), (8, 6), (8, 7), (9, 8)}.
Summary of terminology
Degree
G H
b c d a b c
f e g e d f
G H
b c d a b c
f e g e d f
a b c
e d f
Underlying graph
Complete graphs
K1
K2 K3 K4
K5
K6
Cycle graphs
C3 C4 C5 C6
Wheel graphs
1
0
0
1
1
0
0
1 1
0
0
1
1
0
0
1 1
0
0
1
1
0
0
1
1
0
0
1 1
0
0
1
1
0
0
1
W3 W4
1
0
0
1 1
0 1
0
1
0 0
1 0
1
0
1
1
0
0
1 1
0
0
1 1
0
0
1 1
0
0
1 1
0
0
1
1
0
0
1
1
0 1
0 1
0
0
1 0
1 0
1
W5 W6
n-Cubes
Q1 Q2 Q3
Bipartite graphs
K3,3
K2,3
Subgraphs
u
G
v z
H1 u z
H2
z
u
w
H3
u w
G1 G2
Adjacency lists
a b
c d
a b
c d
Adjacency matrices
a b
d c
a b
d c
Definition 5.15
a b c
0011 0011 0011
G h H j
a b
p
c
d e k
m n
f g
Components
a b c
d e f
Definition 5.17
a b
c G
d e
a b
c H
d e
Isomorphic graphs
a 1
9 2
e f b 8 10 3
j g
i h 7 4
d c 6 5
Invariant properties
G H
a b s t
e f w x
h g z y
d c v u
a b
G w x
H
d c
z
y
There are several ways that walks and cycles can help
determine whether two graphs are isomorphic. For ex-
ample, the existence of a cycle of a particular length is
5.5. ISOMORPHISM OF GRAPHS 97
G H
a b x
r
f c
t s
e d
G H
b w
a
v
c x
e z
d y
98 TOPIC 5. GRAPHS
0011110011111111111
graph is shown below.
00000000000
00000000000
11111111111
00000000000
11111111111
00000000000
11111111111
110011111111111
00000000000
00000000000
11111111111 1100
1100 1100
00111100
5.6. EULER CIRCUITS AND WALKS 99
a b a b
G1 e G2 e
c d c d
a b
G3
c
e d
1
0
0
1 1
0
0
1 1
0
0
1 1
0
0
1
G1 1
0
0
1
G2
1
0
0
1 1
0
0
1 1
0
0
1 1
0
0
1
1
0
0
1
G3
1
0
0
1 1
0
0
1
1
0
0
1
begin
11
00 0011
00
11 1100
a j
11
00 11
00
00
11
e
11
00 00111100 1100
i
00
11
b
00
11
00
11
d 00
11
f
11001100 1100
g
h
11
00 0011
00
11 1100
c k
102 TOPIC 5. GRAPHS
G1 G2
G3
V = {x0 , x1 , . . . , xn−1 , xn }.
A cycle
x0 , x1 , . . . , xn−1 , xn , x0
(with n > 1) in a graph G = (V, E) is called a Hamilton
cycle if x0 , x1 , . . . , xn−1 , xn is a Hamilton path.
Hamilton’s puzzle
11
00
00
11
11
00
0110 01 00111100 0110
11
00 10 01 10
00
11
0110 00 11 01 0110
0011
1010 0011 0011
01 0011
Example 5.41 Which of the simple graphs below have a
Hamilton cycle or, if not, a Hamilton path?
b c b c
G1
a g d G2 g
f e f e
G1 G2
C
111
000 111
000
B 30
30 35 25
50
A D
111
000 40
111
000
Figure 5.1:
6.1 Introduction
nr .
107
108 TOPIC 6. ENUMERATION
|A| n!
|B| = = .
n1 !n2 ! · · · nk ! n1 !n2 ! · · · nk !
WHANGAMOMONA
6.3 Selections
ψ((x1 , x2 , . . . , xr )) = {x1 , x2 , . . . , xr }.
(b) How many poker hands contain cards all of the same
suit?
Solution.
4 10
(a) 1 4
= 840.
h i
4 10 10 10 10 10 10
(b) 2 3 1
+ 2 2
+ 1 3
= 26, 550.
(a) 9 people?
(b) 5 men and 4 women?
(a) 9 men or 9 women?
Solution.
(a) 29
9
= 10, 015, 005.
(b) 5 × 13
16
4
= 3, 123, 120.
16 13
(c) 9 + 9 = 11, 440 + 715 = 12, 155.
Figure 6.1
1. 16 muffins?
2. 16 muffins with at least one of each kind?
3. 16 muffins with at least two peach and at least three
chocolate?
4. 16 muffins with no more than two broccoli?
5. 16 muffins with at least two cheese, at least three
chocolate and no more than two broccoli muffins?
Solution.
7+3−1
(a) 3−1
= 36.
(b) 4+3−1
3−1
= 15. First give $100 to each stranger and then
share out the remaining $400.
|M3 ∪ M5 ∪ M7 |
= |M3 | + |M5 | + |M7 |
− |M3 ∩ M5 | + |M3 ∩ M7 | + |M5 ∩ M7 |
+ |M3 ∩ M5 ∩ M7 |
= |M3 | + |M5 | + |M7 |
− |M15 | + |M21 | + |M35 | + |M105 |
= (333 + 200 + 142) − (66 + 47 + 28) + 9
= 543
k
X
N= |Ai |
i=1
k
X
< x
i=1
= kx
=N
Alphabet
Strings
Concatenation operation
Substrings
119
120 TOPIC 7. DETERMINISTIC FINITE AUTOMATA
Languages
We denote languages by U , V , W , L, . . ..
Boolean operations
3. The complement of U is Σ⋆ \ U .
Concatenation operation
v = σ1 . . . σn
Find-baa(v)-algorithm
The algorithm makes its transitions from one state to
another depending on the input symbol σ read. The
transition function is T : {0, 1, 2, 3} × {a, b} → {0, 1, 2, 3}
given by the following Table.
a b
0 0 1
1 2 1
2 3 1
3 3 3
a
a
b
a b
0 1 2 b 6
b a
a b
a
8
b b b
3 4 5 a 7
b
a
a
2. While i ≤ n do
(a) Set σ = σi .
(b) Set s = T (s, σ).
(c) Print s.
(d) Increment i
7.2. OPERATIONS ON LANGUAGES 123
a 3 b
a b
b a
0 1 2
b a
126 TOPIC 7. DETERMINISTIC FINITE AUTOMATA
Union automata
(1) (2)
Let M1 = (S1 , q0 , T1 , F1 ) and M2 = (S2 , q0 , T2 , F2 ),
and let two DFA recognizing L1 = L(M1 ) and L2 =
L(M2 ).
The Union Problem: Design a DFA M = (S, q0 , T, F )
that recognizes L1 ∪ L2 .
Intersection automata
(1) (2)
Let M1 = (S1 , q0 , T1 , F1 ) and M2 = (S2 , q0 , T2 , F2 ),
and let two DFA recognizing L1 = L(M1 ) and L2 =
L(M2 ).
The intersection problem: Design a DFA M = (S, q0 , T, F )
that recognizes L1 ∩ L2 .
Complementation automata
Codewords
129
130 TOPIC 8. CODES
10
01 01
01 01 01 01
01 01
By using a binary tree we have found a way to produce
codewords that have the prefix property. It remains to
find a method for assigning shorter codewords to the
more frequently used symbols.
Weighted trees
0011 0011
11
00 0011 0011 00119
00113 119 00115
00 00116 01 00116
10
0110 0110
3 5
Codes
Distance of a code
110011
Perfect codes
Public-key cryptosystems
f (M ) = M e mod n
M = (f (M ))d mod n
S = Md mod n
M = Se mod n
Selecting p, q and d
141
142 INDEX
vertex, 70 onto, 65
greatest element, 59 or (∨), 2
ordered pair, 47
Hamilton cycle, 103 out-degree, 78
Hamilton path, 103
Hamming distance, 134 partial order, 53
handshaking theorem, partition, 52
77 Pascal’s identity, 114
Hasse diagram, 58 path in graph, 90
Huffman’s algorithm, permutation, 108
132 r-permutation, 108
hypothesis, 3, 8 pigeonhole principle,
116–118
idempotent laws, 6 poset, 53
identity function, 66 power set, 48
image (function), 62, 63 predicate, 15
implication laws, 6 prefix, 119
in-degree, 78 prefix property, 130
incident edge, 76 preimage (f ← (y)), 67
inclusion/exclusion prime, 21
principle, private key, 138
114–116 product rule
inclusive or (∨), 3 (enumeration),
initial state, 121 107
injective, 65 proof by contradiction,
integers (Z), 21 11
inverse function, 66 proposition, 1
compound, 1, 4
language, 120 contingent, 5
language accepted, 123 contradiction, 5
lattice diagram, 58 simple, 1
laws tautology, 5
associative, 6 pseudograph, 72
commutative, 6 public key, 138
De Morgan’s, 6
distributive, 6 quantifier, 16
idempotent, 6 quotient, 26
implication, 6
least element, 59 r-permutation, 108
lexicographic ordering, range (function), 63
55 rational numbers (Q),
logical connectives 21
(¬, ∧, ∨, →, ↔), 2 reflexive, 49
(⊕, nand, nor), 5 remainder, 26
logical equivalence (⇔),
5 selections, 109, 112
logical implication (⇒), selections with
7 repetitions,
loop, 72 111
loop invariant, 39 set
lower bound, 61 disjoint, 107
simple graph, 70
maximal, 59 simple proposition, 1
minimal, 59 state, 121
modus ponens, 3 statement, 1
multigraph, 71 strongly connected, 93
subgraph, 83
n-cube, 81 substring, 119
natural numbers (N), surjective, 65
21 symmetric, 49
not (¬), 2
tautology, 5, 18
one-to-one, 65 total order, 54
INDEX 143