All Chapter Slide
All Chapter Slide
Mathematics
Dr. Atiqur Rahman
ড. আতিকু র রহমান
Ph.D.(CQUPT, China), MS.Engg.(CU), B.Sc.(CU)
Associate Professor
Department of Computer Science and Engineering
University of Chittagong
Course objectives
We will focus on two major goals:
Basic tools and techniques in discrete
mathematics
– Propositional logic
– Set Theory
– Simple algorithms
– Induction, recursion
– Counting techniques (Combinatorics)
Precise and rigorous mathematical reasoning
– Writing proofs
2
Let’s get started with...
Logic!
The basic building blocks of logic—
propositions.
A proposition is a declarative sentence (that is, a sentence that declares
a fact) that is either true or false, but not both.
Propositions:
York University is in Toronto
York University is in downtown Toronto
All students at York are Computer Sc. majors.
Not propositions:
Do you like this class?
There are x students in this class.
5
The Statement/Proposition Game
“y > 5”
Is this a statement? no
It’s a request.
Is this a proposition? no
13
Combining Propositions
• Boolean logic:
• Implications:
16
Negation (NOT)
P P
true (T) false (F)
false (F) true (T)
19
Conjunction (AND)
Binary Operator, Symbol:
P Q PQ
T T T
T F F
F T F
F F F
22
Exclusive Or (XOR)
Binary Operator, Symbol:
P Q P Q
T T F
T F T
F T T
F F F
P Q P Q (P)(Q)
T T F F F
T F F T T
F T T F T
F F T T T
T T F F T
T F T T T
F T T T T
F F T T T
32
Tautologies and Contradictions
36
E.g.: Proof using contrapositive
Prove: If x2 is even, x is even
Proof 1: x2 = 2a for some integer a. Since 2 is
prime, 2 must divide x.
Proof 2: if x is not even, x is odd. Therefore
x2 is odd. This is the contrapositive of the
original assertion.
37
Converse
Converse of p q is q p
Not logically equivalent to conditional
Ex 1: “If you get 100% in this course, you will
get an A+” and “If you get an A+ in this
course, you scored 100%” are not
equivalent.
Ex 2: If you won the lottery, you are rich.
38
Other conditionals
Inverse:
inverse of p q is p q
How is this related to the converse?
Biconditional:
“If and only if”
True if p, q have same truth values, false
otherwise. Q: How is this related to XOR?
Can also be defined as (p q) (q p)
39
Example
Q16(c). Determine whether these
biconditionals are true or false.
40
Readings and notes
Read pages 1-12.
Think about the notion of truth tables.
Master the rationale behind the
definition of conditionals.
Practice translating English sentences
to propositional logic statements.
41
Next
Ch. 1.2, 1.3: Propositional Logic -
contd
– Compound propositions, precedence rules
– Tautologies and logical equivalences
– Read only the first section called
“Translating English Sentences” in 1.2.
42
Compound Propositions
Example: p q r : Could be
interpreted as (p q) r or p (q
r)
precedence order: (IMP!)
(Overruled by brackets)
We use this order to compute truth
values of compound propositions.
43
Tautology
A compound proposition that is always TRUE,
e.g. q q
Logical equivalence redefined: p, q are logical
equivalences if p q is a tautology.
Symbolically p q.
Intuition: p q is true precisely when p, q
have the same truth values.
44
Manipulating Propositions
Compound propositions can be simplified by
using simple rules.
Read page 25 - 28.
Some are obvious, e.g. Identity, Domination,
Idempotence, double negation,
commutativity, associativity
Less obvious: Distributive, De Morgan’s laws,
Absorption
45
Distributive Laws
p (q r) (p q) (p r)
Intuition (not a proof!) – For the LHS to be true: p must
be true and q or r must be true. This is the same as
saying p and q must be true or p and r must be true.
p (q r) (p q) (p r)
Intuition (less obvious) – For the LHS to be true: p must
be true or both q and r must be true. This is the same
as saying p or q must be true and p or r must be true.
46
De Morgan’s Laws
(q r) q r
Intuition – For the LHS to be true: neither q nor r can
be true. This is the same as saying q and r must be
false.
(q r) q r
Intuition – For the LHS to be true: q r must be
false. This is the same as saying q or r must be
false.
48
Next: Predicate Logic
Ch 1.4
– Predicates and quantifiers
– Rules of Inference
49
Predicate Logic
A predicate is a proposition that is a function
of one or more variables.
E.g.: P(x): x is an even number. So P(1) is
false, P(2) is true,….
Examples of predicates:
– Domain ASCII characters - IsAlpha(x) :
TRUE iff x is an alphabetical character.
– Domain Integer numbers - IsInt(x):
TRUE iff x is an integer.
– Domain Primes: Prime(x) - TRUE if x is
prime, FALSE otherwise.
50
Propositional Functions
Propositional function (open sentence):
statement involving one or more variables,
e.g.: x-3 > 5.
53
Quantifiers
Describes the values of a variable
that make the predicate true.
E.g. x P(x)
Domain or universe: range of values
of a variable (sometimes implicit)
54
Two Popular Quantifiers
Universal: x P(x) – “P(x) for all x in the
domain”
Existential: x P(x) – “P(x) for some x in the
domain” or “there exists x such that P(x) is
TRUE”.
Either is meaningless if the domain is not
known/specified.
Examples (domain real numbers)
x (x2 >= 0)
x (x >1)
– (x>1) (x2 > x) – quantifier with restricted
domain
55
Universal Quantification
Let P(x) be a propositional function.
Is it true? yes
65
Nested Quantifiers
Allows simultaneous quantification of many
variables.
E.g. – domain integers,
x y z x2 + y2 = z2
n x y z xn + yn = zn (Fermat’s Last
Theorem)
Domain real numbers:
x y z (x < z < y) (y < z < x)
Is this true?
66
Nested Quantifiers - 2
x y (x + y = 0) is true over the integers
Assume an arbitrary integer x.
To show that there exists a y that satisfies the
requirement of the predicate, choose y = -x.
Clearly y is an integer, and thus is in the
domain.
So x + y = x + (-x) = x – x = 0.
Since we assumed nothing about x (other than
it is an integer), the argument holds for any
integer x.
Therefore, the predicate is TRUE.
67
Nested Quantifiers - 3
Caveat: In general, order matters! Consider
the following propositions over the integer
domain:
x y (x < y) and y x (x < y)
x y (x < y) : “there is no maximum integer”
y x (x < y) : “there is a maximum integer”
Not the same meaning at all!!!
68
… and now for something
completely different…
Set Theory
Actually, you will see that logic and
set theory are very closely related.
“Standard” Sets:
• Natural numbers N = {0, 1, 2, 3, …}
• Integers Z = {…, -2, -1, 0, 1, 2, …}
• Positive Integers Z+ = {1, 2, 3, 4, …}
• Real Numbers R = {47.3, -12, , …}
• Rational Numbers Q = {1.5, 2.6, -3.8, 15, …}
(correct definition will follow)
Examples:
A = {3, 9}, B = {5, 9, 1, 3}, AB? true
A = {3, 3, 3, 9}, B = {5, 9, 1, 3}, A B ? true
A = {1, 2, 3}, B = {2, 3, 4}, AB? false
Fall 2002 CMSC 203 - Discrete Structures 75
Subsets
Useful rules:
• A = B (A B) (B A)
• (A B) (B C) A C (see Venn Diagram)
B
A C
Proper subsets:
AB “A is a proper subset of B”
A B x (xA xB) x (xB xA)
or
A B x (xA xB) x (xB xA)
Examples:
A = {Mercedes, BMW, Porsche}, |A| = 3
B = {1, {2, 3}, {4, 5}, 6} |B| = 4
C= |C| = 0
D = { xN | x 7000 } |D| = 7001
E = { xN | x 7000 } E is infinite!
Examples:
A = {x, y, z}
P(A) = {, {x}, {y}, {z}, {x, y}, {x, z}, {y, z}, {x, y, z}}
A=
P(A) = {}
Note: |A| = 0, |P(A)| = 1
Fall 2002 CMSC 203 - Discrete Structures 79
The Power Set
Cardinality of power sets:
| P(A) | = 2|A|
• Imagine each element in A has an “on/off” switch
• Each possible switch configuration in A
corresponds to one element in 2A
A 1 2 3 4 5 6 7 8
x x x x x x x x x
y y y y y y y y y
z z z z z z z z z
• For 3 elements in A, there are
222 = 8 elements in P(A)
Fall 2002 CMSC 203 - Discrete Structures 80
Cartesian Product
The ordered n-tuple (a1, a2, a3, …, an) is an
ordered collection of objects.
Two ordered n-tuples (a1, a2, a3, …, an) and
(b1, b2, b3, …, bn) are equal if and only if they
contain exactly the same elements in the same
order, i.e. ai = bi for 1 i n.
Functions
f(Linda) = Moscow
f(Max) = Boston
f(Kathy) = Hong Kong
f(Peter) = New York
f(Linda) = Moscow
f(Max) = Boston
f(Kathy) = Hong Kong
f(Peter) = Boston
Example:
f1(x) = 3x, f2(x) = x + 5
(f1 + f2)(x) = f1(x) + f2(x) = 3x + x + 5 = 4x + 5
(f1f2)(x) = f1(x) f2(x) = 3x (x + 5) = 3x2 + 15x
Fall 2002 CMSC 203 - Discrete Structures 97
Functions
Examples:
In the following examples, we use the arrow
representation to illustrate functions f:AB.
Paul
Lübeck
Peter Moscow
Lübeck
Helena Lübeck
(f-1f)(x) = f-1(f(x)) = x
Sequences
S: 2 4 6 8 10 …
Fall 2002 CMSC 203 - Discrete Structures 122
Sequences
We use the notation {an} to describe a sequence.
1, 3, 5, 7, 9, … an = 2n - 1
What does a
j m
j stand for?
We write it as
j 1
j2 .
6
It is 1 + 2 + 3 + 4 + 5 + 6 = 21.
100
Observe that:
1 + 2 + 3 +…+ n/2 + (n/2 + 1) +…+ (n - 2) + (n - 1) + n
= [1 + n] + [2 + (n - 1)] + [3 + (n - 2)] +…+ [n/2 + (n/2 + 1)]
= n(n + 1)/2.
(a 1)
Observe that:
S = 1 + a + a 2 + a3 + … + a n
aS = a + a2 + a3 + … + an + a(n+1)
so, (aS - S) = (a - 1)S = a(n+1) - 1
(a 1)
n
n(n 1)(2n 1)
3. j 1
j
2
6
n
n (n 1)
2 2
4. j 1
j
3
ij
i 1 j 1
5
(i 2i )
i 1
5
3i
i 1
3 6 9 12 15 45
Fall 2002 CMSC 203 - Discrete Structures 132
Double Summations
Algorithms
search interval
a c d f g h j l m o p r s u v x z
center element
search interval
a c d f g h j l m o p r s u v x z
center element
search interval
a c d f g h j l m o p r s u v x z
center element
search interval
a c d f g h j l m o p r s u v x z
center element
search interval
a c d f g h j l m o p r s u v x z
center element
found !
Fall 2002 CMSC 203 - Discrete Structures 144
Algorithm Examples
procedure binary_search(x: integer; a1, a2, …, an:
integers)
i := 1 {i is left endpoint of search interval}
j := n {j is right endpoint of search interval}
while (i < j)
begin
m := (i + j)/2
if x > am then i := m + 1
else j := m
end
if x = ai then location := i
else location := 0
{location is the subscript of the term that equals
x, or is zero if x is not found}
Fall 2002 CMSC 203 - Discrete Structures 145
Complexity
f(x) is O(x2).
Number Theory
• if a | b and a | c, then a | (b + c)
Example: 3 | 6 and 3 | 9, so 3 | 15.
• if a | b and b | c, then a | c
Example: 4 | 8 and 8 | 24, so 4 | 24.
17 = 53 + 2.
• 17 is the dividend,
• 5 is the divisor,
• 3 is called the quotient, and
• 2 is called the remainder.
Example:
a = 60 = 22 31 51
b = 54 = 21 33 50
gcd(a, b) = 21 31 50 = 6
Fall 2002 CMSC 203 - Discrete Structures 172
Relatively Prime Integers
Definition:
Two integers a and b are relatively prime if
gcd(a, b) = 1.
Examples:
Are 15 and 28 relatively prime?
Yes, gcd(15, 28) = 1.
Are 55 and 28 relatively prime?
Yes, gcd(55, 28) = 1.
Are 35 and 28 relatively prime?
No, gcd(35, 28) = 7.
Fall 2002 CMSC 203 - Discrete Structures 173
Relatively Prime Integers
Definition:
The integers a1, a2, …, an are pairwise relatively
prime if gcd(ai, aj) = 1 whenever 1 i < j n.
Examples:
Are 15, 17, and 27 pairwise relatively prime?
No, because gcd(15, 27) = 3.
Are 15, 17, and 28 pairwise relatively prime?
Yes, because gcd(15, 17) = 1, gcd(15, 28) = 1 and
gcd(17, 28) = 1.
Fall 2002 CMSC 203 - Discrete Structures 174
Least Common Multiples
Definition:
The least common multiple of the positive
integers a and b is the smallest positive integer
that is divisible by both a and b.
We denote the least common multiple of a and b
by lcm(a, b).
Examples:
lcm(3, 7) = 21
lcm(4, 6) = 12
lcm(5, 10) = 10
Fall 2002 CMSC 203 - Discrete Structures 175
Least Common Multiples
Using prime factorizations:
Example:
a = 60 = 22 31 51
b = 54 = 21 33 50
lcm(a, b) = 22 33 51 = 4275 = 540
Fall 2002 CMSC 203 - Discrete Structures 176
GCD and LCM
a = 60 = 22 31 51
b = 54 = 21 33 50
gcd(a, b) = 21 31 50 =6
lcm(a, b) = 22 33 51 = 540
Examples:
9 mod 4 = 1
9 mod 3 = 0
9 mod 10 = 9
-13 mod 4 = 3
In other words:
a b (mod m) if and only if a mod m = b mod m.
So we divide 14 by 7:
14 = 72 + 0
We find that 7 | 14, and thus gcd(14, 7) = 7.
Therefore, s = a + b = (11001)2.
Fall 2002 CMSC 203 - Discrete Structures 192
Addition of Integers
How do we (humans) add two integers?
1 11 carry
Example: 7583
+ 4932
1 25 1 5
1 1 carry
Binary expansions: (1011)2
+ (1010)2
(101 01 )2
Therefore, s = a + b = (11001)2.
Fall 2002 CMSC 203 - Discrete Structures 196
Addition of Integers
procedure add(a, b: positive integers)
c := 0
for j := 0 to n-1
begin
d := (aj + bj + c)/2
sj := aj + bj + c – 2d
c := d
end
sn := c
{the binary expansion of the sum is (snsn-1…s1s0)2}
Mathematical
Reasoning
pq pq
_____ qr Hypothetical
Simplification _____ syllogism
p
pr
p
pq
q
_____ Conjunction p Disjunctive
pq _____ syllogism
q
Fall 2002 CMSC 203 - Discrete Structures 204
Arguments
Another example:
“If it rains today, then we will not have a barbeque
today. If we do not have a barbeque today, then
we will have a barbeque tomorrow.
Therefore, if it rains today, then we will have a
barbeque tomorrow.”
pq
qr Hypothetical
_____ syllogism
pr
i: “Gary is intelligent.”
a: “Gary is a good actor.”
c: “Gary can count from 1 to 10.”
Step 1: c Hypothesis
Step 2: ic Hypothesis
Step 3: i Modus tollens Steps 1 & 2
Step 4: ai Hypothesis
Step 5: a Disjunctive Syllogism
Steps 3 & 4
x P(x)
______________________ Existential
P(c) for some element cU instantiation
Example:
x P(x)
__________ Universal
P(c) if cU instantiation
Fall 2002 CMSC 203 - Discrete Structures 215
Proving Theorems
Direct proof:
An implication pq can be proved by showing that
if p is true, then q is also true.
Example: Give a direct proof of the theorem
“If n is odd, then n2 is odd.”
Idea: Assume that the hypothesis of this
implication is true (n is odd). Then use rules of
inference and known theorems to show that q
must also be true (n2 is odd).
n is odd.
Therefore, 3n + 2 is even.
Mathematical
Induction
End of proof.
1 + 2 + … + n = n (n + 1)/2
1 + 2 + … + n = n (n + 1)/2
1 + 2 + … + n + (n + 1) = n (n + 1)/2 + (n + 1)
= (2n + 2 + n (n + 1))/2
= (2n + 2 + n2 + n)/2
= (2 + 3n + n2 )/2
= (n + 1) (n + 2)/2
= (n + 1) ((n + 1) + 1)/2
Fall 2002 CMSC 203 - Discrete Structures 227
Induction
End of proof.
End of proof.
Recursion
a0 = 1
an+1 = 2an for n = 0, 1, 2, …
f(0) = 3
f(n + 1) = 2f(n) + 3
f(0) = 3
f(1) = 2f(0) + 3 = 23 + 3 = 9
f(2) = 2f(1) + 3 = 29 + 3 = 21
f(3) = 2f(2) + 3 = 221 + 3 = 45
f(4) = 2f(3) + 3 = 245 + 3 = 93
f(0) = 1
f(n + 1) = (n + 1)f(n)
f(0) = 1
f(1) = 1f(0) = 11 = 1
f(2) = 2f(1) = 21 = 2
f(3) = 3f(2) = 32 = 6
f(4) = 4f(3) = 46 = 24
Conclusion of Part I: A S.
Fall 2002 CMSC 203 - Discrete Structures 243
Recursively Defined Sets
Part II: To show: S A.
Basis step: To show:
All initial elements of S are in A. 3 is in A. True.
Inductive step: To show:
(x + y) is in A whenever x and y are in S.
If x and y are both in A, it follows that 3 | x and
3 | y. From Theorem I, Section 2.3, it follows
that 3 | (x + y).
(x – y)
((z / 3) – y)
((z / 3) – (6 + 5))
((z / (2 * 4)) – (6 + 5))
f(4)
f(3) f(2)
f(2)
f(1) f(1) f(0)
f(1) f(0)
Counting
General formula:
P(n, r) = n!/(n – r)!
Therefore, we have:
P(n, r) = C(n, r)P(r, r)
Fall 2002 CMSC 203 - Discrete Structures 274
Permutations and Combinations
C(n, r) = P(n, r)/P(r, r)
= n!/(n – r)!/(r!/(r – r)!)
= n!/(r!(n – r)!)
Pascal’s Identity:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
… … … … … …
Fall 2002 CMSC 203 - Discrete Structures 281
Pascal’s Triangle
Since we have C(n + 1, k) = C(n, k – 1) + C(n, k) and
C(0, 0) = 1, we can use Pascal’s triangle to simplify
the computation of C(n, k):
C(0, 0) = 1
C(1, 0) = 1 C(1, 1) = 1
n
C(2, 0) = 1 C(2, 1) = 2 C(2, 2) = 1
C(3, 0) = 1 C(3, 1) = 3 C(3, 2) = 3 C(3, 3) = 1
C(4, 0) = 1 C(4, 1) = 4 C(4, 2) = 6 C(4, 3) = 4 C(4, 4) = 1
Recurrence
Relations
Solution:
Idea: The number of valid strings equals the
number of valid strings ending with a 0 plus the
number of valid strings ending with a 1.
Relations
1 1 R 1 2 3 4
1 X X X
2 2
2 X X
3 3 3 X
4 4
4
R = {(1, 1), (1, 2), (2, 1), (3, 3), (4, 4)} symmetric
R = {(1, 1)} sym. and
antisym.
R = {(1, 1), (1, 2), (2, 2), (2, 1), (3, 3)} Yes.
R = {(1, 3), (3, 2), (2, 1)} No.
R = {(2, 4), (4, 3), (2, 3), (4, 1)} No.
In other words:
Rn = RR … R (n times the letter R)
Why is that?
0 0
M R 1 0
1 1
1 0 1 1 1 1 0 0
0 1 0 0 1 1 0 0
MR MR
1 0 0 1 1 1 0 0
1 0 1 1 1 1 0 0
0 0
M R 1 0
1 1
a
b
d c
Boolean
Algebra
x F1 F2 F3 F4
0 0 0 1 1
1 0 1 0 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
functions.
x -x
inverter
x x+y
OR gate
y
x xy
AND gate
y
Fall 2002 CMSC 203 - Discrete Structures 390
Logic Gates
Example: How can we build a circuit that computes
the function xy + (-x)y ?
x xy
y
xy + (-x)y
x -x
(-x)y
y
Fall 2002 CMSC 203 - Discrete Structures 391
The
End
Fall 2002 CMSC 203 - Discrete Structures 392