Académique Documents
Professionnel Documents
Culture Documents
Mathematics
and
Department of
Computer Science
Discrete Structures in
Mathematics and Computer Science
Lecture/Study Guide
Contents
4 Graphs 39
4.1 Introduction to graphs . . . . . . . . . . . 39
4.2 Basic terminology . . . . . . . . . . . . . . 45
4.3 Representing graphs . . . . . . . . . . . . 52
4.4 Connectivity, paths and circuits . . . . . . 56
4.5 Euler paths . . . . . . . . . . . . . . . . . 63
4.6 * Isomorphism of graphs . . . . . . . . . . . 68
4.7 * Hamilton paths and circuits . . . . . . . . 74
4.8 * Shortest path problems . . . . . . . . . . 78
i
6 Induction and Recursion 99
6.1 Induction . . . . . . . . . . . . . . . . . . 99
6.2 The second principle of mathematical in-
duction . . . . . . . . . . . . . . . . . . . 103
6.3 Loop invariant theorem . . . . . . . . . . 104
6.4 Recursive definitions . . . . . . . . . . . . 105
6.5 * Recurrence relations . . . . . . . . . . . . 106
6.6 * Solving recurrence relations . . . . . . . . 108
6.6.1 Solving linear homogeneous recur-
rence relations . . . . . . . . . . . 109
7 Trees 113
7.1 Introduction to trees . . . . . . . . . . . . 113
7.2 Some applications of trees . . . . . . . . . 119
7.3 Properties of trees . . . . . . . . . . . . . 121
7.4 Expression trees . . . . . . . . . . . . . . 124
7.5 Spanning trees . . . . . . . . . . . . . . . 125
8 Functions 131
8.1 Functions . . . . . . . . . . . . . . . . . . 131
8.2 Equality of functions . . . . . . . . . . . . 132
8.3 Function composition . . . . . . . . . . . 132
8.4 Partial functions . . . . . . . . . . . . . . 133
8.5 Types of functions . . . . . . . . . . . . . 134
9 Enumeration 137
9.1 Introduction . . . . . . . . . . . . . . . . . 137
9.2 Arrangement problems . . . . . . . . . . . 137
9.3 Selections . . . . . . . . . . . . . . . . . . 139
9.4 Selections with repetitions allowed . . . . 142
9.5 Some properties of binomial coefficients . 144
9.6 The inclusion/exclusion principle . . . . . 145
9.7 The pigeonhole principle . . . . . . . . . . 146
9.8 * Trickier pigeonhole applications . . . . . . 149
12 Codes 163
12.1 Prefix codes and optimal binary trees . . 163
12.2 Introduction to error-correcting codes . . 167
12.3* Gray codes . . . . . . . . . . . . . . . . . 170
12.4* RSA public-key cryptosystem . . . . . . . 172
Index 177
Logic and Proofs
1. “ 2x = 100”
2. “ 2x = 100 for some integer x”
3. “Shake well before opening”
4. “CompSci 225 is the best course at the University
of Auckland”
5. “John Key is the Prime Minister of New Zealand”
Notation
1
Connectives
(p ∧ q) → r
Solution.
¬r → (t ∧ ¬p)
(¬t ∨ p) → r
1.2 Truth values
Truth
Truth tables
Using these rules, we can build up the truth table for any
compound proposition.
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
Solution.
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
0 0 1 1 1 1
0 1 1 0 1 1
1 0 0 1 0 0
1 1 1 0 1 0
p → q ⇔ ¬p ∨ q
⇔ q ∨ ¬p
⇔ ¬¬q ∨ ¬p
⇔ ¬q → ¬p
1. p ∧ ¬q
2. (p → q)
3. (p ∨ ¬q) ∧ (¬p ∨ r)
4. (p ∧ ¬q ∧ ¬r) ∨ ¬(q ∨ ¬r).
Logical implication
Definitions
Theorems
Proofs
Direct proofs
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.
If and only if statements
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 contradiction
Proof by cases
To Prove: (p ∨ q) → r
2.1 Definitions
Factors
Prime numbers
Rational numbers
2 −2 11
Example 2.2 5, 0, −3 (= 3 ), and 123 are rational
numbers.
k
Every integer k is a rational number (because k = 1 ).
15
2.2 Basic properties of integers
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).
• If a | b then ac | bc.
1. Initialise k = 1;
Algorithm correctness
1. Initialise m = n;
Proof of correctness
• m = 90 [initialisation];
90
• Output k = 2, and replace m by 2 = 45;
45
• Output k = 3, and replace m by 3 = 15;
15
• Output k = 3, and replace m by 3 = 5;
• m = 63756 [initialisation];
63756
• Output k = 2, and replace m by 2 = 31878;
31878
• Output k = 2, and replace m by 2 = 15939;
15939
• Output k = 3, and replace m by 3 = 5313;
5313
• Output k = 3, and replace m by 3 = 1771;
1771
• Output k = 7, and replace m by 7 = 253;
253
• Output k = 11, and replace m by 11 = 23;
Euclidean algorithm
Binary arithmetic
(a − b) − (q − s)m = r − t.
• a ≡ a (mod m) [reflexive]
• If a ≡ b (mod m) then b ≡ a (mod m) [symmetric]
• If a ≡ b (mod m) and b ≡ c (mod m) then a ≡ c
(mod m) [transitive]
These are easy to prove from the definition of congruence
mod m (viz. leaving the same remainder on division by
m).
Congruence classes
1. a + c ≡ b + d (mod m).
2. ac ≡ bd (mod m).
3.1 Sets
A = {2, 4, 6, 8}
A = {6, 8, 4, 2}
A = {2, 4, 2, 6, 2, 8}
A = { 2x : x ∈ B }
Notation
27
P The set of positive integers, {1, 2, 3, . . . }.
(This notation is not standard; some authors de-
note this as N or Z≥1 .)
m
{ : m ∈ Z, n ∈ P }.
n
R The set of real numbers.
Example 3.1
{ x ∈ R : x2 − 5x + 6 = 0 } = .
(a) Is A = B?
(b) Is A = C?
Solution.
(a) Yes. We need to show that A ⊆ B and B ⊆ A.
Take a ∈ A. Then a = 2p for some p ∈ Z. If we set
q = p − 1, then q ∈ Z and a = 2p = 2(q + 1) = 2q + 2 so
a ∈ B.Therefore, A ⊆ B.
Next, take b ∈ B. Then b = 2q + 2 for some q ∈ Z. If we set
p = q + 1, then p ∈ Z and b = 2(p − 1) + 2 = 2p − 2 + 2 = 2p.
So b ∈ A. Therefore, B ⊆ A.
Since A ⊆ B and B ⊆ A, we have A = B.
(b) No. 4 ∈ A since 4 = 2(2). But 4 ∈ / C since if it were, then
4 = 3r − 1, so r = 35 ∈
/ Z. So 4 ∈
/ C, and A 6= C
Power Set
P(A) =
We can form new sets from old ones using the operations
of union, intersection and complement. Given two sets
A and B, we define
A ∪ B = { x : x ∈ A or x ∈ B }.
A ∩ B = { x : x ∈ A and x ∈ B }.
A \ B = { x : x ∈ A and x ∈
/ B }.
Universal set
A = {x ∈ U : x ∈
/ A }.
A ∩ (B ∩ C) = (A ∩ B) ∩ C Associative
A ∪ (B ∪ C) = (A ∪ B) ∪ C
A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) Distributive
A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)
A∩A = A Idempotent
A∪A = A
A \ (B ∩ C) = (A \ B) ∪ (A \ C)
A \ (B ∪ C) = (A \ B) ∩ (A \ C)
(B ∩ C) = B∪C De Morgan’s
(B ∪ C) = B∩C
Proof of laws
A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C).
A ∩ (B ∪ C) ⊆ (A ∩ B) ∪ (A ∩ C). (∗)
(A ∩ B) ∪ (A ∩ C) ⊆ A ∩ (B ∪ C).
A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C),
as required.
Cardinality
Solution. |A| = 6
|B| = 5
|A \ B| = |{2, 11, 13}| = 3
|B \ A| = |{1, 9}| = 2
|A ∪ B| = |{1, 2, 3, 5, 7, 9, 11, 13}| = 8
|A ∩ B| = |{3, 5, 7}| = 3
3.3 Ordered pairs, Cartesian products
(x1 , x2 , . . . , xn ) = (y1 , y2 , . . . , yn ) ⇔ xi = yi
for i = 1, 2, . . . , n.
Cartesian product
A × B = { (a, b) : a ∈ A, b ∈ B }.
{1, 2, 3, 4, 5, 6, 7, 8} × {a, b, c, d, e, f, g, h}
of a 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
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
Example 3.16
Proof:
Some implications
1. ∀x p(x) ⇒ ∃x p(x).
Proof:
3. Exercise.
Negating quantifiers
Proof:
2. Exercise.
Example 3.18 Re-write these propositions without using
any negation symbols.
1. ¬∀x ∈ Z, ∃y ∈ Z, xy = 1.
2. ¬∃x ∈ R, ∃y ∈ Q, xy > 1.
Solution.
1. ∃x ∈ Z, ∀y ∈ Z, xy 6= 1.
2. ∀x ∈ R, ∀y ∈ Q, xy 6 1.
Graphs
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).
39
1100110011111111111
11111111111
00000000000 0111111111111
00000000000
00000000000
11111111111 0
1
00000000000
0000000000011111111111
11111111111 1010
Auckland
00000000000
00000000000
11111111111
00000000000
11111111111 00000000000
11111111111
0
1
00000000000
11111111111
00
11
0011111111111
00000000000
11 0
1
00000000000
11111111111 11001100
00000000000
11111111111
Hamilton
0000000000011111111111
11111111111 0
1
00000000000
11111111111
101
00000000000
Tauranga
00000000000
11111111111
00000000000
11111111111 011111111111
00000000000
11111111111
0
1
00000000000
00
11
0000000000011001011111111111
11111111111 00000000000
1010 Marton
1010
1100110011111111111 00000000000
1011111111111
Palmerston North 11001100
1011111111111
00000000000
Napier
1011111111111
00000000000
10
00000000000
1100110011111111111 1011111111111
Wellington
00000000000
10
00000000000
Example 4.2 Now suppose the above graph is not a
graph of roads, but of fibre optic cables between some
computer data centres in these towns. Each data cen-
tre is represented by a vertex, and each optical cable by
an edge.
Multigraphs
001111111111111
11111111111
00000000000 00000000000
Auckland
00000000000
11111111111 00000000000
11111111111
0000000000011111111111
11111111111
a
00000000000 b
00000000000
11111111111 00000000000
11111111111
1111111111111
00000000000 00000000000
11111111111
d c
00
00000000000
11111111111
Hamilton
00000000000
11111111111 0011
Tauranga
00000000000
11111111111 00000000000
11111111111
0000000000011111111111
11111111111 00000000000 e
00000000000
11111111111 00000000000
11111111111
g f
00000000000
11111111111
00000000000001111111111111
11111111111 00000000000
00000000000
1011111111111
1010
Marton
10
00111011111111111 0011
Palmerston North
1011111111111
00000000000
10
00000000000
Napier
1011111111111
00000000000
11111111111
10
00000000000
001111111111111 1011111111111
00000000000
10
00000000000
Wellington
00000000000
11111111111 00000000000
11111111111
0000000000011111111111
11111111111 00000000000
Auckland
00000000000
11111111111
a
00000000000
11111111111 00000000000
11111111111
00000000000
11111111111
b
000
111
00000000000
11111111111
000
111 00000000000
11111111111 000
111
000
111
y
00000000000
11111111111 00000000000
11111111111
Hamilton d
0000000000011111111111
11111111111 00000000000
c Tauranga
00000000000
11111111111
00000000000
11111111111
g 00000000000
11111111111
00000000000
11111111111
e
f
000
111
00000000000111
11111111111
00000000000000
1011111111111
1010 Marton
1010
000
111
000
111 1011111111111
00000000000
11111111111 000
111
000
111
Palmerston North
1011111111111
00000000000
1011111111111
00000000000
Napier
10
00000000000
000
111 1011111111111
00000000000
11111111111
10
00000000000
000
111 Wellington
Directed graphs
00000000000
11111111111
00000000000
11111111111
0
1
00000000000
11111111111
0
1 00000000000
11111111111
00000000000
11111111111
00
11
000
111
00000000000
11111111111
000
111 00
11 00
11
00
11
00
11
00 11111111111
000
111
00000000000
000
111
Hamilton
00000000000
11111111111
00 11111111111
11
11
00000000000
11111111111 00000000000
00
11
00000000000
11111111111
Tauranga
00
11
0
1
00000000000
11111111111
0 11111111111
0
1
1
00000000000
11111111111 00000000000
0
1
0
100
11
00000000000
11111111111
00
11
00000000000111
000
00
11 00
11
11111111111
00
11
000
11100000000000
11111111111
Marton
11
00 11
00
00
11
00
11 00
11
00
11
111
000
000
111 00 111
1011111111111
00000000000
11111111111
00
11 000
00 111
1011111111111
00000000000 000
11
00
11
Palmerston North 00
11 Napier
10 11
00000000000
11
00
00
11
00
11
0000
1111
00
11 1011111111111
00000000000
11111111111
00
11
00000000000
00
11
000
111 10
00000000000
11111111111
Wellington
Directed multigraphs
00000000000
11111111111
00000000000
11111111111 00
11
00
11
00000000000
11111111111
00000000000
11111111111
00
11
00
11
0
1
0
1
00000000000
11111111111
00000000000
11111111111 0
1
1100110011111111111
0
1
0
1
0
1
00
11
0 11111111111
00000000000
00000000000 00
11 11001100
0 11111111111
00000000000
00000000000
11111111111
1Hamilton
0 1 0
1
0
1 1
Tauranga
00000000000
11111111111
00
11
00000000000
11111111111
00
11
00
11
00000000000
11111111111
0
1
0
1
00
11
000000000001100
11111111111
0
1
Marton
1
0 1
0
0
1
0
1 0
1
0
1
0011110011111111111
0 11
1011111111111
00000000000
0
1 001100
Palmerston North
00
11 1011111111111
00000000000
1
0
1
00000000000
0
1
0
1
Napier
0
1 10 1
00000000000
11111111111
0
11100011111111111
0
1
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)}.
Degree
G H
b c d a b c
f e g e d f
a b c
e d f
Underlying graph
K1
K2 K3 K4
K5
K6
Cycle graphs
C3 C4 C5 C6
n-Cubes
Q1 Q2 Q3
Bipartite graphs
K3,3
K2,3
G1 G2
Adjacency lists
c d
a b
c d
Adjacency matrices
a b
d c
a b
d c
Incidence matrices
e3 e6
v5 v4
Incidence matrices can also be used to represent multi-
ple edges and loops. Multiple edges are represented in
the incidence matrix using columns with identical en-
tries, since these edges are incident with the same pair
of vertices. Loops are represented using a column with
exactly one entry equal to 1, corresponding to the vertex
that is incident with this loop.
e2
e7 e6 e5
v4
v5
e8
Definition 4.12
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 4.14
a b
c G
d e
a b
c H
d e
G
a b
d c
11
00
00
11
Euler circuits and paths
a b a b
G1 e G2 e
c d c d
a b
G3
c
e d
Example 4.29 Which of the directed graphs below have
an Euler circuit? Of those that do not, which have an
Euler path?
1
0 1
0 1
0 1
0
0
1 0
1 0
1 0
1
1
0
0
1 G2
G1 0
1
1
0 1
0 1
0 1
0
0
1 0
1 0
1 0
1
1
0
0
1
G3
1
0
0
1 1
0
0
1
1
0
0
1
begin
11
00
the same point?
00
11
a 00111100
j
00
11 11
00 11
00 00111100 1100
00 110011001100
e i
11
b
d 00
11
f
11001100 1100
g
h
11
00 00111100
00
11
c k
G1 G2
G3
G
H
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 paths and circuits can help
determine whether two graphs are isomorphic. For ex-
ample, the existence of a simple circuit of a particular
length is a useful invariant that can be used to show that
two graphs are not isomorphic. In addition, paths can be
used to construct mappings that may be isomorphisms.
Example 4.38 Determine whether the graphs shown be-
low are isomorphic.
G H
a b x
r
f c
t s
e d
z y
G H
b w
a
v
c x
e z
d y
* Subgraphs
v z
H1 u z
H2
z
u
w
H3
u w
V = {x0 , x1 , . . . , xn−1 , xn }
Hamilton’s puzzle
0011
0110 00111100 00111100 0110
00
11
00
11 01 01 01
1010 11001100 1100 11001100 10
10
1100
1010 0011 11001100
01 0011
Example 4.42 Which of the simple graphs below have a
Hamilton circuit 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 4.1:
200 1:00 80
340
0110 Whangarei
1:20
0110 Whangarei
190
0110 Whangarei
180
0110 0110 0110
45 min 75
0110
120
Hamilton
400
0110
30 min
Hamilton
1:30 0110
50
Hamilton
210
210 1:05 85
1010 1010 1010
150 40 min 65
Wellington Wellington Wellington
01 01 01
140 30 min 60
900
10 Picton
3:05
10 Picton
400
10 Picton
0110 Christchurch
0110 Christchurch
0110 Christchurch
10 Dunedin
01 Dunedin
01 Dunedin
00111100b 3 00111100c
2
4
a 00111100 z
11
00 3
2 1
00111100e
0011
d 3
Dijkstra’s Algorithm
procedure Dijkstra(G:weighted connected simple graph,
with all weights positive)
{G has vertices a = v0 , v1 , . . . , vn = z and weights
w(vi , vj ) where w(vi , vj ) = ∞ if {vi , vj } is not an
edge in G}
for i := 1 [to] n
L(vi ) := ∞
L(a) := 0
S := ∅
{The labels are now initialized so that the label of a is
zero and all other labels are ∞, and S is the empty
set}
while z 6∈ S
begin
u := a vertex not in S with L(u) minimal
S := S ∪ {u}
for all vertices v not in S
if L(u) + w(u, v) < L(v) [then]
L(v) := L(u) + w(u, v)
{This adds a vertex to S with minimal label and
updates the labels of vertices not in S}
end { L(z) = length of shortest path from a to z}
00111100b 00111100c
weighted graph displayed below.
5
6
4
a0
00111100 z
0011 1 8 2
2
3
00111100
d0011 10
e
5.1 Introduction
(x, y) ∈ R ⇔ x < y.
Properties of relations
83
Example 5.3 Consider the relation = on any set A.
Which of the above properties hold?
x≡y (mod m) ⇔ m | x − y,
is an equivalence relation.
Example 5.11 Let G = (V, E) be a simple graph. Con-
sider the binary relation on V such that, for u, v ∈ V ,
u R v if and only if there is a path from u to v. This is
an equivalence relation, which we call connectivity.
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] 6= ∅.
Proof: We show that (1) implies (2), (2) implies (3)
and (3) implies (1).
(2) ⇒ (3) Suppose [x] = [y]. Then [x] ∩ [y] = [x] ∩ [x] =
[x]. By reflexivity, x ∼ x, so x ∈ [x], so [x] 6= ∅.
Thus [x] ∩ [y] 6= ∅.
1. Xi ∩ Xj = ∅ when 1 6 i < j 6 t;
2. X1 ∪ X2 ∪ · · · ∪ Xt = X.
Solution. [1] = {1, −1}, [14] = {14, −14}, and [0] = {0}.
Introduction
Poset
Notation
Comparable
Example 5.21 In the poset (P, |), are the integers 3 and
9 comparable? Are 5 and 7 comparable?
Lexicographic order
(a1 , a2 ) ≺ (b1 , b2 ),
Figure 5.1:
(a1 , a2 , . . . , an ) ≺ (b1 , b2 , . . . , bn )
(a1 , a2 , . . . , at ) ≺ (b1 , b2 , . . . , bt ), or
(a1 , a2 , . . . , at ) = (b1 , b2 , . . . bt ) and m < n,
where ≺ in this inequality represents the lexicographic
ordering of S t . In other words, to determine the ordering
of two different strings, the longer string is truncated to
the length of the shorter string, namely, to t = min(m, n)
terms. Then the t-tuples made up of the first t terms of
each string are compared using the lexicographic ordering
on S t . One string is less than another string if the t-tuple
corresponding to the first string is less than the t -tuple
of the second string, or if these two t-tuples are the same,
but the second string is longer. The verification that this
is a partial ordering is left as an exercise.
Directed graph
Redundant edges
4 4 4
3 3 3
2 2 2
1 1 1
Figure 5.2:
Hasse diagrams
a a b
(a) (b)
d d
c b c
a b a
(c) (d)
Figure 5.3:
Example 5.36 Let S be a set. Determine whether there
is a greatest element and a least element in the poset
(P(S), ⊆).
h
j
g f
d e
b c
Figure 5.1
Relational Calculus
Complement relation
Converse relation
Solution. R ({12}) = {... − 36, −24, −12, 0, 12, 24, 36, ...}
R← ({12}) = {±1, ±2, ±3, ±4, ±6, ±12}
6.1 Induction
Mathematical induction
P (n) → P (n + 1)
99
How to write a proof by induction
Important remark
Summation example
Solution.
Let P (n) denote the proposition that the sum of the first n odd
positive integers is n2 .
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
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
Compound interest
Non-consecutive 0’s
Codewords
Characteristic equation
an = c1 an−1 + c2 an−2
Solution.
an = c1 an−1 + c2 an−2
an + 2an−1 + an−2 = 0
Nikolaus
(1623−1708)
113
Since a tree cannot have a simple circuit, a tree cannot
contain multiple edges or loops. Therefore any tree must
be a simple graph.
11
00
00
11 00111100 11001100 00111100 11001100 00111100 11001100
11
00
00
11 00111100 11001100 00111100 11001100 00111100 11001100
11
00
00
11 00111100 00111100 11001100 00111100 11001100 00111100
(a) (b) (c) (d)
Forests
11
00
00
11 00111100 00111100
11
00
00
11 00111100 11001100 00111100 00111100
11001100 00111100 00111100 00111100
Rooted trees
00111100
a
0011
c
11
00
00
11
b 11001100
c 00111100 d
0011
a
0011
e
11
00 00111100 1100 00111100 0011
00
11 1100
b
f g e d
Root a
0110 0110
f g
Root c
a 0110
b 0110 1010 0110 g
f
11
00
01 01 11
00 01
c h i j
01
d 01
e 01
k 01
l 01
m
01 01
01 01 01 01 01
1100
01 01 01 01 01 01 0011 01
11
00
00
11 00111100
01 01 01
(Full) Binary tree Full 3-ary tree
01
01 01
01 01 01 01 01 01
01 01
3-ary tree
00111100 a
00111100
b 11
00
00
11
c
11
00
00
11
d 00111100 00111100h 00111100 i
e
11
00
00
11 00111100 11001100 11
00
00
11 11
00
1100
l
f g j k
00111100
m
H H
H C H H C H
H H
H C H
H C C C H
H C H
H H H
H C H
Isobutane
Butane
0011 bin
11
00 rje
11
00
ard 0011 0011
ple khr
Counting leaves
00111100 a
11
00
00
11
b 11001100
c 00111100 d
11
00
00
11
e 11
00 00
11
1100 0011
f g 11001100
h
00
11 11
00
00 11
11 00
11 00
11 11
00
00 1100 1100 1100
j 00111100
i k l m n
00
11
00
11 p
Balance
Arithmetic expressions
Expression trees
− −
x y + 4
x 9
• •
TA TB
Theorem 7.6 In an expression tree T , every leaf is la-
belled with an integer or integer variable, and every in-
terior vertex is labelled with +, · or −.
00111100
towns. How can this be done?
Tirau
0110
0110 0110
Tauranga
National Park
Taupo
0011 0110
Ohakune
1100 Waioru
00111100
u w 0110
11
00
00
11
z 00111100
y x 0110
Another characterization of connected graphs
2000
0110 Auckland (AK)
10
00
0110
700
0
130
00
12
0110 1600
Hamilton (HM)
Tauranga (TG)
900
0110
800
140
0
New Plymouth (NP)
w7 w9
w8
w10
w6
w3 w2
w4
w5
w1 w11
Solution.
w7
w6
w3 w2
w4
w1
Greedy algorithms
Prim’s algorithm
T := a minimum-weight edge
for i := 1 to n − 2
begin
2000
0110 Auckland (AK)
10
00
0110
700
0
00 130
12
0110 1600
Hamilton (HM)
Tauranga (TG)
900
0110
800
140
0
New Plymouth (NP)
01 01 01 01
a 2 b 3 c 1 d
3 1 2 5
01 01 01 01
e 5 f 4 g 5 h
4 2 4 3
01 01 01 01
i 3 j 3 k 1 l
A second algorithm
Kruskal’s algorithm
for i := 1 to n − 1
2000
0110 Auckland (AK)
10
00
0110
700
0
130
00
12
0110 1600
Hamilton (HM)
Tauranga (TG)
900
0110
800
140
0
New Plymouth (NP)
8.1 Functions
A f B
x f (x)
A f B
Figure 8.1
131
Example 8.1 Let X be the set of all real numbers be-
tween 0 and 100 inclusive, and let Y be the set of all
real numbers between 32 and 212 inclusive. The function
F : X → Y that assigns to each Celsius temperature c its
corresponding Fahrenheit temperature F (c) is defined by
9
F (c) = c + 32
5
What are the domain, codomain and image of F ?
Example 8.2
x g(f (x))
f (x)
g◦f
Figure 8.2
Composition is associative
Figure 8.3
Identity function, inverse function
1A (x) = x.
h = h ◦ 1B
= h ◦ (f ◦ g)
= (h ◦ f ) ◦ g
= 1A ◦ g
=g
So f is a bijection, as required.
Example 8.3 Let f be the function from {a, b, c} to
{1, 2, 3} such that f (a) = 2 , and f (b) = 3 , and f (c) = 1
Is f invertible, and if it is, what is its inverse ?
Preimages
f ← (y) = {x : f (x) = y}
f ← (B) = {x : f (x) ∈ B, B ⊆ T }
9.1 Introduction
nr .
137
Arrangements without replacement
|A| n!
|B| = = .
n1 !n2 ! · · · nk ! n1 !n2 ! · · · nk !
WHANGAMOMONA
Solution.
12!
3!2!2!2!
9.3 Selections
(b) How many poker hands contain cards all of the same
suit?
Solution.
13
4×
5
(a) 41 10
4
= 840.
h i
4 10 10 10 10 10 10
(b) 2 3 1
+ 2 2
+ 1 3
= 26, 550.
(a) 9 people?
Solution.
(a) 29
9
= 10, 015, 005.
(b) 16 × 13
5 4
= 3, 123, 120.
16 13
(c) 9 + 9 = 11, 440 + 715 = 12, 155.
1. 16 muffins?
Solution.
7+3−1
(a) 3−1
= 36.
4+3−1
(b) 3−1
= 15. First give $100 to each stranger and then
share out the remaining $400.
k
X
N= |Ai |
i=1
k
X
< x
i=1
= kx
=N
Writing formulas
(φ1 ∨ φ2 ∨ . . . ∨ φk ),
151
Proof: Let φ be an arbitrary formula. Consider its
truth table. First, notice that if at every row φ is eval-
uated to false then φ is equivalent to (p ∧ ¬p), which is
in disjunctive normal form. Similarly, if at every row φ
is evaluated to true then φ is equivalent to (p ∨ ¬p).
Therefore, we can assume that there are rows at which
φ is evaluated to true and rows at which φ is evaluated
to false. In the table below we write the truth values as
Xi .
q1 q2 . . .. . . .. ... qn−1 qn φ
T T . . .. . . .. ... T T X1
F T . . .. . . .. ... T T X2
... ... . . .. . . .. ... ... ... ...
... ... . . .. . . .. ... ... ... ...
F F . . .. . . .. ... F F X 2n
• φk is a conjunction of literals.
• φk is evaluated to true when the truth values of
q1 , . . ., qn are a1 ,. . ., an , respectively.
• φk is evaluated to false under all other truth as-
signments of q1 , . . ., qn .
p q s (p ∨ q) (¬s ∧ q) φ
T T T T F F
T T F T T T
T F T T F F
T F F T F F
F T T T F F
F T F T T T
F F T F F T
F F F F F T
We can therefore write the formula φ = ((p ∨ ¬q) →
(s ∧ q)) as the DNF ψ = ((p ∧ q ∧ ¬s) ∨ (¬p ∧ q ∧ ¬s) ∨
(¬p ∧ ¬q ∧ s) ∨ (¬p ∧ ¬q ∧ ¬s).
This formula can be simplified by noting that (¬p ∧ ¬q ∧
s) ∨ (¬p ∧ ¬q ∧ ¬s) is equivalent to (¬p ∧ ¬q), but it
is not necessary to perform this simplification in assign-
ments/test/exam.
10.2 Satisfiability
Alphabet
Strings
Concatenation operation
Substrings
155
Languages
We denote languages by U , V , W , L, . . ..
Boolean operations
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
a 3 b
a b
b a
0 1 2
b a
11.4 Automata for operations on languages
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 .
Codewords
163
Prefix property and prefix codes
0110
codewords are produced?
0110 0110
0110 0110 0110 0110
0110 0110
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
00111100 00111100
11
00
00
11 00111100 00111100 00111100
9
11
00
003
11 00111100 11001100 00111100 0110 00111100
9 5 6 6
0110 0110
3 5
A = {0, 1, . . . , b − 1}
* Codes
* Distance of a code
Gray codes
f (M ) = M e mod n
Suppose that we are also in possession of the private key,
d. This may then be used to ‘decrypt’ the original value
M from its encrypted version f (M ) by using
M = (f (M ))d mod n
S = Md mod n
M = Se mod n
Selecting p, q and d
177
Hamilton circuit, 75 Pascal’s identity, 145
Hamilton path, 75 path in graph, 57
Hamming distance, 167 permutation, 138
handshaking theorem, 46 r-permutation, 138
Hasse diagram, 92 pigeonhole principle, 146–
Huffman’s algorithm, 166 149
hypothesis, 3, 8 poset, 87
positive integers (P), 28
idempotent laws, 6, 30 postorder algorithm, 119
identity function, 135 power set, 29
image (function), 131, 132 predicate, 33
implication laws, 6 prefix, 155
in-degree, 47 prefix property, 164
incidence matrix of graph, preimage (f ← (y)), 136
55 preorder algorithm, 119
incident edge, 45 prime, 15
inclusion/exclusion prin- private key, 173
ciple, 145–146 product rule (enumera-
inclusive or (∨), 3 tion), 137
initial state, 157 proof by contradiction, 10
injective, 134 proposition, 1
integers (Z), 15, 28 compound, 1, 4
intersection (∩), 29 contingent, 5
inverse function, 135 contradiction, 5
simple, 1
language, 156 tautology, 5
language accepted, 158 pseudograph, 42
lattice diagram, 92 public key, 173
laws
associative, 6, 30 quantifier, 33
commutative, 6, 30 quotient, 20
De Morgan’s, 6, 30
distributive, 6, 30 r-permutation, 138
for sets, 30–31 range (function), 132
idempotent, 6, 30
rational numbers (Q), 15,
implication, 6 28
least element, 93
real numbers (R), 28
lexicographic ordering, 89
reflexive, 83
logical connectives
relative complement (\),
(¬, ∧, ∨, →, ↔), 2
29
(⊕, nand, nor), 5
remainder, 20
logical equivalence (⇔), 5
replacement, 27
logical equivalence
(Lef trightarrow),
SAT, 153
151
satisfiable, 153
logical implication (⇒), 7
selections, 139, 143
loop, 42
selections with repeti-
loop invariant, 104
tions, 142
lower bound, 95
set, 27
complement (\), 29
maximal, 92
minimal, 92 difference (\), 29
modus ponens, 3 disjoint, 137
multigraph, 41 empty (∅), 27
equal, 28
n-cube, 49 intersection, 29
natural numbers (N), 15, laws, 30–31
27 subset, 28
not (¬), 2 union, 29
set builder, 27
one-to-one, 134 simple graph, 40
onto, 134 simple proposition, 1
or (∨), 2 state, 157
ordered pair, 32 statement, 1
out-degree, 47 strongly connected, 60
subgraph, 73
partial order, 87 subset, 28
partition, 86 substring, 155
surjective, 134 union of graphs, 52
symmetric, 83 union rule (enumeration),
137
tautology, 5, 36, 153 universal quantifier, 33
total order, 88 universal set, 29
transition diagram, 156, upper bound, 95
157
transition function, 156
variable, 15
transitive, 83
Venn diagram, 29
truth table, 3–5
truth value, 3
well-ordered, 88
union (∪), 29 word, 155