Discrete Math
Discrete Math
Reference:
Johnsonbaugh, R., Discrete Mathematics, (6th edition), Pearson Prentice Hall, 2005.
The 5th edition of Johnsonbaugh may be used, but the 6th edition has some notation
changes and some different problem numbering.
Logic is the study of reasoning. We can look at examples involving everyday sentences,
and proceed to more formal mathematical approaches.
Consider the following sentences:
Every even number greater than 2 can be expressed as the sum of two prime numbers
(Goldbach’s conjecture).
Each of the statements is either true or false. The first and third are obviously true, and
the second is obviously false. What do you think about the last?
A proposition is a statement that is either true or false (but not both). Whichever of
these (true or false) is the case is called the truth value of the proposition.
Some statements cannot be considered as propositions e.g.
Fred is a nerd.
The truth value is not well defined. However propositions in mathematics are well defined.
p and q.
p or q.
1
Example 1. If
p: It is raining,
q: It is cold,
Definition. The truth value of the proposition p ∧ q is defined by the truth table
p q p∧q
T T T
T F F
F T F
F F F
In essence, p ∧ q is true provided that both p and q are true, and is false otherwise.
Definition. The truth value of the proposition p ∨ q is defined by the truth table
p q p∨q
T T T
T F T
F T T
F F F
In essence, p ∨ q is true provided that p or q (or both) are true, and is false otherwise.
not p.
p ¬p
T F
F T
2
Example 2. We have
(p ∧ ¬ q) ∨ r.
π = 3.141592653589 79323864 . . . .
Then
(p ∧ ¬ q) ∨ r = (T ∧ ¬ T ) ∨ F
= (T ∧ F ) ∨ F
= F ∨F
= F,
if p then q
p → q.
The proposition p is called the hypothesis (or antecedent) and the proposition q is called
the conclusion (or consequent).
Example 3. The lecturer states that if a student gets more than 50% then the student
will pass the course.
3
If p is false then p → q does not depend on the conclusion’s truth value, and so is regarded
as true.
This last often presents some difficulty in comprehending. We can think of it in this way.
If the student does not get more than 50%, we cannot regard p → q as false, and so it is
considered true. This gives the following truth table.
Definition. The truth value of the conditional proposition p → q is defined by the
following truth table:
p q p→q
T T T
T F F
F T T
F F T
Note that
p only if q
is considered logically the same as
if p then q.
An example of this is the two statements
“The student is eligible to take Maths 3 only if the student has passed Maths 2”
and
“If the student takes Maths 3 then the student has passed Maths 2,”
which are logically equivalent.
Definition. If p and q are propositions, the proposition
p if and only if q
is called a biconditional proposition and is denoted
p ↔ q.
The truth value of the proposition p ↔ q is defined by the following truth table:
p q p↔q
T T T
T F F
F T F
F F T
Note that p ↔ q means that p is a necessary and sufficient condition for q. The proposition
“p if and only if q” can be written “p iff q”.
Definition. Suppose that the propositions P and Q are made up of the propositions
p1 , . . . , pn . We say that P and Q are logically equivalent and write
P ≡ Q,
provided that, given any truth values p1 , . . . , pn , either P and Q are both true, or P and
Q are both false.
4
Example 4. Verify the first of De Morgan’s laws
¬ (p ∨ q) ≡ ¬ p ∧ ¬ q,
and the second,
¬ (p ∧ q) ≡ ¬ p ∨ ¬ q
will be a tutorial exercise.
P = ¬ (p ∧ q),
Q = ¬ p ∨ ¬ q.
p q p ∨ q ¬ p ¬ q ¬ (p ∨ q) ¬ p ∧ ¬ q
T T T F F F F
T F T F T F F
F T T T F F F
F F F T T T T
Thus P and Q are logically equivalent.
Definition. The converse of the conditional proposition p → q is the proposition q → p.
The contrapositive (or transposition) of the conditional proposition p → q is the proposi-
tion ¬ q → ¬ p.
Theorem 1. The conditional proposition p → q and its contrapositive ¬ q → ¬ p are
logically equivalent.
Proof:
The truth table
p q p → q ¬q ¬p ¬q → ¬p
T T T F F T
T F F T F F
F T T F T T
F F T T T T
shows that p → q and ¬ q → ¬ p are logically equivalent.
Some theorems in mathematics are best proved by using the contrapositive. It is likely
that you have seen some in matriculation mathematics or Engineering Mathematics 1 or
2E.
Exercise: Show that p → q ≡ ¬ p ∨ q.
Definition. A compound proposition is a tautology if it is true regardless of the truth
values of its component propositions.
A compound proposition is a contradiction if it is false regardless of the truth values of
its component propositions.
Example 5.
p ¬p p ∨ ¬p p ∧ ¬p
T F T F
F T T F
So p ∨ ¬ p is a tautology, and p ∧ ¬ p is a contradiction.
5
Quantifiers
P (x) : x2 − 5x + 6 = 0,
is said to be a universally quantified statement. The symbol ∀ means “for every”. Thus
the statement
for every x, P (x)
may be written
∀x P (x).
The symbol ∀ is called a universal quantifier.
The statement
∀x P (x)
is true if P (x) is true for every x in D. The statement
∀x P (x)
6
Example 8. The universally quantified statement “for every positive integer n greater
than 1, 2n − 1 is prime” is false.
n=2 22 − 1 = 3
n=3 23 − 1 = 7
n=4 24 − 1 = 15.
We only need a counter example to prove a statement false. We need to prove for all x to
prove a statement true.
∃x P (x)
n=1 21 − 1 = 1
n=2 22 − 1 = 3
n=3 23 − 1 = 7
n=4 24 − 1 = 15
n=5 25 − 1 = 31
n=6 26 − 1 = 63
n=7 27 − 1 = 127
n=8 28 − 1 = 255
n=9 29 − 1 = 511
n = 10 210 − 1 = 1023.
7
The variable x in the propositional function P (x) is called a free variable, that is x is
“free” to roam over the domain of discourse.
The variable x in the universally quantified statement
∀x P (x)
∃x P (x)
Example 10. Verify that the existentially quantified statement “for some real number
1
x, 2 > 1” is false.
x +1
1
We must show that 2 > 1 is false for all real numbers x.
x +1
1 1
Now 2 > 1 is false when 2 ≤ 1 is true. Then
x +1 x +1
0 ≤ x2
1 ≤ x2 + 1
1
≤ 1,
x2 +1
1
and so ≤ 1 is true for all real numbers x.
x2 +1
Theorem 2. Generalised De Morgan Laws for Logic
If P is a propositional function, each pair of propositions in (a) and (b) has the same
truth values (i.e. either both are true or both are false).
Proof of (a):
If ¬ (∀x P (x)) is true, then ∀x P (x) is false.
Hence P (x) is false for at least one x in the domain of discourse, and ¬ P (x) is true for
at least one x in the domain of discourse. Hence ∃x ¬ P (x) is true.
Similarly, if ¬ (∀x P (x)) is false, then ∃x ¬ P (x) is false.
8
Example 10. (Continued)
1
We have that P (x) is the statement > 1, and aim to show that for all real numbers
x2 +1
x, P (x) is false i.e.
∃x P (x)
is false. We do this by verifying that for every real number x, ¬ P (x) is true i.e.
∀x ¬ P (x)
is true. Then
∀x ¬ P (x) is true
¬ (∀x ¬ P (x)) is false
∃x ¬ (¬ P (x)) is false (by De Morgan’s laws)
∃x P (x) is false
Example 11. Consider the well-known proverb
All that glitters is not gold.
This can be interpreted in English in two ways:
9
Discrete Mathematics: Week 2
Nested Quantifiers
This can be restated as: If x > 0 and y > 0, then x + y > 0. We need two universal
quantifiers, and can write the statement symbolically as
The domain of discourse is the set of all real numbers. Multiple quantifiers such as ∀x∀y
are said to be nested quantifiers.
The statement
∀x∀y P (x, y),
with domain of discourse D, is true if, for every x and for every y in D, P (x, y) is true.
The statement
∀x∀y P (x, y),
is false if there is at least one x and at least one y in D such that P (x, y) is false.
For any real number x, there is at least one real number y such that x + y = 0.
We know that this is true, as we can always choose y to be −x. We can write the statement
symbolically as
∀x ∈ R (∃y ∈ R, x + y = 0).
The domain of discourse is the set of all real numbers.
The statement
∀x∃y P (x, y),
with domain of discourse D, is true if, for every x in D, there is at least one y in D for
which P (x, y) is true.
The statement
∀x∃y P (x, y),
is false if there is at least one x in D such that P (x, y) is false for every y in D.
1
Example 3. Consider the nested quantifier
∃y ∈ R, (∀x ∈ R, x + y = 0).
There is some real number y such that x + y = 0 for all real numbers x.
The statement
∃x∀y P (x, y),
with domain of discourse D, is true if there is at least one x in D such that P (x, y) is true
for every y in D.
The statement
∃x∀y P (x, y),
is false if, for every x in D, there is at least one y in D such that P (x, y) is false.
with domain of discourse the set of positive integers. This statement is true as there is at
least one positive integer x and at least one positive integer y, both greater than 1, such
that xy = 6 e.g. x = 2, y = 3.
The statement
∃x∃y P (x, y),
with domain of discourse D, is true if there is at least one x in D and at least one y in D
such that P (x, y) is true.
The statement
∃x∃y P (x, y),
is false if, for every x in D and for every y in D, P (x, y) is false.
Example 5. Using the generalized De Morgan laws for logic, the negation of
∀x∃y P (x, y)
is
¬ (∀x∃y P (x, y)) ≡ ∃x ¬ (∃y P (x, y)) ≡ ∃x∀y ¬ P (x, y).
Note that in the negation, ∀ and ∃ are interchanged.
2
Proofs
definitions which are used to create new concepts in terms of existing one;
undefined terms which are not specifically defined but which are implicitly defined
by the axioms.
A lemma is a theorem that is not too interesting in its own right but is useful in
proving another theorem.
x is in P , x = 0, −x is in P .
Not too interesting in its own right, but can be used to prove other results.
3
Theorems are often of the form
This universally quantified statement is true provided that the conditional proposition
Proof:
Let the two odd integers be 2m+ 1 and 2n+ 1, where m and n are integers. Their product
is
which is odd.
p→q and p ∧ ¬q → r ∧ ¬r
p q r p → q p ∧ ¬q r ∧ ¬r p ∧ ¬q → r ∧ ¬r
T T T T F F T
T T F T F F T
T F T F T F F
T F F F T F F
F T T T F F T
F T F T F F T
F F T T F F T
F F F T F F T
4
Example 10. Prove that the root mean square of two number a and b, a > 0 and b > 0,
is equal to or greater than the arithmentic mean i.e.
s
a2 + b2 a+b
≥ .
2 2
Proof:
s
a2 + b2 a+b
Assume < .
2 2
Since both sides are positive, we can square without changing the direction of the inequal-
ity.
a2 + b2 (a + b)2
<
2 4
2(a2 + b2 ) < a2 + 2ab + b2
a2 − 2ab + b2 < 0
(a − b)2 < 0.
4k 2 = 2n2
2k 2 = n2 ,
5
Proof by contrapositive is based on the fact that
p → q ≡ ¬ q → ¬ p.
The idea is to show that the opposite of the conclusion implies the opposite of the hy-
pothesis.
Example 12.
Theorem: If x and y are real numbers and x + y ≥ 2, then either x ≥ 1 or y ≥ 1.
Proof:
Let p be “x + y ≥ 2” and q be “either x ≥ 1 or y ≥ 1”.
Assume ¬ q: x < 1 and y < 1.
Then x + y < 1 + 1, or x + y < 2, which is ¬ p. Proven.
Proof by cases is used when the original hypothesis naturally divides into various cases.
Example 13.
Theorem: |x + y ≤ |x| + |y| for all real x and y.
Proof:
Consider the four cases as follows, where each of x, y is nonnegative or negative.
1. x, y ≥ 0:
Then x + y ≥ 0, so |x + y| = x + y = |x| + |y|.
2. x, y < 0:
Then x + y < 0, so |x + y| = −(x + y) = −x − y = |x| + |y|.
3. x ≥ 0, y < 0:
Then x + y < x < |x| + |y|, and
−(x + y) = −x − y ≤ −y = |y| ≤ |x| + |y|.
4. x < 0, y ≥ 0:
The same as case 3, swapping the roles of x and y.
6
Definition. An argument is a sequence of propositions written
p1
p2
..
.
pn
... q
or
p1 , p2 , . . . , pn / ... q.
The propositions p1 , p2 , . . . , pn are called the hypotheses and the proposition q is called
the conclusion. The argument is valid provided that if p1 , p2 , . . . , pn are all true, then q
must be true; otherwise the argument is invalid (or a fallacy).
Example 14. Determine whether the argument
p→q
¬q
... ¬ p
is valid.
¬p → ¬q
p
.
.. q
7
The first two lines are the important ones. The second line implies that it is an
invalid argument.
Mathematical Induction
Example 16. An arithmetic teacher sets his class the problem of adding up all the
integers from 1 to 100. Can this be done in under 10 seconds? There is a piece of folklore
relating this to the mathematician Karl Friedrich Gauss (1777–1855) as a boy.
If we pair the numbers
1 + 2 + 3 + · · · + 49 + 50
100 + 99 + 98 + · · · + 52 + 51
we can see that each vertical pair adds to 101. Since there are 50 pairs, the sum is
50 × 101 = 5050.
The general case, the sum of all integers from 1 to n, is
n(n + 1)
1+2+3+ ··· +n = , n = 1, 2, 3, . . . .
2
Definition. Suppose that we have a propositional function S(n) whose domain of dis-
course is the set of positive integers. Suppose that
S(1) is true;
The first part, S(1) is true, is called the Basis Step, and the second part is called the
Inductive Step. It is not necessary to start with n = 1, sometimes n = 0, 2, 3, . . . will
occur.
n(n + 1)
1+2+3+ ··· +n = .
2
8
Then
n(n + 1)
1 + 2 + 3 + · · · + n + (n + 1) = + (n + 1), by the assumption
2
= 12 (n + 1)(n + 2), factorise whenever possible
(n + 1)(n + 2)
= , correct form.
2
Hence, by the Principle of Mathematical Induction, S(n) is true for all n ≥ 1.
n(3n − 1)
1 + 4 + 7 + 10 + · · · + (3n − 2) = , n ≥ 1.
2
n(3n − 1)
1 + 4 + 7 + · · · + (3n − 2) = .
2
Then
n(3n − 1)
1 + 4 + 7 + · · · + (3n − 2) + (3n + 1) = + (3n + 1), by the assumption
2
= 21 3n2 − n + 6n + 2 ,
can’t factorise here
= 12 3n2 + 5n + 2
(n + 1)(3n + 2)
=
2
(n + 1)(3(n + 1) − 1)
= , correct form.
2
Hence, by the Principle of Mathematical Induction, S(n) is true for all n ≥ 1.
9
Discrete Mathematics: Week 3
Mathematical Induction (Continued)
Correct formulae are given in advance. How do we know the correct formula?
Experiment to find a pattern e.g. sum of the odd integers.
n S2n−1
1 1
2 4
3 9
4 16
.. ..
. .
n(n + 1)(2n + 1)
S(n) : 12 + 22 + 32 + · · · + n2 = , n ≥ 1,
6
is true.
1×2×3
Basis Step: For n = 1, LHS = 1 and RHS = = 1. True.
6
Inductive Step: Assume that
n(n + 1)(2n + 1)
12 + 22 + 32 + · · · + n2 = .
6
Then
12 + 22 + 32 + · · · + n2 + (n + 1)2
n(n + 1)(2n + 1)
= + (n + 1)2 , by the assumption
6
1
= 6 (n + 1) 2n2 + n + 6(n + 1) , factorize whenever possible
= 16 (n + 1) 2n2 + 7n + 6
(n + 1)(n + 2)(2n + 3)
=
6
(n + 1)(n + 2)[2(n + 1) + 1]
= , correct form.
6
Since the Basis Step and Inductive Step have been verified, by the Principle of Mathe-
matical Induction, S(n) is true for all n ≥ 1.
1
Example 2. Show that
1 1 1 1 n
S(n) : + + + ··· + = , n ≥ 1,
1·2 2·3 3·4 n(n + 1) (n + 1)
is true.
Work a few terms:
1 1
=
1·2 2
1 1 1 1
+ = +
1·2 2·3 2 6
2
=
3
1 1 1 1 1 1
+ + = + +
1·2 2·3 3·4 2 6 12
3
=
4
1 1 1
Basis Step: For n = 1, LHS = = and RHS = . True.
1·2 2 2
Inductive Step: Assume that
1 1 1 1 n
+ + + ··· + = .
1·2 2·3 3·4 n(n + 1) (n + 1)
Then
1 1 1 1 1
+ + + ··· + +
1·2 2·3 3·4 n(n + 1) (n + 1)(n + 2)
n 1
= + , by the assumption
(n + 1) (n + 1)(n + 2)
1
= [n(n + 2) + 1] , factorize whenever possible
(n + 1)(n + 2)
1
= (n + 1)2
(n + 1)(n + 2)
n+1
= , correct form.
n+2
Since the Basis Step and Inductive Step have been verified, by the Principle of Mathe-
matical Induction, S(n) is true for all n ≥ 1.
Example 3. Divisibility example, Johnsonbaugh 1.7.5.
Show that 5n − 1 is divisible by 4 for all n ≥ 1.
Basis Step: For n = 1, 51 − 1 = 4 is divisible by 4. True.
Inductive Step: Assume that 5n − 1 is divisible by 4.
Then we wish to prove that 5n+1 − 1 is divisible by 4.
5n+1 − 1 = 5 × 5n − 1
= (5n − 1) + 4 × 5n .
2
The first part is divisible by 4 by the assumption, and 4 × 5n is divisible by 4, hence true.
Alternatively, put 5n − 1 = 4m, where m is an integer. Then
5n+1 − 1 = 5 × 5n − 1
= 5 × (4m + 1) − 1, by the assumption
= 4(5m) + 4,
which is divisible by 4.
Since the Basis Step and Inductive Step have been verified, by the Principle of Mathe-
matical Induction, S(n) is true for all n ≥ 1.
a(r n+1 − 1)
a + ar 1 + ar 2 + · · · + ar n =
r−1
for all n ≥ 0.
a(r − 1)
Basis Step: For n = 0, LHS = a and RHS = = a. True.
r−1
Inductive Step: Assume that
a(r n+1 − 1)
a + ar 1 + ar 2 + · · · + ar n = .
r−1
Then
a + ar 1 + ar 2 + · · · + ar n + ar n+1
a(r n+1 − 1)
= + ar n+1 , by the assumption
r−1
a(r n+1 − 1) + ar n+1 (r − 1)
=
r−1
a(r n+1
− 1 + r n+2 − r n+1 )
=
r−1
a(r n+2
− 1)
= .
r−1
Since the Basis Step and Inductive Step have been verified, by the Principle of Mathe-
matical Induction, S(n) is true for all n ≥ 0.
3
Inductive Step: Assume that 4n > 5n2 , n ≥ 3.
We want to show that 4n+1 > 5(n + 1)2 . Then
4n+1 = 4 (4n )
> 4 5n2 , by the assumption
= 5 n2 + 2n2 + n2
> 5 n2 + 2n + 1 , since n ≥ 3
= 5(n + 1)2 .
Since the Basis Step and Inductive Step have been verified, by the Principle of Mathe-
matical Induction, S(n) is true for all n ≥ 3.
The Dutch artist M.C. Escher produced many woodcut and lithograph art works of tiling
problems.
There are two trominos, the right and straight trominos. We will henceforth refer to the
right tromino just as a tromino.
4
Trominos can tile a deficient board, that is an n × n board with one square missing,
providing n 6= 3k. We can see that if n = 3k + 1, then
n2 − 1 = (3k + 1)2 − 1
= 9k 2 + 6k,
and is divisible by 3.
If n = 3k + 2, then
n2 − 1 = (3k + 2)2 − 1
= 9k 2 + 12k + 3,
and is divisible by 3.
We can tile all deficient boards with n 6= 3k, except for some deficient 5 × 5 boards – see
the figure below. Here we will prove by mathematical induction that all 2n × 2n deficient
boards can be tiled with trominos.
Basis Step: For n = 1, the 2 × 2 deficient board is a tromino and can be tiled.
Inductive Step: Assume that any 2n × 2n deficient board can be tiled.
Then we can divide the 2n+1 × 2n+1 deficient board into four 2n × 2n deficient boards,
with one board having the missing square anywhere, the other three boards having missing
squares in the corners by placement of one tromino as shown in the figure.
5
We can tile all four 2n × 2n deficient boards by the hypothesis, and can hence tile the
2n+1 × 2n+1 deficient board.
Since the Basis Step and Inductive Step have been verified, by the Principle of Mathe-
matical Induction, we can tile any 2n × 2n deficient board.
All examples so far have involved the Weak Form of Mathematical Induction, where if
S(n), then S(n + 1) is true. The Strong Form of Mathematical Induction allows us
to assume the truth of all of the preceeding statements.
S(n0 ) is true;
for all n > n0 , if S(k) is true for all k, n0 ≤ k < n, then S(n) is true.
a3 = 3×3−2×1
= 7
a4 = 3×7−2×3
= 15
a5 = 3 × 15 − 2 × 7
= 31.
6
We want to prove that an+1 = 2n+1 − 1. Now
Since the Basis Steps and Inductive Step have been verified, by the Principle of Mathe-
matical Induction, the formula an = 2n − 1 is true for all n ≥ 1.
Example 8. Show that postage of six cents or more can be achieved by using only 2-cent
and 7-cent stamps.
Basis Steps: For n = 6, 7. For six cent postage, use three 2-cent stamps and for seven
cent postage, use one 7-cent stamp.
Inductive Step: Assume n ≥ 8 and assume that postage of k cents or more can be achieved
by using only 2-cent and 7-cent stamps for 6 ≤ k < n.
By the assumption, we can make postage of n − 2 cents. Then add a 2-cent stamp to
make postage of n cents. The inductive step is complete.
Since the Basis Steps and Inductive Step have been verified, by the Principle of Mathe-
matical Induction we can make postage for all n ≥ 6.
2 ≤ p ≤ q ≤ n.
Since by the assumption, p and q are the product of primes, then n+1 = pq is the product
of primes.
Since the Basis Step and Inductive Step have been verified, by the Principle of Mathe-
matical Induction S(n) is true for all n ≥ 2.
7
The Language of Mathematics
Sets
A = { 1, 2, 3, 4 } = { 2, 1, 4, 3 } .
A = { 1, 2, 3, 4 } = { 1, 2, 2, 3, 4 } .
or
B = { x | x is a positive odd integer less than 20 } ,
and
C = { x | x is a positive integer divisible by 3 } .
Read the symbol | as “such that”.
If X is a finite set, we let
4 ∈ A, 4 6∈ B, 4 6∈ C.
The empty set is the set with no elements, denoted ∅. Hence ∅ = { }, |∅| = 0. e.g.
n o
x | x ∈ R and x2 + 1 = 0 .
What is |{ ∅ }|?
Two sets X and Y are equal if they have the same elements.
X = Y if for every x, if x ∈ X then x ∈ Y and for every x, if x ∈ Y then x ∈ X, i.e.
X = Y if ∀x ((x ∈ X → x ∈ Y ) ∧ (x ∈ Y → x ∈ X)).
8
Example 1. n o
X= x | x2 + x − 6 = 0 , Y = { 2, −3 } .
If x ∈ X, then
x2 + x − 6 = 0
(x − 2)(x + 3) = 0
x = 2, −3
x ∈ Y.
If x ∈ Y , then if x = 2,
x2 + x − 6 = 0
x ∈ X,
x2 + x − 6 = 0
x ∈ X.
∀x (x ∈ X → x ∈ Y ),
e.g.
{ 1, 2 } ⊆ { 1, 2, 3, 4 } .
We have
X ⊆ X
∅ ⊆ X
i.e. ∀x (x ∈ ∅ → x ∈ X)
If X ⊆ Y and Y ⊆ X, then X = Y .
If X ⊆ Y and Y is finite, then |X| ≤ |Y |.
If X ⊆ Y , Y is finite, and |X| = |Y |, then X = Y .
9
Discrete Mathematics: Week 4
Sets (Continued)
The power set of the set X, denoted P(X), is the set of all subsets of X.
Example 2. B = { a, b }, A = { a, b, c }.
P(B) = { ∅, { a }, { b }, { a, b } }, |P(B)| = 4.
P(A) = { ∅, { a }, { b }, { c }, { a, b }, { a, c }, { b, c }, { a, b, c } }, |P(A)| = 8.
∅ {a}
{b} { a, b }
{c} { a, c }
{ b, c } { a, b, c }
Basis Step: If n = 0, we have the empty set which has only one subset, itself. Then
|∅| = 0, and 20 = 1. True.
Inductive Step: Assume that a set with n elements has a power set of size 2n .
Let X be a set with n + 1 elements. Remove one element, x, from X to form a set Y .
Then Y has n elements, and by the assumption
|P(Y )| = 2n .
|P(X)| = 2 |P(Y )| ,
then
|P(X)| = 2n+1 .
Since the Basis Step and Inductive Step have been verified, by the Principle of Mathe-
matical Induction, if |X| = n, then |P(X)| = 2n is true for all n ≥ 0.
An alternative proof is to suppose that a 1 represents the presence of an element in a
subset, and a 0 represents its absence.
Then all subsets of X can be represented by a binary string of length |X| = n, and there
are 2n such strings.
1
Set Operations
Given two sets X and Y :
Their union is X ∪ Y = { x | x ∈ X or x ∈ Y }.
Their intersection is X ∩ Y = { x | x ∈ X and x ∈ Y }.
Their difference is X − Y = { x | x ∈ X and x 6∈ Y }.
Example 3. A = { a, b, c, d, e } and B = { 1, 2, 3, 4, a, b }.
A ∪ B = { a, b, c, d, e, 1, 2, 3, 4 }
A ∩ B = { a, b }
A − B = { c, d, e }
B − A = { 1, 2, 3, 4 }
Venn diagrams provide a pictorial view of sets. U, the universal set, is depicted as a
rectangle. Sets A, B, C, contained in U, are drawn as circles.
U U U
A B A B A B
2
U
U A B
A
C
A
C ∩ (A ∪ B)
In Example 4, we have
A B
1
5, 7 2
3
9 8
6
C
0, 4
Theorem 2.1.12 Let U be a universal set and let A, B, and C be subsets of U. The
following properties hold.
(A ∪ B) ∪ C = A ∪ (B ∪ C), (A ∩ B) ∩ C = A ∩ (B ∩ C)
A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C), A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)
3
(g) Bound laws:
A ∪ U = U, A∩∅=∅
(A ∪ B) = A ∩ B, (A ∩ B) = A ∪ B
U U
A B A B
C C
B∪C A ∩ (B ∪ C)
U U
A B A B
C C
A∩B A∩C
x ∈ A ∩ (B ∪ C).
Then
so that
x ∈ (A ∩ B) ∪ (A ∩ C).
4
This only proves that
A ∩ (B ∪ C) ⊆ (A ∩ B) ∪ (A ∩ C).
Let
x ∈ (A ∩ B) ∪ (A ∩ C).
Then
x∈A∩B or x∈A∩C
x ∈ A and x ∈ B or x ∈ A and x ∈ C
x∈A and x ∈ B or x ∈ C
x∈A and x ∈ B ∪ C,
so that
x ∈ A ∩ (B ∪ C).
U U
A B A B
A∪B (A ∪ B) = A ∩ B
U U
A B A B
A B
x ∈ (A ∪ B).
Then
x 6 ∈ A∪B
x 6∈ A and x 6∈ B
x∈A and x∈B
x ∈ A ∩ B.
5
Let
x ∈ A ∩ B.
Then
We write n n
[ [ \ \
S= Ai , S= Ai .
i=1 i=1
Example 5. Let
S = { A1 , A2 , A3 , . . . }
where
An = { n, n + 1, n + 2, . . . }.
That is,
A1 = { 1, 2, 3, . . . }
A2 = { 2, 3, 4, . . . }
A3 = { 3, 4, 5, . . . },
etc.
Obviously [
S = { 1, 2, 3, . . . } = A1 ,
and \
S = ∅,
as
\
1∈
/ S as 1 ∈
/ A2 , A3 , . . .
\
2∈
/ S as 2 ∈
/ A3 , A4 , . . .
etc.
6
A collection S of non-empty subsets of X is a partition of X if each element of X belongs
to exactly one member of X. That is, S is pairwise disjoint and S = X.
S
Example 6.
(a) X = { 1, 2, 3, 4, 5, 6, 7, 8 }
Then
S = { {2, 4, 8 }, { 1, 3, 5, 7 }, { 6 } }
is a partition of S.
(b) X = { x | x ∈ R }
S = { { x | x is rational }, { x | x is real } }
is not a partition of X.
T = { { x | x is rational }, { x | x is irrational } }
is a partition of X.
Entrees (set E)
Mains (set M)
7
Then E = { a, b, c } and M = { 1, 2, 3, 4 }.
The Cartesian product lists the 12 possible dinners consisting of one entree and one main
course. So
E × M = { (a, 1), (a, 2), (a, 3), (a, 4), (b, 1), (b, 2), (b, 3), (b, 4), (c, 1), (c, 2), (c, 3), (c, 4) } .
This is actually a cut-down version of the menu. In fact, there are 5 entrees and 8 main
courses. How many dinners are possible?
8
Relations
Relations Johnsonbaugh 3.1
We can consider a relation as a table linking the elements of two sets e.g. product vs price.
X
1 Y
a
2
b
3
c
4
X = { 2, 3, 4 } and Y = { 3, 4, 5, 6, 7 }
9
We could write R as a table:
X Y
2 4
2 6
3 3
3 6
4 4
1 2
3 4
a b
c d
10
Definition. A relation R on a set X is called reflexive if (x, x) ∈ R for every x ∈ X.
Example 2 is antisymmetric. Between any two distinct vertices there is at most one
directed edge.
Can a relation R be both symmetric and antisymmetric?
Do we really need to list all pairs? If x = y, and (x, y), (y, z) ∈ R, then (x, z) ∈ R is
automatically true. Hence the table need be only
11
Discrete Mathematics: Week 5
Relations (Continued)
Relations can be used to order elements of a set. For example, the relation R on the
positive integers defined by
(x, y) ∈ R if x ≤ y
orders the integers, and is reflexive, antisymmetric and transitive i.e.
reflexive : x≤x
antisymmetric : if x ≤ y and x 6= y, then y 6≤ x
transitive : if x ≤ y and y ≤ z, then x ≤ z.
Example 4. X = { 2, 3, 4, 5, 6 }
R is the relation defined by (x, y) ∈ R if y is larger than x by an even number or zero.
Hence
R = { (2, 2), (2, 4), (2, 6), (3, 3), (3, 5), (4, 4), (4, 6), (5, 5), (6, 6) }.
The digraph is
3 5
2 4
This is
1
If every pair of elements in X is comparable, we call R a total order.
Example 2 is a total order. Either (x, y) ∈ R or (y, x) ∈ R for all x, y = 1, 2, 3, 4.
More generally, the less than or equals relation on the positive integers is a total order,
since either x ≤ y or y ≤ x (or both if x = y).
Example 4 is not a total order. Why?
Partial orders can be used in task scheduling.
Example 5. Johnsonbaugh 3.1.21
The set T of tasks in taking an indoor flash photograph is as follows.
2. Focus camera.
Some tasks must be done before others, some can be done in either order.
Define the relation R on T by
Then
R = { (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (1, 2), (1, 5), (2, 5), (3, 5), (4, 5) } .
R is reflexive, antisymmetric and transitive, and is a partial order. Why is R not a total
order?
Possible solutions are 1, 2, 3, 4, 5 or 3, 4, 1, 2, 5.
Definition. Let R be a relation from X to Y . The inverse of R, denoted R−1 , is the
relation from X to Y defined by
Example 1. (Continued)
X = { 2, 3, 4 } and Y = { 3, 4, 5, 6, 7 }
then
R−1 = { (4, 2), (6, 2), (3, 3), (6, 3), (4, 4) }
and might be described as (y, x) ∈ R−1 if y is divisible by x.
2
Definition. Let R1 be a relation from X to Y and R2 be a relation from Y to Z. The
composition of R1 and R2 , denoted R2 ◦ R1 , is the relation from X to Z defined by
Example 6.
A = { 1, 2, 3, 4 } B = { a, b, c, d } C = { x, y, z }
1 2 3 4
A
a b c d B
C
x y z
3
Matrices of Relations
X = { 1, 2, 3, 4 } and (x, y) ∈ R if x ≤ y.
R = { (1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4) } .
Then the matrix is
1 2 3 4
1 1 1 1 1
2
0 1 1 1
3 0 0 1 1
4 0 0 0 1
The matrices depend on the ordering of the elements in the sets X and Y .
A relation R on X has a square matrix.
The relation R on a set X which has a matrix A is
• reflexive if and only if (iff) A has 1’s on the main diagonal. Recall
A = { 1, 2, 3, 4 } B = { a, b, c, d } C = { x, y, z }
4
a b c d
1 1 0 0 0
A1 = matrix of R = 2
0 0 0 1
3 1 1 0 1
4 0 0 0 0
x y z
a 0 0 0
A2 = matrix of S = b
1 0 1
c 0 1 0
d 0 0 1
x y z
1 0 0 0 0 0 0 1 0 0 0
A1 A2 =
0 0 0 1
1 0 1
= 2
0 0 1
1 1 0 1 0 1 0 3 1 0 2
0 0 0 0 0 0 1 4 0 0 0
What does the 2 in (A1 A2 )(3,3) mean?
The composition of the relations is
S ◦ R = { (2, z), (3, x), (3, z) }.
So if we convert the ‘2’ to a ‘1’, we have the matrix of S ◦ R.
5
Example 2. (from §3.1, revisited)
R is a relation on X = { 1, 2, 3, 4 } defined by (x, y) ∈ R if x ≤ y, for x, y ∈ X.
R = { (1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4) }.
1 2 3 4
1 1 1 1 1
A= 2
0 1 1 1
3 0 0 1 1
4 0 0 0 1
1 2 3 4
1 1 2 3 4
A2 = 2
0 1 2 3
3 0 0 1 2
4 0 0 0 1
Then A2 has 1’s only in positions where A has 1’s, and the relation is transitive.
A= b
0 0 1 0
c 0 1 0 0
d 0 0 0 1
a b c d
a 1 0 0 0
A2 = b
0 1 0 0
c 0 0 1 0
d 0 0 0 1
Then A2 has 1’s in the (b, b) and (c, c) positions, whereas A does not. The relation is not
transitive i.e. (b, c) ∈ R and (c, b) ∈ R, but (b, b) 6∈ R and (c, c) 6∈ R.
6
Functions Johnsonbaugh 2.2
{ y | (x, y) ∈ f }
X Y
1 a
2 b
3 c
X
1 Y
a
2
b
3
c
4
X Y
1 a
2 b
3 c
7
There must be exactly one arrow from every element in the domain. There cannot be no
arrow, or more than one arrow.
6 mod 3 = ?
9 mod 10 = ?
14 mod 3 = ?
365 mod 7 = ?
This last result tells us that 29th March next year will be a Thursday.
Definition. The floor of x, denoted ⌊ x ⌋, is the greatest integer less than or equal to x.
The ceiling of x, denoted ⌈ x ⌉, is the least integer greater than or equal to x.
⌊ 1.4 ⌋ = ? ⌈ 1.4 ⌉ = ?
⌊6⌋ = ? ⌈6⌉ = ?
⌊ −5.7 ⌋ = ? ⌈ −5.7 ⌉ = ?
The graphs of the floor and ceiling functions are shown below.
y
Floor function 2 [ )
1 [ )
[ )
−3 −2 −1 0 1 2 3 x
[ ) −1
[ ) −2
8
y
Ceiling function 2 ( ]
1( ]
( ]
−3 −2 −1 0 1 2 3 x
( ] −1
( ] −2
Store the numbers 15, 558, 32, 132, 102, 5 in the eleven cells.
15 = 1 × 11 + 4 so 15 mod 11 = 4
558 = 50 × 11 + 8 so 558 mod 11 = 8
32 = 2 × 11 + 10 so 32 mod 11 = 10
132 = 12 × 11 + 0 so 132 mod 11 = 0
102 = 9 × 11 + 3 so 102 mod 11 = 3
5 = 0 × 11 + 5 so 5 mod 11 = 5
These all store in the appropriate cells as shown in the diagram below.
9
Definition. A function f from X to Y is said to be one-to-one (or injective) if for each
y ∈ Y , there is at most one x ∈ X with f (x) = y.
Example 3. f = { (1, a), (2, b), (3, c) } from X = { 1, 2, 3 } to Y = { a, b, c, d } is a one-
to-one function.
f = { (1, a), (2, b), (3, a) } from X = { 1, 2, 3 } to Y = { a, b, c, d } is a function, but not
one-to-one.
The arrow diagrams illustrate this.
Y Y
X a X a
1 1
b b
2 2
c c
3 3
d d
X Y
1 a
2 b
3 c
If Y = { a, b, c, d }, f is not onto.
Definition. A function that is both one-to-one and onto is called a bijection.
is a bijection.
Inverse Function
Suppose that f is a one-to-one, onto function from X to Y . The inverse relation
{ (y, x) | (x, y) ∈ f }
10
Since f is onto, the range of f is Y , and hence the domain of f −1 is Y .
Since f is one-to-one, there is only one x ∈ X for which (y, x) ∈ f −1 , so f −1 is a function.
We can obtain the arrow diagram for f −1 by reversing the directions of the arrows for f .
Since f is one-to-one, f −1 is one-to-one. Since f is a function, the domain of f is X and
hence f −1 is onto.
Example 5. R1 = { (1, b), (2, a), (3, c) } from X = { 1, 2, 3 } to Y = { a, b, c, d }.
Or we could say
R1 = { (1, b), (2, a), (3, c) } ⊆ { 1, 2, 3 } × { a, b, c, d }.
R1 is a function, is one-to-one, but is not onto. This is evident also from the arrow
diagram.
Y
X a
1
b
2
c
3
d
X Y
1 a
2 b
3 c
4 d
11
Discrete Mathematics: Week 6
Algorithms
Introduction
• Finiteness The algorithm terminates; that is, it stops after finitely many
instructions have been executed.
• Correctness The output produced by the algorithm is correct; that is, the
algorithm correctly solves the problem.
Algorithms are written in pseudocode, which resembles real computer code. Johnsonbaugh
in the 6th edition has rewritten algorithms to be more like Java.
Algorithm 4.1.1 Finding the Maximum of Three Numbers
This algorithm finds the largest of the numbers a, b, and c.
Input: a, b, c
Output: large (the largest of a, b, and c)
1. max3 (a, b, c) {
2. large = a
4. large = b
6. large = c
7. return large
8. }
1
Algorithms consist of a title, a brief description of the algorithm, the input to and output
from the algorithm, and functions containing the instructions of the algorithm. This
algorithm has one function.
Sometimes lines are numbered to make it convenient to refer to them.
line 1 max3 is the name of the function, and a, b, c are input parameters or variables.
line 2 = is the assignment operator. Testing equality uses ==. large is assigned the value
of a.
arithmetic operators +, −, ∗, /, ( , )
relational operators ==, ¬ =, <, >, ≤, ≥
logical operators ∧, ∨, ¬
2
The notation // signals that the rest of the line is a comment. A more common
notation is to use %, which is used, for example, by Matlab and postscript. The
Matlab equivalent command is
if expression
commands if true
else
commands if false
end
Matlab can extend the if structure further with elseif.
We can assign values to the input variables and use a simulation called a trace to evaluate
the operation of the algorithm. For example, we set
a = 6, b = 1, c = 9.
Then
line 5 The if statement c > large is true, so at line 6 large is set to the value of c, namely 9.
3
The structure of the for loop is
for var = init to limit
action
If action consists of multiple statements, enclose them in braces.
The for loop specifies the initial and final integer values between which the operations are
processed. Alternatively, we can use a while loop.
while (condition)
action
action is repeated as long as condition is true.
The Matlab equivalent command is
for i = m:n
commands
end
Using a while loop, Algorithm 4.1.2 would become
max (s, n) {
large = s1
i=2
while i ≤ n {
if (si > large)
large = si
i=i+1
}
return large
}
The Matlab equivalent command is
while expression
commands
end
4
Examples of Algorithms
while (ti+j−1 == pj ) {
j =j+1
if (j > m)
return i
}
}
return 0
}
The algorithm indexes the characters in the text with i and those in the pattern with j.
The search starts with the first character in the text, and if the pattern is not found,
finishes with character n − m + 1 in the text, when the last characters in the pattern and
text coincide.
If there is a match for the first indices in the pattern and text, then the pattern index j
is incremented by 1, and the next characters compared. This continues until either the
pattern is found, or there is not a match. In the latter case, the text index i is incremented
by 1, the pattern index j is reset to 1, and the match process repeats.
5
Example 1. This shows the operation of the text search algorithm in a search for the
string “001” in the string “010001”.
1. The text index i and the pattern index j are initially set to 1, and the first characters
compared. There is a match.
2. The pattern index j is incremented to 2, and the pattern and text characters j and
i + j − 1 compared. There is not a match.
3. The text index i is incremented to 2 and the pattern index j reset to 1. There is
not a match in the corresponding characters.
4. The text index i is incremented to 3 and the pattern index j left at 1. There is a
match.
5. The pattern index j is incremented to 2, and the pattern and text characters j and
i + j − 1 compared. There is a match.
6. The pattern index j is incremented to 3, and the pattern and text characters j and
i + j − 1 compared. There is not a match.
7. The text index i is incremented to 4 and the pattern index j reset to 1. There is a
match.
8. The pattern index j is incremented to 2, and the pattern and text characters j and
i + j − 1 compared. There is a match.
9. The pattern index j is incremented to 3, and the pattern and text characters j and
i + j − 1 compared. There is a match, and the pattern is found.
6
Example 2. Algorithm Testing Whether a Positive Integer is Prime
This algorithm finds whether a positive integer is prime or composite.
Input: m, a positive integer
Output: true, if m is prime; false, if m is not prime
is prime(m) {
√
for i = 2 to m
if (m mod i == 0)
return false
return true
}
√
Note that the modulus operation need √ only go up to m, since if m is not prime, one
factor will be less than or equal to m and one factor will be greater than or equal to
√
m. If i mod m is 0, then i divides m, false is returned, and function is terminated. If
the for loop runs to its conclusion, control passes to the fifth line and true is returned.
7
Analysis of Algorithms
(c) A set X contains red items and black items. Count all subsets that contain at least
one red item. Since there are 2n subsets, the time taken behaves as 2n .
(d) In the travelling salesperson problem, the salesperson visits n towns in some order.
There are 12 (n − 1)! possible tours of n towns.
Question: Which grows the faster, 2n or 21 (n − 1)! .
8
Number of Steps
for Input Time to Execute if n =
of Size n 3 6 9 12
1 10 sec
−6
10 sec
−6
10 sec
−6
10 sec
−6
Number of Steps
for Input Time to Execute if n =
of Size n 50 100 1000
1 10 sec
−6
10 sec
−6
10 sec
−6
Number of Steps
for Input Time to Execute if n =
5
of Size n 10 106
1 10−6 sec 10−6 sec
lg lg n 4 × 10−6 sec 4 × 10−6 sec
lg n 2 × 10−5 sec 2 × 10−5 sec
n 0.1 sec 1 sec
n lg n 2 sec 20 sec
n2 3 hr 12 days
n3 32 yr 31, 710 yr
2n 3 × 1030089 yr 3 × 10301016 yr
TABLE 4.3.1 Time to execute an algorithm if one step takes 1 microsecond to execute.
9
Often we are less interested in the best- or worst-case times for an algorithm to execute
than in how the times grow as n increases.
n3 n
t(n) = + n2 − .
3 3
Then for n = 10, 100, 1, 000, 10, 000 we have the table
n n3 /3 + n2 − n/3 n3 /3
10 430 333
100 343, 300 333, 333
1, 000 334, 333, 000 333, 333, 333
10, 000 3.334 × 1011 3.333 × 1011
For large n,
n3
t(n) ≈ .
3
Hence t(n) is of order n3 , ignoring the constant 31 .
10
Example 3. f (n) = 4n + 3. Then
f (n) ≤ 4n + 3n
= 7n,
f (n) ≥ 4n,
so f (n) = Ω(n).
Therefore f (n) = Θ(n).
f (n) ≥ 2n2 ,
so f (n) = Ω (n2 ).
Therefore f (n) = Θ (n2 ).
n3 n
Example 2. (Continued) t(n) = + n2 − .
3 3
n3
t(n) ≤ + n2
3
n3
≤ + n3
3
4 3
= n,
3
so t(n) = O (n3 ). Also
n3 n2
t(n) ≥ + n2 −
3 3
3
n 2
= + n2
3 3
n3
≥ ,
3
so t(n) = Ω (n3 ).
Therefore t(n) = Θ (n3 ).
11
Theorem 4.3.4 Let
Proof:
p(n) = ak nk + ak−1 nk−1 + · · · + a1 n + a0
≤ ak nk + ak−1 nk + · · · + a1 nk + a0 nk
= (ak + ak−1 + · · · + a1 + a0 ) nk
= C1 nk ,
so p(n) = O nk . Also
p(n) ≥ ak nk ,
so p(n) = Ω nk .
Therefore t(n) = Θ nk .
y
y = 2n
256
y = n2
128
64 y = n lg n
32
16 y=n
8
y = lg n
4
2
y=1
1
1 2 3 4 5 6 7 8 9 10 11 12 13 n
12
Example 5. f (n) = 2n + 3 lg n.
Remember that lg n represents log2 n.
Does your calculator have an lg button? If not,
y = lg n
n = 2y
ln n = ln (2y )
= y ln 2, so
ln n
y = .
ln 2
Now lg n < n for all n ≥ 1. (See the preceding graph). Therefore
f (n) = 2n + 3 lg n
< 2n + 3n
= 5n,
f (n) ≥ 2n,
so f (n) = Ω (n).
Therefore f (n) = Θ (n).
Example 6. f (n) = 1 + 2 + 3 + · · · + (n − 1) + n.
n(n + 1)
This example assumes that we don’t know that the sum is .
2
1 + 2 + 3 + · · · + (n − 1) + n ≤ n + n + n + · · · + n + n
= n2 ,
so f (n) = O (n).
Also
1 + 2 + 3 + · · · + (n − 1) + n ≤ 1 + 1 + 1 + · · · + 1 + 1
= n,
so f (n) = Ω (n).
This seems to be too low an estimate. We can do better. Let’s be trickier, and throw
away approximately the first half of the series.
n n
1 + 2 + 3 + · · · + (n − 1) + n ≥ + + 1 + · · · + (n − 1) + n
2 2
n n n
≥ + + ··· + .
2 2 2
13
n+1
If n = 8 4 + 5 + 6 + 7 + 8 = 5 terms i.e. .
2
n+1
If n = 9 5 + 6 + 7 + 8 + 9 = 5 terms i.e. .
2
If n = 2k k + (k + 1) + · · · + 2k = 2k − (k − 1) = k + 1 terms.
If n = 2k + 1 (k + 1) + (k + 2) + · · · + (2k + 1) = (2k + 1) − k = k + 1 terms.
n+1
Hence there are terms. Therefore
2
n+1 n
f (n) ≥
2 2
n n
≥ ·
2 2
n2
= ,
4
so f (n) = Ω (n2 ).
Therefore f (n) = Θ (n).
n(n + 1)
If we know that f (n) = , then
2
f (n) = 12 n2 + 1
2n
≤ 12 n2 + 1 2
2n
= n2 ,
so f (n) = O (n2 ).
Also
f (n) ≥ 12 n2 ,
so f (n) = Ω (n2 ).
Therefore f (n) = Θ (n2 ).
14
Discrete Mathematics: Week 7
Analysis of Algorithms (Continued)
f (n) = 1k + 2k + 3k + · · · + (n − 1)k + nk
≤ nk + nk + nk + · · · + nk + nk
= n × nk
= nk+1 .
So f (n) = O nk+1 .
n k n k
f (n) ≥ + + 1 + · · · + (n − 1)k + nk
2 2
n n n
k k k
≥ + + ··· +
2 2 2
n+1 n
k
=
2 2
n+1 n
k
≥
2 2
1 h i
= (n + 1)n k
2k+1
1 k+1
= n + nk
2k+1
1 k+1
≥ n .
2 k+1
So f (n) = Ω nk+1 .
.·. f (n) = Θ nk+1 .
n! = n × (n − 1) × (n − 2) × · · · × 3 × 2 × 1.
Hence 1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
1
etc.
My calculator runs out at 69! , as 70! > 10100 . What is the limit on your calculator?
Then 0! is defined to be 1. What is 1 12 ! ?
Stirling’s formula gives an approximation for n! :
√ n
n
n! ≈ 2πn .
e
Hence
ln n! ≈ 12 ln 2π + 21 ln n + n ln n − n.
So we suspect that
lg n! = Θ (n lg n) .
Then, taking lg of n! ,
lg n! = lg n + lg (n − 1) + · · · + lg 2 + lg 1
≤ lg n + lg n + · · · + lg n + lg n
= n lg n.
Definition. If an algorithm requires t(n) units of time to terminate in the best case for
an input of size n, and
t(n) = O (g(n)) ,
we say that the best-case time required by the algorithm is of order at most g(n) or that
the best-case time required by the algorithm is O (g(n)).
If an algorithm requires t(n) units of time to terminate in the worst case for an input of
size n, and
t(n) = O (g(n)) ,
2
we say that the worst-case time required by the algorithm is of order at most g(n) or that
the worst-case time required by the algorithm is O (g(n)).
If an algorithm requires t(n) units of time to terminate in the average case for an input
of size n, and
t(n) = O (g(n)) ,
we say that the average-case time required by the algorithm is of order at most g(n) or
that the average-case time required by the algorithm is O (g(n)).
Replace O by Ω and “at most” by “at least” to obtain the definition of what it means
for the best-case, worst-case or average-case time of an algorithm to be of order at least
g(n).
If the best case time is O (g(n)) and Ω (g(n)), then the best-case time required by the
algorithm is Θ (g(n)).
Similar definitions apply for the worst-case and average-case times.
Example 1. (b) (Continued)
Johnsonbaugh Algorithm 4.3.17. Searching for a key in an unordered sequence.
The best-case time is 1 i.e. Θ(1).
The worst-case time is n i.e. Θ(n).
The average-case time is
(1 + 2 + 3 + · · · + n) + n n2 + 3n
=
n+1 2(n + 1)
i.e. Θ(n).
Example 9. Consider the pseudocode
for i = 1 to n
for j = 1 to i
x=x+1
Find a theta notation for the number of times the statement x = x + 1 is executed.
The number of times is
1 + 2 + 3 + · · · + n = 12 n2 + 12 n,
which is Θ(n2 ).
Example 10. Consider the pseudocode
i=n
while (i ≥ 1) {
x=x+1
i = ⌊ i/2 ⌋
}
Find a theta notation for the number of times the statement x = x + 1 is executed.
3
Suppose that n = 8. Then for
i=8 x=x+1 is executed
i=4 x=x+1 is executed
i=2 x=x+1 is executed
i=1 x=x+1 is executed
i=0 x=x+1 is not executed.
t(n) ≤
1 − 21
= 2n 1 − 21k
≤ 2n.
4
.·. t(n) = O(n).
.·. t(n) = Θ(n).
A “good” algorithm has a worst-case polynomial time, and such problems are called
feasible or tractable problems. Exponential or factorial worst-case time problems are
called intractable, and require a long time to execute even for reletively small n.
There are problems for which there is no algorithm. These are called unsolvable. Such a
problem is the halting problem: given an arbitrary program and a set of inputs, will the
program ever halt?
A large number of solvable problems have an undetermined status. They are thought to
be intractable, but none have ever been proved to be intractable. Such a problem is the
travelling salesperson problem.
5
Recursive Algorithms
Problem Solution
0! 1
1! 1 · 0! = 1
2! 2 · 1! = 2
3! 3 · 2! = 6
4! 4 · 3! = 24
5! 5 · 4! = 120
1. factorial (n) {
2. if (n == 0)
3. return 1
4. return n∗factorial (n − 1)
5. }
6
We can see how the algorithm computes n!.
If n = 1, proceed to line 4 since n 6= 0, and compute
n · (n − 1)! = 1 · 0! = 1 · 1 = 1.
n · (n − 1)! = 2 · 1! = 2 · 1 = 2,
and so on.
Example 2. This algorithm recursively finds the smallest of a finite sequence of numbers.
Algorithm Recursively Finding the Minimum Value in a Sequence
This algorithm finds the smallest of the numbers s1 , s2 , . . . , sn .
Input: s, n
Output: small, (the smallest value in the sequence s)
min(s, n) {
if (n == 1) {
small = s1
return small
}
else {
small = min(s, n − 1)
if small ≤ sn
return small
else {
small = sn
return small
}
}
}
7
If n = 1, there is only one number in the sequence, and it is returned.
If n = 2, then min(s, 1) is recursively called, and this returns s1 . This is compared with
s2 , and the smaller returned.
If n = 3, then min(s, 2) is recursively called, and this recursively calls min(s, 1), which
returns s1 . Then min(s, 2) returns the smaller of s1 and s2 , and this is compared with s3
and the smaller returned.
Theorem: This algorithm correctly returns the value of the smallest of a finite sequence
of numbers.
Proof: Basis Step: For n = 1, the algorithm returns the only number in the sequence.
Inductive Step: Assume that the algorithm correctly returns the smallest value in sequence
of length n − 1.
Then for a sequence of length n, n > 1, the algorithm correctly computes the smallest of
s1 , s2 , . . . , sn−1 , compares this with sn , and returns the smaller.
Therefore the algorithm correctly returns the smallest number in a sequence of length n.
Input: n
Output: walk (n)
walk (n) {
if (n == 1 ∨ n == 2)
8
return n
return walk (n − 1) + walk (n − 2)
}
We can see that the sequence generated is
f1 = 1
f2 = 1
fn = fn−1 + fn−2 , for all n ≥ 3.
The Fibonacci sequences arises in many natural situations, as well as mathematical ones.
For example, a pine cone can have 13 clockwise spirals and 8 counterclockwise spirals.
The ratio of successive terms in the Fibonacci sequence has a limit of the golden ratio,
√
lim fn 1 + 5
n → ∞ fn−1 = φ = 2
= 1.6180339887498 . . . .
55
For example, = 1.6176 . . . .
34
A classical formula for the Fibonacci numbers is
" √ !n √ !n #
1 1+ 5 1− 5
√ − .
5 2 2
√
All those 5 occurrences, and the answer is an integer!
9
Discrete Mathematics: Week 8
Graph Theory
Introduction to Graphs
Graph theory was introduced by Leonhard Euler in 1736, as a means of solving the
Königsberg bridge problem. There was a revival of graph theory in the 1920’s, with the
first text produced in 1936. Graph theory involves a lot of terminology, as will be seen.
A graph is drawn with dots and lines, the dots being vertices and the lines are edges. The
important information in the graph is the connections, not the positions of the vertices
and edges.
v1
v1 e1 v2
e2 e1 v3 e6
e3
e6 e4 v3 e2
e5
v4
e3 e4
v5 v4 v2 e5 v5
A path starts at one vertex v1 , travels along an edge to vertex v2 , and so on, arriving at
vn .
For a path to traverse every edge exactly once, and return to the original vertex, an even
number of edges must touch each vertex, for example, the graph in Example 1. In the
graph shown below, a path can traverse each edge exactly once, but will not return to the
original vertex.
v1 v2
v4 v3
Definition 8.1.1
A graph (or undirected graph) G consists of a set V of vertices (or nodes) and a set E of
edges (or arcs) such that each edge e ∈ E is associated with an unordered pair of vertices.
If there is a unique edge e associated with the vertices v and w, we write e = (v, w) or
1
e = (w, v). In this context, (v, w) denotes an edge between v and w in an undirected
graph and not an ordered pair.
A directed graph (or digraph) G consists of a set V of vertices (or nodes) and a set E of
edges (or arcs) such that each edge e ∈ E is associated with an ordered pair of vertices. If
there is a unique edge e associated with ordered pair (v, w) of vertices, we write e = (v, w),
which denotes an edge from v to w.
An edge e in a graph (undirected or directed) that is associated with the pair of vertices
v and w is said to be incident on v and w, and v and w are said to be incident on e and
to be adjacent vertices.
If G is a graph (undirected or directed) with vertices V and edges E, we write G = (V, E).
Unless specified otherwise, the sets E and V are assumed to be finite and V is assumed
to be nonempty.
Example 1. (Continued)
V = { v1 , v2 , v3 , v4 , v5 }
E = { e1 , e2 , e3 , e4 , e5 , e6 }
e2 = (v2 , v3 ) = (v3 , v2 )
Edge e4 is incident on vertices v2 and v4 , and vertices v2 and v4 are adjacent.
Example 2. The graph shown below is a directed graph.
v1
e1
e3
v2 v3
e4
v4
e2 e5
e6
v6
v5
e7
v7
e1
v1 v2
e2
2
An edge incident on a single vertex is called a loop e.g. e7 .
A vertex not incident to any edge is called isolated e.g. v7 .
A graph with neither loops nor parallel edges is called a simple graph.
b
8
a 8 b
a
6
6
2
6 9 2 6 9
4
c
3
4 12
d 5
e 5 c
4
e
4 3
12
d
Path Length
b, a, c, d, e 17
b, a, d, c, e 20
b, c, a, d, e 16
b, c, d, a, e 19
b, d, a, c, e 23
b, d, c, a, e 23
(a, b), (a, c), (a, d), (a, e), (b, c), (b, d), (b, e), (c, d), (c, e), (d, e).
3
Example 4. Johnsonbaugh Example 8.1.7: Similarity Graphs.
A particular algorithm in C++ is implemented by a number of persons. We wish to group
“like” programs into classes based on program properties. The properties are:
For a fixed number S, inset an edge between vertices v and w if s(v, w) < S. For example,
if S = 25 we have the graph:
v1 v2
v3
v4 v5
We say that v and w are in the same class if v = w or if there is a path from v to w. Here
the classes are { 1, 3, 5 }, { 2 } and { 4 }. What are the classes if S = 40?
4
Definition 8.1.9
The complete graph on n vertices, denoted Kn , is the simple graph with n vertices in
which there is an edge between each pair of distinct vertices.
Definition 8.1.11
A graph G = (V, E) is bipartite if there exist subsets V1 and V2 (either possibly empty)
of V such that V1 ∩ V2 = ∅, V1 ∪ V2 = V , and each edge in E is incident on one vertex in
V1 and one vertex in V2 .
V1 = { v1 , v2 , v3 } and V2 = { v4 , v5 } .
v1
v4
v2
v5
v3
Note that it is not required that there is an edge between every vertex in V1 and every
vertex in V2 .
v1 v2
v3 v7
v4
v5 v6
v8 v9
5
It is often easier to prove that a graph is not bipartite by arguing a contradiction.
Suppose the graph is bipartite. Then we can partition the set of vertices into two subsets
V1 and V2 .
Consider v4 , v5 and v6 . Since v4 and v5 are adjacent, v4 is in V1 (say) and v5 is in V2 .
Since v5 and v6 are adjacent, v6 is in V1 .
But v4 and v6 are adjacent and both are in V1 . Contradiction. Hence the graph is not
bipartite.
Definition 8.1.15
The complete bipartite graph on m and n vertices, denoted Km,n , is the simple graph
whose vertex set is partitioned into sets V1 with m vertices and V2 with n vertices in
which the edge set consists of all edges of the form (v1 , v2 ) with v1 ∈ V1 and v2 ∈ V2 .
Example 8. The complete bipartite graph on two and four vertices, K2,4 , is:
6
Paths and Cycles
Definition 8.2.1
Let v0 and vn be vertices in a graph. A path from v0 to vn of length n is an alternating
sequence of n + 1 vertices and n edges beginning with vertex v0 and ending with vertex
vn ,
(v0 , e1 , v1 , e2 , v2 , . . . vn−1 , en , vn ),
in which edge ei is incident on vertices vi−1 and vi for i = 1, . . . , n.
The formalism in Definition 8.2.1 means: Start at vertex v0 ; go along edge e1 to v1 ; go
along edge e2 to v2 ; and so on.
Definition 8.2.4
A graph G is connected if given any vertices v and w in G, there is a path from v to w.
Example 1. Consider the following graph.
v2
e11
v1
e10 v7
e2
e1 e12
v4 e9
v6
v3 e3 e5
e4
e6
v5
e8 v9
e7
v8
is not connected.
A connected graph consists of one “piece”, and a graph that is not connected consists of
two or more “pieces”. These “pieces” are subgraphs of the original graph called compo-
nents.
7
Definition 8.2.8
Let G = (V, E) be a graph. We call (V ′ , E ′ ) a subgraph of G if
(a) V ′ ⊆ V and E ′ ⊆ E.
v2
e11
v1
v7
e2
e1 v4
v3 e3
e4
v5
Definition 8.2.11
Let G be a graph and let v be a vertex in G. The subgraph G′ of G consisting of all edges
and vertices in G that are contained in some path beginning at v is called the component
of G containing v.
A connected graph, such as Example 1, has only one component, itself.
A graph such as
e1
G1 : v1 v4
e1
v1 v4 and
has two
G: v2 e2 components G2 : v2 e2
v5 v5
v3 e3 v3 e3
A cycle (or circuit) is a path of nonzero length from v to v with no repeated edges.
8
A simple cycle is a cycle from v to v, in which, except for the beginning and ending
vertices that are both equal to v, there are no repeated vertices.
Example 1. (Continued)
Pregel
B C
River
The problem is to start at any location, walk over each bridge exactly once, and finish at
the starting location.
Leonhard Euler solved the problem in 1736. The problem can be represented as the
following graph, where the vertices represent the locations and the edges represent the
bridges.
B C
9
Euler showed that there is no solution, as all vertices have an odd number of incident
edges.
A cycle in graph G that includes all of the edges and all of the vertices of G is called an
Euler cycle, in honour of Euler.
We introduce the idea of the degree of a vertex v, δ(v). This is the number of edges
incident on v.
Theorem 8.2.17
If a graph G has an Euler cycle, then G is connected and every vertex has even degree.
Proof: Suppose that G has an Euler cycle.
We have seen the argument that during the cycle, the path leaves each vertex for every
time that it is entered, and so all vertices must be of even degree.
If v and w are vertices of G, then the portion of the Euler cycle between v and w is a
path from v to w, and so G is connected.
Theorem 8.2.18
If G is a connected graph and every vertex has even degree, then G has an Euler cycle.
Proof: Johnsonbaugh gives a mathematical induction proof. See the text.
Example 2. For the Königsberg bridge problem,
δ(A) = 3, δ(B) = 5, δ(C) = 3, δ(D) = 3,
so there is not an Euler cycle.
If the graph G is as shown below, then G is connected an every vertex has even degree.
δ (v1 ) = δ (v2 ) = δ (v3 ) = δ (v5 ) = 4, δ (v4 ) = 6, δ (v6 ) = δ (v7 ) = 2.
v1
v2
v4
v3 v5
v6 v7
10
Theorem 8.2.21
If G is a graph with m edges and vertices {v1 , v2 , . . . , vn }, then
n
X
δ(vi ) = 2m.
i=1
In particular, the sum of degrees of all of the vertices of the graph is even.
Proof: Each edge is counted twice, once from vi to vj , and once from vj to vi .
Corollary 8.2.22
In any graph, there are an even number of vertices of odd degree.
Theorem 8.2.23
A graph has a path with no repeated edges from v to w (v 6= w) containing all the edges
and vertices if and only if it is connected and v and w are the only vertices of odd degree.
For example, the graph below.
v1
v2 v5
v3 v4
Proof: Add an edge from v to w. Now thare is an Euler cycle as all vertices are of even
degree.
Theorem 8.2.24
If a graph G contains a cycle from v to v, G contains a simple cycle from v to v.
Proof: If C is a cycle from v0 to vn (v0 = vn ), and C is not a simple cycle, then there
must be vertices vi = vj in the path where i < j < n. Remove the portion of the path
between vi and vj , and repeat the procedure if necessary. The cycle is eventually reduced
to a simple cycle.
11
Discrete Mathematics: Week 9
Trees
Introduction
Example 1. Johnsonbaugh example on the draw for the semifinals and finals at Wim-
bledon (many years ago).
Seles
Seles
Navratilova
Graf
Sabatini
Graf WIMBLEDON
CHAMPION
Graf
FINALS
SEMIFINALS
The draw can be represented as a graph called a tree. It is rotated clockwise through 90◦
so as to appear as is shown on the right. If it is rotated through 90◦ the other way then
it would appear as a natural tree.
v4 height = 2
v2
v1
v5 level 0
v1 v2 v3
v6 level 1
v3 level 2
v7 v4 v5 v6 v7
Definition 9.1.1
A (free) tree T is a simple graph satisfying the following: If v and w are vertices in T ,
then there is a unique simple path from v to w.
A rooted tree is a tree in which a particular vertex is designated the root.
The level of a vertex v is the length of the simple path from the root to v.
The height of a rooted tree is the maximum level number that occurs.
1
PSfrag replacemen
Example 2. Johnsonbaugh 9.1.4 The tree T shown below will become the rooted tree T ′
if we designate vertex e as the root.
i e
d
a b g
g
d f
b j
e i
a c h
h
f
c j
Huffman Codes
Character representation is often by fixed-length bit strings e.g. ASCII where eight bit
strings are used for the 256 extended character set.
Huffman codes represent characters by variable-length bit strings. Short bit strings are
used to represent the most frequently used characters.
A Huffman code is most easily defined by a rooted tree. To decode a bit string, begin at
the root and move down the tree until a character is encountered. The bit, 0 or 1, means
move right or left. When a character is encountered, begin again at the root.
Given a tree that defines a Huffman code, any bit string can be uniquely decoded even
though the characters are represented by variable-length bit strings.
Root
1 0
A 1 0
O
1 0
R
1 0
T S
2
Algorithm 9.1.9 Constructing an Optimal Huffman Code
This algorithm constructs an optimal Huffman code from a table giving the frequency
of occurrence of the characters to be represented. The output is a rooted tree with the
vertices at the lowest levels labelled with the frequencies and with the edges labelled with
bits. The coding tree is obtained by replacing each frequency by a character having that
frequency.
huffman(f, n) {
if (n == 2) {
let f1 and f2 denote the frequencies
let T be as in the figure (on the left)
return T
}
let fi and fj denote the smallest frequencies
replace fi and fj in the list by fi + fj
T ′ = huffman(f, n − 1)
replace a vertex in T ′ labelled fi + fj by the
tree shown in the figure (on the right) to obtain the tree T
return T
}
1 0 1 0
f1 f2 fi fj
Example 4. We have the following table of frequencies.
Letter Frequency
A 10
B 12
C 17
D 8
E 22
3
The algorithm begins by repeatedly replacing the two smallest frequencies with the sum
until a two element sequence is obtained.
The algorithm then constructs trees working backward, beginning with the two element
sequence 29, 40.
1 0 1 0 1 0 1 0
29 40 29 1 0 1 0 1 0 1 0 1 0
18 22 12 17 18 22 12 17 1 0 22
8 10
1 0
1 0 1 0
B C 1 0 E
D A
4
Terminology and Characterizations of Trees
Definition 9.2.1
Let T be a tree with root v0 . Suppose that x, y and z are vertices in T and that
(v0 , v1 , . . . , vn ) is a simple path in T . Then
(h) The subtree of T rooted at x is the graph with vertex set V and edge set E, where
V is x with the descendants of x and
e i
h
b
d f j
a c
5
The subtree rooted at e is:
e
b
d f
a c
Uranus
Kronos
6
Theorem 9.2.3
Let T be a graph with n vertices. The following are equivalent.
(a) T is a tree.
if (a), then (b); if (b), then (c); if (c), then (d); if (d), then (a),
7
Spanning Trees Johnsonbaugh 9.3
Definition 9.3.1
A tree T is a spanning tree of a graph G if T is a subgraph of G that contains all of the
vertices of G.
a b
c d
e f
h
g
Theorem 9.3.4
A graph G has a spanning tree if and only if G is connected.
Proof:
If G has a spanning tree, G is connected.
If G is connected, progressively remove edges from cycles until G is acyclic.
This procedure is inefficient in practice.
Algorithm 9.3.6 Breadth-First Search for a Spanning Tree
This algorithm is formally given in Johnsonbaugh, but a more informal description of the
procedure is as follows.
Select an ordering, e.g. abcdef gh of the vertices of G.
Select the first vertex as the root e.g. a.
The tree T initially consists of the single vertex a and no edges.
Add to T all edges (a, x) and the vertices on which they are incident, x = b to h, that do
not produce a cycle when added to T . This gives all level 1 vertices.
Repeat with the vertices on level 1, then level 2, and so on, until no further edges can be
added.
8
Example 1. (Continued)
Select the ordering abcdef gh.
Select a as the root.
Add edges (a, b), (a, c), (a, g).
a b
c d
e f
h
g
9
Select the ordering abcdef gh.
Select a as the root.
Add edges (a, b), (b, d), (d, c), (c, e), (e, f ), (f, h).
Backtrack to f , then e. Add (e, g).
Backtrack to e, c, d, b, a. Ends.
Definition 9.4.1
Let G be a weighted graph. A minimal spanning tree of G is a spanning tree of G with
minimum weight.
Example 1. There are six cities 1–6, and the costs of building roads between certain
pairs of them are shown on the following graph.
1 4 2
3 2 5
1
4
3
5 6
3
6
2
6
10
1 4 2
3 2 5
4
3
5
3
Example 1. (Continued)
Begin with vertex 1. Edges with one vertex in the tree and one vertex not in the tree are:
Edge Weight
(1, 2) 4
Select edge (1, 3)
(1, 3) 2
with minimum weight.
(1, 5) 3
Edges with one vertex in the tree and one vertex not in the tree are:
Edge Weight
(1, 2) 4
(1, 5) 3
Select edge (3, 4)
(3, 4) 1
with minimum weight.
(3, 5) 6
(3, 6) 3
Edges with one vertex in the tree and one vertex not in the tree are:
Edge Weight The minimum spanning tree
(1, 2) 4 will be constructed when
(1, 5) 3 either (1, 5) or (3, 6)
(3, 5) 6 is selected.
(3, 6) 3 Select edge (1, 5).
(4, 2) 5 with minimum weight.
(4, 6) 6
11
Edges with one vertex in the tree and one vertex not in the tree are:
Edge Weight
(1, 2) 4
(3, 6) 3
Select edge (5, 6)
(4, 2) 5
with minimum weight.
(4, 6) 6
(5, 6) 2
Edges with one vertex in the tree and one vertex not in the tree are:
Edge Weight
(1, 2) 4 Select edge (1, 2)
(4, 2) 5 with minimum weight.
The minimal spanning tree, shown below, has weight 12.
1 4 2
3 2
1
4
3
5
2
6
12
Discrete Mathematics: Week 10
Binary Trees
Definition 9.5.1
A binary tree is a rooted tree in which each vertex has either no children, one child, or
two children. If a vertex has one child, that child is designated as either a left child or
a right child (but not both). If a vertex has two children, one child is designated a left
child and the other child is designated a right child.
b c
e
d
f g
A full binary tree is a binary tree in which each vertex has either two children or zero
children.
Theorem 9.5.4
If T is a full binary tree with i internal vertices, then T has i + 1 terminal vertices and
2i + 1 total vertices.
Proof:
The i internal vertices each have two children, so there are 2i children.
Only the root is a nonchild.
Therefore the total number of vertices is 2i + 1, and the number of terminal vertices is
(2i + 1) − i = i + 1.
1
Contestant 1
Contestant 2
Contestant 3
Contestant 4
Winner
Contestant 5
Contestant 6
Contestant 7
Theorem 9.5.6
If a binary tree of height h has t terminal vertices, then
lg t ≤ h.
(Or equivalently, t ≤ 2h .)
Proof: We will prove t ≤ 2h by mathematical induction.
Basis Step: If h = 0, the binary tree has a single vertex, and t = 1. Then t = 2h = 20 = 1.
Inductive Step: Assume true for a binary tree with height less than h.
Let T be a binary tree of height t > 0 with t terminal vertices. Suppose that the root of
T has one child. Eliminate the root and edge incident on the root. The remaining tree
has height h − 1 and t terminal vertices. By the assumption,
t ≤ 2h−1 < 2h
h1 ≤ h − 1 and h2 ≤ h − 1,
and
t = t1 + t2 ≤ 2h1 + 2h2 ≤ 2h−1 + ≤ 2h−1 = 2h .
Hence the inductive step is verified.
Since the Basis Step and Inductive Step have been verified, the Principle of Mathematical
Induction tells us that the theorem is true.
2
Definition 9.5.8
A binary search tree is a binary tree T in which data are associated with the vertices.
The data are arranged so that, for each vertex v in T , each data item in the left subtree
of v is less than the data item in v, and each data item in the right subtree of v is greater
than the data item in v.
Algorithm 9.5.10 in Johnsonbaugh gives a formal approach for constructing a binary
search tree. A less formal approach is as follows.
• Compare each following item in turn with the current vertex, beginning with the
root.
• Move to the next item and start over, comparing with the root.
o, n, p, d, u, j, t, l, m,
n p
d u
j t
3
Example 4. Build a binary search tree for the words
“by nineteen ninety no Australian child will be living in poverty”.
by
Australian nineteen
be child ninety
living no
in will
poverty
4
For example, if a tree contains a million items, then
so that a search will find an item, or determine if it is not present, in at most 20 steps.
There are algorithms to minimize the height of a binary search tree.
Bread-first search and depth-first search are ways to traverse a tree in a systematic way
such that each vertex is visited exactly once. This section considers three other tree-
traversal methods, which are defined recursively. Johnsonbaugh gives formal algorithms
for each of these, we will consider simpler formulations.
Preorder Traversal Recursive algorithm 9.6.1
Preorder the root: process the root, preorder the left child, preorder the right child.
Inorder Traversal Recursive algorithm 9.6.3
Inorder the root: inorder the left child, process the root, inorder the right child.
Postorder Traversal Recursive algorithm 9.6.5
Postorder the root: postorder the left child, postorder the right child, process the root.
B F
C D G
E H
I J
Preorder: A B F
ABC D F G
ABCDEFG H
ABCDEFGHIJ
5
Inorder: B A F
CB D AF G
CBDEAF H G
CBDEAFIHJG
Postorder: B F A
C D B G FA
CEDB H GFA
CEDBIJHGFA
Arithmetic Expressions
The operators +, −, × and ÷ ( or +, −, ∗, / ) operate on pairs of operands or expressions,
where the operator appears between its operands. An example is
(A + B) × C − D ÷ E.
× ÷
+ C D E
A B
For inorder traversal, parentheses are put around each operation. The parentheses dictate
the order of the operations, and the hierarchy of the operators need not be specified. Some
pairs of pararentheses may not be necessary.
Inorder: × − ÷
+ ×C − (D ÷ E )
(((A + B ) × C ) − (D ÷ E ))
6
The preorder traversal is as follows. This is known as the prefix form of the expression or
Polish notation. No parentheses are required for unambiguous evaluation.
Preorder: − × ÷
− × + C ÷ DE
− × +ABC ÷ DE
The postorder traversal is as follows. This is known as the postfix form of the expression or
reverse Polish notation. Again, no parentheses are required for unambiguous evaluation.
Postorder: × ÷ −
+ C × DE ÷ −
AB + C × DE ÷ −