Discrete Structures Lectures
Discrete Structures Lectures
قومية كلية
ل معتمدة من الهيئة ال عتماد
ضمان جودة التعليم واال
• Characteristics of Propositions:
• Definiteness: A proposition must have a clear and unambiguous meaning.
• Truth Value: A proposition can only have one of two truth values: true or false.
• Simplicity: Propositions should generally be simple and declarative statements.
Propositional logic (Cont.)
• Examples of Propositions:
• "The Earth is round."
• "2 + 2 = 4."
• "It is raining outside."
• "The cat is on the mat."
Propositional logic (Cont.)
• Non-Examples of Propositions:
• "What is your name?" (This is a question, not a statement.)
• "Go away!" (This is a command, not a statement.)
• x+3=5
Proposition (True or False)
• "The sky is blue" True
• "Go to bed " False
• "It is raining" True
• "It is not raining." True
Proposition (True or False)
Which of the following are propositions?
• System software is a required course for sophomores (proposition)
• Margaret wrote Gone with the Wind (proposition)
• How are you?
• 1+2=3 (proposition)
• 1+2=4 (proposition)
• n2+1 is a prime number
• n2+1 is a prime number for n=2 (proposition)
• The number x is an integer
• Get up and do your exercises.
Constructing Propositions
• An entire proposition is often denoted by a single propositional
variable. Propositional variables are typically among p, q, r, s, t,…
p ¬p
T F
F T
1-Negation (Cont.)
• Examples:
• Proposition: "It is raining."
• Negation: "It is not raining."
p q p→q
T T T
T F F
F T T
F F T
5-Implication (Cont.)
• Examples:
• Proposition 1: "It is raining."
• Proposition 2: "The ground is wet."
• Implication: "If it is raining, then the ground is wet."
• Example:
• If I weight more than 120 pounds (p), I shall enroll in an exercise class (q)
• p q s=p→q
• T T T
• T F F I broke my promise
• F T T
• F F T
5-Implication (Cont.)
• Ways to express p→q :
• “if p, then q”
• “p implies q”
• “if p, q”
• “p only if q”
• “p is sufficient for q”
• “a sufficient condition for q is p”
• “q if p”
• “q whenever p”
• “q when p”
• “q is necessary for p”
• “a necessary condition for p is q”
• “q follows from p”
• “q unless ¬p”
5-Implication (Cont.)
• Example: Let p be the statement “Maria learns discrete mathematics” and q the statement “Maria will find a
good job.” Express the statement p → q as a statement in English.
• “If Maria learns discrete mathematics, then she will find a good job.”
• “Maria will find a good job when she learns discrete mathematics.”
• “For Maria to get a good job, it is sufficient for her to learn discrete mathematics.”
5-Implication (Cont.)
• Properties of Implication:
• Contrapositive: p → q ≡ ¬q → ¬p (The contrapositive of an implication is
logically equivalent to the original implication.)
• Converse: q → p (The converse of an implication is not necessarily
equivalent to the original implication.)
• Inverse: ¬p → ¬q (The inverse of an implication is not necessarily
equivalent to the original implication.)
5-Implication (Cont.)
• If n is divisible by 4, then n is an even integer. (Use if-then for an implication relationship.)
• Note: p → q does NOT imply p → q,
• p is sufficient, but not necessary condition of q.
• If n is not divisible by 4, then n is not an even integer. (Incorrect)
Sufficient Condition
• Being in the inner circle is sufficient for
being in the outer circle.
• "Being a human" is sufficient for
"being a mammal." (All humans are
mammals.)
• Being a square is a sufficient condition
(but not a necessary one) for
something's having four sides.
• A condition p is sufficient for a
condition q if and only if whenever p is
true, q must also be true.
• Symbolically: p → q
Necessary Condition
• Being in the outer circle is necessary for
being in the inner circle.
• air is a necessary condition for human
life
• it is by no means a sufficient condition, i.e.
it does not, by itself, i.e. alone, suffice for
human life.
• A condition p is necessary for a
condition q, p must be true in order
for q to be true.
• Symbolically: q → p
• If x is a necessary condition for y, then y is a sufficient condition for x.
• And, equivalently, If y is a sufficient condition for x, then x is a
necessary condition for y.
• "Since every square must have four sides (i.e. since having four sides is a
necessary condition for being a square), being a square is a sufficient
condition (but not a necessary one) for something's having four sides." Put
another way: "All squares (must) have four sides; but not all four-sided
things [e.g. trapezoids] are squares."
6-Biconditional
• Biconditional is a logical connective that relates two
propositions. It is often expressed as "if and only if" or "p if and
only if q." The biconditional is true if and only if both propositions
have the same truth value.
• Truth Table:
p q p q
T T T
T F F
F T F
F F T
6-Biconditional (Cont.)
• Examples:
• Proposition 1: "It is raining."
• Proposition 2: "The ground is wet."
• Biconditional: "It is raining if and only if the ground is wet."
p q ¬p p→q ¬p∨q
F F T T T
F T T T T
T F F F F
T T F T T
Logically Equivalent (Cont.)
p→q ≡ ( q) → ( p) ?
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
( (p r) ( (q r) p) ) ( ((p q) ) p )
Solution
( (p r) ( (q r) p) ) ( ((p q) ) p ) ≡
( (p r) ( (q r) p) ) ( p q) p ≡ (De Morgans)
( (p r) ( (q r) p) ) ( p q) p ≡ (Double Negation)
(p p) ( (p r) (q r) (p q)) ≡ (Comm+Assoc, or)
T ( (p r) (q r) (p q)) ≡ (Inverse Law)
T (Domination Law)
Rules of Inference
• Consider the following argument involving propositions (which, by definition, is a sequence of propositions):
“If you have a current password, then you can log onto the network.”
“You have a current password.”
Therefore,
“You can log onto the network.”
• We would like to determine whether this is a valid argument. That is, we would like to determine whether the
conclusion “You can log onto the network” must be true when the premises “If you have a current password,
then you can log onto the network” and “You have a current password” are both true.
Rules of Inference (Cont.)
•The general form of a rule of inference is:
p1
p2
..
.
pn
____
q
•The rule states that if p1 and p2 and … and pn are all true, then q is true as well.
•P1, P2 … called the premises of the argument
•q is the conclusion for the argument.
•if this statement is always true (a tautology), it is a valid argument.
•These rules of inference can be used in any mathematical argument and do not
require any proof.
Rules of Inference (Cont.)
• Essentially, to show that this statement is always true (a tautology), we must show that if all of p1 through pn are true, then q must be true as well.
• Another way to look at this is that we must show that if q is ever false, then at least ONE of p1 through pn must be false as well. (This is the
contrapositive of the original assertion.)
• Let us look at an example of how you might go about proving a statement in a general form, given some extra information.
• Let p, q, are r be the following statements:
• p: Roger studies
• q: Roger plays racketball
• r: Roger passes discrete mathematics
• Let the premises be the following:
• p1 : If Roger studies, he will pass discrete mathematics.
• p2 : If Roger does not play racketball, then he will study.
• p3 :Roger failed discrete mathematics.
• Is the argument (p1 and p2 and p3) → q is valid?
• P1: p→ r
• P2: q → p
• P3: r
• ((p→r) (q → p) (r) ) → q
• We can prove that this is a tautology by using a truth table; verifying that this expression is true for all 8 possible sets of values for p, q and r
Rules of Inference (Cont.)
Another way we can show this statement is to use the laws of logic to simplify the statement as follows:
((p→r) ( q → p) ( r) ) → q
[(p r) (q p) (r)] → r (Identity for implication)
[(p r) (q p) (q)] → r (Double Negation)
[((p r) (q p)) (q)] r (Identity for implication)
((p r) (q p)) (q) r (De Morgan’s Law)
((p r) (q p)) q r (Double Negation)
(p r) (q p) q r (De Morgan’s Law)
(p r) (q p) q r (De Morgan’s Law)
(p r) (q p) q r (Double Negation)
(r (p r)) (q (q p)) (Associate+Commutative)
((r p) (r r)) ((q q) (q p)) (Distributive)
((r p) T) (T (q p)) (Inverse Laws)
(r p) (q p) (Identity Laws)
(p p) (r q) (Associate+Commutative)
T (r q) (Inverse Laws)
T (Domination Laws)
Rules of Inference (Cont.)
• Rules of inference provide the justification of the steps used in a proof.
• One important rule is called modus ponens or the law of detachment. It is based on the
tautology (p(p→q)) → q. We write it in the following way:
p
p→q
____
Q
pq
____
p Simplification
p
q
_____
pq Conjunction
Rules of Inference (Cont.)
¬q
p→q
_____
¬p Modus tollens [(p→q) q] → p is a tautology
p→q
q→r
_____
p→r Hypothetical syllogism (p→q) (q →r) → (p→r) is a tautology
Rules of Inference (Cont.)
pq
¬p
_____
q Disjunctive syllogism [(p q) p] →q is a tautology
p∨q
¬p ∨ r
_____
∴q∨r Resolution
Rules of Inference (Cont.)
• Show that the following implication is a tautology
• [(pq) (p r)] → [(q r)]
• You could use the truth table Or
• p1 /\ p2 /\ p3 → q
• a: Jonathan has his driver’s license.
• b: Jonathan’s new car is out of gas
• c: Jonathan likes to drive his new car
• p1: (¬a) \/ b
• p2: a \/ (¬c)
• p3: (¬b) \/ (¬c)
• q: (¬c)
Solution: Let p be the proposition “You did every problem in this book.” Let q be the proposition
“You learned discrete mathematics.” Then this argument is of the form: if p → q and q, then p.
This is an example of an incorrect argument using the fallacy of affirming the conclusion.
Indeed, it is possible for you to learn discrete mathematics in some way other than by doing
every problem in this book. (You may learn discrete mathematics by reading, listening to
lectures, doing some, but not all, the problems in this book, and so on.)
Assiut University
قومية كلية
ل معتمدة من الهيئة ال عتماد
ضمان جودة التعليم واال
• Example: Let P(x) denote the statement “x > 3.” What are the truth values of P(4) and P(2)?
• We obtain the statement P(4) by setting x = 4 in the statement “x > 3.” Hence, P(4), which is the statement “4 >
3,” is true. However, P(2), which is the statement “2 > 3,” is false.
• Example: Let Q(x, y) denote the statement “x = y + 3.” What are the truth values of the propositions Q(1, 2)
and Q(3, 0)?
• To obtain Q(1, 2), set x = 1 and y = 2 in the statement Q(x, y). Hence, Q(1, 2) is the
statement “1 = 2 + 3,” which
is false. The statement Q(3, 0) is the proposition “3 = 0 + 3,” which is true.
Predicate logic
• Predicate logic is a formal system of logic that extends propositional
logic by introducing quantifiers (∀ and ∃) and predicates. It allows us to
reason about individuals and their properties, as well as relationships
between individuals.
• Components of Predicate Logic:
• Constants: Symbols representing specific individuals or objects.
• Variables: Symbols representing arbitrary individuals or objects.
• Predicates: Symbols representing properties or relations of individuals.
• Quantifiers: Symbols used to express the quantity of individuals that satisfy a
predicate:
• Universal quantifier (∀): "for all"
• Existential quantifier (∃): "there exists"
• Logical operators: AND (∧), OR (∨), NOT (¬), IMPLIES (→), and IF AND ONLY IF
( ).
Predicate logic (Cont.)
• Predicate P(x): "x is a dog.“
• Predicate logic: "For all x, if x is a dog, then x has four legs." (implication)
• Let P(x):=x can fly. Let Q(x):=x are blue. Let the domain of x be all species of
animals.
• P(penguins)∨P(geese) is true.
• P(blue jays)∧Q(blue jays) is true.
• P(penguins)→Q(tigers) is true.
Predicate logic (Cont.)
• Examples of Predicate Logic Formulas:
• ∀x P(x): "For all x, P(x) is true."
• ∃x Q(x): "There exists an x such that Q(x) is true."
• ∀x∀y R(x, y): "For all x and y, R(x, y) is true."
• ∃x∃y S(x, y): "There exists an x and a y such that S(x, y) is true."
Quantifiers
• Types of Quantifiers:
1.Universal Quantifier (∀):
1. Meaning: "For all" or "for every."
2. Usage: Indicates that a statement is true for every element in each set.
3. Example: ∀x ∈ ℝ, x² ≥ 0 (For all real numbers x, x squared is greater than or equal to
0.)
2.Existential Quantifier (∃):
1. Meaning: "There exists" or "there is at least one."
2. Usage: Indicates that there is at least one element in each set that satisfies the
statement.
3. Example: ∃x ∈ ℤ, x is even (There exists an integer x that is even.)
Quantifiers (Cont.)
Example:
• Consider these simple open statements:
• p(x): x > 0
• q(x): x2 – 2x – 3 = 0
• r(x): x < 0
• s(x): x2 > 0
2) There exist two consecutive entries in A where the second entry is twice the first:
x (A(x+1)=2A[x] )
Example:
• S(x) : x is a square
• E(x): x is equilateral
• x [s(x) → e(x)] is a true statement
(a) x [s(x) → e(x)] x [e(x) → s(x)] contrapositive
(b) x [e(x) → s(x)] the converse is false
(c) x [ s(x) → e(x)] the inverse is false
Example
• Let L(x, y) be the proposition “x likes y”. How can we express the following statements?
• Someone likes everybody. x y L(x, y)
• Everybody likes everybody. x y L(x, y)
• Somebody likes somebody. x y L(x, y)
• Everybody likes somebody. x y L(x, y)
Quantifiers
So now, consider each of these implications:
1) x [p(x) q(x)] → [x p(x) x q(x)]
[x p(x)][x q(x) ] ( →) x[ p(x)q(x) ]
p(x) = “x >10”
q(x) = “x=<10”
• if p(x,y) is “x+y=17”
• x y p(x, y) is true y=17-x ( for every integer x, there exists y such that x+y=17)
• x y p(x, y) is false (There exists an integer y so that for all integers x, x+y =17)
The Rule of Universal Specification:
If an open statement p (x) is proved to be true for all x , i.e. x p(x), then it is true for any particular x=a,
p(a).
1) x p(x) Premise
2) p(c) Step 1 & Rule of Universal Specification
3) x [p(x) q(x) → r(x)] Premise
4) p(c) q(c) → r(c) Step 3 & Rule of Universal Specification
5) T q(c) → r(c) Substitution from Step 2
6) q(c) → r(c) Identity Law
7) r(c) → q(c) Contrapositive
8) x [r(x) → q(x)] Step 7 & Rule of Universal Generalization
Example
Let p(n), q(n), and r(n) be the following open statements:
p(n): n is an even number.
q(n): n is an odd number.
r(n): n2+n is even.
Since all integers are either even or odd, prove that (p(n) q(n)) → r(n).
The reason is that you came to an incorrect conclusion. You must have arrived at it by making an
incorrect step. The ONLY possible incorrect step that could have been taken was the assumption made in
the beginning. Hence, that is wrong, which implies the truth of what you are trying to prove.
Distributing Quantifiers into Compound
Expressions
• Rules for Distribution:
• ∀x (P(x) ∧ Q(x)) ≡ (∀x P(x)) ∧ (∀x Q(x))
• ∃x (P(x) ∨ Q(x)) ≡ (∃x P(x)) ∨ (∃x Q(x))
• Solution: Let D(x) denote “x is in this discrete mathematics class,” and let C(x) denote “x has taken a course in
computer science.” Then the premises are ∀x(D(x) → C(x)) and D(Marla). The conclusion is C(Marla).
The following steps can be used to establish the conclusion from the premises.
Step Reason
1. ∀x(D(x) → C(x)) Premise
2. D(Marla)→C(Marla) Universal instantiation from (1)
3. D(Marla) Premise
4. C(Marla) Modus ponens from (2) and (3)
Assiut University
قومية كلية
ل معتمدة من الهيئة ال عتماد
ضمان جودة التعليم واال
• Proof.
Assume x is arbitrary element from set A, xA, (1)
But x A → x A (any other proposition) since p →p q is a tautology.
So, (1) implies, that x A x B, (2)
By the definition of set union, (2) is equivalent to xA B, (3)
We showed that arbitrary element x from set A belongs to the union A B, that is A AB.
Direct proof (Cont.)
• Example 6: Theorem: Let A and B be any two sets. Prove that AB A.
Proof
We need to prove that for any x if x AB, then xA, i.e. x [x AB → x A].
So, take arbitrary x AB (1)
By the definition of the set intersection (1) implies that x A xB, (2)
(2) implies that x A, (3) by p q → p, i. e. p q → p is a tautology.
We showed that any element from AB belongs to A. By the definition of subset relation, it means that AB
A.
Direct proof (Cont.)
Example 7: Theorem: Prove that the sum of two odd integers is an even integer..
Proof: Let a, b denote two (arbitrary) odd integers. We want to prove that a + b is even.
By the definition of odd integers, there exist integers c and d such that
a=2c+1 (1)
b=2d+1 (2)
• Proof: Assume n is not odd. Then, we must prove 3n+2 is not odd.
Since n is not odd, it is even and we have n=2k for some integer k.
Then, 3n+2=3(2k)+2=6k+2=2(3k+1). Letting k′=3k+1 we
have 3n+2=2k′. Hence, it is even.
Proof by Contrapositive (Cont.)
• Prove that if n = ab, where a and b are positive integers, then 𝑎 ≤ 𝑛 or 𝑏 ≤ 𝑛 .
• The first step in a proof by contraposition is to assume that the conclusion of the conditional statement “If n =
ab, where a and b are positive integers, then 𝑎 ≤ 𝑛 or 𝑏 ≤ 𝑛” is false.
• That is, we assume that the statement (𝑎 ≤ 𝑛) ∨ (𝑏 ≤ 𝑛) is false. Using the meaning of disjunction together
with De Morgan’s law, we see that this implies a > 𝑛 and b > 𝑛. We can multiply these inequalities together
(using the fact that if 0 < s < t and 0 < u < v, then su < tv) to obtain ab > 𝑛 · 𝑛 = n. This shows that ab ≠ n,
which contradicts the statement n = ab.
• Because the negation of the conclusion of the conditional statement implies that the hypothesis is false, the
original conditional statement is true.
Proof by Contrapositive (Cont.)
Example: Theorem: If A B, then AB AB.
Proof:
A B → AB AB
p→ q
AB = AB → A = B
q→p
We can prove the equivalent contrapositive proposition
Proof. To prove AB = AB → A = B assume AB = AB and prove 1) A B and 2) B A
1) xA → x AB → x AB → xB
2) xB → x A B → x A B → xA
Proof by Contrapositive (Cont.)
Example: If A B = , then B A.
Proof:
The contrapositive of this statement is the following:
if ( B A) then (A B = ).
If we prove this statement, we have also proven the original, since a statement and its contrapositive are
equivalent.
So, we will assume that ( B A). Under this assumption we must show that A B .
If we have that ( B A), then there exists an element x such that x is IN B but NOT IN the complement of
A. Symbolically we have:
x | xB xA
If we have an element xA, by definition of complement, we must have that xA. So we have
x | xB xA.
But, this means that x AB!!! From that we can conclude that AB , as desired.
Proof by contradiction
• Proof by contradiction is a powerful method of indirect proof
where you assume the negation of the statement you want to
prove and then show that this assumption leads to a logical
contradiction. This contradiction implies that the original
statement must be true.
• By contrapositive, if ¬p→F holds then T→p holds.
Therefore, p must be true.
Proof by contradiction (Cont.)
• Example: There is no largest integer.
• Proof: Assume that there does exist some largest integer. Call such an
integer n. But, n+1 is also an integer and n+1 is strictly larger than n.
Therefore, our assumption that a largest integer exists is false. That is,
there is no largest integer is true.
Proof by contradiction (Cont.)
• Prove that 2 is irrational by giving a proof by contradiction.
• Solution: Let p be the proposition “ 2 is irrational.” To start a proof by contradiction, we suppose that ¬p is true.
Note that ¬p is the statement “It is not the case that 2 is irrational,” which says that 2 is rational. We will
shointeger,ng that ¬p is true leads to a contradiction.
• If 2 is rational, there exist integers a and b with 2 = a/b, where b ≠ 0 and a and b have no common factors (so that
the fraction a/b is in lowest terms.) (Here, we are using the fact that every rational number can be written in lowest
terms.) Because 2 = a/b, when both sides of this equation are squared, it follows that 2 =a2/b2 .
• Hence, 2b2 = a2. By the definition of an even integer it follows that a2 is even. We next use the fact that if a2 is even,
a must also be even. Furthermore, because a is even, by the definition of an even integer, a = 2c for some integer c.
Thus, 2b2 = 4c2.
• Dividing both sides of this equation by 2 gives b2 = 2c2 By the definition of even, this means that b2 is even. Again
using the fact that if the square of an integer is even, then the integer itself must be even, we conclude that b must be
even as well.
• We have now shown that the assumption of ¬p leads to the equation 2 = a/b, where a and b have no common
factors, but both a and b are even, that is, 2 divides both a and b.
• Because our assumption of ¬p leads to the contradiction that 2 divides both a and b and 2 does not divide both a
and b, ¬p must be false. That is, the statement p, “ 2 is irrational,” is true. We have proved that 2 is irrational.
Proof by contradiction (Cont.)
• Give a proof by contradiction of the theorem “If 3n + 2 is odd, then n is odd.”
• Solution: Let p be “3n + 2 is odd” and q be “n is odd.” To construct a proof by contradiction, assume that both
p and ¬q are true. That is, assume that 3n + 2 is odd and that n is not odd.
• Because n is not odd, we know that it is even. Because n is even, there is an integer k such that n = 2k. This
implies that 3n + 2 = 3(2k) + 2 = 6k + 2 = 2(3k + 1). Because 3n + 2 is 2t , where t = 3k + 1, 3n + 2 is even.
• Note that the statement “3n + 2 is even” is equivalent to the statement ¬p, because an integer is even if and
only if it is not odd.
• Because both p and ¬p are true, we have a contradiction. This completes the proof by contradiction, proving
that if 3n + 2 is odd, then n is odd.
Proof by contradiction (Cont.)
Example: Theorem: For any two sets A and B (A−B)(AB) =
Proof by contradiction. Assume
(A−B)(AB) , (1)
i.e., that there exists at least one element in the intersection.
≡ x [x (A−B)(AB)] definition of empty set
≡ x [x(A−B) x(AB)] definition of
x [(xA x B)( xA xB)] definition of set difference and
x [xA (xB xB)] commutative, associative law
≡ x [xA x ] since BB=
≡ x [xA] definition of
≡ x [x] domination law
≡F since x [x ]
We come to the contradiction that proves the original proposition.
Proof by contradiction (Cont.)
Example: If A B = , then B A.
So we first assume that A B = . Next we will also assume that B A is false.
If we have ( B A), then we must have an element x that is in B that is NOT in the complement of A.
Symbolically we have:
x | xB xA
this is equivalent to...
x | xB xA.
But this is impossible – it contradicts our assumption A B = . Thus, we must have made a mistake in our
proof. Every step we took was logical EXCEPT for our assumption. Thus, this is what must be wrong...we can
conclude that the opposite of it, is indeed true. This proves that
If A B = , then B A.
Mistakes in Proofs
• What is wrong with this famous supposed “proof” that 1 = 2?
• “Proof:“ We use these steps, where a and b are two equal positive integers.
• Step Reason
1. a = b Given
2. a2 = ab Multiply both sides of (1) by a
3. a2 − b2 = ab − b2 Subtract b2 from both sides of (2)
4. (a − b)(a + b) = b(a − b) Factor both sides of (3)
5. a + b = b Divide both sides of (4) by a − b
6. 2b = b Replace a by b in (5) because a = b and simplify
7. 2 = 1 Divide both sides of (6) by b
• Solution: Every step is valid except for one, step 5 where we divided both sides by a − b. The error is that a −
b equals zero; division of both sides of an equation by the same quantity is valid if this quantity is not zero.
Exhaustive Proof and Proof by Cases
• To prove a conditional statement of the form
(p1 ∨ p2 ∨ · · · ∨ pn) → q is tautology. We can use as a rule of inference:
[(p1 ∨ p2 ∨ · · · ∨ pn) → q] [(p1 → q) ∧ (p2 → q) ∧ · · · ∧ (pn → q)]
• This shows that the original conditional statement with a hypothesis made up of a disjunction of
the propositions p1, p2, . . . , pn can be proved by proving each of the n conditional statements pi →
q, i = 1, 2, . . . , n, individually.
Proof by exhaustion
• Proof by exhaustion is a method of proof that involves testing a
statement for all possible cases or instances within a finite
domain. It's particularly effective when the domain is small or can
be easily enumerated.
• Steps involved:
1.Identify the possible cases: Determine all the distinct values or
combinations of values that the variables in the statement can take.
2.Test the statement for each case: Verify that the statement is true for
each of the possible cases.
3.Conclude: If the statement is true for all cases, then it is true for the
entire domain.
Proof by exhaustion (Cont.)
• Example: For any integer n between 1 and 4, n^2 is greater than or
equal to n.
• Proof by exhaustion:
• Case 1: n = 1: 1^2 = 1, which is greater than or equal to 1.
• Case 2: n = 2: 2^2 = 4, which is greater than or equal to 2.
• Case 3: n = 3: 3^2 = 9, which is greater than or equal to 3.
• Case 4: n = 4: 4^2 = 16, which is greater than or equal to 4.
• Conclusion: Since the statement is true for all integers between 1 and 4,
it is true for the entire domain.
Proof by exhaustion (Cont.)
• Truth table method. To prove a statement about small number of Boolean variables make a truth table and
check all possible cases.
Example: (((A B) C) B) ≡ (B C)
One way is to use a membership table:
• Steps:
1.Identify the cases: Determine the different cases or conditions that can occur.
2.Prove the statement for each case: Prove the statement separately for each
identified case using appropriate proof techniques.
3.Conclude: Since the statement is true for all cases, it is true for the entire
domain.
Proof by Case Analysis (Cont.)
• Example:
• Statement: For any integer n, n^2 is either even or odd.
• Proof by case analysis:
• Case 1: n is even. If n is even, then n = 2k for some integer k. Therefore, n^2 = (2k)^2
= 4k^2 = 2(2k^2), which is even.
• Case 2: n is odd. If n is odd, then n = 2k + 1 for some integer k. Therefore, n^2 = (2k +
1)^2 = 4k^2 + 4k + 1 = 2(2k^2 + 2k) + 1, which is odd.
• Conclusion: Since n is either even or odd, and we have shown that n^2 is
even in the first case and odd in the second case, it follows that n^2 is
either even or odd for any integer n.
Assiut University
قومية كلية
ل معتمدة من الهيئة ال عتماد
ضمان جودة التعليم واال
• Example1: The set of natural numbers N are the “whole numbers”: N = {0,
1, 2, 3, . . .}
• Example2: The set of integers Z are the whole numbers and their
negatives: Z = {. . . ,−2,−1, 0, 1, 2, . . .}
Sets (Cont.)
Sets are used to group objects together.
• A set is a collection of objects (members, elements). A = {3, 7, 14}
• A set is completely defined by its elements.
• An element ‘belongs to’ a set (or a set contains its elements).
• If a set is defined by listing its elements, all that matter is which elements included, not the order. {3, 7, 14}
is equal to {7, 3, 14}
• An element can appear more then once: Sets {3, 7, 14} and {3, 3, 14, 7, 7} are equal.
Typically, sets will be denoted by uppercase letters.
Sets (Cont.)
• Types of Sets:
• Empty set (∅): A set containing no elements.
• Finite set: A set with a countable number of elements.
• Infinite set: A set with an uncountable number of elements.
• Universal set (U): The set containing all elements under consideration in
a particular context.
Sets (Cont.)
There are some other sets we should be familiar with since they come up so often. Here they are:
• Z = {0, 1, -1, 2, -2, ...} (the set of integers)
• N = {0, 1, 2, 3, ...} (the set of non-negative integers)
• Z+ = {1, 2, 3, ...} (the set of positive integers)
• Q = {a/b | a, bZ b0} “|” such as
• R = the set of real numbers...
Sets (Cont.)
We have two usual methods of denoting the elements in a set:
1) Explicitly list the elements inside of a set of curly braces ({}), as follows: {1, 2, 3, 4}
• Example1: The set of even integers can be written as: { x | x is an integer, and x is divisible by 2 }.
• Example2: The set of prime numbers less than 10 can be written as: { x | x is a prime number and x < 10 }
• Example3: O = {x | x is an odd positive integer less than 10} = {x ∈ Z+ | x is odd and x < 10}.
Sets (Cont.)
• set of odd positive integers less then 10:
B={x | x is positive odd integer less then 10}
To prove that A B takes all x A and show that x B. If x A, then either x =43 or x=47.
x=43=410+3 and x=47= 411+3. So, in either case x has the form of an element of B, thus x B. Therefore A
B.
Sets (Cont.)
• EXAMPLE: The sets {1, 3, 5} and {3, 5, 1} are equal, because they
have the same elements.
• Note that the order in which the elements of a set are listed does not
matter.
• Note also that it does not matter if an element of a set is listed more than
once, so {1, 3, 3, 3, 5, 5, 5, 5} is the same as the set {1, 3, 5} because they
have the same elements.
Sets (Cont.)
Equality of two sets
Two sets are equal if they contain the same elements. For example:
{1, 3, 5} = {3, 5, 1, 5}
{1, 4, 9, 16} = {x2 | x Z+ x2 <20}
More rigorously:
Definition. Two sets are equal if and only if each of them is a subset of the other.
A = B A B B A
First show A B. Take any x A, then either x=13 or x=17. 13=43+1 →13 B, 17 = 44+1 →17 B, therefore A B. Similarly show
that B A.
Sets (Cont.)
• Cardinality: The cardinality of a set is the number of elements in the
set. It is denoted by |A| for a set A.
• Let A be the set of odd positive integers less than 10. Then |A| = 5.
• Let S be the set of letters in the English alphabet. Then |S| = 26.
• What is the cardinality of each of the following sets?
|{a}| =1
|{{a}}| =1
|{a, {a}}| =2
|{a, {a}, {a, {a}}}| =3
• Basic Structure
• A Venn diagram typically consists of overlapping circles enclosed within a
rectangle. Each circle represents a set. The overlapping regions between
circles show the elements common to both sets.
Venn diagrams (Cont.)
• Universal Set (U): This is the set containing all elements under consideration
in a given context. The rectangle usually represents the universal set.
• Intersection (A ∩ B): The intersection of sets A and B is the set of elements
that belong to both A and B. It is represented by the overlapping region of the
circles for A and B.
• Union (A ∪ B): The union of sets A and B is the set of elements that belong to
either A or B (or both). It is represented by the entire area covered by both
circles.
• Complement (A'): The complement of set A is the set of elements in the
universal set that are not in A. It is represented by the area outside of circle A
within the rectangle.
• Difference (A - B): The difference of sets A and B is the set of elements that
belong to A but not to B. It is represented by the portion of circle A that does
not overlap with circle B.
Venn diagrams (Cont.)
• Example:
• Let's consider two sets:
• A: Set of even numbers
• B: Set of multiples of 3
• Venn diagram with two overlapping circles, one labeled A for even
numbers and the other labeled B for multiples of 3.
• The overlapping region represents numbers that are both even and
multiples of 3.
• A ∩ B: Numbers that are both even and multiples of 3 (e.g., 6, 12, 18)
• A ∪ B: Numbers that are either even or multiples of 3 (or both)
• A': Odd numbers
• A - B: Even numbers that are not multiples of 3 (e.g., 2, 4, 8)
Power Sets
• Given a set S, the power set of S is the set of all subsets of the set S. The power set of S is denoted
by P(S).
• What is the power set of the set {0, 1, 2}?
• Solution: The power set P({0, 1, 2}) is the set of all subsets of {0, 1, 2}. Hence, P({0, 1, 2}) = {∅, {0},
{1}, {2}, {0, 1}, {0, 2}, {1, 2}, {0, 1, 2}}. Note that the empty set and the set itself are members of this
set of subsets.
• Theorem:The cardinality of the power set of S (S with cardinality |S|=n) is 2n, |Power(S)|=2|S|.
Cartesian Products
• Let A and B be sets. The Cartesian product of A and B, denoted by A × B, is the set
of all ordered pairs (a, b), where a ∈ A and b ∈ B. Hence, A × B = {(a, b) | a ∈ A ∧ b
∈ B}.
• Let A and B be sets. The intersection of the sets A and B, denoted by A ∩ B, is the set containing those
elements in both A and B. A ∩ B = {x | x ∈ A ∧ x ∈ B}.
Set Operations (Cont.)
• Two sets are called disjoint if their intersection is the empty set.
• Let A = {1, 3, 5, 7, 9} and B = {2, 4, 6, 8, 10}. Because A ∩ B = ∅, A and B are disjoint.
• Let A and B be sets. The difference of A and B, denoted by A − B, is the set containing those elements that are
in A but not in B. The difference of A and B is also called the complement of B with respect to A. A − B = {x | x
∈ A ∧ x ∉ B}.
• The difference of sets A and B is sometimes denoted by A\B.
• The difference of {1, 3, 5} and {1, 2, 3} is the set {5}; that is, {1, 3, 5} − {1, 2, 3} = {5}.
• Let U be the universal set. The complement of the set A, denoted by 𝐴,ҧ is the complement of A with respect to
U. Therefore, the complement of the set A is U − A. 𝐴ҧ = {x ∈ U | x ∉ A}.
Set Operations (Cont.)
• Example
If U = {1,2,3 … 9,10} A ={1,2,3,4,5} B={3,4,5,6,7}
• A B ={1,2,3,4,5,6,7}
• A B ={3,4,5}
• A ={6,7,8,9,10}
• B – A={6,7}
• A B={1,2,6,7}
• Definition:
• S, T U. The sets S and T are called disjoint or mutually disjoint,
when S T =
Set Operations (Cont.)
Do not confuse and (or ) signs!
Let A ={a, , {b}}.
• aA (T)
• {a} A (F)
• a A (F)
• {a} A (T)
• {a} A (T)
• {b} A (F)
• {b} A (T)
• {a, {b}} A (T)
• {a, {b}} A (T)
• A (T)
• A (T)
Set Identities
Set Identities (Cont.)
• AB = B A
Proof
AB = { x | xA xB }, by definition of union
= { x | xB xA }, by commutative property of
= B A, by definition of union
• A (B C) = (A B) C
Proof
A (BC)
={x | (x A) (x BC)]}, by definition of
={x | (x A) [(x B) (x C)]}, by definition of
= {x |[(x A) (x B)] (x C)}, by associative property of
= {x |(x A B) (x C)}, by definition of
= (A B) C, by definition of
Set Identities (Cont.)
• A (B C) = (A B) (A C)
Proof
A B C BC A (BC) A B A C (A B) (A C)
1 1 1 1 1 1 1 1
1 1 0 0 1 1 1 1
1 0 1 0 1 1 1 1
1 0 0 0 1 1 1 1
0 1 1 1 1 1 1 1
0 1 0 0 0 1 0 0
0 0 1 0 0 0 1 0
0 0 0 0 0 0 0 0
Set Identities (Cont.)
• A = A, A U = A Identity Laws
Proof
A = {x | x A x}, by definition of a set union
= {x | x A F}, since x [x] , x=F
= {x | x A}, by identity law p F p
=A
In the proof of A U = A you can consider x U =T and use another identity law p T ≡ p
Set Identities (Cont.)
prove that A = (A – B) (A B), for all sets A and B.
Proof
• Example: Is the function f (x) = x + 1 from the set of integers to the set of integers
onto?
• Solution: This function is onto, because for every integer y there is an integer x such that f (x) = y. To see
this, note that f (x) = y if and only if x + 1 = y, which holds if and only if x = y − 1.
Examples of Different Types of
Correspondences
One-to-one correspondence (bijection)
• The function f is a one-to-one correspondence, or a bijection, if it is both one-to-one and onto. We also say
that such a function is bijective.
• Example: Let f be the function from {a, b, c, d} to {1, 2, 3, 4} with f (a) = 4, f (b) = 2, f (c) = 1, and f (d) = 3. Is f
a bijection?
• Solution: The function f is one-to-one and onto. It is one-to-one because no two values in the domain are assigned the
same function value. It is onto because all four elements of the codomain are images of elements in the domain.
Hence, f is a bijection.
Inverse Functions
• Let f be a one-to-one correspondence from the set A to the set B. The inverse function of f is the function that
assigns to an element b belonging to B the unique element a in A such that f (a) = b. The inverse function of f
is denoted by f −1. Hence, f−1(b) = a when f (a) = b.
Inverse Functions (Cont.)
• Example: Let f be the function from {a, b, c} to {1, 2, 3} such that f (a) = 2, f (b) = 3,
and f (c) = 1. Is f invertible, and if it is, what is its inverse?
• Solution: The function f is invertible because it is a one-to-one correspondence. The inverse function f−1
reverses the correspondence given by f , so f−1(1) = c, f−1(2) = a, and f−1(3) = b.
• EXAMPLE: Let A be the set {1, 2, 3, 4}. Which ordered pairs are in the relation R = {(a, b) | a
divides b}?
• Solution: Because (a, b) is in R if and only if a and b are positive integers not exceeding 4 such
that a divides b, we see that R = {(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 4), (3, 3), (4, 4)}.
Relations (Cont.)
• EXAMPLE: How many relations are there on a set with n elements?
• Solution: A relation on a set A is a subset of A × A. Because A × A has n2 elements when A has
2
n elements, and a set with m elements has 2m subsets, there are 2𝑛 subsets of A × A. Thus,
2 2
there are 2𝑛 relations on a set with n elements. For example, there are 23 = 29 = 512 relations
on the set {a, b, c}.
Relations (Cont.)
More examples of relations:
• “parent-of”
• “child-of”
• “likes”
• “meet one another today”
• “less then” = {(a, b) | a, b A and a<b} where A={1, 2, …5}
“less then” = { (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)}
Relations (Cont.)
Properties of Relations:
• Reflexive: A relation R on set A is reflexive if (a, a) ∈ R for all a ∈ A.
• EXAMPLE: Consider the following relations on {1, 2, 3, 4}:
R1 = {(1, 1), (1, 2), (2, 1), (2, 2), (3, 4), (4, 1), (4, 4)},
R2 = {(1, 1), (1, 2), (2, 1)},
R3 = {(1, 1), (1, 2), (1, 4), (2, 1), (2, 2), (3, 3), (4, 1), (4, 4)},
R4 = {(2, 1), (3, 1), (3, 2), (4, 1), (4, 2), (4, 3)},
R5 = {(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)},
R6 = {(3, 4)}.
Which of these relations are reflexive?
Solution: The relations R3 and R5 are reflexive because they both contain all pairs of the form (a, a). R1, R2, R4,
and R6 are not reflexive because (3, 3) is not in any of these relations.
Relations (Cont.)
• Example: Is the “divides” relation on the set of positive integers reflexive?
Solution: Because a | a whenever a is a positive integer, the “divides” relation is reflexive.
(Note that if we replace the set of positive integers with the set of all integers the relation is not
reflexive because 0 does not divide 0.)
Relations (Cont.)
• Symmetric: A relation R on set A is symmetric if (a, b) ∈ R implies (b, a) ∈ R for all a, b ∈ A.
• The relation R on the set A is symmetric if ∀a∀b((a, b) ∈ R → (b, a) ∈ R).
• Antisymmetric: A relation R on a set A such that for all a, b ∈ A, if (a, b) ∈ R and (b, a) ∈ R,
then a = b is called antisymmetric.
• The relation R on the set A is antisymmetric if ∀a∀b(((a, b) ∈ R ∧ (b, a) ∈ R) → (a = b)).
• EXAMPLE: Consider the following relations on {1, 2, 3, 4}:
R1 = {(1, 1), (1, 2), (2, 1), (2, 2), (3, 4), (4, 1), (4, 4)},
R2 = {(1, 1), (1, 2), (2, 1)},
R3 = {(1, 1), (1, 2), (1, 4), (2, 1), (2, 2), (3, 3), (4, 1), (4, 4)},
R4 = {(2, 1), (3, 1), (3, 2), (4, 1), (4, 2), (4, 3)},
R5 = {(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)},
R6 = {(3, 4)}.
Which of the relations are symmetric and which are antisymmetric?
• Solution: The relations R2 and R3 are symmetric, because in each case (b, a) belongs to the relation whenever (a, b) does.
• None of the other relations is symmetric. This is done by finding a pair (a, b) such that it is in the relation but (b, a) is not.
• R4, R5, and R6 are all antisymmetric. For each of these relations there is no pair of elements a and b with a ≠ b such that both (a, b)
and (b, a) belong to the relation.
Relations (Cont.)
• Example: Is the “divides” relation on the set of positive integers symmetric? Is it
antisymmetric?
• Solution: This relation is not symmetric because 1| 2, but 2 does not divide 1.
It is antisymmetric, for if a and b are positive integers with a |b and b |a, then
a = b.
Relations (Cont.)
• Transitive: A relation R on set A is transitive if (a, b) ∈ R and (b, c) ∈ R
implies (a, c) ∈ R for all a, b, c ∈ A.
• The relation R on a set A is transitive if we have ∀a∀b∀c(((a, b) ∈ R ∧ (b, c) ∈ R) → (a, c) ∈ R).
• EXAMPLE: Consider the following relations on {1, 2, 3, 4}:
R1 = {(1, 1), (1, 2), (2, 1), (2, 2), (3, 4), (4, 1), (4, 4)},
R2 = {(1, 1), (1, 2), (2, 1)},
R3 = {(1, 1), (1, 2), (1, 4), (2, 1), (2, 2), (3, 3), (4, 1), (4, 4)},
R4 = {(2, 1), (3, 1), (3, 2), (4, 1), (4, 2), (4, 3)},
R5 = {(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)},
R6 = {(3, 4)}.
Which of the relations are transitive?
• EXAMPLE: Let R be the relation on the set of real numbers such that aRb if and
only if a − b is an integer. Is R an equivalence relation?
• Solution: Because a −a = 0 is an integer for all real numbers a, aRa for all real
numbers a. Hence, R is reflexive.
• Now suppose that aRb. Then a −b is an integer, so b−a is also an integer.
Hence, bRa. It follows that R is symmetric.
• If aRb and bRc, then a −b and b−c are integers. Therefore, a −c = (a −b) + (b−c)
is also an integer. Hence, aRc. Thus, R is transitive.
• Consequently, R is an equivalence relation.
Relations (Cont.)
• EXAMPLE: Suppose that R is the relation on the set of strings of English letters
such that aRb if and only if l(a) = l(b), where l(x) is the length of the string x. Is R
an equivalence relation?
• Solution: Because l(a) = l(a), it follows that aRa whenever a is a string, so that
R is reflexive.
• Next, suppose that aRb, so that l(a) = l(b). Then bRa, because l(b) = l(a). Hence,
R is symmetric.
• Finally, suppose that aRb and bRc. Then l(a) = l(b) and l(b) = l(c). Hence, l(a) =
l(c), so aRc. Consequently, R is transitive.
• Because R is reflexive, symmetric, and transitive, it is an equivalence relation.
Relations (Cont.)
Example: Is the following relation reflexive, irreflexive, symmetric, antisymmetric, or transitive? R =
{(a,b) | a,bZ+ a, 2a, and b are side lengths of a triangle} Note: For all triangles, the sum of the lengths
of any two sides must exceed the length of the third side.
• Reflexive? No – because (a,a)R, this is because a triangle can not have side lengths, a, a and 2a.
• Irreflexive? Yes – the previous argument holds for all positive integers a.
• Symmetric? No – (a, 2a)R, since we can have a triangle with side lengths a, 2a and 2a. However, (2a,
a)R because we can not have a triangle with side lengths 2a, 4a and a.
• Antisymmetric? Yes – If we have a b, then we have (a,b)R. To prove this, consider a forming a
triangle with side lengths a, 2a, and b. We know that we must have a+b > 2a for a triangle to be formed.
BUT, a+b a+a = 2a, which means that a+b is NOT greater than 2a. Thus, in this situation, we have
(a,b)R. Thus, for any element (a,b)R, we must have a < b. For each of these elements, we can
guarantee that (b,a)R since b > a.
• Transitive? No – (a, 2a)R as shown above, and we also know that (2a, 4a)R, by a similar analysis.
But we can show that (a,4a)R because a triangle can not have side lengths a, 2a and 4a.
Relations (Cont.)
• Give an example of a relation on A ={a, b, c} that is
a) reflexive but not symmetric
R={(a, a), (b, b), (c, c), (a, b)}
• Properties of Functions:
• Injective (one-to-one): A function is injective if no two different elements
in the domain map to the same element in the codomain.
• Surjective (onto): A function is surjective if every element in the codomain
has at least one element in the domain that maps to it.
• Bijective (one-to-one correspondence): A function is bijective if it is both
injective and surjective.
Relations(Cont.)
• Equivalence Classes:
• An equivalence relation partitions a set into disjoint subsets called
equivalence classes. Elements within the same equivalence class are
considered equivalent under the relation.
• Let R be an equivalence relation on a set A. The set of all elements that
are related to an element a of A is called the equivalence class of a. The
equivalence class of a with respect to R is denoted by [a]R. When only one
relation is under consideration, we can delete the subscript R and write
[a] for this equivalence class.
• [a]R = {s | (a, s) ∈ R}.
Relations(Cont.)
• Example: Let R be the relation on the set of integers such that aRb if and only if a
= b or a = −b. What is the equivalence class of an integer for the equivalence
relation R.
• Solution: Because an integer is equivalent to itself and its negative in this
equivalence relation, it follows that [a] = {−a, a}. This set contains two distinct
integers unless a = 0. For instance, [7] = {−7, 7}, [−5] = {−5, 5}, and [0] = {0}.
Relations(Cont.)
• Example: What are the sets in the partition of the integers arising from congruence modulo 4?
• Solution: There are four congruence classes, corresponding to [0]4, [1] 4, [2] 4, and [3] 4. They are
the sets:
[0] 4 = {. . . ,−8,−4, 0, 4, 8, . . . },
[1] 4 = {. . . ,−7,−3, 1, 5, 9, . . . },
[2] 4 = {. . . ,−6,−2, 2, 6, 10, . . . },
[3] 4 = {. . . ,−5,−1, 3, 7, 11, . . . }.
These congruence classes are disjoint, and every integer is in exactly one of them.
Assiut University
قومية كلية
ل معتمدة من الهيئة ال عتماد
ضمان جودة التعليم واال
Closure properties under +, −, : If a, b are integers, then a+b, a −b, a b are integers.
Additive inverse
For all integer a, a + (−a) = (−a)+a = 0
Division
If a, b Z, b 0, then it may be that (a / b) Z, so integers are not closed under division.
Divisibility
• If a and b are integers with a ≠ 0, we say that a divides b if there is an integer c such that b = ac, or
equivalently, if b/a is an integer. When a divides b we say that a is a factor or divisor of b, and that b is a
multiple of a. The notation a | b denotes that a divides b.
• We can express a | b using quantifiers as ∃c(ac = b), where the universe of discourse is the set of integers.
For any a, b Z
3) If a | b and b | a, then a = b
4) If a | b and b>0 and a>0, then a b
5) If mZ, m0, then a | b if and only if (ma)|(mb)
Divisibility (Cont.)
For any a, b, c
THEOREM: If a | b and b | c, then a | c.
Proof.
a | b → b = ax for some xZ (by definition of divisibility)
b | c → c = by for some yZ (by definition of divisibility)
By substitution we have c = (ax)y.
By associative law, c = a(xy).
xy=k is an integer by the closure property of integers under
multiplication.
c = ak means that a | c .
Divisibility (Cont.)
THEOREM : If a | b then a | bc.
Proof
By the definition of divisibility, a | b implies that there is some integer x such that b = ax.
For any integer c, bc = (ax)c = a(xc) by associative property of multiplication.
By the closure property integers under multiplication
xc = k, an integer, so bc =ak, i. e. a | bc
Divisibility (Cont.)
• THEOREM : If a | b and a | c then a | (bx + cy) for any x, y Z
Proof is left as an exercise.
Divisibility (Cont.)
• THEOREM
• THE DIVISION ALGORITHM Let a be an integer and d a positive integer. Then there are unique
integers q and r, with 0 ≤ r < d, such that a = dq + r.
• Note that the remainder cannot be negative.
• In the equality given in the division algorithm, d is called the divisor, a is called the dividend, q is
called the
quotient, and r is called the remainder. This notation is used to express the quotient and
remainder: q = a div d, r = a mod d.
• when a is an integer and d is a positive integer, we have
• a div d = 𝑎/𝑑
• Example: What are the quotient and remainder when 101 is divided by 11?
• We have 101 = 11 ・ 9 + 2.
• the quotient when 101 is divided by 11 is 9 = 101 div 11, and the remainder is 2 = 101 mod 11.
• Example: What are the quotient and remainder when −11 is divided by 3?
• Hence, the quotient when −11 is divided by 3 is −4 = −11 div 3, and the remainder is 1 = −11 mod 3.
• Consequently, the remainder is not −2, even though −11 = 3(−3) − 2, because r = −2 does not satisfy 0 ≤ r < 3.
Modular Arithmetic
• We now introduce a notation that indicates that two integers have the same remainder when they are divided
by the positive integer m.
• DEFINITION: If a and b are integers and m is a positive integer, then a is congruent to b modulo m if m
divides a − b. We use the notation a ≡ b (mod m) to indicate that a is congruent to b modulo m. We say that a
≡ b (mod m) is a congruence and that m is its modulus (plural moduli).
• THEOREM: Let a and b be integers, and let m be a positive integer. Then a ≡ b (mod m) if and only if a mod
m = b mod m.
• Example: Determine whether 17 is congruent to 5 modulo 6
• Because 6 divides 17 − 5 = 12, we see that 17 ≡ 5 (mod 6).
• Example: Determine whether 24 is congruent to 14 modulo 6
• 24 − 14 = 10 is not divisible by 6, we see that 24 is not congruent to 14 (mod 6).
Arithmetic Modulo m
• We can define arithmetic operations on Zm, the set of nonnegative integers less than m, that is, the set {0, 1, . .
. , m − 1}.
• We define addition of these integers, denoted by +m by a +m b = (a + b) mod m.
• We define multiplication of these integers, denoted by ・m by a ・m b = (a ・ b) mod m.
• Example: find 7 +11 9
• 7 +11 9 = (7 + 9) mod 11 = 16 mod 11 = 5
• DEFINITION: The integers a and b are relatively prime if their greatest common divisor is 1.
• Example: it follows that the integers 17 and 22 are relatively prime, because gcd(17, 22) = 1.
Greatest Common Divisors (Cont.)
Suppose that the prime factorizations of the integers a and b are:
a a
a = p1 1 p2 2 ...pna n b b
b = p1 1 p22 ...pnbn
(ai, bi 0 )
• Theorem. For any two integers a and b ab = gcd (a, b) lcm(a, b)
a a a b b b
a b = ( p1 1 p2 2 ... pk k ) ( p1 1 p22 ... pkk )
a + b1 a + b2 a + bk
= p1 1 p2 2 ... pk k
125 = 1*87 + 38
= 2*38 + 11
= 3*11 + 5
11 = 2*5 + 1
= 5*1
125 = 6*20 + 5
= 4*5,
For example, to find integer solution for 85x +34y = 51, find the gcd(85, 34) using Euclid Algorithm:
85=342+17
34= 172
So, gcd(85, 34)=17.
Since 17|51 a solution exists
• So, if a solution (x, y) exists, then there are infinitely many solutions (x −6k, y+15k), k is any integer.
Assiut University
قومية كلية
ل معتمدة من الهيئة ال عتماد
ضمان جودة التعليم واال
• Proof by induction:
• Base case: For n = 1, 1 = 1(1+1)/2, which is true.
• Inductive step: Assume the statement is true for n = k: 1 + 2 + 3 + ... + k =
k(k+1)/2. Now, we need to prove it's true for n = k+1:
1 + 2 + 3 + ... + k + (k+1) = k(k+1)/2 + (k+1)
Simplifying the right side: (k+1)(k+2)/2
• Therefore, the statement is true for n = k+1.
Proof by induction (Cont.)
• Example: Use mathematical induction to show that
1 + 2 + 22 +· · ·+2n = 2n+1 − 1 for all nonnegative integers n.
• Proof by induction: Solution: Let P(n) be the proposition that 1 + 2 + 22 +· · ·+2n = 2n+1 − 1 for the integer n.
• BASIS STEP: P(0) is true because 20 = 1 = 21 − 1.
• INDUCTIVE STEP: We assume that P(k) is true for an arbitrary nonnegative integer k. That is, we assume that
1 + 2 + 22 +· · ·+2k = 2k+1 − 1.
• We must show that when we assume that P(k) is true, then P(k + 1) is also true. That is, we must show that
1 + 2 + 22 +· · ·+2k + 2k+1 = 2(k+1)+1 − 1 = 2k+2 − 1
assuming the inductive hypothesis P(k). Under the assumption of P(k), we see that
1 + 2 + 22 +· · ·+2k + 2k+1 = (1 + 2 + 22 +· · ·+2k) + 2k+1
= (2k+1 − 1) + 2k+1 using IH
= 2 · 2k+1 − 1
= 2k+2 − 1.
• We have completed the inductive step.
Strong induction
• Strong induction is a mathematical proof technique that extends
the principle of mathematical induction. Strong induction allows
you to assume that the statement holds for all values up to a
particular value.
• We can prove the following formula for the nth Fibonacci number
using strong induction:
F(n) = [(1 + √5)/2]^n - [(1 - √5)/2]^n / √5
Strong induction (Cont.)
• Base Cases:
• Inductive Conclusion: We need to prove that the formula holds for k+1. Using the recursive definition of Fibonacci numbers:
• Therefore, by strong induction, the formula holds for all non-negative integers n.
Recursive Definitions
• Recursively Defined Functions
• BASIS STEP: Specify the value of the function at zero.
• RECURSIVE STEP: Give a rule for finding its value at an integer from its values at smaller integers.
• We illustrate the workings of the function with a trace when the input is a = 5, b = 8.
• gcd(5, 8) = gcd(8 mod 5, 5) = gcd(3, 5).
• gcd(3, 5) = gcd(5 mod 3, 3) = gcd(2, 3)
• cd(2, 3) = gcd(3 mod 2, 2) = gcd(1, 2)
• gcd(1, 2) = gcd(2 mod 1, 1) = gcd(0, 1).
• Finally, to find gcd(0, 1) it uses the first step with a = 0 to find that gcd(0, 1) = 1.
• Consequently, the function finds that gcd(5, 8) = 1.
Recursive Algorithms (Cont.)
• Example: Give a recursive algorithm for Fibonacci Numbers
• Call Graphs: Graphs can be used to model telephone calls made in a network.
• Each telephone number is represented by a vertex and each telephone call is represented by a directed edge.
• The edge representing a call starts at the telephone number from which the call was made and ends at the telephone
number to which the call was made.
• We need directed edges because the direction in which the call is made matters.
• We need multiple directed edges because we want to represent each call made from a particular telephone number to a
second number.
Graph Models (Cont.)
• The Web Graph: The World Wide Web can be modelled as a directed graph where each Web page is
represented by a vertex and where an edge starts at the Web page a and ends at the Web page b if there is a
link on a pointing to b. Because new Web pages are created and others removed somewhere on the Web
almost every second, the Web graph changes on an almost continual basis.
• Citation Graphs: Graphs can be used to represent citations in different types of documents. In such graphs,
each document is represented by a vertex, and there is an edge from one document to a second document if
the first document cites the second in its citation list.
• Road Networks: Graphs can be used to model road networks. In such models, vertices represent intersections
and edges represent roads.
• ….etc.
Graph Terminology
• Two vertices u and v in an undirected graph G are called adjacent (or neighbors) in G if u and v are endpoints
of an edge e of G.
• Such an edge e is called incident with the vertices u and v and e is said to connect u and v.
• The set of all neighbours of a vertex v of G = (V ,E), denoted by N(v), is called the neighborhood of v. If A is a
subset of V , we denote by N(A) the set of all vertices in G that are adjacent to at least one vertex in A. So, N(A)
= Uv∈A N(v).
• The degree of a vertex in an undirected graph is the number of edges incident with it, except that a loop at a
vertex contributes twice to the degree of that vertex. The degree of the vertex v is denoted by deg(v).
Graph Terminology (Cont.)
• EXAMPLE:
• deg(a) = 2, deg(b) = deg(c) = deg(f ) = 4, deg(d ) = 1, deg(e) = 3, and deg(g) = 0.
• The neighborhoods of these vertices are N(a) = {b, f }, N(b) = {a, c, e, f }, N(c) =
{b, d, e, f }, N(d) = {c}, N(e) = {b, c, f }, N(f ) = {a, b, c, e}, and N(g) = ∅.
Graph Terminology (Cont.)
• EXAMPLE
• deg(a) = 4, deg(b) = deg(e) = 6, deg(c) = 1, and deg(d ) = 5.
• The neighborhoods of these vertices are N(a) = {b, d, e}, N(b) = {a, b, c, d, e}, N(c) = {b}, N(d) = {a, b, e},
and N(e) = {a, b, d}.
Graph Terminology (Cont.)
THE HANDSHAKING THEOREM Let G = (V ,E) be an undirected graph with m edges. Then
2𝑚 = 𝑑ⅇ𝑔 𝑣
𝑣𝜖𝑉
(Note that this applies even if multiple edges and loops are present.)
• Example: How many edges are there in a graph with 10 vertices each of degree six?
• Solution: Because the sum of the degrees of the vertices is 6 ・ 10 = 60, it follows that 2m = 60 where m
is the number of edges. Therefore, m = 30.
Graph Terminology (Cont.)
• When (u, v) is an edge of the graph G with directed edges, the vertex u is called the initial vertex of (u, v), and v
is called the terminal or end vertex of (u, v). The initial vertex and terminal vertex of a loop are the same.
• In a graph with directed edges the in-degree of a vertex v, denoted by deg− (v), is the number of edges with v as
their terminal vertex. The out-degree of v, denoted by deg+ (v), is the number of edges with v as their initial
vertex. (Note that a loop at a vertex contributes 1 to both the in-degree and the out-degree of this vertex.)
• EXAMPLE: Find the in-degree and out-degree of each vertex in the graph G with directed edges.
• Solution: The in-degrees in G are deg−(a) = 2, deg−(b) = 2, deg−(c) = 3, deg−(d) = 2, deg−(e) = 3, and
deg−(f ) = 0.
• The out-degrees are deg+ (a) = 4, deg+(b) = 1, deg+(c) = 2, deg+(d) = 2, deg+(e) = 3, and deg+(f ) = 0.
Graph Terminology (Cont.)
• THEOREM: Let G = (V ,E) be a graph with directed edges. Then
deg − 𝑣 = deg + 𝑣 = 𝐸
𝑣∈V 𝑣∈V
Some Special Simple Graphs
• Complete Graphs: A complete graph on n vertices, denoted by Kn, is a simple graph that contains exactly
one edge between each pair of distinct vertices.
Some Special Simple Graphs (Cont.)
• Cycles: A cycle Cn, n ≥ 3, consists of n vertices v1, v2, . . . , vn and edges {v1, v2}, {v2, v3}, . . . , {vn−1, vn},
and {vn, v1}.
Some Special Simple Graphs (Cont.)
• Wheels: We obtain a wheel Wn when we add an additional vertex to a cycle Cn, for n ≥ 3, and connect this
new vertex to each of the n vertices in Cn, by new edges.
Some Special Simple Graphs (Cont.)
• n-Cubes An n-dimensional hypercube, or n-cube, denoted by Qn, is a graph that has vertices representing
the 2n bit strings of length n. Two vertices are adjacent if and only if the bit strings that they represent differ in
exactly one bit position.
Some Special Simple Graphs (Cont.)
• A simple graph G is called bipartite if its vertex set V can be partitioned into two disjoint sets V1 and V2 such
that every edge in the graph connects a vertex in V1 and a vertex in V2 (so that no edge in G connects either
two vertices in V1 or two vertices in V2). When this condition holds, we call the pair (V1, V2) a bipartition of
the vertex set V of G.
• For example, consider the graph representing marriages between men and women in a village, where each
person is represented by a vertex and a marriage is represented by an edge. In this graph, each edge connects a
vertex in the subset of vertices representing males and a vertex in the subset of vertices representing females.
• The following figure show a bipartite graph, because its vertex set can be partitioned into the two sets V1 =
{v1, v3, v5} and V2 = {v2, v4, v6}, and every edge of C6 connects a vertex in V1 and a vertex in V2.
Some Special Simple Graphs (Cont.)
• THEOREM: A simple graph is bipartite if and only if it is possible to assign one of two different colors to
each vertex of the graph so that no two adjacent vertices are assigned the same color.
• Example: Use the Theorem to determine whether the following graph is bipartite.
• Solution: Yes
• Example: Use the Theorem to determine whether the following graph is bipartite.
• Solution: No
Representing Graphs
• Adjacency lists, which specify the vertices that are adjacent to each vertex of the graph.
• Example:T1 is a full binary tree because each of its internal vertices has two children.
• T2 is a full 3-ary tree because each of its internal vertices has three children.
• In T3 each internal vertex has five children, so T3 is a full 5-ary tree.
• T4 is not a full m-ary tree for any m because some of its internal vertices have two children and others
have three children.
Trees as Models
• An Organizational Tree for a Computer Company
Trees as Models (Cont.)
• A Computer File System
Properties of Trees
• THEOREM: A tree with n vertices has n − 1 edges.
• Proof: We will use mathematical induction to prove this theorem.
• BASIS STEP: When n = 1, a tree with n = 1 vertex has no edges. It follows that the theorem is true for n = 1.
• INDUCTIVE STEP: The inductive hypothesis states that every tree with k vertices has k − 1 edges, where k is
a positive integer. Suppose that a tree T has k + 1 vertices and that v is a leaf of T (which must exist because
the tree is finite), and let w be the parent of v. Removing from T the vertex v and the edge connecting w to v
produces a tree T with k vertices, because the resulting graph is still connected and has no simple circuits. By
the inductive hypothesis, T has k − 1 edges. It follows that T has k edges because it has one more edge than T,
the edge connecting v and w. This completes the inductive step.
Properties of Trees (Cont.)
• The level of a vertex v in a rooted tree is the length of the unique path from the root to this vertex. The level
of the root is defined to be zero.
• The height of a rooted tree is the maximum of the levels of vertices.
• In other words, the height of a rooted tree is the length of the longest path from the root to any vertex.
• EXAMPLE: Find the level of each vertex in the following rooted tree. What is the height of this tree?
• Solution: The root a is at level 0.
• Vertices b, j , and k are at level 1.
• Vertices c, e, f , and l are at level 2.
• Vertices d, g, i, m, and n are at level 3.
• Vertex h is at level 4.
• Because the largest level of any vertex is 4, this tree has height 4.
Properties of Trees (Cont.)
• A rooted m-ary tree of height h is balanced if all leaves are at levels h or h − 1.
• EXAMPLE: Which of the following rooted trees are balanced?
• Solution: T1 is balanced, because all its leaves are at levels 3 and 4.
• T2 is not balanced, because it has leaves at levels 2, 3, and 4.
• T3 is balanced, because all its leaves are at level 3.
Properties of Trees (Cont.)
• THEOREM: There are at most mh leaves in an m-ary tree of height h.
Binary Search Trees
• A binary search tree, which is a binary tree in which each child of a vertex is designated as a right or left
child, no vertex has more than one right child or left child,
• each vertex is labeled with a key.
• Vertices are assigned keys so that the key of a vertex is both larger than the keys of all vertices in its left
subtree and smaller than the keys of all vertices in its right subtree.
• EXAMPLE: Form a binary search tree for the words mathematics, physics, geography, zoology, meteorology,
geology, psychology, and chemistry (using alphabetical order).
ALGORITHM: Locating an Item in or Adding an Item to a
Binary Search Tree
• THEOREM:A sorting algorithm based on binary comparisons requires at least log 𝑛! comparisons.
• log 𝑛! is θ(n log n),
HUFFMAN CODING
• Huffman coding is a fundamental algorithm in data compression, the subject devoted to reducing the number
of bits required to represent information.
• Huffman coding is extensively used to compress bit strings representing text and it also plays an important
role in compressing audio and image files.
• The algorithm begins with a forest of trees each consisting of one vertex, where each vertex has a symbol as
its label and where the weight of this vertex equals the frequency of the symbol that is its label.
• At each step, we combine two trees having the least total weight into a single tree by introducing a new root
and placing the tree with larger weight as its left subtree and the tree with smaller weight as its right subtree.
• Furthermore, we assign the sum of the weights of the two subtrees of this tree as the total weight of the tree.
• Example: Use Huffman coding to encode the following symbols with the frequencies listed: A: 0.08, B: 0.10,
C: 0.12, D: 0.15, E: 0.20, F: 0.35. What is the average number of bits used to encode a character?
HUFFMAN CODING (Cont.)
• Solution: The encoding produced encodes A by 111, B by 110, C by 011, D by 010, E by 10, and F by 00. The
average number of bits used to encode a symbol using this encoding is
• 3 · 0.08 + 3 · 0.10 + 3 · 0.12 + 3 · 0.15 + 2 · 0.20 + 2 · 0.35 = 2.45.
Tree Traversal
• Ordered rooted trees are often used to store information.We need procedures for visiting each vertex of an
ordered rooted tree to access data.
The Universal Address System of an Ordered Rooted Tree
Traversal Algorithms
• Preorder traversal
• Inorder traversal
• Postorder traversal
Preorder traversal
ALGORITHM:Preorder Traversal.
procedure preorder(T : ordered rooted tree)
r := root of T
list r
for each child c of r from left to right
T (c) := subtree with c as its root
preorder(T (c))
Inorder traversal
ALGORITHM:Postorder Traversal.
procedure postorder(T : ordered rooted tree)
r := root of T
for each child c of r from left to right
T (c) := subtree with c as its root
postorder(T (c))
list r