0% found this document useful (0 votes)
17 views412 pages

Propositional Logic Propositional Equivalences: Chapter 1: Logic and Proof

The document covers the fundamentals of propositional logic and equivalences, including definitions, operators, and truth tables. It introduces concepts such as negation, conjunction, disjunction, conditional statements, and biconditional statements. Additionally, it provides examples and exercises to illustrate the application of these logical principles.

Uploaded by

yj7njnjdpd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views412 pages

Propositional Logic Propositional Equivalences: Chapter 1: Logic and Proof

The document covers the fundamentals of propositional logic and equivalences, including definitions, operators, and truth tables. It introduces concepts such as negation, conjunction, disjunction, conditional statements, and biconditional statements. Additionally, it provides examples and exercises to illustrate the application of these logical principles.

Uploaded by

yj7njnjdpd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 412

Discrete Mathematics

Chapter 1: Logic and Proof

1.1 Propositional Logic


1.2 Propositional Equivalences
Yan Huang
School of Computer Science and Engineering
South China University of Technology

1
Thanks to Dr Patrick Chan for providing the Slides
Agenda
▪ Ch1.1 Propositional Logic
▪ Proposition
▪ Propositional Operator
▪ Compound Proposition
▪ Applications
▪ Ch1.2 Propositional Equivalences
▪ Logical Equivalences
▪ Using De Morgan's Laws
▪ Constructing New Logical Equivalences

2
Warm Up…
• John is a cop. John knows first aid. Therefore, all cops know
first aid

3
Warm Up…
▪ Human walks by two legs. Human is mammal. Mammal walks
by two legs.

4
Warm Up…
▪ The clock alarm of my iphone does not work today. The
clock alarm of iphone does not work on 1-1-2011. So, today
is 1-1-2011

5
Warm Up…
▪ Some students work hard to study. Some students fail in
examination. So, some work hard students fail in examination.

6
Small Quiz
▪ Next few pages contain 4 questions
▪ Write down the answer of each question on a paper
▪ Remember
▪ No Discussion
▪ Do not modify answers you written down

7
Small Quiz: Question 1
▪ According to the law, only a person who is elder than 21-year-
old can have alcoholic drink
▪ You are a police. Which person(s) you need to check?

Drink Tea Drink Beer 23-year-old 19-year-old

8
Small Quiz: Question 2
▪ According to a policy of a company, if someone surf the
Internet longer than 2 hours, he/she has to earn more than
300k
▪ You are the boss of this company. Which staff(s) you need
to check?

1h Surfing 3h Surfing Earned 200k Earned 400k

9
Small Quiz: Question 3
▪ A company publishes a desk:
▪ Each card has two sides: a character and a number
▪ If one side of a card is a vowel, the number on the other
side should be even number
▪ You are a QC staff. Which card(s) you need to check?

E T 4 7
10
Small Quiz: Question 4
▪ A company publishes another desk:
▪ Each card has two sides: a shape and a color
▪ If one side of a card is a circle, the color on the other side should
be yellow
▪ You are a QC staff. Which card(s) you need to check?

11
Small Game: Answer
• Q1: Only a person who is elder than
21-year-old can have alcoholic drink
• Q2: If someone surf the Internet
longer than 2 hours, he/she has to
earn more than 300k
• Q3: If one side of a card is a vowel,
the number on the other side is
even number
• Q4: If one side of a card is a circle,
the color on the other side is yellow
12
Introduction
▪ In this chapter, we will explain how to
▪ make up a correct mathematical argument
▪ prove the arguments

13
Propositions
▪ Proposition (also called statement) is a declarative sentence
(declares a fact) that is either true or false, but not both
▪ Truth value of a proposition is either True/False (T/F) to indicate
its correctness

▪ Example:
▪ Keep quiet

Not declarative
▪ 1 hour has 50 minutes

False
▪ 1+1=3 False
▪ x+2=4 Can be either true or false
Can be turned into proposition when x is defined
14
Propositions
▪ Proposition Variable is letters denote propositions
▪ Conventional letters are p,q,r,s,……P,Q ,……
▪ Example: r : Peter is a boy

▪ Proposition Logic is the area of logic that deals with


propositions
▪ Logic Operators
▪ NOT ▪ If… then
▪ AND (Conditional Statement )
▪ OR ▪ If and Only If
▪ XOR (Biconditional Statement)
15
Proposition Logic
Negation Operator (Not)
▪ Definition
▪ Let p be a proposition
▪ Negation of p is the statement “It is not the case that p”
▪ Notation: ¬p
▪ Read as “not p”
▪ Truth value
▪ Opposite of the truth value of p
▪ Example:
▪ p: you are a student
▪ ¬p: You are not a student

16
Proposition Logic
Conjunction Operator (AND)
▪ Definition
▪ Let p and q be propositions
▪ Conjunction of p and q is “p and q”
▪ Notation: p ⋀ q
▪ ⋀ points up like an “A”, which means “⋀ ND”
▪ Truth value
▪ True when both p and q are true
▪ False otherwise
▪ Example:
▪ p: Peter likes to play, q: Peter likes to read
▪ p ⋀ q : Peter likes to play and Peter likes to read
17
Proposition Logic
Disjunction Operator (OR)
▪ Definition
▪ Let p and q be propositions
▪ Disjunction of p and q is “p or q”
▪ Notation: p ⋁ q
▪ ⋁ points up like an “r”, means “O ⋁”
▪ Truth value
▪ False when both p and q are false
▪ True otherwise
▪ Example:
▪ p: Peter likes to play, q: Peter likes to read
▪ p ⋁ q : Peter likes to play or Peter likes to read
18
Proposition Logic
Disjunction Operator (OR)
▪ In English, OR has more than one meanings
▪ Example:
▪ Jackie is a singer OR Jackie is an actor
▪ Either one or both (inclusive)
▪ Disjunction operation (OR, ⋁ )
▪ Jackie is a man OR Jackie is a woman
▪ Either one but no both (exclusive)
▪ Exclusive OR operation (⊕ )

19
Proposition Logic
Exclusive OR Operator (XOR)
▪ Definition
▪ Let p and q be propositions
▪ Notation: p ⊕ q,
▪ Truth value
▪ True when exactly one of p and q is true
▪ False otherwise
▪ Example:
▪ p: You can have a tea, q: You can have a coffee
▪ p ⊕ q : You can have a tea or a coffee, but not both (exclusive or)

20
Small Exercise
▪ Given
p: “Today is Friday” q: “It is raining today”
▪ What is…?
▪ ¬p
Today is not Friday
▪ p ⋀ q
Today is Friday and it is raining today
▪ p⋁q
Today is Friday or it is raining today
▪ p⊕q
Either today is Friday or it is raining today, but not both
21
Small Exercise
▪ Given
p: “x > 50” q: “x < 100”
▪ What is…?
▪ ¬p
x ≤ 50
▪ p ⋀ q
Only q is T Only p is T
100 > x > 50
▪ p⋁q
p is T
x can be any number q is T

▪ p⊕q 50 100
x ≥ 100 or x ≤ 50 Both q and p is T

22
Proposition Logic
Truth Table
▪ Truth Table displays the relationships between the truth values of
propositions
▪ Example:
▪ Truth Table of Negation Operation

P ¬P
T F
F T

Operand Column Result Column

23
Proposition Logic
Truth Table

NOT AND OR XOR


p q ¬p p ⋀q p⋁q p ⊕q

T T F T T F
T F F F T T
F T T F T T
F F T F F F

24
Proposition Logic
Conditional Statement (imply)
▪ Definition
▪ Let p and q be propositions
▪ Conditional statement is “if p, then q”
▪ Notation: p → q
▪ p is called the hypothesis (or antecedent or premise)
▪ q is called the conclusion (or consequence)
▪ Truth value p q p→q
▪ False when p is true and q is false T T T
▪ True otherwise T F F
▪ Example F T T
▪ p: you work hard, q: you will pass this subject F F T
▪ p → q : If you work hard, then you will pass this subject

25
Proposition Logic
Conditional Statement (imply)
▪ Example:
▪ p: “You give me twenty dollars”
▪ q: “We are the best friends”
▪ What is p → q?
▪ If you give me twenty dollars, then we are the best friends
▪ Assume p → q is true, what does “you do not give me twenty dollars”
(¬p) mean?
▪ Does it mean
▪ “We are not the best friend” (¬p → ¬q)?

26
Proposition Logic
Conditional Statement (imply)
▪ Example: p → q and its Contrapositive are equivalent
▪ Given p → q
Converse and Inverse are equivalent
“If it rains, the floor is wet”
▪ Situation 1 (¬p → ¬q) Inverse
If it does not rain, the floor is not wet
▪ Situation 2 (q → p ) Converse
If the floor is wet, it rains
▪ Situation 3 (¬q → ¬p) ✓ Contrapositive
If the floor is not wet, it does not rain

27
Proposition Logic
Necessary Condition

▪ To say that p is a necessary condition for q, it is impossible


to have q without p
▪ Example
▪ Breathing is necessary condition for human life
▪ You cannot find a non-breathing human who is alive

▪ Taking a flight is not necessary condition to go to


Beijing
▪ You can go to Beijing by train, bus…
28
Proposition Logic
Sufficient Condition

▪ To say that p is a sufficient condition for q, the presence of p


guarantees the presence of q

▪ Example
▪ Being divisible by 4 is sufficient for being an even number

▪ Working hard is not sufficient for having a good


examination result

29
Proposition Logic
Necessary / Sufficient Condition
▪ Relation between conditional statement and necessary / sufficient condition
▪ Necessary Condition
▪ E.g. Breathing is necessary condition for human life

P Q P is necessary
condition of Q
T T T p q p→q
T F T T T T
F T F
T F F
F F T
F T T
F F T
▪ Sufficient Condition
▪ E.g. Being divisible by 4 is sufficient for being an even number

P Q P is sufficient
condition of Q
▪ p → q is equivalent to:
T T T
T F F ▪ p is sufficient condition of q
F T T ▪ q is necessary condition of p
F F T

30
Proposition Logic
Conditional Statement (imply)
▪ Other equivalent forms for P → Q:
▪ P is a sufficient condition for Q
▪ Q is a necessary condition for P
▪ P implies Q
▪ If P, then Q
▪ If P, Q
▪ Q if P
▪ Q whenever P
P cannot be true when Q is not true
▪ P only if Q Q is necessary condition for P

31
Proposition Logic
Conditional Statement (imply)
▪ Remark:
▪ No causality is implied in P → Q
▪ P may not cause Q
▪ For example:
▪ If I have more money than Bill Gates, then a rabbit lives on the moon

32
1->2

33
Proposition Logic
Conditional Statement (imply)
▪ Example:
▪ A mother tells her child that “If you finish your homework, then you can eat the ice-
cream”
▪ What does it mean?

▪ Case 1 (p → q)
▪ Homework is finished, you can eat the ice-cream
▪ Homework is not finished, you can/cannot eat the ice-cream

▪ Case 2
▪ Homework is finished, you can eat the ice-cream
▪ Homework is not finished, you cannot eat the ice-cream
34
Proposition Logic
Biconditional Statement (equivalent)
▪ Definition
▪ Let p and q be propositions
▪ Biconditional statement is “p if and only if q” (iff)
▪ Notation: p ↔ q
▪ Also called bi-implications, equivalence
▪ Equivalent to (p → q) ⋀ (q → p)
p q p↔ q
▪ Truth value T T T
▪ True when p and q have the same truth values T F F
F T F
▪ False otherwise F F T

35
Proposition Logic
Necessary / Sufficient Condition
▪ p is necessary but not sufficient for q
q→p
▪ p is sufficient but not necessary for q
p→q
▪ p is both necessary and sufficient for q
p →q ⋀ p → q q↔p
▪ q is also both necessary and sufficient for p

36
Proposition Logic

▪ Remarks:
▪ In ordinary speech, words like “or” and “if-then” may have
multiple meanings
▪ In this technical subject, we assume that
▪ “or” means inclusive or ( ∨ )
▪ “if-then” means implication (→)

37
Proposition Logic

▪ Summary

Formal Name Nickname Symbol


Negation Operator NOT ¬
Conjunction Operator AND ⋀
Disjunction Operator OR ⋁
Exclusive-OR Operator XOR ⨁
Conditional Statement Imply →
Biconditional Statement Equivalent ↔

38
Proposition Logic

▪ Summary

p q ¬p p⋀q p⋁q p⨁ q p→q p↔q

T T F T T F T T
T F F F T T F F
F T T F T T T F
F F T F F F T T

39
Compound Proposition
▪ Compound Propositions are formed from existing propositions using proposition logical
operators
▪ Example: Beijing is the capital of China and 1+1=2

▪ How can we determine the truth values of the complicated compound propositions
involving any number of propositional variables?
▪ Example:
What is the truth value for every situations? p → ¬q ↔ s ⋀ q ⨁ p

40
Compound Proposition

▪ Precedence of Logical Operator

Precedence Operator
1 ¬ NOT
2 ⋀ AND
3 ⋁ ⨁ OR XOR
4 → Imply
5 ↔ Equivalent

▪ Example:

✓ ✓
▪ p⋁q⋀r ▪ ¬s⋀f ▪ a ↔ f→b


▪ p ⋁ ( q ⋀ r) ▪ (¬ s )⋀ r ▪ (a↔f) →b
▪ (p ⋁ q) ⋀ r ▪ ¬ (s ⋀ f ) ▪ a↔(f→b )

41
Compound Proposition

42
Compound Proposition

▪ Truth tables can be used to determine the truth values of the


complicated compound propositions

▪ Algorithm:

1. Write down all the combinations of the compositional variables

2. Find the truth value of each compound expression that occurs in the
compound proposition according to the operator precedence

43
Compound Proposition

2. Write down all the combinations of the compositional variables

44
Compound Proposition

2. Write down all the combinations of the compositional variables

45
Compound Proposition

2. Find the truth value of each compound expression


that occurs in the compound proposition according to
the operator precedence

46
Compound Proposition

2. Find the truth value of each compound expression


that occurs in the compound proposition according to
the operator precedence

47
Compound Proposition

2. Find the truth value of each compound expression


that occurs in the compound proposition according to
the operator precedence

48
Compound Proposition

2. Find the truth value of each


compound expression that occurs in
the compound proposition according
to the operator precedence

49
Compound Proposition

2. Find the truth value of


each compound expression
that occurs in the compound
proposition according to the
operator precedence
50
Small Exercise

▪ Write down the truth table for the following compound


statement:

(p ∨ ( r ∧ q ))⟷(p ⊕ ( ¬r ) )

p q ¬p p ⋀ q p ⋁ q p ⨁ q p→q p↔q
T T F T T F T T
T F F F T T F F
F T T F T T T F
F F T F F F T T
51
Small Exercise

52
Translating English Sentences

▪ Human language is often ambiguous

▪ Translating human language into compound propositions (logical expression)


removes the ambiguity

53
Translating English Sentences
▪ Algorithm:
1. Remove the connective operators
2. Let a variable for each complete concept
3. Use the operators to connect the variables
4. Adding brackets in suitable positions will be helpful

▪ Example:
▪ You can access the Internet from campus only if you are a computer science major or you
are not a freshman
p →(q ⋁ ¬ s )
▪ p: “You can access the Internet from campus”
▪ q: “You are a computer science major”
▪ s: “You are a freshman”

54
Applications

System Specifications
▪ Specifications are the essential part of the system and software
engineering

▪ Specifications should be consistent, otherwise, no way to develop a


system that satisfies all specifications
▪ Consistence means all specifications can be true

55
Applications

System Specifications
▪ Example:

▪ There are three specifications for a particular system, are they


consistent?
▪ “The diagnostic message is stored in the buffer or it is retransmitted.”

▪ “The diagnostic message is not stored in the buffer.”

▪ “If the diagnostic message is stored in the buffer, then it is retransmitted.”

56
Applications 1.Remove the connective operators
2.Let a variable for each complete concept
System Specifications 3.Use the operators to connect the variables
4. Adding brackets in suitable positions will be helpful

▪ “The diagnostic message is stored in the buffer or it is retransmitted.”


▪ “The diagnostic message is not stored in the buffer.”

▪ “If the diagnostic message is stored in the buffer, then it is retransmitted.”

▪ P: The diagnostic message is stored in the buffer


▪ Q: The diagnostic message is retransmitted

▪ These specifications are consistent

57
Applications

Logic and Bit Operations


▪ Information stored in a computer is represented by bits
E.g. A = 0100 0001

▪ Bit = Binary Digit, i.e. 0 or 1 (F or T)


▪ Logic connectives can be used as bit operation
▪ Bitwise OR (⋁)
▪ the OR of the corresponding bits in the two strings
▪ Bitwise AND (⋀ )
▪ the AND of the corresponding bits in the two strings
▪ Bitwise XOR (⨁ )
▪ the XOR of the corresponding bits in the two strings

58
Applications

Logic and Bit Operations


▪ Example:
E.g. A = 0100 0001

A 1011 0110
B 0001 1101
Bit-wise OR 1011 1111

Bit-wise AND 0001 0100


Bit-wise XOR 1010 1011

59
Applications

Logic Puzzles
▪ Puzzles that can be solved using logic are known as logic puzzles

▪ Can be solved by using rules of logic

▪ Example:
▪ There are two kinds of people on an island
▪ Batman: Always tell the truth
▪ Joker: Always lie

▪ One day, you encounter two peoples A and B


▪ A says “B is a Batman”
▪ B says “The two of us are opposite types”

▪ What are A and B?

60
A B P Q
Batman Batman T F
Batman Joker F T
Joker Batman T T
Joker Joker F F

61
Types of Proposition
▪ Tautology P ¬P P ⋁ ¬P
▪ A compound proposition which is always true T F T
▪ Example: P ⋁ ¬P F T T

▪ Contradiction
P ¬P P ∧ ¬P
▪ A compound proposition which is always false T F F
▪ Example: P ∧ ¬P F T F

▪ Contingency
P
▪ A compound proposition which is neither a P ∧ ¬P P ⊕ (P ∧ ¬P)
T F T
tautology nor a contradiction
F F F
▪ Example: P ⊕(P ∧ ¬P)
62
Types of Proposition
▪ Are they Tautology, Contradiction or Contingency?
▪ P→P Tautology

▪ P⊕P Contradiction

▪ P↔P Tautology

▪ P→Q Contingency

▪ ¬P ⋁ Q Contingency

▪ ¬(P → Q) ∧ Q Contradiction

63
Logically Equivalence

▪ An important type of step used in a mathematical argument is


the replacement of a statement with another statement with the
same truth value

▪ We would like to discuss about the equivalences of arguments

64
Logically Equivalence

▪ Definition
Two propositions P and Q are logically equivalent if P ↔ Q is a
tautology

▪ Notation: P ⇔ Q or P ≡ Q

65
Logically Equivalence
▪ Truth Table can be used to test if compositions are logically
equivalent
▪ Example:
• if ¬p ⋁ q and p → q are logically equivalent?

¬p ⋁ q ⇔ p → q
66
Logically Equivalence

▪ Example:
Show p ⋁ (q ∧ r) ⇔ (p ⋁ q) ∧ (p ⋁ r)

67
Logically Equivalence p q s T
T T T T
T T T F
▪ Characteristic of Truth Table T T F T
▪ Assume n is the number of variables, Raw of tables = 2n T T F F

E.g. 20 variables, 220 = 1048576 T F T T


T F T F
▪ Not efficient
T F F T
p q s T F F F
T T T F T T T
▪ Besides the Truth Table, we will introduce T T F F T T F
a series of logical equivalences T F T F T F T
T F F F T F F
p q F T T F F T T
T T F T F F F T F
p T F
F F T F F F T
T F T
F F F F F F F F F F

68
69
70
71
72
73
74
▪ Distributive Laws
p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r)

p ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r)

75
76
77
78
▪ De Morgan’s Laws Extension

79
▪ De Morgan’s Laws Extension

80
81
82
83
84
85
86
87
88
89
Test

90
主观题 10分

作答 91
主观题 10分

作答 92
主观题 10分

作答 93
主观题 10分

请您编辑题干

作答 94
主观题 10分

请您编辑题干

作答 95
主观题 10分

作答 96
97
Discrete Mathematic

Chapter 1: Logic and Proof


1.5
Rules of Inference
1.6
Introduction to Proofs
Yan Huang
School of Computer Science and Engineering
South China University of Technology

Thanks to Dr Patrick Chan for providing the Slides

Agenda
 Rules of Inference
 Rules of Inference for Quantifiers

Chapter 1.5 & 1.6 2


Recall…
 John is a cop. John knows first aid. Therefore,
all cops know first aid

Chapter 1.5 & 1.6 3

Recall…
 Some students work hard to study. Some
students fail in examination. So, some work
hard students fail in examination.

Chapter 1.5 & 1.6 4


Argument p: It rains
q: The floor is wet

Premise / If it rains, the floor is wet p


Hypothesis It rains pq
Conclusion  The floor is wet
 q

therefore Argument Argument Form

 Argument in propositional logic is a sequence of


propositions
 Premises / Hypothesis: All except the final proposition
 Conclusion: The final proposition
 Argument form represents the argument by
variables
Chapter 1.5 & 1.6 5

Argument: Valid? p1
 Given an argument, where p2
 p1, p2, …, pn be the premises

 q be the conclusion pn
 The argument is valid when q
(p1  p2  …  pn)  q is a tautology
 When all premises are true, the conclusion should be true
 When not all premises are true, the conclusion can be either
true or false
p q pq
T T T
Focus on this case
T F F
Check if it happens
F T T
F F T
Chapter 1.5 & 1.6 6
Argument
 Example: Argument is valid
pq If it rains, the floor is wet
p It rains
q  The floor is wet

( p  (p  q) )  q Tautology
Need to check if p q pq p  (p  q) (p  (p  q))  q
the conclusion is T T T T T
true or not T F F F T
F T T F T
Must be true F F T F T

Chapter 1.5 & 1.6 7

Rules of Inference
 How to show an argument is valid?
 Truth Table
 May be tedious when the number of variables is
large
 Rules of Inference
 Firstly establish the validity of some relatively
simple argument forms, called rules of inference
 These rules of inference can be used as building
blocks to construct more complicated valid
argument forms

Chapter 1.5 & 1.6 8


Rules of Inference
p
 Modus Ponens
pq
 Affirm by affirming
q

 Modus Tollens
q
 Deny by denying pq
 p

Chapter 1.5 & 1.6 9

Rules of Inference
 Hypothetical Syllogism pq
qr
pr
 Disjunctive Syllogism
pq
p

q

Chapter 1.5 & 1.6 10


Rules of Inference
 Addition p

pq
 Simplification p q

p
 Conjunction p
q
pq
Chapter 1.5 & 1.6 11

Rules of Inference
 Resolution
pq
p=T p=F p  r
q = T/F q=T
r=T r = T/F qr

 Example
 I go to swim or I play tennis
 I do not go to swim or I play football
 Therefore, I play tennis or I play football

Chapter 1.5 & 1.6 12


Rules of Inference ()
Modus Ponens ((p  q)  (p))  q

Modus Tollens ((q) (p  q))  p

Hypothetical Syllogism ((p  q)  (q  r))  (p  r)

Disjunctive Syllogism ((p  q)  (p))  q

Addition (p)  p  q

Simplification ((p)  q))  p

Conjunction ((p)  (q))  (p  q)

Resolution ((p  q)  (p  r))  (q  r)

Chapter 1.5 & 1.6 13

Rules of Equivalence (↔)


Identify Laws pTp
 Recall… pFp
Domination Laws pTT
pFF
Idempotent Laws ppp
ppp
Negation Laws p  ¬p  T
p  ¬p  F
Double Negation Law ¬ (¬p)  p
Commutative Laws pqqp
pqqp
Associative Laws p  (q  r)  (p  q)  r
p  (q  r)  (p  q)  r
Distributive Laws p  (q  r)  (p  q)  (p  r)
p  (q  r)  (p  q)  (p  r)
Absorption Laws p  (p  q)  p
p  (p  q)  p
De Morgan’s Laws ¬(p  q)  ¬p  ¬q
¬(p  q)  ¬p  ¬q
Chapter 1.5 & 1.6 14
Comparison between
Inference and Equivalence
 Inference (p  q)  Equivalence (p ↔ q)
 Meaning:  Meaning:
If p, then q p is equal to q
 p  q does not mean  p ↔ q mean q ↔ p
qp  Only equivalence rules
 Either inference or can be used
equivalence rules can  p ↔ q can be proved
be used by showing p  q and
 p ↔ q implies p  q qp
  is used in proof   is used in proof
 Equivalence (↔) is a more restrictive relation than
Inference ()

Chapter 1.5 & 1.6 15

Using Rules of Inference


 Example 1:
 Given:
 It is not sunny this afternoon and it is colder than
yesterday.
 We will go swimming only if it is sunny
 If we do not go swimming, then we will take a canoe trip
 If we take a canoe trip, then we will be home by sunset
 Can these propositions lead to the conclusion
"We will be home by sunset“ ?

Chapter 1.5 & 1.6 16


Let p: It is sunny this afternoon
q: It is colder than yesterday
r: We go swimming
s: We take a canoe trip
t: We will be home by sunset

 It is not sunny this afternoon and it is colder


¬p  q
than yesterday
rp  We will go swimming only if it is sunny
 If we do not go swimming, then we will take
¬r  s
a canoe trip
 If we take a canoe trip, then we will be
st
home by sunset
t  We will be home by sunset

Chapter 1.5 & 1.6 17

Using Rules of Inference


Step Reason
1. ¬p  q Premise
Hypothesis: 2. ¬p Simplification using (1)
¬p  q 3. rp Premise
rp 4. ¬r Modus tollens using (2) and (3)
¬r  s 5. ¬r  s Premise

st 6. s Modus ponens using (4) and (5)


7. st Premise
Conclusion: 8. t Modus ponens using (6) and (7)
t
Therefore, the propositions can lead to the conclusion
We will be home by sunset
Chapter 1.5 & 1.6 18
Using Rules of Inference
 Or, another presentation method:
Hypothesis:
(¬p  q)  (r  p)  (¬r  s)  (s  t)
¬p  q
rp  ¬p  (r  p)  (¬r  s)  (s  t) By Simplification
¬r  s
 ¬r  (¬r  s )  (s  t) By Modus Tollens
st
 s  (s  t) By Modus Ponens
Conclusion:
t t By Modus Ponens

Chapter 1.5 & 1.6 19

 Small Exercise 
 Given:
 If you send me an e-mail message,
then I will finish writing the program
 If you do not send me an e-mail message,
then I will go to sleep early
 If I go to sleep early,
then I will wake up feeling refreshed
 Can these propositions lead to the conclusion
"If I do not finish writing the program,
then I will wake up feeling refreshed."

Chapter 1.5 & 1.6 20


Let p: you send me an e-mail message
q: I will finish writing the program
r: I will go to sleep early
s: I will wake up feeling refreshed

pq  If you send me an e-mail message,


then I will finish writing the program
 If you do not send me an e-mail message,
¬p  r then I will go to sleep early
 If I go to sleep early, then I will wake up
rs feeling refreshed

 If I do not finish writing the program,


¬q  s then I will wake up feeling refreshed

Chapter 1.5 & 1.6 21

 Small Exercise 
Step Reason
1. pq Premise
Hypothesis: 2. ¬q  ¬p Contrapositive of (1)
pq 3. ¬p  r Premise

¬p  r 4. ¬q  r Hypothetical Syllogism using (2) and


(3)
rs
5. rs Premise

Conclusion: 6. ¬q  s Hypothetical Syllogism using (4) and


(5)
¬q  s
Therefore, the propositions can lead to the conclusion
If I do not finish writing the program,
then I will wake up feeling refreshed
Chapter 1.5 & 1.6 22
 Small Exercise 
 Or, another presentation method:
Hypothesis:
(p  q)  (¬p  r)  (r  s)
pq
¬p  r  (¬q  ¬p)  (¬p  r)  (r  s) Contrapositive
rs
 (¬q  r)  (r  s) By Hypothetical Syllogism
Conclusion:
¬q  s  (¬q  s) By Hypothetical Syllogism

Chapter 1.5 & 1.6 23

Using Rules of Inference


Fallacies
 Are the following arguments correct?
 Example 1 (Fallacy of affirming the conclusion)
Hypothesis
 If you success, you work hard pq
 You work hard q
Conclusion
 You success  p
 Example 2 (Fallacy of denying the hypothesis)
Hypothesis
 If you success, you work hard pq
 You do not success ¬p
Conclusion
 You do not work hard  ¬q

Chapter 1.5 & 1.6 24


Rules of Inference for Quantifiers
 Universal Instantiation  Existential Instantiation

x P(x) x P(x)
 P(a)  P(c) for some element c
where a is a particular member
of the domain

 Universal Generalization  Existential Generalization

P(b) for an arbitrary b P(d) for some element d


 x P(x)  x P(x)
Be noted that b that we select must be
an arbitrary, and not a specific

Chapter 1.5 & 1.6 25

Rules of Inference for Quantifiers


 Example 1
 Given
 Everyone in this discrete mathematics class has
taken a course in computer science
 Marla is a student in this class
 These premises imply the conclusion
"Marla has taken a course in computer science"

Chapter 1.5 & 1.6 26


Let DC(x): x studies in discrete mathematics
CS(x): x studies in computer science
Domain of x: student

 Everyone in this discrete


x (DC(x)  CS(x)) mathematics class has taken a
course in computer science
DC(Marla)  Marla is a student in this class

 Marla has taken a course in


CS(Marla)
computer science

Chapter 1.5 & 1.6 27

Rules of Inference for Quantifiers


Premise: Conclusion:
x (DC(x)  CS(x)) CS(Marla)
DC(Marla)

Step Reason
1. x (DC(x)  CS(x)) Premise
2. DC(Marla)  CS(Marla) Universal Instantiation from (1)
3. DC(Marla) Premise
4. CS(Marla) Modus ponens using (2) and (3)

Therefore, the propositions can lead to the conclusion


Marla has taken a course in computer science

Chapter 1.5 & 1.6 28


Using Rules of Inference
for Quantifiers
 Or, another presentation method:
Premise: Conclusion:
x (DC(x)  CS(x)) CS(Marla)
DC(Marla)

x (DC(x)  CS(x))  DC(Marla)


By Universal Instantiation

 (DC(Marla)  CS(Marla))  DC(Marla)

 CS(Marla) By Modus ponens

Chapter 1.5 & 1.6 29

 Small Exercise 
 Given
 A student in this class has not read the book
 Everyone in this class passed the first exam
 These premises imply the conclusion
"Someone who passed the first exam has not
read the book"

Chapter 1.5 & 1.6 30


Let C(x): x in this class
RB(x): x reads the book
PE(x): x passes the first exam
Domain of x: any person

 A student in this class has not


x (C(x)  ¬RB(x)) read the book
 Everyone in this class passed
x (C(x)  PE(x)) the first exam

 Someone who passed the first


x (PE(x)  ¬RB(x)) exam has not read the book

We cannot define the domain as student in this


class since the conclusion means anyone

Chapter 1.5 & 1.6 31

 Small Exercise 
Premise: Conclusion:
x (C(x)  ¬RB(x))
x (PE(x)  ¬RB(x))
x (C(x)  PE(x))
Step Reason
1. x (C(x)  ¬RB(x)) Premise
2. C(a)  ¬RB(a) Existential Instantiation from (1)
3. C(a) Simplification from (2)
4. x (C(x)  PE(x)) Premise
5. C(a)  PE(a) Universal Instantiation from (4)
6. PE(a) Modus ponens from (3) and (5)
7. ¬RB(a) Simplification from (2)
8. PE(a)  ¬RB(a) Conjunction from (6) and (7)
9. x (PE(x)  ¬RB(x)) Existential Generalization from (8)

Therefore, the propositions can lead to the conclusion


Someone who passed the first exam has not read the book
Chapter 1.5 & 1.6 32
 Small Exercise 
 Or, another presentation method:
(x (C(x)  ¬RB(x)))  (x (C(x)  PE(x)))

 C(a)  ¬RB(a)  (x (C(x)  PE(x))) By Existential Instantiation

 C(a)  ¬RB(a)  (C(a)  PE(a)) By Universal Instantiation

 PE(a)  ¬RB(a) By Modus ponens

 x (PE(x)  ¬RB(x)) By Existential Generalization

Premise: Conclusion:
x (C(x)  ¬RB(x))
x (PE(x)  ¬RB(x))
x (C(x)  PE(x))
Chapter 1.5 & 1.6 33

Combining Rules of Inference


 The rules of inference of Propositions and
Quantified Statements can be combined
 Universal Modus Ponens
x (P(x)  Q(x)) (x (P(x)  Q(x)))  (P(a))
P(a), where a is a particular By Universal Instantiation
element in the domain  (P(a)  Q(a))  (P(a))
 Q(a)  Q(a) By Modus Ponens

 Universal Modus Ponens


x (P(x)  Q(x)) (x (P(x)  Q(x)))  (¬Q(a))
By Universal Instantiation
¬Q(a), where a is a particular
 (P(a)  Q(a))  (¬Q(a))
element in the domain
 ¬P(a) By Modus Tollens
 ¬P(a)
Chapter 1.5 & 1.6 34
Combining Rules of Inference
 Example:
 Given
 For all positive integers n,
if n is greater than 4, then n2 is less than 2n
is true.
 Show that 1002 < 2100

Chapter 1.5 & 1.6 35

Combining Rules of Inference


 Example:
For all positive integers n,
if n is greater than 4, then n2 is less than 2n

P(n): n > 4
Q(n): n2 < 2n
n (P(n)  Q(n))
P(100) (since 100 > 4)
 Q(100) (1002 < 2100) By Universal Modus Ponens

Chapter 1.5 & 1.6 36


Summary
 What we have learnt in previous lectures?
 Proposition
 Operator
 Predicates
Show if an argument
 Quantifier is valid
 Truth Table
 Rules of Equivalence
 Rules of Inference
 This is called the formal proof
 very clear and precise
 extremely long and hard to follow
Chapter 1.5 & 1.6 37

Informal Proofs
 Informal proofs can often explain to
humans why theorems are true
 Proof of mathematical theorems
 Applications to computer science
 Move from formal proofs toward more
informal proofs

Chapter 1.5 & 1.6 38


Informal Proofs
 In practice, the proofs of theorems designed
for human consumption are almost always
informal proofs
 More than one rule of inference may be used in
each step
 Steps may be skipped
 The axioms being assumed
 e.g. even number can be written as 2k, where k is
integer
 The rules of inference used are not explicitly
stated

Chapter 1.5 & 1.6 39

Proof for Theorems


 Types of Theorem
 Implication (P(x)  Q(x))
 Equivalence (P(x)  Q(x))
 Statement (P(x))
 Type of proof
 Universal Quantification (For all...)
 Existential Quantification (For some...)
 Uniqueness Quantification (Only one...)

Chapter 1.5 & 1.6 40


Universal Quantification
Proof for Theorems: Methods
 Implication (P(x)  Q(x))
 Direct Proof
Assume P(x) is true, show Q(x) is true
 Indirect Proof: Proof by Contraposition
Assume Q(x) is true and show P(x) is true
 Equivalence (P(x)  Q(x))
 As P(x)  Q(x)  (P(x)  Q(x))  (Q(x)  P(x))
 Statement (P(x))
 Indirect Proof: Proof by Contradiction

Chapter 1.5 & 1.6 41

Universal Quantification: Proof of Theorems: Implication


Direct Proof
 Direct proofs lead from the hypothesis of a
theorem to the conclusion
1. Assume the premises are true
2. Show the conclusion is true

p1
Assume p1 show
premise

are true
pn
q
Chapter 1.5 & 1.6 42
Universal Quantification: Proof of Theorems: Implication
Direct Proof: Example 1
 Prove “If n is an odd integer, then n2 is odd”
 Given,
 The integer n is even
if there exists an integer k such that n = 2k
 The integer n is odd
if there exists an integer k such that n = 2k+1

Chapter 1.5 & 1.6 43

Show
If n is an odd integer, then n2 is odd
1. Assume the hypothesis is true
“n is odd” is true
 By definition, n = 2k + 1, where k is a integer
2. Show the conclusion is correct
n2 is odd
n2 = (2k + 1)2 = 4k2 + 4k + 1 = 2(2k2 + 2k) + 1
 By definition, as (2k2 + 2k) is an integer we can
conclude that n2 is an odd integer
 Therefore, “if n is an odd integer, then n2 is an
odd integer” has been proved
Chapter 1.5 & 1.6 44
Universal Quantification: Proof of Theorems: Implication
Direct Proof: Example 2
 Prove “If m and n are both perfect squares,
then nm is also a perfect square”
 Given
 An integer a is a perfect square
if there is an integer b such that a = b2

Chapter 1.5 & 1.6 45

Show
If m and n are both perfect squares, then nm is also a
perfect square

1. Assume m and n are both perfect squares


 By definition, m = a2 and n = b2, where a
and b are integers
2. Show that mn is a perfect square
 mn = a2b2 = (ab)2, where ab is an integer
 By the definition, we can conclude that mn is
a perfect square
 Therefore, “An integer a is a perfect square if
there is an integer b such that a = b2” has
been proved
Chapter 1.5 & 1.6 46
Universal Quantification: Proof of Theorems: Implication
Direct Proof: Example 3
 Prove “if n is an integer and 3n + 2 is odd,
then n is odd”
 Assume 3n + 2 is an odd integer
 3n + 2 = 2k + 1 for some integer k
 Show that n is odd
3n + 2 = 2k + 1
3n = 2k - 1
2k - 1
n=
3
Chapter 1.5 & 1.6 47

Universal Quantification: Proof of Theorems: Implication


Indirect Proof
 Sometimes, direct proofs may reach dead
ends
 Indirect proof may help
 Prove theorems not directly
 Do not start with the hypothesis and end with
the conclusion

Chapter 1.5 & 1.6 48


Universal Quantification: Proof of Theorems: Implication: Indirect Proof

Proof by Contraposition
 Recall, contrapositive:
p  q  q  p
 p  q can be proved by showing q  p is
true
1. Assume the conclusion is not true
2. Show either one premise is not true
(p1  p2  …  pn)  q
 q  (p1  p2  …  pn)
 q  (p1 p2  …  pn)
Chapter 1.5 & 1.6 49

Universal Quantification: Proof of Theorems: Implication: Indirect Proof

Proof by Contraposition: Example 1


 Prove “if n is an integer and 3n + 2 is odd, then n is
odd”
1. Assume the conclusion is false q  p
n is not odd
 n = 2k, where k is an integer
2. Show that the premises are not correct
3n + 2 is not odd
 3 (2k) + 2 = 6k + 2 = 2(3k + 2)
 As if n is not odd, 3n + 2 is not odd
Therefore, if n is an integer and 3n + 2 is odd, then
n is odd
Chapter 1.5 & 1.6 50
Universal Quantification: Proof of Theorems: Implication: Indirect Proof

Proof by Contraposition: Example 2


 Prove “if n = ab, where a and b are positive integers,
then a ≤√ n or b ≤√ n ”

1. Assume a >√ n and b >√ n is true


2. Show n ≠ ab
 ab > (√ n)2 = n
 Therefore, ab ≠ n

 Therefore, if n = ab, where a and b are positive


integers, then a ≤ √ n or b ≤ √ n

Chapter 1.5 & 1.6 51

 Small Exercise 
 Prove that “the sum of two rational numbers is
rational”
 Given
 The real number r is rational if there exist integers
p and q with q ≠ 0 such that r = p / q
 A real number that is not rational is called
irrational

Chapter 1.5 & 1.6 52


 Small Exercise 
 Direct Proof
 Suppose that r and s are rational numbers
 r = p / q, s = t / u, where q ≠ 0 and u ≠ 0
 Show that r+s is rational number
p t pu + qt
r + s = q + u = qu
 As q ≠ 0 and u ≠ 0, qu ≠ 0
 Therefore, r + s is rational
 Therefore, direct proof succeeded

Chapter 1.5 & 1.6 53

 Small Exercise 
 Prove “if n is an integer and n2 is odd, then n
is odd”

 Direct proof
 Suppose that n is an integer and n2 is odd
 Exists an integer k such that n2 = 2k + 1
 Show n is odd
 Show (n = ± √2k + 1) is odd
 May not be useful

Chapter 1.5 & 1.6 54


 Small Exercise 
 Proof by contraposition
 Suppose n is not odd
 n = 2k, where k is an integer
 Show n2 is not odd
 n2 = (2k)2 = 4k2
 n2 is even
 Therefore, proof by contraposition succeeded

Chapter 1.5 & 1.6 55

Universal Quantification
Proof of Theorems: Equivalence
 Recall, p  q  (pq)  (qp)
 To prove equivalence, we can show p  q
and q  p are both true

Chapter 1.5 & 1.6 56


Universal Quantification: Methods of Proving Theorems
Equivalence: Example
 Prove “If n is a positive integer, then n is odd
if and only if n2 is odd”

 Two steps
1. If n is a positive integer,
(shown in slides 43)
if n is odd, then n is odd
2

2. If n is a positive integer,
(shown in slides 54)
if n2 is odd, then n is odd
 Therefore, it is true

Chapter 1.5 & 1.6 57

Universal Quantification
Proof of Theorems: Equivalence
 How to show p1, p2, p3 and p4 are equivalence?
 p1  p2
 p1  p3 p1
 p1  p4 p2
 p2  p3
 p2  p4 p4
p3
 p3  p4
 Not necessary
 E.g. if p1  p2 and p2  p3, then p1  p3

Chapter 1.5 & 1.6 58


Universal Quantification
Proof of Theorems: Equivalence
p1  p2  p3  …  pn
 When proving a group of statements are equivalent,
any chain of conditional statements can
established as long as it is possible to work through
the chain to go from anyone of these statements to
any other statement
p1 p1 p1
p2 p2 p2

p4 p4 p4
p3 p3 p3
Chapter 1.5 & 1.6 59

Universal Quantification: Methods of Proving Theorems


Statement: Example Trap??

Can you prove “You love me” ? p


How?
If you love me, p  q
you will buy me iphone5

What does it mean if you…


Prove
1. Buy iphone q nothing
2. Do not buy iphone
q  p

Chapter 1.5 & 1.6 60


Universal Quantification: Methods of Proving Theorems
Statement: Example (Correct)
Can you prove “You love me” ? p
How?
If you do not love me,
you will not buy me iphone5
p  q
What does it mean if you…
1. Buy iphone q p
2. Do not buy iphone
Prove
q nothing

Chapter 1.5 & 1.6 61

Universal Quantification: Methods of Proving Theorems: Statement

Proof by Contradiction
 By using Proof by Contradiction,
If you want to show p is true, you need:
 p  q is true P Q P  Q
 q is false T T T
T F F
F T T
F F T

 Recall, Proof by Contradiction of p  q is


p  q

Chapter 1.5 & 1.6 62


Universal Quantification: Methods of Proving Theorems: Statement

Proof by Contradiction
 Procedures of Proof by Contradiction to
prove p is correct :
1. Understand the meaning of p
2. Find out what p implies (p  q is true)
3. Show that q is not correct

Chapter 1.5 & 1.6 63

Universal Quantification: Methods of Proving Theorems: Statement

Proof by Contradiction: Example 1


 Prove √2 is irrational Not “if… then…” format
Only one statement

1. Understand the meaning of p


√ 2 is rational
2. Find out what p implies q
If√ 2 is rational, there exist integers p and q with
√ 2 = p / q, where p and q have no common factors
 So that the fraction p / q is in lowest terms
3. Show that q is not correct
Show “there exist integers p and q with √ 2 = p / q”
is not true

Chapter 1.5 & 1.6 64


Show “there exist integers p and q with √2 = p / q” is not true

√2 = p / q , where q ≠ 0
2q2 = p2
 p2 is an even number
 If p2 is even, so p = 2a, and a is an integer
2q2 = 4a2
q2 = 2a2
 q is also even
 As p and q are even, they have a common factor
2, which leads the contradiction
 Therefore, “ √ 2 is irrational” is true
Chapter 1.5 & 1.6 65

Universal Quantification: Methods of Proving Theorems: Statement

Proof by Contradiction: Example 2


 Show that at least four of any 22 days must
fall on the same day of the week.

Chapter 1.5 & 1.6 66


Let p: "At least four of 22 chosen days fall on
the same day of the week."

1. Understand the meaning of p


At most three of 22 chosen days fall on the same
day of the week
2. Find out what p implies
As at most three day fall on the same week day,
therefore a week should have at least 22 / 3 days
3. Show that q is not correct
A week only has 7 days, therefore, q is not correct
Therefore, p is correct

Chapter 1.5 & 1.6 67

Universal Quantification: Methods of Proving Theorems: Statement

Proof by Contradiction
 Proof by Contradiction can also be used to
show P(x)  Q(x) (implication)
 Let S(x) : P(x)  Q(x) and prove S(x) is
correct
 S(x) : P(x)  Q(x)
 S(x)  (P(x)  Q(x)) is true
 P(x)  Q(x) is false S(x)
= (P(x)  Q(x))
= (P(x)  Q(x))
= P(x)  Q(x)
Chapter 1.5 & 1.6 68
Universal Quantification: Methods of Proving Theorems: Statement

Proof by Contradiction: Example 3


 Show "If 3n + 2 is odd, then n is odd“
Be noted that proof by contraposition can be used (shown in slide 50)

 Let P(n): Q(3n+2)  Q(n),


where Q(n) : “n is odd”
 P(n) implies:
P(n)  (Q(3n+2)  Q(n))
 (Q(3n+2)  Q(n))
 Q(3n+2)  Q(n)

Chapter 1.5 & 1.6 69

Universal Quantification: Methods of Proving Theorems: Statement

Proof by Contradiction: Example 3


 P(n) implies “Q(3n+2)  Q(n)”
 Q(n) imply…
 n is even, n = 2k, where k is integer
 3n+2 = 3(2k)+2 = 2(3k+1)
 Therefore, 3n+2 is even (Q(3n+2))
 Q(3n+2)  Q(3n+2) is false
 Therefore, P(n) must be false
 Therefore,
 Q(3n+2)  Q(n) is true

Chapter 1.5 & 1.6 70


Universal Quantification
Exhaustive Proof and Proof by Cases
 Sometimes, a theorem cannot be proved easily
using a single argument that holds for all possible
cases
 Rather than considering (p  q) directly, we can
consider different cases separately
 This argument is named Proof by Cases:
(p1  p2  …  pn)  q
 [(p1  q)  (p2  q)  ...  (pn  q)]

 E.g. x2 ≥ 0, we can x < 0, x = 0 and x > 0

Chapter 1.5 & 1.6 71

Universal Quantification
Exhaustive Proof
 Exhaustive Proofs
 Prove all the possibilities
 Example
 Prove that (n + 1)3 > 3n if n is a positive integer
with n ≤ 4
 Prove all the possibilities: n = 1, 2, 3 and 4

Chapter 1.5 & 1.6 72


Universal Quantification
Exhaustive Proof: Example 1
 Prove that (n + 1)3 ≥ 3n if n is a positive
integer with n ≤ 4
When n = 1 When n = 2
LHS: (n + 1)3 = 8 LHS: (n + 1)3 = 27
RHS: 3n = 3 RHS: 3n = 9
LHS > RHS LHS > RHS

When n = 3 When n = 4
LHS: (n + 64 1)3 = LHS: (n + 1)3 = 125
RHS: 3n = 27 RHS: 3n = 81
LHS > RHS LHS > RHS
 Therefore, (n + 1)3 > 3n is valid

Chapter 1.5 & 1.6 73

Universal Quantification
Exhaustive Proof: Example 2
 Given
 An integer is a perfect power if it equals na , where a is an integer
greater than 1
 Prove that the only consecutive positive integers not exceeding
100 that are perfect powers are 8 and 9
 By exhaustive proof, list all the perfect powers not exceeding 100

n=1 2 3 4 5 6 7 8 9 10
a=2 1 4 9 16 25 36 49 64 81 100
3 1 8 27 64
4 1 32 81
5 1 64
>5 1

 Therefore, only 8 and 9 are consecutive

Chapter 1.5 & 1.6 74


Universal Quantification
Proof by Cases
 Drawback of Exhaustive Proofs is to check
only a relatively small number of instances of
a statement
 Proof by Cases
 Prove all situations
 Example
 Prove that if n is an integer, then n2 > n
 Prove all the situations: n is positive, equal and
negative

Chapter 1.5 & 1.6 75

Universal Quantification
Proof by Cases: Example 1
 Prove that if n is an integer, then n2 ≥ n
When n ≥ 1
n2 = n x n ≥ n x 1 = n, therefore n2 ≥ n
When n = 0
n2 = n = 0, therefore, n2 = n
When n ≤ -1
n2 > 0 and n < 0, therefore n2 ≥ n
 Therefore, this theorem is valid

Chapter 1.5 & 1.6 76


Universal Quantification
Proof by Cases: Example 2
 Use a proof by cases to show that | x y | = |x| |y|, where
x and y are real numbers
(Recall |a| = a, when a ≥ 0 ; |a| = -a when a < 0)

When x ≥ 0 and y ≥ 0 When x < 0 and y ≥ 0


| x y | = x y = |x| |y| | x y | = - x y = (-x) (y)= |x| |y|

When x ≥ 0 and y < 0 When x < 0 and y < 0


| x y | = - x y = (x) (-y)= |x| |y| | x y | = x y = (-x) (-y)= |x| |y|

 Therefore, this theorem is valid

Chapter 1.5 & 1.6 77

Existence Proofs
 We will focus on the theorems which are
assertions that objects of a particular type
exist ()
 A theorem of this type is a proposition of the
form x P(x), where P is a predicate
 The proof of this proposition is Existence
Proof
 By finding an element a such that P(a) is true

Chapter 1.5 & 1.6 78


Existence Proofs
 Example:
 Show that there is a positive integer that can be
written as the sum of cubes of positive integers
in two different ways

 After considerable computation (such as a


computer search), we find that
1729 = 103 + 93 = 123 + 13
 An example is given, the proof is done

Chapter 1.5 & 1.6 79

Uniqueness Proof
 The theorems which assert the existence of a
unique element with a particular property will be
discussed
 The two parts of a uniqueness proof are:
 Existence (An element with the property exists)
 Show that an element x with the property exists
 Uniqueness (No other element has this property)
 Show that if y ≠ x, y does not have the property.
 Equivalently, we can show that if x and y both have
the desired property, then x = y
x (P(x)  y( P(y)  (y = x) ) )

Existence Uniqueness
Chapter 1.5 & 1.6 80
Uniqueness Proof: Example
 Show that if a and b are real numbers and a ≠ 0,
then there is a unique real number r such that
ar + b = 0

 Existence Part
 The real number t = -b / a is a solution of ar + b = 0 because
a(-b/a) + b = -b + b = 0
 Consequently, a real number t exists for which at + b = 0
 Uniqueness Part
 Suppose that s is a real number such that as + b = 0
at + b = as + b t is – b / a
at = as a is nonzero
t=s
 This means that if s ≠ t, then as + b ≠ 0
Chapter 1.5 & 1.6 81

Tips
 DO NOT over simplify the proof
 “Obviously" or "clearly” in proofs indicate that steps
have been omitted that the author expects the reader
to be able to fill in
 Unfortunately, this assumption is often not warranted
 We will assiduously try to avoid using these words and
try not to omit too many steps
 However, if we
included all steps in
proofs, our proofs
would often be too
long

Chapter 1.5 & 1.6 82


Discrete Mathematic

Chapter 2: Set Theory


2.1
Sets
2.2
Set Operations
Yan Huang
School of Computer Science and Engineering
South China University of Technology

Thanks to Dr Patrick Chan for providing the Slides

Agenda
 Ch 2.1
 Set
 The Power Set
 Cartesian Products
 Using Set Notation with Quantifiers
 Truth Sets of Quantifiers
 Ch 2.2
 Set Combination
 Set Identifies
 Generalized Unions and Intersections
Ch 2.1 & 2.2 2
Set
 Definition
A set is an unordered collection of objects
 The objects in a set are called the elements, or
members, of the set
 Notation:
 a  A denote that a is an
element of the set A
 a  A denotes that a is not
an element of the set A

Ch 2.1 & 2.2 3

Set
 There are many ways to express the sets
 Listing all the elements
 Set builder notation
 Venn diagrams

Ch 2.1 & 2.2 4


Set
Listing all the elements
S = {e1, e2, e3,…,en}
where ei is element in the set
 Example
 All vowels in the English alphabet: V = {a, e, i, o, u}
 Odd positive integers < 10: O = {1, 3, 5, 7, 9}
 Unrelated elements: U = {John, 3, *}
 Ellipsis (…) can be used to represent the general
pattern of elements
 Positive integers less than 100 can be denoted by
{1, 2, 3, ..., 99}
Ch 2.1 & 2.2 5

Set
Set Builder
 Describe the properties the elements must have to
be members
S = {x | P(x)}
S contains all the elements which make the predicate
P true
 Example:
 R = { x | x is integer < 100 and > 40}
 O = { x | x is an odd positive integer less than 10}
= {x  Z+ | x is odd and x < 10}
 Z+ is the set of positive integers

Ch 2.1 & 2.2 6


Set
Set Builder
 Important Sets:
 Real Numbers
R
 Natural Numbers
N = {0, 1, 2, 3, ...}, counting numbers
(sometimes not consider 0)
 Integers
Z = {..., -3, -2, -1, 0, 1, 2, 3, 4, ...}
 Positive / Negative Integers: Z+ / Z-
 Rational Numbers
Q = { p / q | pZ, pZ, and q≠0 }

Ch 2.1 & 2.2 7

Set
Venn Diagrams
 Venn Diagrams are named after the English
mathematician John Venn
 A rectangle represents the
universal set U v w
 Contains all the objects under e a
consideration
 U may varies depends on z U
which objects are of interest

 Inside the rectangle, circles, or other geometrical


figures are used to represent sets
 Points may represents elements
Ch 2.1 & 2.2 8
Set
Venn Diagrams
 Example
 A Venn diagram that represents V, the set of
vowels in the English alphabet
 Rectangle : U
 26 letters of the V
English alphabet a u
 Circle: V e o
i
 the set of vowels
U
 Elements: a, e, i, o, u

Ch 2.1 & 2.2 9

Set
 Two sets are equal if and only if they have
the same elements
 A and B are sets
 A and B are equal if and only if
x (x  A ↔ x  B)
 Notation (=)
 We write A = B if A and B are equal sets

Ch 2.1 & 2.2 10


Empty Set and Singleton Set
 Empty set (null set) is a special set that has
no elements, denoted by  or { }
 Example
 The set of all positive integers that are greater
than their squares is the null set

 A set with one element is called a


singleton set

Ch 2.1 & 2.2 11

Set with Empty set


 A common error is to confuse with
  : the empty set
  : the set consisting of just the empty set
 Singleton set:
The single element is the empty set itself

 A useful analogy: Folders


 The empty set
 An empty folder
 The set consisting of just
the empty set:
 A folder with exactly one folder
inside, namely, the empty folder

Ch 2.1 & 2.2 12


Subset
 The set A is said to be a subset of B if and only
if every element of A is also an element of B
 We use the notation A  B to indicate that A is a
subset of the set B
 We see that A  B if and only if the
quantification

x (x  A  x  B)

Ch 2.1 & 2.2 13

Subset Subset: x (x  A  x  B)
 Every nonempty set S is guaranteed to have
at least two subsets,
 Empty set (  S)
 x   is always false
 Set S itself (S  S)
 x  S  x  S must be true

Ch 2.1 & 2.2 14


Subset
 If A and B are sets with A  B and B  A,
then A = B
 A = B, where A and B are sets,
if and only if
x (x  A → x  B) and AB
x (x  B → x  A), BA
or equivalently if and only if
x (x  A ↔ x  B) A= B

Ch 2.1 & 2.2 15

Subset: Proper Subset


 When we wish to emphasize that a set A is a subset of
the set B but that A ≠ B, we write A  B and say that A
is a proper subset of B

 For A  B to be true, it must be


the case that A  B and there
must exist an element x of B that
is not an element of A A
 That is, A is a proper subset of B B U
if

x (x  A  x  B)  x (x  B  x  A)

Ch 2.1 & 2.2 16


Subset
 Sets may have other sets as members
 Example:
 A = {, {a}, {b}, {a, b}}
 B = {x I x is a subset of the set {a, b}}
 Note that A = B
{a}  A, but a  A

Ch 2.1 & 2.2 17

Finite and Infinite Subset


 Let S be a set
 If there are exist n distinct elements in S
 S is a finite set and that n is the cardinality of S
 The cardinality of S is denoted by |S|
 Example:
 A be the set of odd positive integers less than 10, |A| = 5
 S be the set of letters in the English alphabet, |S| = 26
 || = 0
 A set is said to be infinite if it is not finite
 The set of positive integers is infinite

Ch 2.1 & 2.2 18


Power Set
 Many problems involve testing all
combinations of elements of a set to see if
they satisfy some properties
 Power set of S is a set has as its members
all the subsets of S
 The power set of S is denoted by P(S)
 If a set has n elements, then its power set
has 2n elements

Ch 2.1 & 2.2 19

Power Set: Example


 What is the power set of {0, 1, 2}?
 P({0, 1, 2}) =
{, {0}, {1}, {2}, {0,1}, {0,2}, {1, 2}, {0,1,2}}
 What is the power set of {a}?
 P({a}) = {, {a}}
 What is the power set of ?
 P() = {}
 What is the power set of {}?
 P({}) = {, {}}
Ch 2.1 & 2.2 20
Ordered n-tuple
 The order of elements in a collection is often
important
 However, sets are unordered
 Ordered n-tuple (a1 ,a2 ,... ,an) is the ordered
collection that has
 a1 as its first element
 a2 as its second element
 ...
 an as its nth element

Ch 2.1 & 2.2 21

Ordered n-tuple
 Two ordered n-tuples are equal if and only if
each corresponding pair of their elements is
equal
(a1, a2, ..., an) = (b1, b2, ..., bn)
if and only if ai = bi, for i = 1,2, ..., n

Ch 2.1 & 2.2 22


Ordered n-tuple
 Ordered 2-tuples are called ordered pairs
 The ordered pairs (a, b) and (c, d) are equal if
and only if a = c and b = d
 Note that (a, b) and (b, a) are not equal
unless a = b

Ch 2.1 & 2.2 23

Ordered n-tuple
Cartesian Products
 A subset R of the Cartesian product
A x B is called a relation from the set A to the
set B
 The elements of R are ordered pairs, where
the first element belongs to A and the second
to B

Ch 2.1 & 2.2 24


Ordered n-tuple
Cartesian Products
 Let A and B be sets
 The Cartesian product of A and B, denoted by
A x B, is the set of all ordered pairs (a, b),
where a  A and b  B
A x B = {(a, b) | a  A  b  B}

Ch 2.1 & 2.2 25

Ordered n-tuple
Cartesian Products: Example 1
 Given A = {1, 2} and B = {a, b, c}
 What are A x B and B x A?

 AxB=
{(1, a), (1, b), (1, c), (2, a), (2, b), (2, c)}
 BxA=
{(a, 1), (a, 2), (b, 1), (b, 2), (c, 1), (c, 2)}

 A x B and B x A are not equal, unless


 A =  or B =  (so that A x B = ) or
 A=B

Ch 2.1 & 2.2 26


Ordered n-tuple
Cartesian Products: Example 3
 Given
 A represent the set of all students at a
university
 B represent the set of all courses offered at the
university
 What is the meaning of A x B?

 A x B represents all possible enrollments of


students in courses at the university

Ch 2.1 & 2.2 27

Ordered n-tuple
Cartesian Products
 Generally, the Cartesian product of the sets
A1, A2 , ..., An, denoted by A1 x A2 x... x An, is
the set of ordered n-tuples (a1, a2, ..., an),
where ai belongs to Ai for i = 1, 2, ..., n.
A1 x A2 x ... x An =
{(a1, a2, ..., an ) | ai  Ai for i = 1,2,...,n}

Ch 2.1 & 2.2 28


Ordered n-tuple
Cartesian Products: Example 3
 What is A x B x C, where
A = {0, 1}, B = {1, 2}, and C = {0, 1, 2}?

 AxB xC=
{(0,1,0), (0,1,1), (0,1,2),
(0,2,0), (0,2,1), (0,2,2),
(1,1,0), (1,1,1), (1,1,2),
(1,2,0), (1,2,1), (1,2,2)}

Ch 2.1 & 2.2 29

Set Notation with Quantifiers


 Sometimes we restrict the domain of a
quantified statement explicitly by making use
of set
 Example
 x  S (P(x)) denotes the universal
quantification of P(x) over all elements in the
set S
 x  S (P(x)) is shorthand for x (x  S  P(x))
 Similarly, x  S (P(x)) denotes the existential
quantification of P(x) over all elements in S
 x  S (P(x)) is shorthand for x (x  S  P(x))

Ch 2.1 & 2.2 30


Set Notation with Quantifiers
 Example
 What do the statements
x  R (x2 ≥ 0) and x  Z (x2 = 1) mean?
 x  R (x2 ≥ 0)
 For every real number x, x2 ≥ 0
 The square of every real number is nonnegative
 x  Z (x2 = 1)
 There exists an integer x such that x2 = 1
 There is an integer whose square is 1

Ch 2.1 & 2.2 31

Truth Sets of Quantifiers


 We will now tie together concepts from set
theory and from predicate logic
 Given a predicate P, and a domain D, we
define the truth set of P to be the set of
elements x in D for which P(x) is true
 The truth set of P(x) is denoted by
{x  D I P(x)}

Ch 2.1 & 2.2 32


Truth Sets of Quantifiers
 Given the domain is the set of integers, what is the truth
set of the following predicate?
 P(x) is "|x| = 1"
 |x| = 1 when x = 1 or x = -1
 The truth set of P is the set {-1, 1}
 Q(x) is "x2 = 2"
 There is no integer x for which x2 = 2
 The truth set of Q is empty set
 R(x) is "|x| = x"
 |x| = x if and only if x ≥ 0
 The truth set of R is N, the set of nonnegative
integers

Ch 2.1 & 2.2 33

Truth Sets of Quantifiers


 Note that
 x P(x) is true over the domain U
if and only if the truth set of P is the set U
 x P(x) is true over the domain U
if and only if the truth set of P is non empty

Ch 2.1 & 2.2 34


Set Combination
 Two sets can be combined in many different
ways
 Complement ( )
 Union ()
 Intersection ()
 Difference (-)
 Symmetric Difference (

Ch 2.1 & 2.2 35

Set Combination

Complement
 Let U be the universal set
The complement of the set A, denoted by A, is the
complement of A with respect to U
 The complement of the set A is U - A.
 An element x belongs to A if and only if x  A
A = {x I x  A}

U
Ch 2.1 & 2.2 36
Set Combination

Union
 Let A and B be sets
Union of the sets A and B, denoted by A U B, is the
set that contains those elements that are either in A
or in B, or in both
 An element x belongs to the union of the sets A and
B if and only if x belongs to A or x belongs to B
AUB={x|xAxB}

A B
 Notation: U (Union)
U
Ch 2.1 & 2.2 37

Set Combination

Intersection
 Let A and B be sets
Intersection of the sets A and B, denoted by A ∩ B,
is the set containing those elements in both A and B
 An element x belongs to the intersection of the sets
A and B if and only if x belongs to A and B
A∩B={x|xAxB}

 Notation: ∩ (i∩teraction) A B

U
Ch 2.1 & 2.2 38
Set Combination

Difference
 Let A and B be sets
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
 An element x belongs to the difference of A and
B if and only if
x  A and x  B
A – B = {x | x  A  x  B}
A–B=A∩B
A B

U
Ch 2.1 & 2.2 39

Set Combination

Symmetric Difference
 Let A and B be sets
Symmetric Difference of A and B, denoted by A 
B, is the set containing those elements is either in A
or B, but not in both
 An element x belongs to the symmetric different of
the sets A and B if and only if x belongs to A XOR B
A  B = { x | (x  A  x  B) 
¬(x  A  x  B) }

A  B = (A - B) U (B - A) A B

A  B = (A U B) - (B A) U
Ch 2.1 & 2.2 40
Set Combination

Summary
 A = {x I x  A} A
U
 AUB ={x|xAxB} A B
U

 A∩ B= {x|xAxB} A B
U
 A - B = {x | x  A  x  B} A B
U

 A  B = { x | (x  A  x  B) 
A B
¬(x  A  x  B) } U

Ch 2.1 & 2.2 41

Set Combination: Example


 Universal set is {1…6},
 A = {1, 3, 5} and B = {1, 2, 3}
 A = {2, 4, 6}
 A U B = {1, 2, 3, 5}
 A ∩ B = {1, 3}
 A – B = {5}
 B – A = {2}
 A  B = {2, 5}

Ch 2.1 & 2.2 42


Set Combination: Property

A – B = {x | x  A  x  B}
A∩B={x|xAxB}

A–B=A∩B

Ch 2.1 & 2.2 43

Set Combination: Property

|AUB|=|A|+|B|-|A∩B|

A B = A B + A B - A B

 The generalization of this result to unions of an


arbitrary number of sets is called the principle of
inclusion-exclusion

Ch 2.1 & 2.2 44


Set Combination: Property
 Principle of Inclusion-Exclusion for three sets:

|AUB UC|

= |A|+|B|+|C| = + +

-|A∩B|
-|B∩C| - - -
-|A∩C|
+
+|A∩B∩C|
Ch 2.1 & 2.2 45

Set Combination: Property


 Two sets are called disjoint if their
intersection is the empty set

A B

 Example:
 A = {1,3,5,7,9} and B = {2,4,6,8,10}
 A∩B=
 A and B are disjoint

Ch 2.1 & 2.2 46


Set Identifies
Identify Laws pTp
pFp
Recall… Domination Laws pTT
pFF
In Chapter 1
Idempotent Laws ppp
ppp
Double Negation Law ¬ (¬p)  p
Commutative Laws pqqp
pqqp
Associative Laws p  (q  r)  (p  q)  r
p  (q  r)  (p  q)  r
Distributive Laws p  (q  r)  (p  q)  (p  r)
p  (q  r)  (p  q)  (p  r)
De Morgan’s Laws ¬(p  q)  ¬p  ¬q
¬(p  q)  ¬p  ¬q
Absorption Laws p  (p  q)  p
p  (p  q)  p
Negation Laws p  ¬p  T
p  ¬p  F
Ch 2.1 & 2.2 47

Identity Laws AU=A


A∩U=A
For Domination Laws AUU=U
Set… A∩=
Idempotent Laws AUA=A
A∩A=A
Complementation Law (A)=A
Commutative Laws AUB=BUA
A∩B=B∩A
Associative Laws A U (B U C) = (A U B) U C
A ∩ (B ∩ C) = (A ∩ B) ∩ C
Distributive Laws A ∩ (B U C) = (A ∩ B) U (A ∩ C)
A U (B ∩ C) = (A U B) ∩ (A U C)
De Morgan’s Laws AUB=A∩B
A∩B =AU B
Absorption Laws A U (A ∩ B) = A
A ∩ (A U B) = A
Complement Laws AUA= U
A∩A=
Ch 2.1 & 2.2 48
Set Identifies
 How to show two sets (A and B) are identical?
 Membership Table
 Builder Notation
 Subset (i.e. A  B and B  A)

Ch 2.1 & 2.2 49

Set Identifies

Membership Table
 Prove that A ∩ B = A U B
 Using membership table

A B A∩B A∩B A B AUB


1 1 1 0 0 0 0
1 0 0 1 0 1 1
0 1 0 1 1 0 1
0 0 0 1 1 1 1

Ch 2.1 & 2.2 50


Set Identifies

Builder Notation
 Prove that A ∩ B = A U B
 Using Builder Notation and equivalence rules
A∩B
= { x | x  (A ∩ B) }
= { x | ¬((x  A)  (x  B)) }
= { x | ¬(x  A)  ¬(x  B)) }
= { x | (x  A)  (x  B) }
= { x | (x  A)  (x  B) }
=AUB

Ch 2.1 & 2.2 51

Set Identifies

Subset
 Prove that A ∩ B = A U B
 Using subset (implication & equivalence rules)

 Show A ∩ B  A U B  Show A U B  A ∩ B
A∩B AUB
Let x  (A ∩ B) Let (x  A)  (x  B)
= ¬((x  A)  (x  B)) = (x  A)  (x  B)
= ¬(x  A)  ¬(x  B)) = ¬(x  A)  ¬(x  B))
= (x  A)  (x  B) = ¬((x  A)  (x  B))
= (x  A)  (x  B) = x  (A ∩ B)
Therefore, subset of A U B Therefore, subset of A ∩ B

Ch 2.1 & 2.2 52


Generalized
Unions and Intersections
 Union of a collection of sets is the set that
contains those elements that are members of at
least one set in the collection
n
A1  A2  ...  An  i 1 Ai
 Intersection of a collection of sets is the set that
contains those elements that are members of all the
sets in the collection
n
A1  A2  ...  An  i 1 Ai

n maybe infinite
Ch 2.1 & 2.2 53

Generalized
Unions and Intersections
 Another notation Set of i, e.g. {1..n}

 iI
Ai  x | i  I ( x  Ai )

x is union of all Ai For any i, x Ai is correct


x is an element in any Ai

Set of i, e.g. {1..n}

 iI
Ai  x | i  I ( x  Ai )

x is intersection of all Ai For all i, x Ai is correct


x is an element in all Ai
Ch 2.1 & 2.2 54
Generalized
Unions and Intersections
 Example 1
 Let A = {0,2,4,6,8} , B = {0,1,2,3,4},
C = {0,3,6,9}

 What are A U B U C and A ∩ B ∩ C?


A U B U C = {0,1,2,3,4,6,8,9}

A ∩ B ∩ C = {0}

Ch 2.1 & 2.2 55

Generalized
Unions and Intersections
 Example 2
 Suppose that Ai = {1,2,3,...,i} for i =
1,2,3,…

 iI
Ai   1,2,3,..., i  1,2,3,...i
iI

 iI
Ai   1,2,3,..., i  1
iI

Ch 2.1 & 2.2 56


Computer Representation of Sets
 Many ways to represent sets in a computer
 One method is to store the elements of the set in an
unordered fashion
 E.g. in C++, we can use set to store set
 set <int> a;
 a.insert(9);
 The operations of computing the union, intersection, or
difference of two sets would be time-consuming
 Including searching a large amount of element

 A easier way is discussed

Ch 2.1 & 2.2 57

Computer Representation of Sets


 Assume the universal set U is
 Finite
 Reasonable size
 Smaller than the memory size

 Methods
 First, specify an arbitrary ordering of
the elements of U, for instance a1,
a2, . . . , an a1 a2 a3 a4
 Represent a subset A with the bit
string of length n, where the ith bit in
this string is 1010
 1 if ai belongs to A
 0 if ai does not belong to A
0111
Ch 2.1 & 2.2 58
Computer Representation of Sets
 Equal =
 Union bitwise OR
 Intersection bitwise AND
 Complement bitwise NOT

1010

0111 1000

Ch 2.1 & 2.2 59

Computer Representation of Sets


 Example
 Let U = {1,2,3,4,5,6,7,8,9,10}
A = {1, 3, 5, 7, 9}
B = {1, 2, 3, 4, 5}
 What is the bit string of
A 1010101010
B 1111100000
B 0000011111
 A ∩ B 1010100000
 A U B 1111101010

Ch 2.1 & 2.2 60


Discrete Mathematic

Chapter 2: Set Theory


2.3
Functions
Yan Huang
School of Computer Science and Engineering
South China University of Technology

Thanks to Dr Patrick Chan for providing the Slides

Agenda
 Functions
 One-to-One Functions
 Onto Functions
 Increase/Decrease Functions
 Inverse Functions
 Composition of Functions
 Graphs of Functions
 Floor and Ceiling Functions
 Factorial Functions

Chapter 2.3 Functions 2


Functions
 The concept of a function is extremely
important in mathematics and computer
science
 Sometimes, a function is called mapping or
transformation

Chapter 2.3 Functions 3

Functions
 In Mathematic…
 f(x) = x2
 g(x, y) = x + y
 In Programming…
 float square (float x) {return x^2}
 float sum (float x, float y) {return x+y}

Input Process Output

Chapter 2.3 Functions 4


Functions
 Let A and B be nonempty sets
Function f from A to B is an assignment of
exactly one element of B to each element of A
 Denoted by f (a) = b
b is the unique element of B Mickey
assigned by the function f to A
B
the element a of A Minnie C
 If f is a function from A to B, D
E
we write f : A  B Donald
F
Goofy

Chapter 2.3 Functions 5

Functions
 A function f : A  B can also be defined in terms of
a relation from A to B
 A relation from A to B is a subset of A x B
 By defining using a relation, a function from A to B
contains unique ordered pair (a, b) for every
element a  A
AxB
( ,A) ( ,A) ( ,A) ( ,A)
( ,B) ( ,B) ( ,B) ( ,B)
( ,C) ( ,C) ( ,C) ( ,C)
( ,D) ( ,D) ( ,D) ( ,D)
( ,E) ( ,E) ( ,E) ( ,E)
A B
( ,F) ( ,F) ( ,F) ( ,F)
Chapter 2.3 Functions 6
Functions
 If f is a function from A to B,
preimage image
 A is the domain of f f
 B is the codomain of f
a b=f(a)
 If f(a) = b, f
 b is the image of a
A B
domain codomain
 a is a preimage of b
 The range of f is the set of all images of elements
of A Range

 If f is a function from A to B,
we say that f maps A to B

Chapter 2.3 Functions 7

Functions Input Process Output

 A function is defined by specifying


1. Domain f
domain codomain
2. Codomain
3. Mapping of elements of the domain to
elements in the codomain
 Two functions are equal when these three
things are the same
 Therefore, either these three things is
changed, we have a new function

Chapter 2.3 Functions 8


Functions: Example 1
 What are the domain, codomain, and range of the
function that assigns grades to students?
 Let G be the function that assigns a grade to a
student, E.g. G(Mickey) = B

 Domain of G =
{Mickey, Minnie, Donald,
Goofy}
 Codomain of G =
{A, B, C, D, E, F}
 Range of G = {A, B, F}

Chapter 2.3 Functions 9

Functions: Example 2
 Let f be the function that assigns the last two bits of
a bit string of length 2 or greater to that string. What
are the domain, codomain and range?
 For example, f (11010) = 10

 Domain is the set of


all bit strings of length 2 or greater
 Codomain is set {00, 01, 10, 11}
 Range is set {00, 01, 10, 11}

Chapter 2.3 Functions 10


Function Example 3

In/Decrease Functions
 Function f whose domain and codomain are
subsets of the set of real numbers is called
 Increasing if f(x) ≤ f(y)
 Decreasing if f(x) ≥ f(y)
 Strictly Increasing if f(x) < f(y)
 Strictly Decreasing if f(x) > f(y)
whenever x < y and x and y are in the domain of f
f(x) f(x)

Strictly Increasing Increasing


x x
Chapter 2.3 Functions 11

Function Example 3

In/Decrease Functions
 A function f is
 Increasing if xy (x<y  f(x) ≤ f(y))
 Decreasing if xy (x<y  f(x) ≥ f(y))
 Strictly Increasing if xy (x<y  f(x) < f(y))
 Strictly Decreasing if xy (x<y  f(x) > f(y))
where the universe of discourse is the domain of f

Chapter 2.3 Functions 12


Function Example 4

Identity Function
 Let A be a set. The identity function on A is
the function A : A  A, where A(x) = x, for all
x  A (Note that  is the Greek letter iota)
 Assigns each element to itself
 Domain = Codomain
y
y = (x) = x

Chapter 2.3 Functions 13

Function Example 5

Floor and Ceiling Function


 Let x be a real number
 Floor Function
 Rounds x down to the closest integer less than or
equal to x
 Notation: x
 Sometimes call greatest integer function and
denoted by [x]
 Ceiling Functions
 Rounds x up to the closest integer greater than or
equal to x
 Notation: x

Chapter 2.3 Functions 14


Function Example 5

Floor and Ceiling Function


 Example
 0.5 = 0  3.1 = 3
 0.5 = 1  3.1 = 4
 -0.5 = -1  7 = 7
 -0.5 = 0  7 = 7

 Floor Function (x)


Rounds x down to the closest integer less than or equal to x
 Ceiling Functions (x)
Rounds x up to the closest integer greater than or equal to x

Chapter 2.3 Functions 15

Function Operation
 Let f1 and f2 be functions from A to R
f1 + f2 and f1 f2 are also functions from A to R
defined by
(f1 + f2)(x) = f1(x) + f2(x)
(f1 f2)(x) = f1(x) f2(x)

 Functions f1 + f2 and f1 f2 have been defined by


specifying their values at x in terms of the values of
f1 and f2 at x

Chapter 2.3 Functions 16


Function Operation: Example
 Let f1 and f2 be functions from R to R
such that f1(x) = x2 and f2(x) = x - x2
 What are the functions f1 + f2 and f1 f2 ?

(f1 + f2)(x) (f1 f2)(x)


= f1(x)+ f2(x) = f1(x) f2(x)
= x2 + (x - x2) = x2 (x - x2)
=x = x3 - x4

Chapter 2.3 Functions 17

Functions: Programming
 Domain and codomain of functions are often
specified in programming languages
 Example,
 Pascal : function floor (x: real) : integer
 Java : int floor (float x) {...}
 C++: int floor (float x) {…}
 Domain is the set of real numbers
 Codomain is the set of integers

Chapter 2.3 Functions 18


Images of Subset
 Image of S is denoted by f (S), (S is a set)
f (S) = { t | s S (t = f(s))} , or
f (S) = {f (s) | sS}
 The image of S under the function f is the subset of
B that consists of the images of the elements of S

S image of S
( f (S) )

range of f
image of A
Chapter 2.3 Functions 19

Images of Subset
 f (S) may be ambiguous:
 A set (image of S) ({ f (s) | sS })
 Function f for the set S
(input of a function is a set)
 We assume f (S) is a set in this course

Chapter 2.3 Functions 20


Functions

Images of Subset: Example


 Let
A = {a, b, c, d, e} and B = {1, 2, 3, 4} with
f(a) = 2, f(b) = 1, f(c) = 4, f(d) = 1, and f(e) = 1
 Given S = {b, c, d}, what is the image of S?

 Image is the set f(S) = {1, 4}

Chapter 2.3 Functions 21

One-to-One Functions
 Function f is said to be one-to-one, or injective,
if and only if f(a) = f(b) implies that a = b
for all a and b in the domain of f
 One-to-one functions never assign the same value
to two different domain elements

A A
B B
C C
D D
E E
F F
Not one-to-one One-to-one
Chapter 2.3 Functions
(Many-to-one) 22
One-to-One Functions
 By taking the contrapositive of the definition, a
function f is one-to-one if and only if f(a) ≠ f(b)
whenever a ≠ b
 We can express that f is one-to-one using
quantifiers as
a b (f(a) = f(b)  a = b) or
a b (a ≠ b  f(a) ≠ f(b)),
where the universe of discourse is the domain of
the function

Chapter 2.3 Functions 23

One-to-One Functions
Example 1
 Determine if the function f(x) = x + 1 from the
set of real numbers to itself is one-to-one

 Suppose f (n) = f (m)


 n+1 = m+1, therefore n = m
 f (x) is one-to-one

Chapter 2.3 Functions 24


One-to-One Functions

Example 2
 Determine whether the function f(x) = x2 from
the set of integers to the set of integers is
one-to-one

 Suppose f (n) = f (m)


 n2 = m2
 n may be equal to –m
 n2 = m2 does not imply n = m
 f (x) is not one-to-one
Chapter 2.3 Functions 25

Onto Functions
 Function f from A to B is called onto, or surjective,
if and only if for every element b  B, there is an
element a  A with f(a) = b
 A function f is onto if y x (f(x) = y), where
 Domain for x is the domain of the function
 Domain for y is the codomain of the function
A
B
C A
D B
E C
F
Not onto Onto
Chapter 2.3 Functions 26
Onto Functions

Example
 Example 1
 Is the function f(x) = x2 from the set of integers
to the set of integers onto?
 There is no integer x with x2 = -1, therefore, not
onto

 Example 2
 Is the function f(x) = x + 1 from the set of
integers to the set of integers onto?
 Yes
Chapter 2.3 Functions 27

One-to-one and Onto Functions


 The function f is a one-to-one correspondence, or
a bijection, if it is both one-to-one and onto
 Suppose that f is a function from a set A to itself
 If A is finite, then f is one-to-one if and only if it is onto
 If A is infinite, this is not necessarily the case
 We will discuss it later

1 bijection
a a a 1
b 2 1 b 2
3 b
c c 2 c 3
d 4 3 d 4
5 d
one-to-one one-to-one one-to-one
onto onto onto
Chapter 2.3 Functions 28
One-to-one and Onto Functions

Example 1
 The identity function on A is the function A :
A  A, where A(x) = x, for all x  A
 Assigns each element to itself

 One-to-one?

 Onto?
 So it is a bijection
 y
y = (x) = x

Chapter 2.3 Functions 29

One-to-one and Onto Functions


Example 2
 Either strictly increasing or strictly decreasing
function must be one-to-one
 Either increasing, but not strictly increasing,
or decreasing, but not strictly decreasing, is
not necessarily one-to-one

f(x) f(x)

Strictly Increasing Increasing


x x
Chapter 2.3 Functions 30
One-to-one and Onto Functions

Example 3

a 1 a 1
b 2 b 2
c 3 3
d 4 c 4
Not one-to-one Not a function
Not onto

Chapter 2.3 Functions 31

f
Inverse Functions
f -1
 Recall, the definition of a function domain
codomain
 Let A and B be nonempty sets
Function f from A to B is an assignment of
exactly one element of B to each element of A
 Every element (a) in domain (A) has only one image ( f (a))
 Inverse function f -1 (reverse processing)
 f -1 : B  A
 Every element (b) in domain (B) has only one image
( f -1 )
a 1 a 1 a 1 a 1
2 b b 2 b 2
b c 2 c 3
3 3 c
c 4 d 3 d 4 d 4
One-to-one Not one-to-one One-to-one Not one-to-one
Not onto Onto Onto Not onto

Chapter 2.3 Functions 32


Inverse Functions
 Let f be a one-to-one correspondence from the set
A to the set B, f(a) = b
 Inverse function of f, denoted by f -1, is the
function that assigns to an element b belonging to B
the unique element a in A,
f -1(b) = a when f(a) = b
f-1
a 1
a=f-1(b) f b=f(a) b 2
f-1 c 3
A B d 4
f
 Be sure not to confuse the function f -1 with the
function 1 / f
Chapter 2.3 Functions 33

Inverse Functions
 If a function f is not a one-to-one correspondence,
an inverse function of f cannot be defined
 f is not one-to-one a 1
Some b in the codomain is the image of b 2
c
more than one element a d 3

 f is not onto a 1
Some b in the codomain is the image of b 2
no element a 3
c 4

Chapter 2.3 Functions 34


Inverse Functions
 A function having an inverse function is called
invertible
 Therefore, a function is not invertible if it is not
a one-to-one correspondence

Chapter 2.3 Functions 35

Inverse Functions
Example 1
 Let f be the function from R to R with
f(x) = x2 . Is f invertible?

 Because f(-2) = f(2) = 4, f is not one-to-one.


 If an inverse function were defined, it would
have to assign two elements to 4
 Hence, f is not invertible

Chapter 2.3 Functions 36


Inverse Functions

Example 2
 Show that if we restrict the function f(x) = x2 to a function
from the set of all nonnegative real numbers to the set of all
nonnegative real numbers, then f is invertible

 One-to-One Function Proof


 If f(x) = f(y), then x2 = y2, so x2 - y2 = (x + y)(x - y) = 0
 This means that x + y = 0 or x - y = 0, so x = -y or x = y
 Because both x and y are nonnegative, we must have
x = y. It is one-to-one
 Onto Function Proof
 The codomain is the set of all nonnegative real numbers, so
each nonnegative real number has a square root. It is onto
 Therefore, f is invertible

Chapter 2.3 Functions 37

Composition of Function
 Let
 g be a function from the set A to the set B
 f be a function from the set B to the set C
 The composition of the functions f and g, denoted
by f ο g, is defined by
(f ο g)(a) = f( g(a) )

g f
A B C
(f ο g)

Chapter 2.3 Functions 38


Composition of Function
 To find (f ο g)(a) = f( g(a) )
 the function g is applied to a to obtain g(a)
 the function f is applied to the result g(a) to obtain
f( g(a) )
 Note that the composition f ο g cannot be defined
unless the range of g is a subset of the domain of f

(f ο g)(a)
g(a) f(g(a))
a g(a) f(g(a))
g f
A B C
(f ο g)
Chapter 2.3 Functions 39

Composition of Function
Example
 Let f and g be the functions from the set of integers
to the set of integers defined by f(x) = 2x + 3 and
g(x) = 3x + 2
 What is the composition of f and g?
What is the composition of g and f?

 Both the compositions f o g and g o f are defined


(f o g)(x) = f(g(x)) (g o f)(x) = g(f(x))
= f(3x + 2) = g(2x + 3)
= 2(3x + 2) + 3 = 3(2x + 3) + 2
= 6x + 7 = 6x + 11
Chapter 2.3 Functions 40
Composition of Function
 The commutative law does not hold for the
composition of functions
foggof
 It is associative :
f o (g o h) = (f o g) o h

Chapter 2.3 Functions 41

Composition of Function
 Let f is a one-to-one correspondence between A
and B, then
f -1 o f = A and f o f -1 = B
where A & B are identity functions on sets A & B
respectively
 Such that:
 (f-1 o f )(a) = f -1 ( f(a) ) = f-1( b ) = a
 (f o f -1)(b) = f ( f -1(b) ) = f ( a ) = b

Chapter 2.3 Functions 42


Graphs of Functions
 Let f be a function from the set A to the set B.
The graph of the function f is the set of
ordered pairs
{(a, b) | a  A and f(a) = b}

 It is often displayed pictorially to aid in


understanding the behavior of the function

Chapter 2.3 Functions 43

Graphs of Functions
Example 1
 Display the graph of the function
f(n) = 2n + 1 from the set of integers to the
set of integers
 The graph of f is the set of f (n)
ordered pairs of the form
(1, 3)
(n, f(n)) = (n, 2n + 1),
where n is an integer (0, 1)
n

Chapter 2.3 Functions 44


Graphs of Functions

Example 2
 Display the graph of the function f(x) = x2
from the set of integers to the set of integers
 The graph of f is the set
of ordered pairs of the
form (x, x2), where x is
an integer

Chapter 2.3 Functions 45

Graphs of Functions
Example 3
 Display the graph of the function f(x) = x2
from the set of real to the set of real

 The graph of f is the set


of ordered pairs of the
form (x, x2), where x is
an real (infinite Set)

Chapter 2.3 Functions 46


Graphs of Functions

Example 4
 The graph of the floor  The graph of the ceiling
function x function x

f (x) = x f (x) = x

x x

[n, n + 1) (n, n + 1]

Chapter 2.3 Functions 47


Discrete Mathematic

Chapter 5: Relation
5.1
Relations and
Their Properties
5.2
n-ary Relations and
Their Applications
5.3
Representing Relations

Yan Huang
School of Computer Science and Engineering
South China University of Technology

Thanks to Dr Patrick Chan for providing the Slides

Agenda
 What is Relation?
 Representation of Relation
 Graph
 Matrix
 Operators of Relation
 Properties of Relation

Ch 5.1, 5.2, 5.3 2


Recall, Function is…
 Let A and B be nonempty sets
Function f from A to B is an assignment of
exactly one element of B to each element of A
 By defining using a relation, a function from A to
B contains unique ordered pair (a, b) for every
element a  A
AxB Mickey
A
( ,A) ( ,A) ( ,A) ( ,A) B
( ,B) ( ,B) ( ,B) ( ,B) Minnie C
( ,C) ( ,C) ( ,C) ( ,C) D
E
( ,D) ( ,D) ( ,D) ( ,D) Donald
F
( ,E) ( ,E) ( ,E) ( ,E)
( ,F) ( ,F) ( ,F) ( ,F) Goofy
Ch 5.1, 5.2, 5.3 3

What is Relation?
Relation: x likes y

Ch 5.1, 5.2, 5.3 4


Relation
 Let A and B be sets
A binary relation from A to B is a subset of A x
B

 Recall, for example:


 A = {a1, a2} and B = {b1, b2, b3}
 A x B = { (a1, b1), (a1, b2), (a1, b3),
(a2, b2), (a1, b2), (a1, b3)}

Ch 5.1, 5.2, 5.3 5

Relation
 R is defined as
 A binary relation from A to B
 Ordered pairs, which
 First element comes from A
 Second element comes from B
 aRb: (a, b)  R
 aRb: (a, b)  R
 Moreover, when (a, b) belongs to R,
a is said to be related to b by R

Ch 5.1, 5.2, 5.3 6


Relation: Example
 S = {Peter, Paul, Mary}
 C = {C++, DisMath}

 Given
 Peter takes C++ Peter R C++ Peter R DisMath
 Paul takes DisMath Paul R C++ Paul R DisMath
 Mary takes none of them Mary R C++ Mary R DisMath

 R = {(Peter, C++), (Paul, DisMath)}


 (S x C) – R = R

Ch 5.1, 5.2, 5.3 7

Relation VS Function
 Function  Relation
from a set A to a set B from a set A to a set B
 All elements of A are  Some elements of A
assigned to B are assigned to B
 Exactly one element of  Zero, One or more
B to each element of A elements of B to an
element of A
 Function is a special case of Relation
a 1 a 1 a 1
b 2 b 2 2
c 3 c 3 b 3
d 4 d 4 c 4
Function Not a Function Not a Function
Relation Relation Relation
Ch 5.1, 5.2, 5.3 8
Relation Representation

Graph
 Relations can be represented by Directed Graph
 You will learn the directed graph in detail in
<Discrete Math Part 2>

a d
 Graph G = (V, E) consists of Edge
 a set of vertices V
Vertex
 a set of edges E,
 a connection between a pair of b c
vertices
V = { a, b, c, d}
E = { (a,b), (b,c), (b,d), (c,d) }

Ch 5.1, 5.2, 5.3 9

Relation Representation

Graph

a d a d

b c b c

 Undirected Graph  Directed Graph


 Edges are not directed (Digraph)
 E.g. (a, d) = (d, a)  Edges are directed
 E.g. (a, d) ≠ (d, a)

Ch 5.1, 5.2, 5.3 10


Relation Representation

Graph
 G to present a relation from A to B is
 vertices V  A U B a 1
b 2
 edges E  A × B c 3
d 4
 For example
 If there is an ordered pair (x, y) in R,
then there is an edge from x to y in D

x y

Ch 5.1, 5.2, 5.3 11

Relation Representation

Graph: Example
 Peter R C++, Peter R DisMath
Paul R C++, Paul R DisMath
Mary R C++, Mary R DisMath

Peter
C++
Paul
DisMath
Mary
Directed Graph

Ch 5.1, 5.2, 5.3 12


Relation Representation

Matrix
 Let R be a relation from A = {a1, a2, . . . , am} to B =
{b1, b2, . . . , bn}
 An mn connection matrix M for R is defined by
1 if (ai, bj)  R
mij = b1 b2 b3 b4
0 if (ai, bj)  R
a1 0 0 0 0
a2 1 0 0 0
a3 0 1 1 0
a4 1 0 0 0
a5 0 0 1 1

Ch 5.1, 5.2, 5.3 13

Relation Representation

Matrix: Example
 Peter R C++, Peter R DisMath
Paul R C++, Paul R DisMath
Mary R C++, Mary R DisMath

Peter
C++ 1 0
Paul 0 1
DisMath 0 0
Mary
Directed Graph Matrix

Ch 5.1, 5.2, 5.3 14


Relation on One Set
 Relation on the set A is a relation from A to A
 Special case in relations

 Example:
 A = {1, 2, 3, 4}
 R = {(1,1), (1,4), (2,1), (2,3), (2,4), (3,1), (4,1),
(4,2)}

Ch 5.1, 5.2, 5.3 15

Relation on One Set

Example 1
 Let A be the set {1, 2, 3, 4}, which ordered pairs are
in the relation R = {(a, b) | a divides b}?

 R = { (1,1), (1,2), (1,3), (1,4), (2,2), (2,4), (3,3), (4,4) }

1 2
1 1 1 1
0 1 0 1
0 0 1 0
0 0 0 1
3 4

Ch 5.1, 5.2, 5.3 16


Relation on One Set

Example 2
 How many different relations are there on a set
with n elements?

 Suppose A has n elements


 Recall, a relation on a set A is a subset of A x A
 A x A has n2 elements
 If a set has m element, its has 2m subsets
n2
 Therefore, the answer is 2

Ch 5.1, 5.2, 5.3 17

Relation on One Set

Relation Properties
 Reflexive
a ( (a, a)  R )
 Irreflexive
a ( (a  A)  ((a, a)  R) )
 Transitive
abc ( ((a,b)R(b,c)R)  ((a,c)R))

Ch 5.1, 5.2, 5.3 18


Relation on One Set

Relation Properties
 Symmetric
a b ( ((a, b)R)  ((b, a)R) )
 Asymmetric ( (a,a) cannot be an element in R )
a b ( ((a, b)R)  ((b, a)R) )
 Antisymmetric ( (a,a) may be an element in R )
a b ( ((a, b)R  (b, a)R)  (a = b) )

 Asymmetry = Antisymmetry + Irreflexivity

Ch 5.1, 5.2, 5.3 19

Relation on One Set

Relation Properties: Graph


Reflexive
a ( (a, a)  R )
Every node has a self-loop

Irreflexive
a ( (a  A)  ((a, a)  R) )
No node links to itself

Transitive
abc ( ((a,b)R(b,c)R)  ((a,c)R))
Every two adjacent forms a triangle
(Not easy to observe in Graph)
Ch 5.1, 5.2, 5.3 20
Relation on One Set

Relation Properties: Graph


Symmetric
a b ( ((a, b)R)  ((b, a)R) )
Every link is bidirectional

Asymmetric
a b ( ((a, b)R)  ((b, a)R) )
No link is bidirectional (Antisymmetric)
No node links to itself (Irreflexive)

Antisymmetric
a b ( ((a, b)R  (b, a)R)  (a = b) )
No link is bidirectional

Ch 5.1, 5.2, 5.3 21

Relation on One Set

Relation Properties: Matrix


1
1
? Reflexive
a ( (a, a)  R )
1
? 1
All 1’s on diagonal

0
0
? Irreflexive
a ( (a  A)  ((a, a)  R) )
0
? 0
All 0’s on diagonal

Transitive
abc ( ((a,b)R(b,c)R)  ((a,c)R))
Not easy to observe in Matrix

Ch 5.1, 5.2, 5.3 22


Relation on One Set

Relation Properties: Matrix


1
x Symmetric
1
a b ( ((a, b)R)  ((b, a)R) )
0
x All identical across diagonal
0

0 0 Asymmetric
0
1 0 a b ( ((a, b)R)  ((b, a)R) )
0 0 All 1’s are across from 0’s (Antisymmetric)
0 1 0 All 0’s on diagonal (Irreflexive)

0 Antisymmetric
0
1 a b ( ((a, b)R  (b, a)R)  (a = b) )
0 All 1’s are across from 0’s
0 1
Ch 5.1, 5.2, 5.3 23

Relation on One Set: Properties of Relation

Example 1
 Consider the following relations on {1, 2, 3, 4},
Which properties these relations have?
 R1 = {(1,1), (1,2), (2,1), (2,2), (3,4), (4,1), (4,4)}
Reflexive Irreflexive Transitive Symmetric Asymmetric Antisymmetric

 R2 = {(1,1), (1,2), (2,1)}


Reflexive Irreflexive Transitive Symmetric Asymmetric Antisymmetric

 R3 = {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (4,1), (4,4)}


Reflexive Irreflexive Transitive Symmetric Asymmetric Antisymmetric

 R6 = {(3,4)}
Reflexive Irreflexive Transitive Symmetric Asymmetric Antisymmetric

Ch 5.1, 5.2, 5.3 24


Relation on One Set: Properties of Relation

Example 2
Reflexive Reflexive
Irreflexive Irreflexive
Transitive Transitive
Symmetric Symmetric
Asymmetric Asymmetric
Antisymmetric Antisymmetric

Reflexive Reflexive
Irreflexive Irreflexive
Transitive Transitive
Symmetric Symmetric
Asymmetric Asymmetric
Antisymmetric Antisymmetric

Ch 5.1, 5.2, 5.3 25

Relation on One Set: Properties of Relation

Example 3
 Let A = Z+, R = { (a,b)  A  A | a divides b }
Is R symmetric, asymmetric, or antisymmetric?

 Symmetric (a b ( ((a, b)R)  ((b, a)R) ))


 If aRb, it does not follow that bRa

 Asymmetric (a b ( ((a, b)R)  ((b, a)R) )
 If a=b, then aRb and bRa
)

 Antisymmetric (a b ( ((a, b)R  (b, a)R)  (a = b) ))
 If aRb and bRa, then a=b 
Ch 5.1, 5.2, 5.3 26
Combining Relations
 As R is a subsets of A x B,
the set operations can be applied
 Complement ( )
 Union (U)
 Intersection (∩)
 Difference (-)
 Symmetric Complement (

Ch 5.1, 5.2, 5.3 27

Combining Relations

Example
 Given, A = {1,2,3}, B = {1,2,3,4}
 R1 = {(1,1),(2,2),(3,3)},
R2 = {(1,1),(1,2),(1,3),(1,4)}

 R1 U R2 = {(1,1),(1,2),(1,3),(1,4),(2,2),(3,3)}
 R1 ∩ R2 = {(1,1)}
 R1 - R2 = {(2,2),(3,3)}
 R2 - R1 = {(1,2),(1,3),(1,4)}
 R1  R2 = {(1,2),(1,3),(1,4),(2,2),(3,3)}
Ch 5.1, 5.2, 5.3 28
Combining Relations
 Let R be relation from a set A to a set B
 Inverse Relation (R-1) = {(b,a) | (a,b)  R}
 Complementary Relation (R) = {(a,b) | (a,b)  R}

 Example
 X = {a, b, c} Y={1, 2}
 R = {(a, 1), (b, 2), (c, 1)}

 R-1 = {(1, a), (2, b), (1, c)}


 E = X  Y = {(a, 1), (b, 1), (c, 1), (a, 2), (b, 2), (c, 2)}
 R = {(a, 2), (b, 1), (c, 2)} = E - R
Ch 5.1, 5.2, 5.3 29

Combining Relations

Theorems
 Let R1 and R2 be relations from A to B. Then
 (R-1)-1 = R
 (R1 U R2)-1 = R1-1 U R2-1
 (R1 ∩ R2)-1 = R1-1 ∩ R2-1
 (A  B)-1 = B  A
 -1 = 
 ( R )-1 = (R-1)
 (R1 - R2)-1 = R1-1 - R2-1
 If R1  R2 then R1-1  R2-1

Ch 5.1, 5.2, 5.3 30


Combining Relations: Theorems

Example for the Proof


 Proof (R1 U R2)-1 = R1-1 U R2-1
Recall...
 Assume  AUB={x|xAxB}
(a,b)  R1 & (a,b)  R2  R-1 = {(b,a) | (a,b)  R}
 L.H.S.
 (R1 U R2) = {(a,b) | (a,b)  R1  (a,b)  R2 }
 (R1 U R2)-1 = {(b,a) | (a,b)  R2  (a,b)  R2 }
 R.H.S.
 R1-1 = {(b,a) | (a,b)  R1}
 R2-1 = {(b,a) | (a,b)  R2}
 R1-1 U R2-1 = {(b,a) | (a,b)  R2  (a,b)  R2 }

Ch 5.1, 5.2, 5.3 31

Combining Relations

Example 1
 Given
 R1 is symmetric
 R2 is antisymmetric
 Does it R1 U R2 is transitive?

 Not transitive by giving a counterexample


 R1 = {(1,2),(2,1)} which is symmetric
 R2 = {(1,2),(1,3)} which is antisymmetric
 R1 U R2 = {(1,2),(2,1),(1,3)}, not transitive
Ch 5.1, 5.2, 5.3 32
Combining Relations

Example 2
 Given R1 and R2 are transitive on A
 Does R1 U R2 is transitive?

 Not transitive by giving a counterexample


 A = {1, 2}
 R1 = {(1,2)}, which is transitive
 R2 = {(2,1)}, which is transitive
 R1 U R2 = {(1,2), (2,1)}, not transitive

Ch 5.1, 5.2, 5.3 33

Combining Relations: Matrix


 Suppose that R1 and R2 are relations on a set
A represented by the matrices M R1 and M R2,
respectively
 Join operator (OR)
MR1 U R2 = MR1 MR2

 Meet operator (AND)


MR1 ∩ R2 = MR1 MR2

Ch 5.1, 5.2, 5.3 34


Combining Relations: Matrix
 Example

1 0 1
1 0 1
M R1  R2  M R1  M R2  1 1 1
M R1  1 0 0  
0 1 0 1 1 0

1 0 1  1 0 1
M R2  0 1 1 M R1  R2  M R1  M R2  0 0 0
 
1 0 0 0 0 0

Ch 5.1, 5.2, 5.3 35

Combining Relations

Composite
 Recall, the composition in functions…
 Let
 g be a function from the set A to the set B
 f be a function from the set B to the set C
 The composition of the functions f and g, denoted
by f ο g, is defined by (f ο g)(a) = f( g(a) )

(f ο g)(a)
g(a) f(g(a))
a g(a) f(g(a))
g f
A B C
(f ο g)
Ch 5.1, 5.2, 5.3 36
Combining Relations

Composite
 Let
 R be a relation from a set A to a set B
 S be a relation from a set B to a set C
 The composite of R and S is the relation consisting
of ordered pairs (a, c), where
 a  A, c  C, and
 There exists an element b  B, such that (a, b)  R
and (b, c)  S
 The composite of R and S is denoted by S o R

Ch 5.1, 5.2, 5.3 37

Combining Relations

Composite
 Suppose
 R be a relation from a set A to a set B
 S be a relation from a set B to a set C
 (x, y)  S o R implies z ( (x, z)R  (z, y)S)

(x, y)  S o R
(x,z)R (z,y)S
x z y
R S
A B C
(S ο R)
 Remark: May be more than one element z,
where (x, z)R and (z, y)S
Ch 5.1, 5.2, 5.3 38
Combining Relations

Composite: Example
 What is the composite of the relations R and S,
where
 R is the relation from {1,2,3} to {1,2,3,4} with
R = {(1,1),(1,4),(2,3),(3,1),(3,4)}
 S is the relation from {1,2,3,4} to {0,1,2} with
S = {(1,0),(1,2),(2,0),(3,1),(3,2),(4,1)}?

 S o R = { (1,0),(1,2),(1,1),(2,1),(2,2),(3,0),(3,2),(3,1)}
1
1 0 1 0
2
2 1 2 1
3
3 2 3 2
4
Ch 5.1, 5.2, 5.3 39

Combining Relations

Composite: Properties
 Let R1 and R2 be relations on the set A.
 Show (R1 o R2)-1 = R2-1 o R1-1
 Proof:
Let (x, y)  (R1 o R2)-1
(x, y)  S o R implies
(x, y)  (R1 o R2)-1
z ( (x, z)R  (z, y)S)
 (y, x)  R1 o R2
 z ( (y, z)R2  (z, x)R1)
 z ( (z, y)R2-1  (x, z)R1-1 )
 (x, y)  R2-1 o R1-1
Ch 5.1, 5.2, 5.3 40
Combining Relations

Composite: Properties
 Let F,G and H be relations on the set A, then
 F o (G U H) = (F o G) U (F o H)
 F o (G ∩ H)  (F o G) ∩ (F o H)
 (G U H) o F = (G o F) U (H o F)
 (G ∩ H) o F  (G o F) ∩ (H o F)

Ch 5.1, 5.2, 5.3 41

Combining Relations: Relation on One Set

Composite
 Let R be a relation on the set A. The powers
Rn , n = 1, 2, 3, ..., are defined recursively by
 R1 = R
 R2 = R o R
 R3 = R2 o R = (R o R) o R
...
 Rn+1 = Rn o R

Ch 5.1, 5.2, 5.3 42


Combining Relations: Relation on One Set

Composite: Example
R R
1 1 1
2 2 2
 Let R = {(1,1), (2,1), (3,2), (4,3)} 3 3 3
4 4 4
 Find the powers Rn, n = 2,3,4,…
R R2
1 1 1
2 2 2
3 3 3
4 4 4
 R2 = R o R = {(1,1),(2,1),(3,1),(4,2)}
R R3
 R3 = R2 o R = {(1,1),(2,1),(3,1),(4,1)} 1
2
1
2
1
2

 R4 = R3 o R = {(1,1),(2,1),(3,1),(4,1)} 3
4
3
4
3
4

 Rn = R3 for n = 5, 6, 7, ….
Ch 5.1, 5.2, 5.3 43

Combining Relations: Relation on One Set

Composite: Matrix
 Suppose
 R1 be relation from set A to set B represented by MR1
 R2 be relation from set B to set C represented by MR2
 The matrix for the composite of R1 and R2 is:
MR 2 o R1

 Size of MR1 and MR2 is |A| x |B| and |B| x |C|


 Size of MR2 o R1 is |A| x |C|

Ch 5.1, 5.2, 5.3 44


Combining Relations: Relation on One Set

Composite: Matrix
n : the number of row of R1
 Define: the number of column of R2

MR 2 o R1 = MR 2
MR 1
n
where ( M R2 M R1 )ij   [( M R1 )ik  ( M R2 ) kj ]
k 1
An element in the matrix

 Such that x11 x12 … …


( M R2 M R1 )ij = 1 x21 ...
...
… xij …
if and only if
(M R1)ik = (M R2)kj = 1 for some k
... ...
Ch 5.1, 5.2, 5.3 45

Combining Relations: Relation on One Set

Composite: Matrix: Example


4 n : the number of column of R1
the number of row of R2
0 1 0 0 
5
M R  0 1 0 0 3
3 x 4 0 0 0 1 0 0 0 0 1 
5 M SoR   0 0 0 0 1  3
0 0 1 0 0  0 1 0 0 0 
0 0 0 0 1
MS   4 i = 1, j = 5
1
2
3
4
1 0 0 0 0
4 x 5 0  k= 1 4
3
2
 1 0 0 0 n=4
n
( M R2 M R1 ) ij   [( M R1 ) ik  ( M R2 ) kj ]
k 1
Ch 5.1, 5.2, 5.3 46
Combining Relations: Relation on One Set

Composite: Matrix
 The powers Rn can defined using matrix as:
M R n  M R 
n

 Example 0 1 0 
 Find the matrix representing M R  0 1 1
 
the relation R2 1 0 0

0 1 0  0 1 0  0 1 1
M R 2  M R 2   0 1 1   1 1 1
 0 1 1     
1 0 0 1 0 0 0 1 0
Ch 5.1, 5.2, 5.3 47

Combining Relations: Relation on One Set

Composite: Property 1
 Theorem
If R  S, then S o R  S o S

 Assume (x,y)  SoR, there exists a element z,


which (x,z)R and (z,y)S
 As R  S and (x,z)R, (x,z)S
 Therefore, as (x,z)S and (z,y)S, (x,y)SoS
 SoRSoS

 It implies:
If R  S and T  U, then R o T  S o U

Ch 5.1, 5.2, 5.3 48


Combining Relations: Relation on One Set

Composite: Property 2
 An ordered pairs (x, y) is in Rn iff there is a path of
length n from x to y in R
1 2 1 2 1 2 1 2

3 4 3 4 3 4 3 4

R R2 The picture can't be displayed.


R3 The picture can't be displayed.
R4
0 1 0 0 0 0 0 1
0 0 0 1 0 0 1 0
 
0 1 0 0 0 0 0 1
   
0 0 1 0 0 1 0 0

Ch 5.1, 5.2, 5.3 49

Combining Relations: Relation on One Set

Composite: Property 2
 An ordered pairs (x, y) is in Rn iff there is a path of
length n from x to y in R
1 2 1 2 1 2 1 2

3 4 3 4 3 4 3 4

R R2 R3 R4
 Example
 In R, 1 > 2 > 4, length = 2  (1,4)  R2
 In R, 3 > 2 > 4 > 3, length = 3  (3,3)  R3
 (1,2)  R4  In R, 1 > 2 > 4 > 3 > 2, length = 4
Ch 5.1, 5.2, 5.3 50
Combining Relations: Relation on One Set

Composite: Property 2
 Theorem
Let R be a relation on A. There is a path of
length n from a to b in R iff (a, b)  Rn

Ch 5.1, 5.2, 5.3 51

Combining Relations: Relation on One Set

Composite: Property 2
 Proof by Induction a path of length n from a to
b iff (a, b)  Rn
 Show n=1 is true
 An arc from a to b is a path of length 1, which is in R1 = R
 Hence the assertion is true for n = 1
 Assume it is true for k. Show it is true for k+1
 As it is true for n = 1,
suppose (a, x) is a path of length 1, then (a, x) R
 As it is true for n = k,
suppose (x, b) is a path of length k, then (x, b) Rk
 Considering, (a, x)  R and (x, b)  Rk,
(a, b)  Rk+1 = Rk o R as there exists an element x, such
that (a, x)  R and (x, b)  Rk
 The length of (a,b) is k+1

Ch 5.1, 5.2, 5.3 52


Combining Relations: Relation on One Set

Composite: Property 3
 R is transitive iff Rn  R for n > 0.

 Proof
1. (Rn  R)  R is transitive
 Suppose (a,b)  R and (b,c)  R
 (a,c) is an element of R2 as R2 = R o R
 As R2  R , (a,c)  R
 Hence R is transitive

Ch 5.1, 5.2, 5.3 53

Combining Relations: Relation on One Set

Composite: Property 3
2. R is transitive  (Rn  R)
 Use a proof by induction:
 Basis: Obviously true for n = 1.
 Induction: Assume true for n, show it is
true for n + 1
 For any (x, y) is in Rn+1, there is a z such that
(x, z)  R and (z, y)  Rn
 But since Rn  R, (z, y)  R
 As R is transitive, (x, z) and (z, y) are in R,
so (x, y) is in R
 Therefore, Rn+1  R

Ch 5.1, 5.2, 5.3 54


Combining Relations: Relation on One Set

Composite: Property 4
 Proof: If R is transitive, Rn is also transitive

 When n = 1, R is transitive
 Assume Rk is transitive
 Show Rk+1 is transitive
Given (a,b)  Rk+1 and (b,c)  Rk+1, show (a,c) 
Rk+1
 Rk+1 = Rk o R
 As (a,b)  Rk+1, (d,b)  Rk and (a,d)  R
 As (b,c)  Rk+1, (f,c)  Rk and (b,f)  R
 As (a,c)  Rk+1, (?,c)  Rk and (a,?)  R

Ch 5.1, 5.2, 5.3 55

Combining Relations: Relation on One Set

Composite: Property 4
 Given (a,b)  Rk+1 and (b,c)  Rk+1, show (a,c)  Rk+1
 Rk+1 = Rk o R
 As (a,b)  Rk+1, (d,b)  Rk and (a,d)  R
 As (b,c)  Rk+1, (f,c)  Rk and (b,f)  R
 As (a,c)  Rk+1, (?,c)  Rk and (a,?)  R
 As “R is transitive iff Rn  R for n > 0”
 (d,b)  Rk  R
 As R is transitive, (d,b)  R and (b,f)  R imply (d,f)  R
 As R is transitive, (d,f)  R and (a,d)  R imply (a,f)  R
 Therefore, by considering, (f,c)  Rk and (a,f)  R, (a,c) 
Rk+1

Ch 5.1, 5.2, 5.3 56


Combining Relations: Relation on One Set

Composite: Property 4
 Proof: If R is transitive, Rn is also transitive

 When n = 1, R is transitive
 Assume Rk is transitive
 Show Rk+1 is transitive
Given (a,b)  Rk+1 and (b,c)  Rk+1, show (a,c) 
Rk+1
 Rk+1 = Rk o R
 As (a,b)  Rk+1, (a,d)  Rk and (d,b)  R
 As (b,c)  Rk+1, (b,f)  Rk and (f,c)  R
 As (a,c)  Rk+1, (a,?)  Rk and (?,c)  R

Ch 5.1, 5.2, 5.3 57

Combining Relations: Relation on One Set

Composite: Property 4
 Given (a,b)  Rk+1 and (b,c)  Rk+1, show (a,c)  Rk+1
 Rk+1 = Rk o R
 As (a,b)  Rk+1, (a,d)  Rk and (d,b)  R
 As (b,c)  Rk+1, (b,f)  Rk and (f,c)  R
 As (a,c)  Rk+1, (a,?)  Rk and (?,c)  R
 As “R is transitive iff Rn  R for n > 0”
 (b,f)  Rk  R
 As R is transitive, (d,b)  R and (b,f)  R imply (d,f)  R
 As R is transitive, (d,f)  R and (f,c)  R imply (d,c)  R
 Therefore, by considering, (a,d)  Rk and (d,c)  R, (a,c)
 Rk+1

Ch 5.1, 5.2, 5.3 58


n-ary Relation
 Let A1, A2, ..., An be sets
An n-ary relation on these sets is a subset
of A1 x A2 x … x An
 Domains of the relation:
the sets A1, A2, ..., An
 Degree of the relation: n

Ch 5.1, 5.2, 5.3 59

n-ary Relation: Example


 Let R be the relation on Z x Z x Z+ consisting of
triples
(a, b, m), where a, b, and m are integers with
m ≥ 1 and a = b (mod m), (i.e. m divides a-b)

 Degree of the relation? 3


 First domain is: the set of all integers
 Second domain is: the set of all integers
 Third domain: the set of positive integers
 Do they belong to R?
 (8,2,3) Y  (7,2,3) N
 (-1,9,5) Y  (-2,-8,5) N
Ch 5.1, 5.2, 5.3 60
Relational Database VS n-ary Relation
 A database consists of records made up of fields
 Each record is a n-tuple (n fields)
 For example:
ID num Name Major GPA
 888323 Adams Data Structure 85
 231455 Peter C++ 61
 Domain: ID num, Name, Major, GPA
 Relation: (888323, Adams, Data Structure, 85),
(231455, Sam, C++, 61)
 Relations are displayed as tables
ID_number Student_name Major Grade
888323 Adams Data Structure 85
231455 Peter C++ 61
678543 Sam Data Structure 98
Ch 5.1, 5.2, 5.3 61

Relational Database VS n-ary Relation

 n-ary relation can be:


 Determining all n-tuples satisfy certain
conditions
 Joining the records in different tables
ID_number Major Grade ID_number Student_name
888323 Data Structure 85 231455 Adams
231455 C++ 61 888323 Peter
678543 Data Structure 98 102147 Sam
453876 Discrete Math 83 453876 Goodfriend
678543 Rao
786576 Stevens

Ch 5.1, 5.2, 5.3 62


Discrete Mathematic

Chapter 5: Relation
5.4
Closures of Relations
5.5
Equivalence Relations
Yan Huang
School of Computer Science and Engineering
South China University of Technology

Thanks to Dr Patrick Chan for providing the Slides

Agenda
 5.4 Closures of Relations
 Reflexive Closure
 Symmetric Closure
 Transitive Closure
 5.5 Equivalence Relations
 Equivalence Relations
 Equivalence Class
 Partition

Ch 5.4 & 5.5 2


Introduction: Closures
 Is it symmetric?
 How can we produce a symmetric relation
containing R that is as small as possible?
1 2

3 4

Ch 5.4 & 5.5 3

Closure
 Let R be a relation on a set A
 S is called the closure of R with respect to
property P if
 S with property P
 S is a subset of every relation with property P
containing R
 Minimum terms are added to R to fulfill the
requirements of property P

Ch 5.4 & 5.5 4


Closure
 Reflexive Closure
 a ( (a, a)  R )

 Symmetric Closure
 a b ( ((a, b)R)  ((b, a)R) )

 Transitive Closure
 abc ( ((a,b)R(b,c)R)  ((a,c)R))

Ch 5.4 & 5.5 5

Closure
Reflexive Closure: Example
 R = {(1,1), (1,2), (2,1), (3,2)}
on the set A = {1, 2, 3}
 R is not reflexive
1 2  How can we produce a
reflexive relation containing
R that is as small as possible?
 Add (2,2) and (3,3)
 R’ = {(1,1), (1,2), (2,1), (3,2),
3 (2,2), (3,3)}
 R’ is reflexive closure of R
 Any reflexive relation that
contains R must contain R’

Ch 5.4 & 5.5 6


Closure
Reflexive Closure
 r(R) denotes the reflexive closure of R
 How to create a reflexive closure for R?
 Graphical view
 Add loop for each element
 Mathematical View
 Let D (or I) be the diagonal relation (equality
relation) on R, where D = {(x, x) | x  R}
 The reflexive closure of R is R D

Ch 5.4 & 5.5 7

Closure
Symmetric Closure: Example
 R = {(1,2), (1,2), (2,2), (2,3),
(3,1), (3,2)} on {1, 2, 3}
 R is not symmetric
1 2  How can we produce a
symmetric relation containing R
that is as small as possible?
 Add (2,1) and (1,3)
 R’ = {(1,2), (1,2), (2,2), (2,3),
(3,1), (3,2), (2,1), (1,3)}
3
 R’ is symmetric closure of R
 Any symmetric relation that
contains R must contain R’

Ch 5.4 & 5.5 8


Closure
Symmetric Closure
 s(R) denotes the symmetric closure of R
 How to create a symmetric closure for R?
 Graphical view
 Add edges in the opposite direction
 Mathematical View
 Let R-1 be the inverse of R,
where R-1 = {(y,x) | (x,y)  R}
 The symmetric closure of R is R R-1

 Theorem: R is symmetric iff R = R-1


Ch 5.4 & 5.5 9

Closure
Transitive Closure: Example
 R = {(1,2), (1,3), (2,3), (3,4)}
on {1,2,3,4}
 R is not transitive
1 2
 How can we produce a
transitive relation containing
R that is as small as possible?
 Add (1,4), (2,4)
3
 R’ = {(1,2), (1,3), (2,3), (3,4),
(1,4), (2,4)}
 R’ is transitive closure of R
4  Any transitive relation that
contains R must also contain R’

Ch 5.4 & 5.5 10


Closure
Transitive Closure
 t(R) denotes the transitive closure of R
 How to create a transitive closure for R?
 Graphical view
 If there is a path from a to b and b to c,
add an edge from a to c b
 However, it is not easy a e
 Example:
c

 Mathematical View d f
 Transitive Closure of R is R*
Ch 5.4 & 5.5 11

Closure
Transitive Closure
 The connectivity relation of the relation R,
denoted R*, is the union of Ri, where i =
1,2,3,…

R*   R n
n 1

 Transitive Closure of R is R*

Ch 5.4 & 5.5 12


Closure
Transitive Closure
 Theorem
If R  S, then R o S  S o S
 Theorem
If R is transitive then so is Rn
 Theorem
R is transitive iff Rn  R for n > 0

Ch 5.4 & 5.5 13

Closure
Transitive Closure
 Proof: Transitive Closure of R is R*
1. R* is a transitive relation
2. R* contains R
3. R* is the smallest transitive relation which
contains R

Ch 5.4 & 5.5 14



Closure
R   Rn
*

Transitive Closure n 1

Proof: 1. R* is a transitive relation


 Suppose (x, y) and (y, z) are in R*
Show (x, z) is in R*
 By definition of R*, (x, y) is in Rm for some m
and (y, z) is in Rn for some n.
 Then (x, z) is in Rn o Rm = Rm+n which is
contained in R*
 Hence, R* must be transitive
Proof: 2. R* contains R
 The proof is obvious by the definition of R*
Ch 5.4 & 5.5 15


Closure
R   Rn
*

Transitive Closure n 1

Proof: 3. R* is the smallest transitive relation


which contains R
 Now suppose S is any transitive relation that contains
R Theorem:
 Show S contains R* R is transitive iff Rn  R for n > 0
 Since S is transitive, Sn S
 For the power is 2, Theorem:
R2 = R o R  S o R  S o S If R  S, then R o S  S o S

 It is true for n, Rn  Sn
 Therefore Rn  Sn  S for all n
 Hence S must contain R* since it must also contain the
union of all the powers of R

Ch 5.4 & 5.5 16


Closure
Transitive Closure
 How can we calculate the infinite union?
Infinity

R  R * n

n 1

 If it is necessary to calculate all Ri?

Ch 5.4 & 5.5 17

Closure
Transitive Closure
 A path of length n in a digraph G is a
sequence of edges (x0, x1),(x1, x2),...,(xn-1, xn)

 A cycle is a path with


starting point (x0) = end point (xn)
b
a e
a>e>f>d Path
Length = 3
c a>e>b>c Not a path
c>f>d>c Cycle
d f Length = 3

Ch 5.4 & 5.5 18


Closure
Transitive Closure
 Let A be a set with n elements, and let R be a
relation on A
 If there is a path from a to b, then the length
of this path will not exceed n
b
a e

d f

Ch 5.4 & 5.5 19

 Proof
 Suppose there is a path from a to b in R
 Let m be the length of the shortest path, which is x0, x1,
x2, ..., xm-1, xm, where x0 = a and xm = b
 Assume m > n
 Because n vertices in A and there are m vertices in the
path, at least two vertices in the path are equal
 Suppose that xi = xj with 0 ≤ i < j ≤ m
 There is a path contained a cycle from xi to itself (xj)
 This cycle can be removed to shorten the path
 Hence, the shortest length must be less than or equal
to n
xi+2 …x j-2

xi+1 xj-1
… …
x0 x1 x2 xi-1 xi xi+1
j+1 xm
a xj b
Ch 5.4 & 5.5 20
Closure
Transitive Closure
 From the Theorem, we know that Rk for k > n
does not contain any edge that does not
already appear in the first n powers of R

 Assume R is the relation on set A


 | A|
R   R   Rk
* k

k 1 k 1

Ch 5.4 & 5.5 21

Closure
Transitive Closure
 Theorem
Let MR be the zero-one matrix of the relation
R on a set with n elements. Then the zero-
one matrix of the transitive closure R* is

M R*  M R[1]  M R[ 2 ]  M R[ 3]    M R[ n ]

Remark : M R k  M R[ k ]
M R  M R[1]
Ch 5.4 & 5.5 22
Closure
Transitive Closure: Example
 Find the zero-one matrix of the transitive
closure of the relation R where
1 0 1 1 1 1  1 1 1 
M R  0 1 0 M R[ 2]  0 1 0 M R[3]  0 1 0
1 1 0 1 1 1 1 1 1

M R*  M R  M R[ 2 ]  M R[3]

1 0 1 1 1 1 1 1 1 1 1 1
M R*  0 1 0  0 1 0  0 1 0  0 1 0
1 1 0 1 1 1 1 1 1 1 1 1
Ch 5.4 & 5.5 23

Closure: Transitive Closure


Warshall’s Algorithm
 Warshall’s Algorithm can reduce the complexity of
R* calculation
 For the path
a, x1, x2, …, xm-1, b,
the interior vertices are x1, x2, …, xm-1
 All the vertices of the path except the first and last
vertices
v1 v2
v 1 > v 2 > v4 > v 3
v 2 > v 3 > v1
interior vertices
v4 > v 3
v3 v4
Ch 5.4 & 5.5 24
Closure: Transitive Closure
Warshall’s Algorithm
 Warshall’s algorithm is based on the construction
of a sequence of zero-one matrices, W0, W1, …, Wn,
where W0=MR
w11k  w12k    v1 v2
w14(0) = 0
w k    
Wk   21 w14(1) = 0
  wij k  
  w14(2) = 1
     v3 v4

 wij(k) = 1 if there is a path from vi to vj such that all


the interior vertices of this path are in the set {v1,
v2, …, vk}; otherwise is 0

Ch 5.4 & 5.5 25

Closure: Transitive Closure


Warshall’s Algorithm
 The (i,j)th entry of MR* is 1 iff there is a path
from vi to vj with all the interior vertices in the
set {v1, v2, …, vn}, therefore, Wn = MR*

 Algorithm
 W0 = MR
 For k = 1 … n
 Update each element in Wk by using:

wij[ k ]  wij[ k 1]  ( wik[ k 1]  wkj[ k 1] )

Ch 5.4 & 5.5 26


Closure: Transitive Closure
Warshall’s Algorithm: Example
 Find the matrices W0, W1, W2, W3 and W4 for
the R shown in the directed graph

a b
 Let v1=a, v2=b, v3=c, v4=d.
W0 is the matrix of the relation.
Hence,
0 0 0 1
1 0 1 0
d c W0  
1 0 0 1
 
0 0 1 0

Ch 5.4 & 5.5 27

Closure: Transitive Closure


Warshall’s Algorithm: Example
0 0 0 1 [ k 1] [ k 1] [ k 1]
1 
0 1 0 w [k ]
ij  wij  ( w ik  wkj )
W0  
1 0 0 1
   0 0 0 1  i=1 0 0 0 1 
0 0 1 0
k =1   k =2 
1 0 1 1

1 0 1 1 i=2

W1    W2  
1 0 0 1  i=3 1 0 0 1 
W4 is the matrix of   0 0 1 0 
 0 0 1 0  i=4  
the transitive closure
a b 0 0 0 1 1 0 1 1
k =3   k =4  
1 0 1 1 1 0 1 1
W3   W4   
1 0 0 1 1 0 1 1
   
d c 1 0 1 1 1 0 1 1
Ch 5.4 & 5.5 28
Closure: Theorem
 Let R be binary relation on a nonempty set A
 r(R) = R U D
where D is diagonal relation = {(x, x) | x  R}
 s(R) = R U R-1
where R-1 is inverse = {(y,x) | (x,y)  R}
 t(R) = R* | A|
k
where R* is connectivity relation = R
k 1

Ch 5.4 & 5.5 29

Closure: Theorem
 Let R be binary relation on a nonempty set A
 If R is reflexive, r(R) = R
 If R is symmetric, s(R) = R
 If R is transitive, t(R) = R

Ch 5.4 & 5.5 30


Closure: Theorem
 Let R be binary relation on a nonempty set A
 If R is reflexive, s(R) and t(R) are reflexive
 If R is symmetric, t(R) and r(R) are symmetric
 If R is transitive, r(R) is transitive

Ch 5.4 & 5.5 31

Closure: Theorem
 Suppose R is transitive, is s(R) transitive?

 Let R= {(1,2),(3,2)}
 R is transitive
 s(R) = {(1,2), (2,1), (3,2), (2,3)}
 s(R) is not transitive

Ch 5.4 & 5.5 32


Closure: Theorem
 Let R be binary relation on a nonempty set A
 If R is reflexive, s(R) and t(R) are reflexive
 If R is symmetric, t(R) and r(R) are symmetric
 If R is transitive, r(R) is transitive


 r( s(R) ) = s( r(R) )?


 r( t(R) ) = t( r(R) )?


 s( t(R) ) = t( s(R) )?

Ch 5.4 & 5.5 33

Closure: Theorem
 Proof r( s(R) ) = s( r(R) )

 s( r(R) ) = s(R U D) where D = {(x, x) | x  R}

= (R U D) U (R U D)-1
= (R U D) U (R-1 U D-1)
= (R U R-1) U (D U D-1)
= s(R) U D
= r( s(R) )

Ch 5.4 & 5.5 34


Closure: Theorem
 Do the closure operations distribute
 over the set operations?
 over inverse?
 over complement?
 over set inclusion?

 Example:
 t(R1 - R2) = t(R1) - t(R2) ?
 r(R-1) = ( r(R) )-1 ?

Ch 5.4 & 5.5 35

Equivalence
 What is Equivalence?

 What properties the equivalence should have?

Reflexive Irreflexive Transitive


Symmetric Asymmetric Antisymmetric
Ch 5.4 & 5.5 36
Equivalence
 How to represent “2”
in clock system?
 How to represent “14”
in clock system?
 Clock System is
Arithmetic modulo 12
 “2”, “14”, “26”, “38”… are
equivalence in clock system

Ch 5.4 & 5.5 37

Equivalence Relations
 Definition
A relation R on a set A is an equivalence
relation iff R is reflexive, symmetric and
transitive
a b a b

d c d c
Equivalence Relation
Ch 5.4 & 5.5 38
Equivalence Relations
Example 1
 Suppose that R is the relation on the set of strings of English
letters such that aRb iff g(a)=g(b), where g(x) is the length
of the string x.
Is R an equivalence relation?

 Reflexive
 Since g(a)=g(a), it follows that aRa whenever a is a string
 Symmetric
 Let aRb, so g(a)=g(b), bRa. Therefore, g(b)=g(a)
 Transitive
 Let aRb and bRc, then g(a)=g(b) and g(b)=g(c), so aRc
 Consequently, R is an equivalent relation

Ch 5.4 & 5.5 39

Equivalence Relations
b=x·m+a
Example 2 where x is an integer
x = (b-a) / m
 Definition of Congruence
a ≡ b (mod m)
a is congruent to b modulo m if m divides a-b

 Let m be a positive integer greater than 1.


Show that the relation R = { (a,b) | a ≡ b (mod
m) } is an equivalence relation on the set of
integers

Ch 5.4 & 5.5 40


Equivalence Relations R = { (a,b) | a ≡ b (mod m) }
Example 2 b=x·m+a
where x is an integer
 Reflexive x = (b-a) / m
 a – a = 0 is divisible by m, hence, a ≡ a (mod m)
 Symmetric
 Suppose that (a, b)R, so x = (b-a)/m, where x is an integer
 (-x) = (a-b) / m, -x is also an integer, (b, a)R
 Transitive
 Suppose that (a,b)  R and (b,c)  R
 xm = (b-a) and ym = (c-b), x and y are integers
 a-c = xm+ym = (x+y)m, x+y is also an integer
 Thus, (a, c)R

Ch 5.4 & 5.5 41

Equivalence Relations
Example 3
 Show that the "divides" relation on the set of
positive integers is not an equivalence relation.

 “Divide” relation is not symmetric


 E.g., 2 divide 4 but 4 does not divide 2
 It is not an equivalence relation

Ch 5.4 & 5.5 42


Equivalence
 Two elements a and b that are related by an
equivalence relation are called equivalent
 Notation: a ~ b
a~a c~a
a b a~b c~b
a~c c~c
b~a d~d
b~b
d c b~c

Ch 5.4 & 5.5 43

Equivalence: Examples
 R is the relation on the set of strings of
English letters, where aRb iff g(a)=g(b) and
g(x) is the length of the string x
 “Peter” ~ “Susan”
 “Ann” ~ “May”

 R = { (a,b) | a ≡ b (mod m) } on the set of


integers
 For m = 7, 5 ~ 12
 For m = 12, 14 ~ 2
Ch 5.4 & 5.5 44
Equivalence Classes
 Definition
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
 Example (clock system)
 “2”, “14”, “26”, “38”…
are equivalence
 Therefore, they form
an equivalence class
Ch 5.4 & 5.5 45

Equivalence Classes
 The equivalence class of a with respect to R
is denoted by [a]R
[a]R = {s | (a,s)  R}
 If b  [a]R, b is called a representative of this
equivalence class

Ch 5.4 & 5.5 46


Equivalence Classes
[a]R = {s | (a,s)  R}
Example 1
 Equivalence class of
 [a] = {a, b, c} a b
 [b] = {a, b, c}
 [c] = {a, b, c}
 [a] = [b] = [c]
d c

 [d] = {d}

Ch 5.4 & 5.5 47

Equivalence Classes
[a]R = {s | (a,s)  R}
Example 2
 R = { (a,b) | a ≡ b (mod m) } is an equivalence
relation on the set of integers, where m be a
positive integer greater than 1
 Let m = 5
 R = { (a,b) | a ≡ b (mod 5) }
 [0] = { …, -10, -5, 0, 5, 10, … }
 [1] = { …, -9, -4, 1, 6, 11, … }
 [a] = { …, a-10, a-5, a, a+5, a+10, … }
 General Case, for any m,
 [a] = { …, a-2m, a-m, a, a+m, a+2m, … }

Ch 5.4 & 5.5 48


Equivalence Classes
[a]R = {s | (a,s)  R}
Example 3
 R is the relation on the set of strings of
English letters, where aRb iff g(a)=g(b) and
g(x) is the length of the string x
 [e] = { a, b, c, …, z }
 [Susan] = { happy, email, … }

 For any a,
[a] = the set of all strings of the same length as
a

Ch 5.4 & 5.5 49

Equivalence Classes
Theorem
 Let R be an equivalence relation on a
nonempty set A. The following statements
are equivalent:
1. aRb
2. [a] = [b]
3. [a] ∩ [b]  

Ch 5.4 & 5.5 50


Equivalence Classes
1. aRb
Theorem: Proof 2. [a] = [b]
 Show (1) implies (2) 3. [a] ∩ [b]  
 Assume that aRb
 Suppose c  [a]. Then aRc
 As aRb and R is symmetric, we have bRa
 Furthermore, since R is transitive and bRa and aRc, it
follows that bRc
 Hence, c  [b]
 This shows that [a]  [b]
 The proof that [b]  [a] is similar.
 Show (2) implies (3)
 Assume that [a] = [b]
 It follows that [a] ∩ [b]   since [a] is nonempty
Ch 5.4 & 5.5 51

Equivalence Classes
1. aRb
Theorem: Proof 2. [a] = [b]
 Show that (3) implies (1) 3. [a] ∩ [b]  
 Suppose that [a] ∩ [b] 
 Then there is an element c[a] and c[b]
 In other words, aRc and bRc
 By the symmetric property, cRb
 Then by transitive, since aRc and cRb, we have aRb.

 Since (1) implies (2),(2) implies (3), and (3) implies


(1), the three statements are equivalent.

Ch 5.4 & 5.5 52


Equivalence Classes & Partitions
 Definition
Let S1, S2, ..., Sn be a collection of subsets of
A. The collection forms a partition of A if the
subsets are
1. Nonempty
Si  S1
S2
2. Disjoint S4 S3
Si ∩ Sj =  if i  j
S5
3. Exhaust
n
A
US =A
i=1 i
A

Ch 5.4 & 5.5 53

Equivalence Classes & Partitions


Theorem 1
 Let R be an equivalent relation on a set A.
Then the equivalence classes of R form a
partition of A
 Conversely, given a partition {Si | i  C} of the
set A, there is an equivalence relation R that
has the sets Si, where i  C, as its
equivalence classes

Ch 5.4 & 5.5 54


Equivalence Classes & Partitions
Theorem 2
 Equivalence classes of an equivalence
relation R partition the set A into disjoint
nonempty subsets whose union is entire set
 This partition is denoted A/R and called
 Quotient set, or
 Partition of A induced by R, or
 A modulo R
 The partition is a set of equivalence classes
whose union is the entire set
Ch 5.4 & 5.5 55

Equivalence Classes & Partitions


Example 1
 What are the sets in the partition of the
integers arising from congruence modulo 4?

 There are four congruence classes,


corresponding to [0]4, [1]4, [2]4 and [3]4.
 [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,…}
 The quotient set: Z/R = { [0]4, [1]4, [2]4, [3]4 }
Ch 5.4 & 5.5 56
Equivalence Classes & Partitions
Example 2
 Let A={1, 2, 3}, give all the possible partitions
on A.

1 1 1 1 1
2 3 2 3 2 3 2 3 2 3

{{1,2,3}} {{1}, {2,3}} {{2}, {1,3}} {{3}, {1,2}} {{1}, {2}, {3}}

Ch 5.4 & 5.5 57

Equivalence Classes & Partitions


Example 3
 List the ordered pairs in the equivalence relation R
produced by the partition A1 = {1, 2, 3}, A2 = {4, 5},
and A3 = {6} of S = {1, 2, 3, 4, 5, 6}

 For A1: (1,2), (1,3), (2,3), (2,1), (3,1) , (3,2),


(1,1), (2,2), (3,3)

 For A2: (4,5), (5,4), (4,4), (5,5)

 For A3: (6,6)

Ch 5.4 & 5.5 58


Equivalence Classes & Partitions
Theorem 3
 Let R be a relation on A.
Reflexive, Symmetric, Transitive closure of R,
tsr(R) = t(s(r(R))), is an equivalence relation
on A, called the equivalence relation
induced by R

Ch 5.4 & 5.5 59

Equivalence Classes & Partitions a b


Theorem 3
r(R)
 t(s(r(R)))
1. r(R) d c
2. s(r(R)) a b
3. t(s(r(R)))
s(r(R))
a b d c
a b

t(s(r(R)))
d c
d c
Ch 5.4 & 5.5 60
Equivalence Classes & Partitions
Theorem 3: Proof
 Proof: tsr(R) is an equivalence relation
 Reflexive
 When constructing r(R), a loop is added at every
element in A, therefore, tsr(R) must be reflexive
 Symmetric
 If there is an edge (x, y) then the symmetric
closure of r(R) ensures there is an edge (y, x)

Ch 5.4 & 5.5 61

Equivalence Classes & Partitions


Theorem 3
 Transitive
 When we construct the transitive closure of sr(R),
an edge (a, c) is added if (a, b) and (b, c)
 tsr(R) must be transitive

 As sr(R) is symmetric, if (a, b) and (b, c) in sr(R),


(b, a) and (c, b) are also in sr(R). Therefore,
another edge (c, a) is also added
 It guarantees that tsr(R) is symmetric

Ch 5.4 & 5.5 62


Discrete Mathematic

Chapter 5: Relation

5.6
Partial Orderings
Yan Huang
School of Computer Science and Engineering
South China University of Technology

Thanks to Dr Patrick Chan for providing the Slides

Agenda
 Partial Order
 Total Order
 Lexicographic Order
 Hasse Diagrams
 Minimal/Maximal Element
 Least/Greatest Element
 Lower/Upper Bound
 Greatest Lower/Least Upper Bound

Chapter 5.6 Partial Orderings 2


What is Order?

Chapter 5.6 Partial Orderings 3

What is Order?
 Equivalence (=) concept is discussed
 The abstraction of the following relations
will be discussed in this chapter
 Bigger or Equal / Smaller or Equal (≤, ≥)
 Bigger / Smaller (<, >)

Chapter 5.6 Partial Orderings 4


What is Order?
 What properties “≤” or “≥” should have?
Reflexive Irreflexive Transitive
Symmetric Asymmetric Antisymmetric

 What properties “<” or “>” should have?


Reflexive Irreflexive Transitive
Symmetric Asymmetric Antisymmetric

Chapter 5.6 Partial Orderings 5

Partially Ordered Set


 Definition
Let R be a relation on A.
Then R is a partial order iff R is
 Reflexive
a ( (a, a)  R )
 Antisymmetric
a b ( ((a, b)R  (b, a)R)  (a = b) )
 Transitive
abc ( ((a,b)R(b,c)R)  ((a,c)R))
a b a b a b

c d c d c d
Not Partial Order Partial Order Partial Order
Chapter 5.6 Partial Orderings 6
Partially Ordered Set
 When R is a partial order in A, (A, R) is called
a partially ordered set or a poset
 Recall, aRb denotes that (a,b) R
 If R is a partial ordering relation
a b denotes that (a,b)
 (A, ) is a poset

Chapter 5.6 Partial Orderings 7

Partially Ordered Set


Example 1
 Show that the “greater than or equal” relation (≥) is
a partial ordering on the set of integers

 Reflexive
a ≥ a for every integer a
 Antisymmetric
If a ≥ b and b ≥ a, then a = b
 Transitive
a ≥ b and b ≥ c imply that a ≥ c
 (Z, ≥) is a poset

Chapter 5.6 Partial Orderings 8


Partially Ordered Set
Example 2
 (Z+, | ) is a poset
The divisibility relation | is a partial ordering
on the set of positive integers.
 i.e. a divides b
 (P(S),  ) is a poset
The inclusion relation  is a partial ordering
on the power set of a set S
 i.e. a is the subset of b

Chapter 5.6 Partial Orderings 9

Comparability
 The elements a and b of a poset(S, ) are
called comparable if either a b or b a
 Otherwise, a and b are imcomparable

 Example
 In the poset (Z+, | ),
 Are 3 and 9 comparable? Yes, since 3 | 9
 Are 5 and 7 comparable? No

Chapter 5.6 Partial Orderings 10


Non-Strict & Strict Partial Order
 Non-strict (or reflexive) Partial Order
 Property: Reflexive, Antisymmetric, Transitive

 Strict (or irreflexive) Partial Order


 i.e. a b denotes that a b, but ab
 Property: Irreflexive, Antisymmetric, Transitive
asymmetric

 Generally, partial order refers to

Chapter 5.6 Partial Orderings 11

Total Ordered
 If (S, R) is a poset and every two elements
are comparable, S is called a total ordered or
linear ordered or simple ordered set
 In this case (S, R) is called a chain
a b a b a b

c d c d c d
Not Partial Order Partial Order Partial Order
Not Total Order Not Total Order Total Order

Chapter 5.6 Partial Orderings 12


Total Ordered
Example
 Poset (Z,  )? Totally Ordered
 Since a  b or b  a whenever a and b are
integers

 Poset (Z+, | )? Not totally Ordered


 It contains elements that are incomparable,
such as 5 and 7
Not totally Ordered
 Poset (P(S),  ), where S is a set
 It may not be the case that A  B or B  A

Chapter 5.6 Partial Orderings 13

Lexicographic Order
 What is the order of a letter? A C?
 Alphabetical order C A?

 What is the order of a word?


 Lexicographic Order
 Generalization of Alphabetical order
discrete discreet ?
discreetness discreet ?

Chapter 5.6 Partial Orderings 14


Lexicographic Order
Special Case
 Lexicographic Order is a generalization of the way
the alphabetical order of words is based on the
alphabetical order of letters
 Also known as lexical order, dictionary order,
alphabetical order or lexicographic(al) product
 Given two posets (A1, 1) and (A2, 2) we construct
an Lexicographic Order on A1  A2:
<x1,y1> <x2,y2> iff x1 1 x2 or (x1 = x2 and y1 2 y2 )

Chapter 5.6 Partial Orderings 15

Lexicographic Order For (A1, 1)


and (A2, 2)
<x1,y1> R <x2,y2> iff x1 1 2 or (x1 = x2 and y1
x y2)
Special Case: Example 2

 Let A1 = A2 = Z+ and R1 = R2 = 'divides'.


 If the following relation is
Lexicographic Order R?
 (2, 4) R (2, 8)? Condition 1
 2=2
Condition 2
 4 divides 8
 (2, 4) R (2, 6)? Condition 1  2=2
Condition 2  4 does not divide 6
 (2, 4) R (4, 5)? Condition 1
 2 divides 4
Condition 2
 4 does not divide 5

1. (x1 ≠ x2) and x1 divides x2?


2. (x1 = x2) and (y1 ≠ y2) and (y1 divides y2)?
Chapter 5.6 Partial Orderings 16
Lexicographic Order
General Case
 Given (A1, 1), (A2, 2), …, (An, n)

 The Lexicographic Order  on multiple


Cartesian products: A1  A2  A3  . . .  An
(a1, a2, …, an)  (b1, b2, …, bn)
 iff
 If a1 1 b1, or
 if there is an integer i>0 such that
a1 = b1, …, ai = bi and ai+1 i+1 bi+1
For two posets, (A1, 1) and (A2, 2)
<x1,y1>  <x2,y2> iff x1 1 x2 or (x1 = x2 and y1 2 y2)
Chapter 5.6 Partial Orderings 17

Lexicographic Order
General Case: Example
 Let A1 = A2 = … = An = Z+ and R1 = R2 = … = Ri = 'divides'
 If the following relation is Lexicographic Order R?

 (2, 3, 4, 5) R (2, 3, 8, 2)?  (2, 3, 4, 5) R (3, 6, 8, 10)?

i=1 2 1 2? 2 1 3?

i=2 2 = 2 and 3 2 3? 2 = 3 and 3 2 6?




i=3 2 = 2 and 3 = 3 and 4 3 8? Do not need to check the rest
as a1 ≠ b1

 If a1 1 b1, or
 if there is an integer i>0 such that
a1 = b1, …, ai = bi and ai+1 i+1 bi+1
Chapter 5.6 Partial Orderings 18
Lexicographic Order
String
 Lexicographic order is applied to strings of symbols
where there is an underlying 'alphabetical' order

 Consider the different strings a1a2…am and b1b2…bn


on a partial ordered set S
 Let t = min(m, n), the definition of lexicographic
ordering for string is that the string a1a2…am is less
than b1b2…bn if and only if
 ( (a1,a2,…,at) = (b1,b2,…,bt) and m < n ) or
(a1,a2,…,at) ≤ (b1,b2,…,bt)
lexicographic ordering
using 'alphabetical' order

Chapter 5.6 Partial Orderings 19

If a1 1 b1, or
 Lexicographic Order  (a1,a2,…,at) = (b1,b2,…,bt) and m<n
 if there is an integer i>0 such that or
String: Example 1
a1 = b1, …, ai = bi and ai+1 i+1 bi+1  (a1,a2,…,at) ≤ (b1,b2,…,bt)

 Consider the set of strings of lowercase


English letters. t = min(m, n)
 “discrete” length = 8 t = 8 alphabetical order: e < t
“discreet” length = 8 discreet  discrete

 “discreet” length = 8 t=8


“discreetness” length = 12 discreet  discreetness

t=8
 “fiscrete” length = 8
alphabetical order: d < f
“discreteen” length = 12
discreteen  fiscrete

Chapter 5.6 Partial Orderings 20


Lexicographic Order  (a1,a2,…,at) = (b1,b2,…,bt) and m<n
or
String: Example 2  (a1,a2,…,at) ≤ (b1,b2,…,bt)

 Find the lexicographic ordering of the bit strings


0,01,11,001,010,011,0001 and 0101 based on the
ordering 0  1
0 0
01 0 0≤01
11 0≤01 0≤01≤11
001 0≤01≤11 0≤001≤01≤11
010 0≤001≤01≤11 0≤001≤01≤010≤11
0001 0≤001≤01≤010≤11 0≤0001≤001≤01≤010≤11

Chapter 5.6 Partial Orderings 21

It is Hasse Diagram
Hasse Diagrams
 Show the partial ordering using a graph
 For example {(a, b) I a ≤ b} on the set {1, 2, 3, 4}
4 4 4 4
3 3 3 3
2 2 2 2
1 1 1 1
 The graph is too complicated and try to simplify it:
 A partial ordering must be reflexive:
the loops are not necessary
 A partial ordering must be transitive:
some edges can be removed
 By assuming all edges are pointed upward,
the direction of edges is not necessary
Chapter 5.6 Partial Orderings 22
Hasse Diagrams
 To construct a Hasse diagram:
1. Construct a digraph representation of the
poset (A, R) so that all arcs point up (except
the loops).
2. Eliminate all loops
3. Eliminate all redundant arcs
 Start to eliminate from the top
4. Eliminate the direction of the edge

Chapter 5.6 Partial Orderings 23

Hasse Diagrams
8 12
Example 1
4 6
 Draw the Hasse diagram
representing the partial 2 3
ordering { (a,b) | a divides b }
on A={1,2,3,4,6,8,12} 1

8 12 8 12 8 12

4 6 4 6 4 6

2 3 2 3 2 3

1 1 1
Chapter 5.6 Partial Orderings 24
 Small Exercise  {a,b,c}

 Construct the Hasse diagram of {a,b} {a,c} {b,c}


(P({a, b, c}),  )
 The elements of P({a, b, c}) are {a} {b} {c}
, {a}, {b}, {c}, {a, b}, {a, c}, {b, c},
{a, b, c}
{}
 The digraph is
{a,b,c} {a,b,c}

{a,b} {a,c} {b,c} {a,b} {a,c} {b,c}

{a} {b} {c} {a} {b} {c}

{} {}
Chapter 5.6 Partial Orderings 25

Covering Relation
 Let (S, ) be a poset. (x,y) such that y cover
x is called the covering relation of (S, )
if x  y and there is no element z  S such
that x  z  y {a,b,c}

 Example {a,b} {a,c} {b,c}

 For (P({a, b, c}),  ), {a} {b} {c}


if it is a covering relation?
 ({a,b}, {a})?  {a}  {a,b} {}

 ({a}, {a,b})? 
 ({ }, {a,b})?  { }  {a}  {a,b} or { }  {b}  {a,b}
 ({a}, {a})?  {a} = {a}
Chapter 5.6 Partial Orderings 26
Minimal & Maximal Elements
 Let (A, R) be a poset and S  A.
s (b) in S is a minimal element (maximal
element) of S iff there does not exist an
element x in S such that xRs ( bRx )
b
maximal minimal
element element
{ a, b, c} b c a d
{ a, b, c, d, e} b c, e
c e

Chapter 5.6 Partial Orderings 27

Least & Greatest Elements


 Let (A, R) be a poset and S  A.
s (b) in S is a least element (greatest element)
of S iff sRx (xRb) for every x in S
 It is unique if it exits
b
Greatest Least
element element
{ a, b, c} b c a d
{ a, b, c, d, e} b /
c e
Chapter 5.6 Partial Orderings 28
 Small Exercise 
 Minimal element: a b f h
 Not exist an element x in S such that xRs
 Maximal element: e i g h
 Not exist an element x in S such that bRx
 Least element: /
 sRx for every x in S
 Greatest element: / e
 xRb for every x in S
i c d g
S = {a,b,c,d,e,f,g,h,i}
a b f h

Chapter 5.6 Partial Orderings 29

Well Ordered
 A chain (A, R) is well-ordered iff every
nonempty subset of A has a least element
 Examples:
 (Z, ) is a chain but not well-ordered
 Z does not have least element
 (N, ) is well-ordered
 (N, ) is not well-ordered

Chapter 5.6 Partial Orderings 30


Upper & Lower Bound
 Let (A, R) be a poset and S  A.
s (b) in A is an lower bound ( upper bound ) of
S iff sRx (xRb) for every x in S

b
Upper Lower
Bound Bound
{a, d} b c a d
{c, d, e} d, b /
c e

Chapter 5.6 Partial Orderings 31

Greatest Lower & Least Upper Bounds


 Let (A, R) be a poset and S  A.
s (b) is the least upper bound (greatest lower
bound), denoted lub(S) ( glb(S) ), iff s (b) is an
upper bound (lower bound) for S and sRx (yRb) for
all other upper bounds x (lower bounds y) of S
 It is unique if it exits
Upper Lower b
Bound Bound Least Greatest
{a, d} b c Upper Lower
{c, d, e} d, b / Bound Bound
a d
{a, d} b c
{c, d, e} d /
c e
Chapter 5.6 Partial Orderings 32
 Small Exercise  h i
{a,b,c} {b,c,d,g} {h,i} f g
Minimal Element a b,c h,i d e
Maximal Element b,c g h,i b c
Least Element a / /
a
Greatest Element / g /
Lower Bound a a a,b,c,e,d,g
Upper Bound e,g,i,h g,i,h /
Greatest Lower Bound a a g
Least Upper Bound e g /

Chapter 5.6 Partial Orderings 33

Lattice
 A partially ordered set in which every pair of
elements has both a least upper bound and a
greatest lower bound is called a lattice
Lower Bound: b, c, a
No Greatest Lower Bound
f f No Upper h
Bound
e d e
d e e f g
c
b
d

b

c
b
 c b c d
a a a
a

 Upper Bound: d, e, f
No Least Upper Bound

Chapter 5.6 Partial Orderings


 34
Lattice: Example
 Poset ({1,2,3,4,5}, | ) ? Not Lattice
 2,3 have no upper bounds in {1,2,3,4,5}
 Poset ({1,2,4,8,16}, | ) ? Lattice

4 5 4 3 5 4 3 5

2 3 2 2

1 1 1

Chapter 5.6 Partial Orderings 35

Lattice: Theorem
 Theorem: If L is a lattice, least upper bound and
greatest lower bound of a and b can be defined as
a  b and a  b, respectively.  and  satisfy the
following properties for a,b,c  L.
1. Commutative laws
a  b = b  a and a  b = b  a
2. Associative laws
a  (b  c) = (a  b)  c and a  (b  c) = (a  b)  c
3. Idempotent laws
a  a = a and a  a = a
4. Absorption laws
a  (a  b) = a and a  (a  b) = a
• Be noted that  and  does not necessary to be OR and AND.
They can be any binary operation which fulfill the following properties
Chapter 5.6 Partial Orderings 36
Lattice: Theorem: Example 1
 Given (P({x,y}), )
 Is it a partial order? 1. Reflexive 
2. Antisymmetric 
3. Transitive  lub: a  b
 Is it a lattice?  glb: a  b
 What should be  or  ? 1. Commutative laws
a  b = b  a and a  b = b  a
        2. Associative laws
    a  (b  c) = (a  b)  c and
a  (b  c) = (a  b)  c
  3. Idempotent laws
{a,b} {} a  a = a and a  a = a
4. Absorption laws
a  (a  b) = a and a  (a  b) = a
{a} {b} {a} {b}

{} {a,b}
Chapter 5.6 Partial Orderings 37

Lattice: Theorem: Example 2


 Given L as (P({a, b, c}),  ), {a,b,c}

   and    {a,b} {a,c} {b,c}

 Recall, lub: a  b {a} {b} {c}

glb: a  b {}

 lub of {a} & {a,b}? {a}  {a,b} = {a,b}


 lub of {a} & {b,c}? {a}  {b,c} = {a,b,c}
 glb of {a,b} & {b,c}? {a,b}  {b,c} = {b}

Chapter 5.6 Partial Orderings 38


Distributive Lattice
 A lattice (L,, ) is distributive if the
following identity holds for all a, b, c  L:
 a  (b  c) = (a  b)  (a  c)
 a  (b  c) = (a  b)  (a  c)

 Example, b  (e  c) = b  d = b
(b  e)  (b  c) = a  a = a
 Are they distributive?
d
d

b e c


b c

a
 a

Chapter 5.6 Partial Orderings 39

Bounded Lattice
 A lattice (L, ) is called bounded lattice if
there exist elements α, β L such that for
each x L, x α and β x.
 α is the largest element of L (denoted by 1)
 β is the smallest element of L (denoted by 0)
 If a lattice is bounded, then
 1 is the lub of the lattice
 0 is the glb of the lattice

Chapter 5.6 Partial Orderings 40


lub: a  b
Complemented Lattice glb: a  b

 A bounded lattice (L, ) is complemented


lattice if for each x L, there exists y L
such that x y = 1 and x y = 0
 y is a complement of x (denoted by ¬x)

 In general an element may have more than


one complement

Chapter 5.6 Partial Orderings 41

Complemented Lattice
lub: a  b
Example glb: a  b
y is ¬x if x  y = 1 and x  y = 0

x ¬x x ¬x
1
1
0 1 0 1
c d
c
d
e
1 0 1 0
a b a b
a b,e a b,d
0
0 b a,c b a,c
c b Complemented c b,d
Not
Lattice
Complemented d NO d a,c
Lattice
e a
Chapter 5.6 Partial Orderings 42
Lattice: Principle of Duality
 Any statement that is true for lattice remains
true when is replaced by and  and 
are interchanged throughout the statement.
 Example of dual
  
 
 
{a,b} {}

{a} {b} {a} {b}

{} {a,b}

Chapter 5.6 Partial Orderings 43


Discrete Mathematic

Chapter 3: Counting
3.1
The Basics of Counting
3.2
The Pigeonhole Principle
3.3
Permutations & Combinations
3.5
Generalized Permutations &
Combinations
3.6
Generating Permutations &
Combinations
Yan Huang
School of Computer Science and Engineering
South China University of Technology

Thanks to Dr Patrick Chan for providing the Slides

Agenda
 Basic Counting Principles
 Multiplication / Addition Principle
 Inclusion-Exclusion Principle
 Permutation / Combination
 Distributing Objects into Boxes
 Generating Permutations & Combinations

Chapter 3.1, 3.3, 3.5, 3.6 2


Why Counting?
 The brute force attack is the most common way
(time consumed but effective) in hacking
 How security of your password?
 5 digits at most
 Each digit either
0-9, a-z or A-Z
 How many times a hacker
need to try in the worst
situation?

Chapter 3.1, 3.3, 3.5, 3.6 3

Why Counting?
 Counting problems arise throughout
mathematics and computer science
 For example
 the number of experiment outcomes
 the number of operations in an algorithm
(time complexity)

Chapter 3.1, 3.3, 3.5, 3.6 4


Basic Counting Principle
 Multiplication / Addition Principle
 Inclusion-Exclusion Principle
 Permutation / Combination

Chapter 3.1, 3.3, 3.5, 3.6 5

Basic Counting Principles

Multiplication (Product) Rule


 If a task can be constructed in t successive
steps and step i can be done in ni ways,
where i = 1…t, then the number of different
possible ways is n1 x n2 x ...x nm

Chapter 3.1, 3.3, 3.5, 3.6 6


Basic Counting Principles

Addition (Sum) Rule


 If a task can be done in one of n1 ways, in
one of n2 ways, ... , or in one of nm ways,
where all sets of nj ways are disjoint, then the
number of ways is n1 + n2 + … + nm

Chapter 3.1, 3.3, 3.5, 3.6 7

Basic Counting Principles: Multiplication/Addition Principle

Example 1
 In 1999, a virus named “Melissa” is
created by David L. Smith based on a
Microsoft Word macro
 Melissa sends an email "Here is that
document you asked for, don't show it
to anybody else." to the top 50 people
in the address book

 How many emails are sent after 4 iterations?


 1st iteration: 1
 2nd iteration: 1 x 50 = 50
 3rd iteration: 50 x 50 = 2,500 6,377,551
(By Addition Rule)
 4th iteration: 2500 x 50 = 6,250,000
(By Multiplication Rule)
Chapter 3.1, 3.3, 3.5, 3.6 8
Basic Counting Principles: Multiplication/Addition Principle

Example 2
 A programming language Beginner's All-
purpose Symbolic Instruction Code (BASIC)
 GW-BASIC (1986) in MS-DOS

Chapter 3.1, 3.3, 3.5, 3.6 9

Basic Counting Principles: Multiplication/Addition Principle

Example 2
 In BASIC, the requirements of a variable name
 A string of 1 or 2 alphanumeric characters
(a-z or 0-9)
 Begin with a letter
 Uppercase and lowercase letters are not
distinguished
 Different from the 5 strings of two characters that
are reserved
 How many different variable names are there
in this version of BASIC?
Chapter 3.1, 3.3, 3.5, 3.6 10
Basic Counting
 A string of 1 or 2Principles: Multiplication/Addition
alphanumeric Principle
characters (number & letter)
Example 2
 Begin with a letter
 Uppercase and lowercase letters are not distinguished
 Different from the 5 strings of two characters that are reserved

 Number of variables names containing 1 character (V1)


 V1 = 26, because a one-character variable name must be a
letter

 Number of variables names containing 2 characters (V2)


 For V2, by the product rule there are 26 x 36 strings of length
two that begin with a letter and end with an alphanumeric
character
 However, five of these are excluded, V2 = 26 x 36 – 5 = 931

 Total number is V1 + V2 = 26 + 931 = 957

Chapter 3.1, 3.3, 3.5, 3.6 11

Basic Counting Principles:

Inclusion-Exclusion Principle
 Suppose that a task can be done in A or in B ways
 But some of the set of A ways to do the task are the
same as some of the B ways to do the task
overcount

A B A B

 Avoid the overcount

|AUB|=|A|+|B|-|A∩B|
Chapter 3.1, 3.3, 3.5, 3.6 12
Basic Counting Principles: Inclusion-Exclusion Principle
Example 1
 How many bit strings of length 8, either start with a 1 bit
or end with the two bits 00?

 Start with 1: 27 = 128 ways


 End with 00: 26 = 64 ways
 Some of these strings are the same
 The bit strings of length eight start with
a 1 bit and end with the two bits 00
 25 = 32
 128 + 64 - 32 = 160

Chapter 3.1, 3.3, 3.5, 3.6 13

Basic Counting Principles: Inclusion-Exclusion Principle


Example 2
 A computer company receives 350 applications
 Suppose that
 220 majored in computer science
 147 majored in business
 51 majored both in computer science and in business
 How many of these applicants majored neither in
computer science nor in business?
 Let A1 : the set of students majored in computer science
A2 : the set of students majored in business
 | A1 U A2 | = | A1 | + | A2 | - | A1 ∩ A2 | = 220 + 147 - 51 = 316

 350 - 316 = 34 of the applicants majored neither in computer


science nor in business

Chapter 3.1, 3.3, 3.5, 3.6 14


Basic Counting Principles

Permutation
 A permutation of a set of distinct
n objects is an ordered arrangement
of these objects

1st 2nd
… rth
… nth

n ∙ (n-1) ∙…∙ (n-r+1) ∙…∙ 1 = n!

 General Case
The ordering of r elements selected from n distinct
elements is called r-permutation
n!
n Pr  P ( n, r )  n( n  1)( n  2)...( n  r  1)

( n  r )!
Chapter 3.1, 3.3, 3.5, 3.6 15

Basic Counting Principles

Combination
 The unordered selection of r elements from n
distinct elements is called r-combination
 It is a subset of the set with r elements
n n!
C
n r  C ( n, r )    
 r  r!(n  r )!
 

Chapter 3.1, 3.3, 3.5, 3.6 16


Basic Counting Principles

Combination
 C(n, r) = C(n, n - r)
 Algebraic Proof
n!
C (n, r ) 
r!(n  r )!
n!

(n  (n  r ))!( n  r )!

 C ( n, n  r )

Chapter 3.1, 3.3, 3.5, 3.6 17

Basic Counting Principles

Combination
 Combinatorial proof
 Using counting arguments to prove that both sides of
the identity count the same objects but in different ways
 Using combinatorial proof for nC r = nCn-r
C(n, r) = C(n, n - r)
Suppose that S is a set with n
elements.
Every subset A of S with r
elements corresponds to a
subset of S with n - r elements,
namely A Consequently,
C(n, r) = C(n, n - r)

Chapter 3.1, 3.3, 3.5, 3.6


A A 18
Basic Counting Principles

Permutation / Combination
 Proof
6 3 2
3P2 = 3C2 x 2P2
Number of r-permutations Number of r-permutations
of n elements of r elements

P ( n, r )  C ( n , r )  P ( r , r )
Number of r-combinations
of n elements

P (n, r ) n! /( n  r )! n!
 
P(r , r ) r! /( r  r )! r!(n  r )!
Chapter 3.1, 3.3, 3.5, 3.6 19

Basic Counting Principles: Permutation / Combination

Example
 Your class has 10 students. How many
different ways the committee can be set up:
1. A committee of four 10C4
2. A committee of four and 10C4 · 4C1
one person is to serve as chairperson
3. A committee of four and
10C4 · 4C2
two co-chairpersons
4. Two committees:
10C4 · 4C2 · 10C3· 3C1
 One with four members with two co-chairs
 One with three members and a single chair
Chapter 3.1, 3.3, 3.5, 3.6 20
Counting Problems
 How to apply what you have learn to solve
the counting problems?
 Multiplication / Addition Principle
 Inclusion-Exclusion Principle
 Permutation / Combination
 List all the possibilities

Chapter 3.1, 3.3, 3.5, 3.6 21

Counting Problems
 Many counting problems can be treated as
the ways objects can be placed into boxes

Distinguishable Indinguishable
(labeled) (unlabeled)
Objects
Distinguishable
(labeled)

Boxes
Indinguishable
(unlabeled)
Chapter 3.1, 3.3, 3.5, 3.6 22
Distinguishable Objects 1
Distinguishable Boxes 1
 How many ways are there to arrange the 5
students into a classroom with 5 seats?

 Permutation
 5!

1st 2nd 3rd 4th 5th

Chapter 3.1, 3.3, 3.5, 3.6 23

Distinguishable Objects 1
Distinguishable Boxes 1
 How many ways are there to arrange the 5 students
(A, B, C, D & E) into a classroom with 5 seats and
the student A and B sit next to each other?

 Two situations:

4!
“AB” C D E
2 x 4!
4!
“BA” C D E
Chapter 3.1, 3.3, 3.5, 3.6 24
Distinguishable Objects 1
Distinguishable Boxes 1
 How many ways are there to arrange the 5 students
into the round table with 5 seats?

 A round table means


each pattern counts
5 times
 5! / 5

Chapter 3.1, 3.3, 3.5, 3.6 25

Distinguishable Objects ∞
Distinguishable Boxes 1
 How many strings of length r can be formed
from the English alphabet?

 26 letters
 Each letter can be used repeatedly
 26r strings of length r

Chapter 3.1, 3.3, 3.5, 3.6 26


Distinguishable Objects n
Distinguishable Boxes 1
 How many different strings can be made by
reordering the letters of the word SUCCESS

 SUCCESS contains 3 Ss, 2 Cs, 1 U, and 1 E


 Considering:
 3 Ss: 7 positions are available, 7C3, 4 positions leave
 2 Cs: 4 positions are available, 4C2, 2 positions leave
 1 U: 2 positions are available, 2C1, 1 position leave
 1 E: 1 positions is available, 1C1
 Consequently, the number of different strings
7C3 x 4C2 x 2C1 x 1C1 = 420

Chapter 3.1, 3.3, 3.5, 3.6 27

Distinguishable Objects 1
Distinguishable Boxes m
 How many ways to give an apple, an orange, a
banana and a strawberry to Mickey Mouse and
Donald Duck?
4C0+4C1+4C2+4C3+4C4
 Mickey 0 = 4C0
 Mickey 1 = 4C1
 Mickey 2 = 4C2
 Mickey 3 = 4C3
 Mickey 4 = 4C4
(Donald Duck takes the rest)
Chapter 3.1, 3.3, 3.5, 3.6 28
Distinguishable Objects n
Distinguishable Boxes m
 How many ways to assign 3 apples and 2 oranges
to Mickey Mouse and Donald Duck?

6 x 2 = 12
Chapter 3.1, 3.3, 3.5, 3.6 29

Distinguishable Objects 1
Indistinguishable Boxes 1
 How many poker hands of 5 cards can be
dealt from a standard deck of 52 cards?
A 2 3 4 5

 Combination
=

2 A 4 5 3
 n = 52, r = 5
=

 52C5 3 4 A 5 2
=…

Chapter 3.1, 3.3, 3.5, 3.6 30


Distinguishable Objects 1
Indistinguishable Boxes 1
 How many flushes are possible in standard deck of
52 cards?
 Flush: A set of five cards of the same suit

 Four suits 4 6 2 9 4 5

 Hearts, Spades, Clubs & Flush


Diamonds
2 A 4 7 3
 For each suit 13C5 = 1287
 5 cards are selected Not Flush
from 13 cards
 Answer: 4 x 1287 = 5148
Chapter 3.1, 3.3, 3.5, 3.6 31

Distinguishable Objects n
Indistinguishable Boxes 1
 How many ways to put 3 apples, 2 oranges
and 1 banana to 3 indistinguishable boxes
and each box contains 1 item?

Chapter 3.1, 3.3, 3.5, 3.6 32


Distinguishable Objects 1
Indistinguishable Boxes m
 How many ways are there to put four different
employees into three indistinguishable offices,
when each office can contain any number of
employees?

 Let A, B, C and D be the four employees


 One Office
 4 employees
 Two Offices
 1 employee, 3 employees
 2 employees, 2 employees
 Three Offices
 2 employees, 1 employee, 1 employee

Chapter 3.1, 3.3, 3.5, 3.6 33

Distinguishable Objects 1
Indistinguishable Boxes m
 One Office { {A,B,C,D} , {} , {} }
 4 employees { { A,B,C} , {D} , {} }
 Two Offices { { A,B,D} , {C} , {} }
 1 employee, { { A,C,D} , {B} , {} }
3 employees { { A,C,D} , {A} , {} }
 2 employees, { {A, B} , {C, D} , {} }
2 employees { {A, C} , {B, D} , {} }
{ {A, D} , {B, C} , {} }
 Three Offices
 2 employees, { {A, B} , {C} , {D} }
1 employee, { {A, C} , {B} , {D} }
1 employee { {A, D} , {B} , {C} }
{ {C, D} , {A} , {B} }
{ {B, D} , {A} , {C} }
 14 different ways
{ {B, C} , {A} , {D} }
Chapter 3.1, 3.3, 3.5, 3.6 34
Distinguishable Objects n
Indistinguishable Boxes m
 How many ways to put 3 apples, 2 oranges
and 1 banana to 3 indistinguishable boxes
and each box contains 2 items?

Chapter 3.1, 3.3, 3.5, 3.6 35

Indistinguishable Objects n
Distinguishable Boxes 1
 How many routes are there from the lower-left
corner of 4 x 3 grid to the upper-right corner if the
turtle is restricted to traveling only to right or upward?

URUURRU
 Each any route contains 7 moves:
 4 Upward (U)
 3 Right (R)
 Answer: 7C4

Chapter 3.1, 3.3, 3.5, 3.6 36


Indistinguishable Objects n
Distinguishable Boxes ∞
 How many ways are there to select 5 bills from a
cash box containing $1 bills, $2 bills, $5 bills, $10
bills, $20 bills, $50 bills, and $100 bills?
Assume:
 Order of selecting does not matter
 Bills of each denomination are indistinguishable
 At least five bills of each type

Chapter 3.1, 3.3, 3.5, 3.6 37

Indistinguishable Objects n
Distinguishable Boxes ∞
Select five bills from $1, $2, $5, $10, $20, $50 and $100

Bill
100 50 20 10 5 2 1
Bill Bill Bill Bill

100 50 20 10 5 2 1 ***||*||||*

*||**|*||*| 100 50 20 10 5 2 1

7 - 1 = 6 bars (lines between 7 boxes) ||*|*|*|*|*


5 stars (5 bills)
Total, 11 characters
11C5 = 11! / (5!6!) = 462

Chapter 3.1, 3.3, 3.5, 3.6 38


Indistinguishable Objects n
Distinguishable Boxes ∞
 How many solutions does the equation
x1 + x2 + x3 = 11 have, where x1, x2, and x3
are nonnegative integers?

 Selection of 11 items with x1 items of type 1,


x2 items of type 2, and x3 items of type 3
 n = 3, r = 11
 11+3-1C11
= (13x12) / (1x2) = 78
x1 x2 x3
Chapter 3.1, 3.3, 3.5, 3.6 39

Indistinguishable Objects n
Distinguishable Boxes ∞
 How many solutions does the equation
x1 + x2 + x3 = 11 have, where x1, x2, and x3 integers
and x1 ≥ 1, x2 ≥ 2, and x3 ≥ 3?

 Selection of 11 items with x1 items of type 1, x2


items of type 2, and x3 items of type 3
 At least 1 item of type 1, 2 items of type 2, and 3
items of type 3
 1 + 2 + 3 = 6 have already be located
 Therefore n =3, r = 11 – 6 = 5
 5+3-1C5
= (7x6) / (1x2) = 21
x1 x2 x3
Chapter 3.1, 3.3, 3.5, 3.6 40
Indistinguishable Objects n
Indistinguishable Boxes 1
 How many ways are there to pack 3 copies of
the same book into 5 identical boxes and a
box can contain 1 book?

Answer is 1

Chapter 3.1, 3.3, 3.5, 3.6 41

Indistinguishable Objects n
Indistinguishable Boxes m
 How many ways are there to pack 6 copies of
the same book into 4 identical boxes, where a
box can contain as many as six books?

 By listing all the possibilities


6, 0, 0 ,0 3, 3, 0, 0
5, 1, 0, 0 3, 2, 1, 0
4, 2, 0, 0 3, 1, 1, 1
4, 1, 1, 0 2, 2, 2, 0
2, 2, 1, 1
 There are 9 ways
Chapter 3.1, 3.3, 3.5, 3.6 42
Generating
Permutations & Combinations
 Sometimes permutations or combinations
need to be generated but not just counted
 How can we systemically generate all the
combinations of the elements of a finite set?

Chapter 3.1, 3.3, 3.5, 3.6 43

Generating Combinations
 Recall that the bit string corresponding to a
subset
 For kth position:
 1 : ak is in the subset
 0 : ak is not in the subset
 If all the bit strings of length n can be listed,
then by the correspondence between subsets
and bit strings, a list of all the subsets is
obtained

Chapter 3.1, 3.3, 3.5, 3.6 44


Generating Combinations

Next Larger Bit String


 Algorithm: Generating the next bit string
(bn-1, bn-2, ..., b1, b0), where the current bit
string is not equal to 11...11)
1. i = 0
2. while bi = 1
2.1 bi = 0
2.2 i = i + 1
3. bi = 1
 Treat it as a binary number and add “1” each
time
Chapter 3.1, 3.3, 3.5, 3.6 45

Generating Combinations: Next Larger Bit String

Example
 Find out the next combination using next
larger bit string algorithm for 1. i = 0
2. while bi = 1
1011 2.1 bi = 0
2.2 i = i + 1
3. bi = 1
 Next:
1100

b3 b2 b1 b0

Chapter 3.1, 3.3, 3.5, 3.6 46


Generating Combinations

Next Larger r-combinations


 Algorithm: Generating the next larger r-
combinations after {a1, a2, …, ar} by given a
set {1, 2, 3, . . . , n} {a1, a2}
1. i = r {1, 2, 3, 4}
2. while ai = n - r + i locate the last ai {1, 2}
2.1 i = i - 1 ie ai ≠ n - r + 1
{1, 3}
3. ai = ai + 1 add 1 to ai {1, 4}
4. for j = i + 1 to r From ai+1 to ar {2, 3}
4.1 aj = ai + j - i Assign new values {2, 4}
{3, 4}
Chapter 3.1, 3.3, 3.5, 3.6 47

Generating Combinations
1. i = r
Example 1 2. while ai = n - r + i
2.1 i = i - 1
 Find the next larger 4-combination 3. ai = ai + 1
of the set {1, 2, 3, 4, 5, 6} 4. for j = i + 1 to r
after {1, 2, 5, 6} 4.1 aj = ai + j - i

 a1 = 1, a2 = 2, a3 = 5, and a4 = 6
 The last ai such that ai ≠ n - r + 1 is a2 (i = 2)

 Next larger 4-combination


a4 = 6 = 6 - 4 + 4
 a2 = a 2 + 1 = 2 + 1 =3
 a3 = a 2 + j – i = 3 + 3 – 2 = 4 a3 = 5 = 6 - 4 + 3
 a4 = a 2 + j – i = 3 + 4 – 2 = 5 a2 = 2 ≠ 6 - 4 + 2
 Hence : {1, 3, 4, 5}
Chapter 3.1, 3.3, 3.5, 3.6 48
Generating Combinations

Example 2
 List all 3-combination for the set {a, b, …, e}
 Assume {a, b, …, e} = {1, 2, …, 5}
 For all {a1, a2, a3}
1. i = r
1. {a, b, c} 6. {a, d, e} 2. while ai = n - r + i
2.1 i = i - 1
2. {a, b, d} 7. {b, c, d}
3. ai = ai + 1
3. {a, b, e} 8. {b, c, e} 4. for j = i + 1 to r
4. {a, c, d} 9. {b, d, e} 4.1 aj = ai + j - i
5. {a, c, e} 10. {c, d, e}

Chapter 3.1, 3.3, 3.5, 3.6 49

Generating Permutations
 Any set can be placed in one-to-one
correspondence with the set {1, 2, 3, ..., n}
 The permutations of any set of n elements can
be listed by generating the permutations of the
n smallest positive integers
 The algorithms based on the lexicographic
(or dictionary) ordering is discussed

Chapter 3.1, 3.3, 3.5, 3.6 50


Generating Permutations
 Algorithm: Generating the next permutation of (a1, a2, ..., an)
in Lexicographic Order by given permutation is {1, 2, ..., n},
where (a1, a2, ..., an) is not equal to (n, n-1, ..., 2, 1)
1. j = n – 1
2. while aj > aj+1 j is the largest subscript {a1, a2 , a3}
2.1 j := j – 1 with aj < aj+1 {1, 2, 3}
3. k = n
4. while aj > ak k is the largest subscript {1, 2, 3}
with aj < ak
4.1 k = k – 1 {1, 3, 2}
5. interchange aj and ak
6. r = n {2, 1, 3}
7. s = j + 1 Sort the number
after the jth
{2, 3, 1}
8. while r > s
8.1 interchange ar and as position in {3, 1, 2}
ascending order
8.2 r = r – 1 and s = s + 1 {3, 2, 1}
Chapter 3.1, 3.3, 3.5, 3.6 51

Generating Permutations

Example
 What is the next permutation in lexicographic order
after 362541?

 The last pair of aj and aj+1 where aj < aj+1 is


a3 = 2 and a4 = 5
 The least integer to the right of 2 that is greater than
2 is as = 4
 Exchange aj and as
 Hence, 4 is placed in the third position
 5, 2, 1 are placed in order in the last three positions
 Hence, the next permutation is 364125

Chapter 3.1, 3.3, 3.5, 3.6 52


Generating Permutations

r-Permutations
 How can we list all r-permutations from a
set {1, 2, 3, . . . , n}? r-combination
{a1, a2 , a3}
{1, 2, 3, 4} n-permutation
1. Use “next larger
r-combinations” lists {1, 2, 3} {1, 2, 3}
all r-combinations {1, 2, 4} {1, 3, 2}
2. For each r-combination, {1, 3, 4} {2, 1, 3}
use n-permutation to list {2, 3, 1}
all permutations {2, 3, 4}
{3, 1, 2}
{3, 2, 1}
Chapter 3.1, 3.3, 3.5, 3.6 53

Pigeonhole Principle
 Suppose that a flock of 26 pigeons flies into a
set of 25 pigeonholes to roost
 What can we conclude?

Chapter 3.1, 3.3, 3.5, 3.6 54


Pigeonhole Principle
 A least one of these 25 pigeonholes must
have at least two pigeons in it
 Because there are 26 pigeons but only 25
pigeonholes
 This is
Pigeonhole Principle

Chapter 3.1, 3.3, 3.5, 3.6 55

Pigeonhole Principle
 Pigeonhole Principle
If k is a positive integer and k + 1 or more objects
are placed into k boxes, then there is at least one
box containing two or more of the objects
 Also called the Dirichlet Drawer Principle
the nineteenth-century German mathematician Dirichlet

 Proof by contraposition (p  q  q  p)


 Suppose that none of the k boxes contains more than
one object
 Then the total number of objects would be at most k
 This is a contradiction

Chapter 3.1, 3.3, 3.5, 3.6 56


Pigeonhole Principle
 Corollary
A function f from a set with k + 1 or more
elements to a set with k elements is not one-
to-one

a
b 1
2
c
3
d

Chapter 3.1, 3.3, 3.5, 3.6 57

Pigeonhole Principle
 Example 1
How many words we should have if there must be
at least two that begin with the same letter?
 27 English words, because 26 letters in the English
alphabet

 Example 2
How many people we should have if there must be
at least two with the same birthday?
 367 people because 366 possible birthdays

Chapter 3.1, 3.3, 3.5, 3.6 58


Generalized Pigeonhole Principle
 Pigeonhole Principle states that if k + 1 or
more objects are placed into k boxes, then
there is at least one box containing two or
more of the objects
 How about if we have
 2k + 1 objects?
 3k + 2 object?
 nk + 1 object?

Chapter 3.1, 3.3, 3.5, 3.6 59

Generalized Pigeonhole Principle


 Generalized Pigeonhole Principle
If N objects are placed into k boxes, then there is at
least one box containing at least N/k objects

 Proof by Contradiction
 Suppose that none of the boxes contains more than
N/k - 1 objects
 The total number of objects is at most
N    N  
k     1  k    1  1  N N/k < (N/k) + 1
 k    k  
 This is a contradiction because there are a total of N
objects

Chapter 3.1, 3.3, 3.5, 3.6 60


Generalized Pigeonhole Principle
 A common type of problem asks for the
minimum number of objects such that at least
r of these objects must be in one of k boxes
when these objects are distributed among the
boxes

Chapter 3.1, 3.3, 3.5, 3.6 61

Generalized Pigeonhole Principle


 According to generalized pigeonhole principle,
when we have N objects, there must be at least r
objects in one of the k boxes as long as N/k ≥ r
 N, where N = k(r - 1) + 1, is the smallest integer
satisfying N/k ≥ r

k
N
Chapter 3.1, 3.3, 3.5, 3.6 62
Generalized Pigeonhole Principle
 N/k ≥ r, N = k(r - 1) + 1, is the smallest
integer satisfying N/k ≥ r
 Could a smaller value of N suffice?
 No
 If k(r - 1) objects
 We could put r - 1 of them in each of the k
boxes
 No box would have at least r objects

Chapter 3.1, 3.3, 3.5, 3.6 63

Generalized Pigeonhole Principle N/k ≥ r


Example 1 N = k(r - 1) + 1
 How many people out of 100 people were
born in the same month?

 N = 100
 k = 12
 r=?
 100/12 = 9 who were born in the same
month

Chapter 3.1, 3.3, 3.5, 3.6 64


Generalized Pigeonhole Principle N/k ≥ r
Example 2 N = k(r - 1) + 1
 What is the least number of area codes needed to guarantee
that the 25 million phones in a state can be assigned distinct
10-digit telephone numbers?
 Assume that telephone numbers are of the form NXX-NXX-
XXXX, where the first three digits form the area code, N
represents a digit from 2 to 9 inclusive, and X represents any
digit.

 Different phone numbers for NXX-XXXX is


8 x 106 = 8,000,000
 N = 25,000,000, k = 8,000,000
 At least 25,000,000 / 8,000,000 = 4 of them must have
identical phone numbers
 Hence, at least four area codes are required

Chapter 3.1, 3.3, 3.5, 3.6 65

Generalized Pigeonhole Principle N/k ≥ r


Example 3 N = k(r - 1) + 1
 Show that among any n + 1 positive integers not exceeding
2n there must be an integer that divides one of the other
integers
 Assume we have n + 1 integers a1, a2, ..., an+1
k
 Let a j  2 j q j for j = 1, 2, . . . , n + 1,
where kj is a nonnegative integer and
q1, q2, ..., qn+1 are all odd positive integers less than 2n
 According to pigeonhole principle, because only n odd
positive integers less than 2n, two of the integers q1, q2, ...,
qn+1 must be equal
 Let q be the common value of qi and qj, then, ai  2 ki q and
k
aj  2 j q
 It follows that if ki < kj, then ai divides aj; a j 2k j q k j  ki
otherwise aj divides ai =
ai 2 ki q
= 2

Chapter 3.1, 3.3, 3.5, 3.6 66


Applications: Subsequence
 Suppose that a1, a2, ..., aN is a sequence of
real numbers.
 A subsequence of this sequence is a
sequence of the form ai1 , ai2 ,..., aim
where 1 < i1 < i2 < ... < im < N

Chapter 3.1, 3.3, 3.5, 3.6 67

Applications: Subsequence

Example
 Example:
 a1 , a2, ..., a5 = 5, 8, 2, 3, 1
 5, 3, 1 is a subsequence? a1, a4, a5

 8, 1 is a subsequence? a2, a5

 2, 3, 5, 8 is a subsequence? a3, a4 , a1, a2

Chapter 3.1, 3.3, 3.5, 3.6 68


Applications: Subsequence
 A sequence is called strictly increasing if
each term is larger than the one that
precedes it
 A sequence is called strictly decreasing if
each term is smaller than the one that
precedes it

Chapter 3.1, 3.3, 3.5, 3.6 69

Applications: Subsequence
 Theorem
Every sequence of n2 + 1 distinct real numbers
contains a subsequence of length n + 1 that is
either strictly increasing or strictly decreasing
 Example
 Given a sequence: 8, 11, 9, 1, 4, 6, 12, 10, 5, 7
 10 term = 32 + 1
 What is the length of the longest in / decreasing
subsequences? n+1 = 4
 Increasing sequence  Decreasing sequence
1, 4, 6, 12 11, 9, 6 ,5
1, 4, 6, 7
1, 4, 6, 10
1, 4, 5, 7

Chapter 3.1, 3.3, 3.5, 3.6 70


Applications: Subsequence
Proof
 Let a1 , a2 ,..., an 2 1 be a sequence of n2 + 1 distinct
real numbers
 Associate an ordered pair (ik, dk) to the term ak,
where
 ik is the length of the longest increasing subsequence
starting at ak
 dk is the length of the longest decreasing subsequence
starting at ak

5, 8, 2, 3, 1
(i1, d1) = (2, 3) (i4, d4) = (1, 2)
Chapter 3.1, 3.3, 3.5, 3.6 71

Applications: Subsequence
5, 8, 2, 3, 1
Proof (2, 3) (1, 2)
 Suppose no increasing or decreasing
subsequences is longer than n
 ik and dk are both positive integers less than or
equal to n, for k = 1, 2, . . . , n2 + 1
 By the product rule,
n2 possible ordered pairs for (ik, dk)
 By the pigeonhole principle
two of n2 + 1 ordered pairs are equal
 Therefore, there exist terms as and at,
with s < t such that is = it and ds = dt

Chapter 3.1, 3.3, 3.5, 3.6 72


Applications: Subsequence
Proof …, as, …, at, …
There exist terms as and at, 5, 8, 2, 3, 1
with s < t such that is = it and ds = dt
(2, 3) (1, 2)
 We will show that this is impossible
 Because the terms of the sequence are distinct, either
as < at or as > at
 If as < at, then, because is = it, an increasing
subsequence of length it + 1 can be built starting at as,
by taking as followed by an increasing subsequence of
length it beginning at at
 This is a contradiction
 Similarly, if as > at, it can be shown that ds must be
greater than dt, which is a contradiction

Chapter 3.1, 3.3, 3.5, 3.6 73

Applications: Ramsey Theory


 Ramsey theory, after the English mathematician F.
Ramsey, deals with the distribution of subsets of
elements of sets
 Two people either friends or enemies
A B A B

Friends Enemies
 Mutual Friend/Enemies

A B A B
A B C D are mutual
friends/enemies
C D C D
Chapter 3.1, 3.3, 3.5, 3.6 74
Applications: Ramsey Theory

Example 1
 Assume that in a group of six people
 Show that there are either three mutual
friends or three mutual enemies in the group

A B C D E F

Chapter 3.1, 3.3, 3.5, 3.6 75

Applications: Ramsey Theory

Example 1
 Let A be one of the six people
 According to pigeonhole principle (5/2 = 3),
A at least has three friends, or three enemies
 Former Case: suppose that B, C, and D are friends
 If any two of these three people are friends, then these two and A
form a group of three mutual friends
 Otherwise, B, C, and D form a set of three mutual enemies
 Similar to the latter case

A B C D E F

Chapter 3.1, 3.3, 3.5, 3.6 76


Applications: Ramsey Theory
 Ramsey number R(m, n)
 The minimum number of people at a party such that
there are either m mutual friends or n mutual enemies,
assuming that every pair of people at the party are
friends or enemies
 m and n are positive integers greater than or equal to 2

 Example
 What is R(3, 3)?
 Answer should be 6
 In a group of five people where every two people are
friends or enemies, there may not be three mutual friends
or three mutual enemies

Chapter 3.1, 3.3, 3.5, 3.6 77

Applications: Ramsey Theory


 5 people cannot guarantee having 3 mutual
friends/enemies
B

A C

E D

Chapter 3.1, 3.3, 3.5, 3.6 78


Discrete Mathematic

Chapter 4: Advanced Counting Techniques


4.1
Recurrence Relations
4.2
Solving Linear
Recurrence Relations
4.4
Generating Functions
Yan Huang
School of Computer Science and Engineering
South China University of Technology

Thanks to Dr Patrick Chan for providing the Slides

Agenda
 Recurrence Relations
 Modeling with Recurrence Relations
 Linear Nonhomogeneous Recurrence Relations
with Constant Coefficients
 Generating Functions
 Useful Facts About Power Series
 Extended Binomial Coefficient
 Extended Binomial Theorem
 Counting Problems and Generating Functions
 Using Generating Functions to Solve Recurrence
Relations
Ch. 4.1, 4.2 & 4.4 2
Recursion
How many i-
7 products?

How many i-products the


families of students in our 6 How many
1 How many
i-products? i-products?
school have?

2 4 1 How many
i-products?

How many How many


i-products? i-products?

0 0 1 1 0 1 1 1 1 0 0 1 0 0
Ch. 4.1, 4.2 & 4.4 3

Recurrence Relations
 A recurrence relation for a sequence {an} is an
equation that expresses an in terms of one or more
previous elements (a0, …, an−1)
 A sequence is called a solution of a recurrence
relation if its terms satisfy the recurrence relation

Ch. 4.1, 4.2 & 4.4 4


Recurrence Relations

Example 1
 Let {an} be a sequence that satisfies the
recurrence relation an = an-1 - an-2 for n = 2, 3,
4, . . . , and suppose that a0 = 3 and a1 = 5.
What are a2 and a3?

 From the recurrence relation:


 a2 = a1 – a0 = 5 - 3 = 2
 a3 = a2 – a1 = 2 - 5 = -3

Ch. 4.1, 4.2 & 4.4 5

Recurrence Relations

Example 2
 Consider the recurrence relation
an = 2an−1 − an−2, where n ≥ 2
 Which of the following are solutions?
 an = 3n

 2an−1 − an−2 = 2(3(n - 1)) - 3(n - 2) = 3n = an
 an = 2n
 2an−1 − an−2 = 2(2n-1) – nn-2 = 2n ≠ an
 an = 5

 2an−1 − an−2 = 2 x 10 – 5 = 5 = an

Ch. 4.1, 4.2 & 4.4 6


Recurrence Relations
 The initial conditions for a sequence specify the
terms that precede the first term where the
recurrence relation takes effect
 For example
an = an-1 + an-2, what is the value of a3?
Answer depends on a0 and a1 (initial conditions)
 a0 = 3 and a1 = 5 : a2 = 8, a3 = 13
 a0 = 1 and a1 = 2 : a2 = 3, a3 = 5

 A sequence is determined uniquely by


 Recurrence relation
 Initial conditions

Ch. 4.1, 4.2 & 4.4 7

Modeling with Recurrence Relations

Compound Interest
 Growth of saving in a bank account with r% interest
per given period
 Sn = Sn-1 + r · Sn-1 = (r+1) · Sn-1

 Example:
 Suppose that a person deposits $10,000 in a savings
account at a bank yielding 11 % per year with interest
compounded annually. How much will be in the account
after 30 years?
 S30 = 1.11S29 = 1.11(1.11S28) = …= (1.11)30 10,000

Ch. 4.1, 4.2 & 4.4 8


Modeling with Recurrence Relations

Tower of Hanoi
 Objective
 Get all disks from peg 1 to peg 3
 Rules
 Only move 1 disk at a time
 Never put a larger disk on a smaller one

1 2 3

Ch. 4.1, 4.2 & 4.4 9

Modeling with Recurrence Relations

Tower of Hanoi
More than 1 steps

Break Down
Recursive Call Recursive Call
More than 1 steps 1 step More than 1 steps

Ch. 4.1, 4.2 & 4.4 10


Modeling with Recurrence Relations

Tower of Hanoi
More than 1 steps

Break Down
Recursive Call Recursive Call
More than 1 steps 1 step More than 1 steps

Ch. 4.1, 4.2 & 4.4 11

Modeling with Recurrence Relations

Tower of Hanoi
More than 1 steps

Break Down
Base Case Base Case
1 step 1 step 1 step

Ch. 4.1, 4.2 & 4.4 12


Modeling with Recurrence Relations

Tower of Hanoi

The solution

Ch. 4.1, 4.2 & 4.4 13

Modeling with Recurrence Relations

Tower of Hanoi
 Let Hn be the number of moves for a stack of
n disks.
 Strategy:
 Move top n−1 disks (Hn−1 moves)
 Move bottom disk (1 move)
 Move top n−1 to bottom disk (Hn−1 moves)
 Hn = 2Hn-1 + 1

Ch. 4.1, 4.2 & 4.4 14


Modeling with Recurrence Relations

Fibonacci (Rabbits) Numbers


 A young pair of rabbits At least two Younger than
months old two months
(one of each sex) is
placed on an island
1 1
 A pair of rabbits does
not breed until they are
2 months old 2 1
 After they are 2 months
old, each pair of rabbits 3 1 1.1
produces another pair
each month 4 1 1.1 1.2
 Pn = Pn−1 + Pn−2
1 1.2 1.3
5
1.1 1.1.1
Ch. 4.1, 4.2 & 4.4 15

Modeling with Recurrence Relations

Example 1
 Find a recurrence relation and give initial conditions for the
number of bit strings of length n that do not have two
consecutive 0s.

 Let Pn denote the number of bit strings of length n that do


not have two consecutive 0s
For P4
Any bit string of length n - 1
with no two consecutive 0s 1 Pn-1 0101 0110
0111 1010
Any bit string of length n - 2
with no two consecutive 0s 10 Pn-2 1011 1110
1101 P2
 Pn = Pn-1 + Pn-2 , n ≥ 3 1111
 P1 = 2, P2 = 3
P3
Ch. 4.1, 4.2 & 4.4 16
Modeling with Recurrence Relations

Example 2
 Find a recurrence relation and give initial conditions for the
number of bit strings of length n that do not have three
consecutive 0s.

 Let Pn denote the number of bit strings of length n that do


not have three consecutive 0s
For P4
1 Pn-1
Any bit string of length n - 1
with no three consecutive 0s
0011 0010 0100
Any bit string of length n - 2 0101 0110 1100
with no three consecutive 0s 10 Pn-2 0111 1010 P1
Any bit string of length n - 3
with no three consecutive 0s 100 P
1001 1110
n-2 1011 P2
 Pn = Pn-1 + Pn-2 + Pn-3 , n ≥ 4 1101
 P1 = 2, P2 = 4, P3 = 7 1111
P3
Ch. 4.1, 4.2 & 4.4 17

Solving Linear Recurrence Relations


 Given Pn = Pn−1 + Pn−2, what is P100?
 It is not easy to calculate
 Need a better solution
which is not in
relation form
E.g. Pn = n*10 -1

Ch. 4.1, 4.2 & 4.4 18


Solving Linear Recurrence Relations
 Linear Homogeneous Recurrence of Degree k with
Constant Coefficients is a recurrence of the form
k
an = c1an−1 + … + ckan−k   ci ani
i 1

where the ci are all real numbers, and ck ≠ 0


 Linear: the power of all ai term is one
 Homogeneous: no constant term (no team without ai)
 Recurrence: a sequence {an} which an in terms of an-1, an-2,…
 Degree k: refer to k previous terms an-k
 Constant Coefficients: c1, c2, … independent from n

 The short name is “k-LiHoReCoCo”


Ch. 4.1, 4.2 & 4.4 19

Solving Linear Recurrence Relations


Example
 Mn = Mn−1 + (1.11)Mn−1  an =an-1 + (an-2)2
 1-LiHoReCoCo  Not linear

 Pn = Pn−1 + Pn−2  Hn = 2Hn−1 + 1


 2-LiHoReCoCo  Not homogeneous

 an = an-5
 Bn = nBn-1
 5-LiHoReCoCo
 Non-constant coefficient
k-LiHoReCoCo (n is a variable)
 Linear: the power of all ai term is one
 Homogeneous: no constant term (no team without ai)
 Recurrence: a sequence {an} which an in terms of an-1, an-2,…
 Degree k: refer to k previous terms an-k
 Constant Coefficients: c1, c2, … independent from n
Ch. 4.1, 4.2 & 4.4 20
Solving 2-LiHoReCoCos
 Given 2-LiHoReCoCo: an = c1an−1 + c2an−2 , and a0 =
c and a1 = d

 Assume sn and tn be the solution, an = sn and an = tn


 sn = c1sn−1 + c2sn−2 and tn = c1tn−1 + c2tn−2
 For constants w1 and w2
w1sn + w2tn = w1(c1sn−1 + c2sn−2 ) + w2(c1tn−1 + c2tn−2 )
an = c1(w1sn−1 + w2tn−1 ) + c2(w1tn−2 + w2tn−2 )
an-1 an-2

 Therefore, an = w1sn + w2tn is a solution


Ch. 4.1, 4.2 & 4.4 21

Solving 2-LiHoReCoCos
 By considering 1-LiHoReCoCo, an = c an-1
 Obviously, the general solution is an = cn a0
 Therefore, the solution of the form may be an = rn

 Substitute an = rn to an = c1an−1 + c2an−2 ,


then r2 - c1r - c2 = 0 or r = 0
 r = 0 is a special case since an = 0
 r2 - c1r - c2 is called characteristic equation

Ch. 4.1, 4.2 & 4.4 22


Solving 2-LiHoReCoCos
 Given 2-LiHoReCoCo: an = c1an−1 + c2an−2 , and
a0 = c and a1 = d
 Assume an = w1r1n + w2r2n for
r1 and r2 are different and some constants w1, w2
 We know that r12 - c1r1 - c2 = 0 and r22 - c1r2 - c2 = 0
 Characteristic Equation: r2 - c1r - c2 = 0
 Characteristic Roots: r1 and r2
 w1 and w2 can be calculated by using c and d
a0 = c = w1r10 + w2r20
a1 = d = w1r11 + w2r21

Ch. 4.1, 4.2 & 4.4 23

Solving 2-LiHoReCoCos
Theorem
 Consider an arbitrary 2-LiHoReCoCo:
an = c1an−1 + c2an−2
 By substituting an = rn, we have the characteristic
equation:
r2 − c1r − c2 = 0
 If there has two different roots r1 and r2, then
an = w1r1n + w2r2n
for n ≥ 0 and some constants w1, w2

Ch. 4.1, 4.2 & 4.4 24


Solving 2-LiHoReCoCos
 Proof
Given r1, r2 are the characteristic root
an = c1an−1 + c2an−2  an = w1r1n + w2r2n

where and w1, w2 are constants

 Two steps for the proof


1. Show if an = w1r1n + w2r2n , {an} is a solution of the
recurrence relation
2. Show if {an} is the solution of the recurrence
relation, an = w1r1n + w2r2n for some w1 and w2

Ch. 4.1, 4.2 & 4.4 25

Solving 2-LiHoReCoCos
 Step 1
Show if an = w1r1n + w2r2n , {an} is a solution of the
recurrence relation

  
c1an1  c2 an2  c1 w1r1n1  w2 r2n1  c2 w2 r1n2  w2 r2n2 
 w1r1n2 c1r1  c2   w2 r2n2 c1r2  c2 
 w1r1n2 r12  w2 r2n2 r22 r1 and r2 are the solution of
r 2  c1r  c2  0
 w1r1n  w2 r2n
 an

Ch. 4.1, 4.2 & 4.4 26


Solving 2-LiHoReCoCos
 Step 2
Show if {an} is the solution of the recurrence relation, an =
w1r1n + w2r2n for some w1 and w2
 Suppose that {an} is a solution of the recurrence relation, and the
initial conditions a0 = C0 and a1 = C1 hold
 We want to show that there are constants w1 and w2 such that the
sequence {an} with an = w1r1n + w2r2n satisfies these same initial
conditions
 a0 = C0 = w1 + w2 and a1 = C1 = w1r1 + w2r2
 By solving these two equations:
C1  C0 r2 C0 r2  C1
w1  w2 
r1  r2 r1  r2
 When r1 ≠ r2, {an} with w1r1n + w2r2n satisfy the 2 initial conditions

Ch. 4.1, 4.2 & 4.4 27

Solving 2-LiHoReCoCos
 We know that {an} and {α1r1n + α2r2n} are both
solutions of the recurrence relation an = c1an−1 +
c2an−2 and both satisfy the initial conditions when n =
0 and n = 1
 Because there is a unique solution of 2-
LiHoReCoCo with two initial conditions, it follows
that the two solutions are the same, that is, an =
α1r1n + α2r2n for all nonnegative integers n
 We have completed the proof

Ch. 4.1, 4.2 & 4.4 28


Solving 2-LiHoReCoCos 2-LiHoReCoCo: an = c1an−1 + c2an−2 ,
Characteristic Equation: r2 - c1r - c2 = 0
Example 1 an = w1r1n + w2r2n (r1 and r2 are different)
 Solve the recurrence an = an−1 + 2an−2 given the initial
conditions a0 = 2, a1 = 7

 Characteristic Equation: r2 − r − 2 = 0
 Characteristic Root:
 r = (1 ± 3) / 2  b  b 2  4ac
 r = 2 or r = −1 x
2a
 Therefore, an = w1 2n + w2 (−1)n
 By using a0 = 2, a1 = 7
 a0 = 2 = w120 + w2 (−1)0
 a1 = 7 = w121 + w2 (−1)1
 w1 = 3 and w2 = 1
 Therefore, an = 3·2n − (−1)n
Ch. 4.1, 4.2 & 4.4 29

Solving 2-LiHoReCoCos 2-LiHoReCoCo: an = c1an−1 + c2an−2 ,


Characteristic Equation: r2 - c1r - c2 = 0
Example 2 an = w1r1n + w2r2n (r1 and r2 are different)
 Find an explicit formula for the Fibonacci numbers

 Recall fn = fn-1 + fn-2


 Characteristic equation: r2 - r - 1 = 0
 Characteristic roots: r1  (1  5 ) / 2 r2  (1  5 ) / 2
n n
1 5  1 5 
f n  w1    w2 
 


 2   2 
 Initial conditions f0 = 0 and f1 = 1
f 0  0  w1  w2
1 1
1 5  1 5  w1  w2  
f1  1  w1    w2 
  2 
 5 5
 2   
n n
1 1 5  1 1 5 
fn      

5 2  
5 2 
 
Ch. 4.1, 4.2 & 4.4 30
Solving 2-LiHoReCoCos
with two same roots
Theorem
 Let c1 and c2 be real numbers with c2 ≠ 0.
Suppose that r2 – c1r - c2 = 0 has only one
root r0
 A sequence {an} is a solution of the
recurrence relation an = c1an-1 + c2an-2 if and
only if an = w1r0n + w2nr0n, for n = 0, 1, 2, ...,
where w1 and w2 are constants

Ch. 4.1, 4.2 & 4.4 31

Solving 2-LiHoReCoCos with 2-LiHoReCoCo:


two same rootsan = c1an−1 + c2an−2 ,
Characteristic Equation: r2 - c1r - c2 = 0
Example 1 an = w1r0n + w2 n r0n
 What is the solution of the recurrence relation
an = 6an-1 - 9an-2 with initial conditions a0 = 1 and a1 = 6?

 Characteristic equation: r2 - 6r + 9 = 0
 Only one characteristic root: r = 3
 Hence, the solution to this recurrence relation is
an = w13n + w2n3n
for some constants α1 and α2
 By using the initial conditions,
a0 = 1 = w1, a1 = 6 = 3w1 + 3w2, so w1=1 and w2 = 1
 Consequently, an = 3n + n3n

Ch. 4.1, 4.2 & 4.4 32


Solving 2-LiHoReCoCos

Summary
 Given : an = c1an−1 + c2an−2 and a0 = c and a1 = d
1. Characteristic equation: r2 − c1r − c2 = 0

2a. If Characteristic Root (r1 and r2) are different


1. an = w1r1n + w2r2n is the solution
2. Use a0 = w1 + w2 = c and a1 = w1r1 + w2r2 = d to
solve w1 and w2

2b. If Characteristic Root (r1 and r2) are the same


1. an = w1rn + w2 n rn is the solution
2. Use a0 = w1 = c and a1 = w1r1 + w2r2 = d to solve
w1 and w2

Ch. 4.1, 4.2 & 4.4 33

 Small Exercise 
 What is the solution of the recurrence relation
an = - an-1 + 6an-2 with initial conditions a0 = 0
and a1 = 5?

 What is the solution of the recurrence relation


an = - 2an-1 - an-2 with initial conditions a0 = 5
and a1 = -6?

Ch. 4.1, 4.2 & 4.4 34


 Small Exercise 
 the recurrence relation: an = - an + 6an-2
 Initial conditions a0 = 0 and a1 = 5

 Characteristic Equation: r2 + r - 6 = 0
(r + 3)(r – 2) = 0
 Characteristic Root: r1 = -3, r2 = 2
 Therefore, an = w1 (-3)n + w2 (2)n
 Using the initial condition
 a0 = 0 = w1 + w2
 a1 = 5 = -3w1 + 2w2
 w1 = -1, w2 = 1
 Therefore, an = - (-3)n + (2)n
Ch. 4.1, 4.2 & 4.4 35

 Small Exercise 
 the recurrence relation: an = - 2an - an-2
 Initial conditions a0 = 5 and a1 = -6

 Characteristic Equation: r2 + 2r + 1 = 0
(r + 1)(r + 1) = 0
 Characteristic Root: r1 = -1
 Therefore, an = w1 (-1)n + w2 n (-1)n
 Using the initial condition
 a0 = 5 = w1
 a1 = -6 = -w1 - w2
 w1 = 5, w2 = 1
 Therefore, an = 5 (-1)n + n (-1)n
Ch. 4.1, 4.2 & 4.4 36
Solving k-LiHoReCoCos
k
2-kiHoReCoCo
 k-LiHoReCoCo: a   c a a =c a +c a
n i n i n 1 n−1 2 n−2
i 1
 Characteristic Equation is:
k
r   ci r k i  0
k
r2 − c1r − c2 = 0
i 1
 Theorem
If there are k distinct roots ri , then the solutions to
the recurrence are of the form:
k
an   wi ri n an = w1r1n + w2r2n
i 1

for all n ≥ 0, where the wi are constants

Ch. 4.1, 4.2 & 4.4 37

Solving k-LiHoReCoCos

Example
 Find the solution to the recurrence relation
an = 6an-1 – 11an-2 + 6an-3
with the initial conditions a0 = 2, a1 = 5, and a2 = 15.
 The characteristic equation is:
r3 - 6r2 + 11r – 6 = (r - 1)(r - 2)(r - 3)
 The characteristic roots are r = 1, r = 2, and r = 3
 an = w11n + w22n + w33n
 By using the initial conditions
 a0 = 2 = w1 + w2 + w3
 a1 = 5 = w1 + w2 x 2 + w3 x 3
 a2 = 15 = w1 + w2 x 4 + w3 x 9
 Therefore, w1 = 1, w2 = -1 and w3 = 2
 As a result, an = 1 - 2n + 2 x 3n
Ch. 4.1, 4.2 & 4.4 38
Solving k-LiHoReCoCos
with same roots
 Let c1, c2, ..., ck be real numbers
 Suppose that the characteristic equation
rk – c1rk-1 – ··· – ck = 0
has t distinct roots r1, r2, ..., rt with
multiplicities m1, m2, ..., mt
 i.e. ri appear mi times
 m1 + m2 + ··· + mt = k

Ch. 4.1, 4.2 & 4.4 39

Solving k-LiHoReCoCos
Special case for k=2, One distinct root
an = w1r0n + w2nr0n
with same roots an = (w1 + w2n)r0n
 A sequence {an} is a solution of the recurrence relation
an = c1an-1 + c2an-2 + ... + ckan-k
 If and only if Multiplicities for ri

an  ( w1,0  w1,1n  ...  w1,m1 1n m1 1 )r1n


No. of
 ( w2,0  w2,1n  ...  w2,m2 1n m2 1 )r2n distinct
 ...  (wt ,0  wt ,1n  ...  wt ,mt 1n mt 1 )rt n roots

 mi 1
t  n
    wi , j n ri
 j

i 1  j 0 
for n = 0,1,2, ..., where wi,j are constants for 1 ≤ i ≤ t and
0 ≤ j ≤ mi-1
Ch. 4.1, 4.2 & 4.4 40
m1 1
an  (wroots
Solving k-LiHoReCoCos with same 1, 0  w1,1n  ...  w1, m1 1n )r1n
 (w2,0  w2,1n  ...  w2,m2 1n m2 1 )r2n
Example  ...  (wt ,0  wt ,1n  ...  wt ,mt 1n mt 1 )rt n
 Find the solution to the recurrence relation
H n   H n 1  3H n  2  5H n 3  2 H n  4
with the initial conditions H  1,H  0, H  1,H  2
0 1 2 3

 Characteristic equation: x 4  x 3  3x 2  5 x  2  0
( x  2)( x  1) 3  0
 Roots: 1, 1, 1, 2
 Therefore: H n  ( c1  c2 n  c3 n 2 )( 1) n  c4 2 n
 By initial conditions:
 H 0  c1  c4  1 7 1 2
c1  , c2   , c3  0, c4 
 H   c  c  c  2c  0 9 3 9
 1 1 2 3 4

 H 2  c1  2c2  4c3  4c4  1 Hn 
7 1 2
(1) n  n(1) n  2n
 H 3  c1  3c2  9c3  8c4  2 9 3 9

Ch. 4.1, 4.2 & 4.4 41

Solving k-LiHoReCoCos

Summary k
 Given : an  c a
i 1
i n i and ai = ci , where i = 1, 2, …, k
k
1.Characteristic equation: r  k
 i 0
c r
i 1
k i

2. Characteristic Root (r1, r2, …, rk)

 mi 1
t  n
1. n    i , j ri is the solution of k-LiHoReCoCos
  j
a w n
i 1  j 0 
where mi is the multiplicity of ri
 mi 1t 
2. solve wi by ap = cp     wi , j p j ri p
i 1  j 0 
where p = 1, 2, ..., k
Ch. 4.1, 4.2 & 4.4 42
 Small Exercise 
 What is the solution of the recurrence relation
an = an-1 + an-2 - an-3 with initial conditions a0 =
0, a1 = 8 and a2 = 4?

an  (w1,0  w1,1n  ...  w1,m1 1n m1 1 )r1n


 (w2,0  w2,1n  ...  w2,m2 1n m2 1 )r2n
 ...  (wt ,0  wt ,1n  ...  wt ,mt 1n mt 1 )rtn

Ch. 4.1, 4.2 & 4.4 43

 Small Exercise 
 the recurrence relation: an = an-1 + an-2 - an-3
 Initial conditions a0 = 0, a1 = 8 and a2 = 4

 Characteristic Equation: r3 – r2 – r + 1 = 0
(r – 1)(r – 1)(r + 1) = 0
 Characteristic Root: r1 = 1, r2= 1, r3 = -1
 Therefore, an = (c1 + c2 n) (1)n + c3 (-1)n
 Using the initial condition
 a0 = 0 = c1 + c3
 a1 = 8 = c1 + c2 – c3
 a2 = 4 = c1 + 2c2 + c3
 c1 = 3, c2 = -3, c3 = 2
 Therefore, an = 3 (1 – n) (1)n + 2 (-1)n
Ch. 4.1, 4.2 & 4.4 44
Solving LiNoReCoCos
 Linear nonhomogeneous recurrence of
degree k with constant coefficients
(k-LiNoReCoCos) contain some terms F(n)
that depend only on n but not ai
 General form:
an = c1an−1 + … + ckan−k + F(n)

Associated Homogeneous Recurrence Relation

Ch. 4.1, 4.2 & 4.4 45

Solving LiNoReCoCos
 If { an( p ) } is a particular solution of the
nonhomogeneous linear recurrence relation with
constant coefficients
an = c1an-1 + c2an-2 + ... + ckan-k + F(n)

 Then every solution is of the form { an( p )  an( h ) },


where { a n(h ) } is a solution of the associated
homogeneous recurrence relation
an = c1an-1 + c2an-2 + ... + ckan-k

Ch. 4.1, 4.2 & 4.4 46


Solving LiNoReCoCos
 Proof
 As { a ( p ) } is a particular solution for LiNoReCoCos
n
 Suppose that { bn } is an another solution
an( p)  c1an( p1)  c2an( p2)  ... ck an( pk)  F (n)
bn  c1bn1  c2bn2  ... ck bnk  F(n)
    
bn  an( p)  c1 bn1  an( p1)  c2 bn2  an( p2)  ... ck bnk  an( pk) 
an( h )  c1an( h)1  c2 an( h)2  ...  ck an( h)k
 { b  a ( p ) } is a solution of the associated
n n
homogeneous linear recurrence, named { a (h ) }
n
 Consequently, bn  an  an for all n.
( p) (h)

Ch. 4.1, 4.2 & 4.4 47

Solving LiNoReCoCos an = 3an−1+2n


Example 1 a1= 3
 Find all solutions to an = 3an−1+2n, which solution
has a1 = 3?

 Notice this is a 1-LiNoReCoCo.


 Its associated 1-LiHoReCoCo
 an = 3an−1 and root is 3
(h )
 Solution is a n = c3n

 The solutions of LiNoReCoCo are in the form


an  an( p )  an( h )
Next step
Ch. 4.1, 4.2 & 4.4 48
Solving LiNoReCoCos an = 3an−1+2n
Example a1= 3
 If F(n) is a degree-u polynomial in n, a degree-u
polynomial should be tried as the particular
solution an( p )
 Now, F(n) = 2n
 Try an( p ) = cn + d, c and d are constants
an = 3an-1 + 2n
cn+d = 3(c(n−1)+d) + 2n
(2c+2)n + (3c−2d) = 0
c = −1 and d = −3/2

 Solution is: an( p ) = -n – 3/2


Ch. 4.1, 4.2 & 4.4 49

Solving LiNoReCoCos an = 3an−1+2n


Example a1= 3

 Therefore, we have 3
an  a a
( p) (h) a  n 
( p)
n
n n 2
3
  n   c3n an( h )  c3n
2
 By using a1 = 3
 3 = -1 - 3/2 + 3c
 c = 11/6
 As a result,

Ch. 4.1, 4.2 & 4.4 50


Solving LiNoReCoCos
Particular Solution
 k 
 Suppose {an} satisfies the LiNoReCoCo an    ci an i   F ( n)
 i 1 
where ci (i = 1,2,…k) are real numbers and

F ( n )  (bt n t  bt 1n t 1  ...  b1n  b0 ) s n


where b0, b1, …, bt and s are real numbers

 When s is not a root of the characteristic equation of the


associated linear homogeneous RR, there is a particular solution
( a ( p ) ) of the form
n
( pt n t  pt 1n t 1  ...  p1n  p0 ) s n
 When s is a root of the characteristic equation of the associated
linear homogeneous RR with multiplicity m, there is a particular
solution ( a ( p )) of the form
n
n m ( pt n t  pt 1n t 1  ...  p1n  p0 ) s n
Ch. 4.1, 4.2 & 4.4 51

Solving LiNoReCoCos
F ( n ) : Particular
(b n t  b n tSolution
1
 ...  b n  b )sn
t t 1 1 0
Example 1
s is not a root ( pt n t  pt 1n t 1  ...  p1n  p0 ) s n
 What form does a particular solution t of the tlinear
1
s is a root n m
nonhomogeneous recurrence relation( pt n  pt 1n  ...  p1n  p0 ) s n

an = 6an-1 - 9an-2+F(n) have when ...

 Consider the associated


 F(n) = 3n
homogeneous RR: n 2 ( p0 )3n

an = 6an-1 - 9an-2  F(n) = n3n


n 2 ( p1n  p0 )3n
 Characteristic Equation
r 2  6r  9  ( r  3)2  0  F(n) = n22n
 Characteristic Root is 3, of ( p2 n 2  p1n  p0 ) 2 n
multiplicity m=2  F(n) = (n2 + 1)3n
n 2 ( p2 n 2  p1n  p0 )3n
Ch. 4.1, 4.2 & 4.4 52
t 1
F ( n )  (bt nSolution
Solving LiNoReCoCos : Particular  bt 1n  ...  b1n  b0 ) s
t n

t 1
s is not a root ( pt n  pt 1n  ...  p1n  p0 ) s
t n

Example 2 s is a root n m ( pt n t  pt 1n t 1  ...  p1n  p0 ) s n


 Let an be the sum of the first n positive integers, so that
n
an   k
k 1
 an satisfies the linear nonhomogeneous RR
an = an-1 + n
 Associated linear homogeneous RR is an = an-1
 Root is 1. The solution is an( h )  c (1) n, c is a constant
 Since F(n) = n = n x (1)n, and s = 1 is a root of degree one of
the characteristic equation of the associated linear
homogeneous RR
 So the particular solution has the form n(p1n+p0)

Ch. 4.1, 4.2 & 4.4 53

Solving LiNoReCoCos : Particular Solution


an = an-1 + n
Example 2 an( p )  n( p1n  p0 )
 By solving
p1n2 + p0n = p1 (n - 1)2 + p0(n - 1) + n
 We have p0 = p1 =1/2
 Recall, an  an( p )  an( h )
an( h )  c
an  n(n  1) / 2  c
an( p )  n(n  1) / 2
 By using a1 = 1, so c = 0
 Therefore,
n( n  1)
an 
2

Ch. 4.1, 4.2 & 4.4 54


Solving Linear Recurrence Relations

Summary
 k-LiHoReCoCos with m same roots (without F(x))
 Find the root of characteristic equation
 an  (1,0  1,1n  ...  1,m 1n )r1
m 1
1 n
1
 ( 2,0   2,1n  ...   2,m 1n m 1 )r2n
2
2

 ...  ( t ,0   t ,1n  ...   t ,m 1n m 1 )rt n


t
t

 Use initial terms to find alphas


 k-LiNoReCoCos with m same roots (with F(x))
 Find the solution of characteristic equation of Associated linear
homogeneous RR a n(h )
 Find the particular solution of LiNoReCoCo using
an( p )  n m ( pt n t  pt 1n t 1  ...  p1n  p0 ) s n
 Finally an( p )  an( h )
 Use initial terms to find alphas

Ch. 4.1, 4.2 & 4.4 55

 Small Exercise 
 Find all solutions to an = 7an−1+(2n2+2)3n,
which solution has a1 = 10?

Ch. 4.1, 4.2 & 4.4 56


an = 7an−1+(2n2+2)3n
 Small Exercise  a0 = 10
 Its associated 1-LiHoReCoCo
 an = 7an−1 and root is 7
 Solution is a n(h ) = c7n

 The solutions of 1-LiNoReCoCo are in the form


an  an( p )  an( h )

( p)
 Need to do is find one an

Ch. 4.1, 4.2 & 4.4 57

an = 7an−1+(2n2+2)3n
 Small Exercise  a0 = 10
 Now, F(n) = (2n2+2)3n
( p)
 an = (an2 + bn + c)3n
an = 7an-1 + (2n2+2)3n
(an2+bn+c)3n = 7(a(n-1)2+b(n-1)+c)3n-1 + (2n2+2)3n
3an2+3bn+3c = 7an2 - 14an + 7a + 7bn – 7b + 7c + 6n2 + 6
0 = 4an2 - 14an + 7a + 4bn – 7b + 4c + 6n2 + 6
0 = n2(4a+6)+n(4b-14a) +(4c +7a-7b+6 )
4a+6=0 4b-14a=0 4c +7a-7b+6=0
a=-3/2 b=-21/4 c=-129/16
an( p )  (3n 2 / 2  21n / 4  129 / 6)3n
Ch. 4.1, 4.2 & 4.4 58
an = 7an−1+(2n2+2)3n
 Small Exercise  a0 = 10
 Therefore, we have
an  an( p )  an( h ) an( p )  (3n 2 / 2  21n / 4  129 / 6)3n

 (3n 2 / 2  21n / 4  129 / 6)3n  c7 n an( h )  c7 n

 By using a0 = 10
 a0 = 10 = -129/6 + c
 c = 189/6
 As a result,
an  (3n 2 / 2  21n / 4  129 / 6)3n  189  7 n / 6

Ch. 4.1, 4.2 & 4.4 59

Generating Functions
 Generating functions ( G(x) ) are used to
represent sequences efficiently by coding the
terms of a sequence as coefficients of
powers of a variable x in a formal power
series
 Generating function for the sequence
a0, a1, a2, …, ak, … of real numbers is the
infinite series

G( x)  a0 x  a1x  ak x   ak xk
0 1 k

k 0

Ch. 4.1, 4.2 & 4.4 60


Example
 What is the generating function for the
following sequence?
 {0, 2, …, 2k,…} 
+…=  x
k k
0 + 2x + … + 2k · xk 2
k 0

 {1, 1, 1, 1, 1}
4
1 + x + x2 + x3 + x4 =  x
k 0
k

Ch. 4.1, 4.2 & 4.4 61

Useful Facts About Power Series


 
1 1

k 0
xk

1 x
 x 
a k k

k 0 1  ax

1
 f x   1  f x  
1 x 1  ax
is generating function is generating function
of the sequence of the sequence
1,1,1,1, ... 1, a, a2, a3, ...

Ch. 4.1, 4.2 & 4.4 62


Useful Facts About Power Series
 
 Given: f x    ak x k
g  x    bk x k
k 0 k 0

f x   g x   
 k k
a  b x k

k 0
 
f  x g  x   a x b x k
k
k
k

k 0 k 0
 (a0 x 0  a x  ...)(b0 x 0  b1 x1  ...)
1
1

 x 0 (a0b0 )  x1 (a0b1  a1b0 ) 


x 2 (a0b2  a1b1  a2b0 )  ...

  a j bk  j x k
k

k 0 j 0
Ch. 4.1, 4.2 & 4.4 63

Useful Facts About Power Series

Example
1
 Let h( x)  ,
(1  x ) 2
 Find the coefficients a0, a1, a2, ... in the expansion
h ( x )  k 0 ak x k

1 1 1
h(x)  
1 
(1  x) 2 (1  x) (1  x)
f x     xk
1  x k 0   k   k 
  x  x 
 
 k 0   k 0 
f  x    ak x k
g  x    bk x k
k 0 k 0
 k

  x k
f x   g x    ak  bk x k
k  0 j 0
k 0

f  x g x    a j bk  j x k
k 

ak=k+1   (k  1) x k
k 0 j 0
k 0

Ch. 4.1, 4.2 & 4.4 64


Counting Problems and
Generating Functions
 How can we solve the counting problems,
including the recurrence relation, by using the
Generating Functions?


G( x)  a0 x  a1x  ak x   ak xk
0 1 k

k 0

Ch. 4.1, 4.2 & 4.4 65

Counting Problems and Generating Functions

Example 1
 Find the number of solutions of e1 + e2 + e3 = n when n = 17,
where e1 ,e2, e3 are nonnegative integers with 2  e1  5,
3  e2  6, 4  e3  7

 By considering this generating function for the sequence {an},


where an is the number of solution for n

 ( x 2  x3  x 4  x5 ) 
 ak x k  ( x 3  x 4  x 5  x 6 ) 
k 0 ( x4  x5  x6  x7 )
 As n = 17, a17, which is the coefficient of x17, is the solution
 Answer is 3

Ch. 4.1, 4.2 & 4.4 66


Counting Problems and Generating Functions

Example 2
 In how many different ways can eight identical
cookies be distributed among three distinct children
if each child receives at least two cookies and no
more than four cookies?

 By considering this generating function for the


sequence {an}, where an is the number of solution
for n

 k  ( x 2  x3  x 4 )( x 2  x3  x 4 )( x 2  x 3  x 4 )
a x
k 0
k

 The coefficient of x8 is 6

Ch. 4.1, 4.2 & 4.4 67

Counting Problems and Generating Functions

Example 3
 Solve the recurrence relation ak = 3ak-1 for k=1, 2,
3, … and initial condition a0=2
 Let G(x) be the generating function for the
sequence {ak}, that is


1
a
k 0
k
xk 
1  ax

Ch. 4.1, 4.2 & 4.4 68


Counting Problems and Generating Functions

Example 4
 Solve the recurrence relation ak = -ak-1 + 6ak-2 with
initial conditions a0 = 0 and a1 = 5
 Let G(x) be the generating function for the
sequence {ak}, that is

−ak−1 + 6ak−2

ak−1 6 ak−2

Ch. 4.1, 4.2 & 4.4 69

Counting Problems and Generating Functions

Example 4
ak−1 6 ak−2

ak−1 6 ak−2

ak−1 6 ak−2

6 a0 = 0 and a1 = 5

2 3

Ch. 4.1, 4.2 & 4.4 70


Counting Problems and Generating Functions

Example 4
2 3

2 3
1

2x 3 1  ax

Ch. 4.1, 4.2 & 4.4 71

Counting Problems and Generating Functions

Example 5
 Solve the recurrence relation an = - 2an-1 - an-2 with
initial conditions a0 = 5 and a1 = -6
 Let G(x) be the generating function for the
sequence {ak}, that is

2ak−1 ak−2

ak−1 ak−2

Ch. 4.1, 4.2 & 4.4 72


Counting Problems and Generating Functions

Example 5
ak−1 ak−2

ak−1 ak−2

ak−1 ak−2

a0 = 5 and a1 = -6

1

1  ax

Ch. 4.1, 4.2 & 4.4 73

Counting Problems and Generating Functions


=
1
=

Example 5
1−

When k =0, −1 =0

Ch. 4.1, 4.2 & 4.4 74


Counting Problems and Generating Functions
=
1
=

Example 5
1−

an = 5 (-1)n + n (-1)n

Ch. 4.1, 4.2 & 4.4 75

Counting Problems and Generating Functions

Example 6
 The sequence {an} satisfies the recurrence
relation
an = 8an-1 + 10n-1
and the initial condition a1 = 9
 Use generating functions to find an explicit
formula for an

Ch. 4.1, 4.2 & 4.4 76


Counting Problems and Generating Functions

Example 6 an = 8an-1 + 10n-1 a1 = 9


1
 ak xk 
k 0 1  ax

Ch. 4.1, 4.2 & 4.4 77

Counting Problems and Generating Functions

Example 6 an = 8an-1 + 10n-1 a1 = 9


a1 = 8a0 + 101-1 = 9
a0 = 1


1

k 0
a x 
k k

1  ax

1 n

an  8  10 n
2

Ch. 4.1, 4.2 & 4.4 78
Discrete Mathematic

Chapter 9: Graphs
9.1
Graphs and Graph Model
9.2
Graph Terminology and
Special Types of Graphs
9.3
Representing Graphs and
graph Isomorphism
9.4
Connectivity
Yan Huang
School of Computer Science and Engineering
South China University of Technology

Thanks to Dr Patrick Chan for providing the Slides

Agenda
 Graph
 Terminology
 Connectivity
 Isomorphism

2
Graph
 A graph G = (V, E) consists of a set of vertices V,
and a set of edges E
Vertex (d)
d 4  Vertices (V)
1 e  |V|: the number of vertices
5
4
7  Edges (E)
c 3  Sometimes referred as arc
a 2
 Connection between a pair
1 f of vertices (v, w), where v
b and w belong to V
Edge (b, e)
 |E|: the number of edges
V = { a, b, c, d, e, f }  Weight may be included
E = { (a,b), (a,c), (a,d), (b,e), (c,d), (c,e), (d,e), (e,f) }

Graph Structure
 Key questions about Graph Structure
 Directed / Undirected Edge?
 Single / Multiple Connection?
 Loop?

4
Graph Structure

Directed/Undirected?
 Undirected Graph a d
 Edges are not directed
c
 If (a,b), then (b,a)

b d
 Directed Graph (Digraph)
 Edges are directed a d
 (a,b) does not mean (b,a)
c

b e

Graph Structure

Single/Multiple Connection?
 Simple Graph a d
 No two edges connects the same
pair of vertices c
 Loop is not allowed
 Multigraph b d
 Two vertices may be connected by
more than one edges a u d
y
 An edge cannot be identified
uniquely by a pair of vertices z c v
x t
 Additional name is needed
 E.g. (c,d) means u or v
b e
w

6
Graph Structure

Loop?
 Multigraph does not allow a u d
y
loop
z c v
x t
 Pseudograph is a special
multigraph allows loop b
w
e

a u d
y
 Sometimes, the meanings
z c v
of Pseudograph and x t
a
Multigraph are the same
b e
w

Graph Structure

Summary

No Loop Loop

Undirected Single Edge Simple Graph /

Pesudograph
Multiple Edge Multigraph
(Multigraph)

No Loop Loop

Simple Directed
Directed Single Edge /
Graph
Directed
Multiple Edge Mixed Graph
Multigraph

8
Adjacent / Neighbor
 Undirected graph  Directed graph
 Let (v1, v2) is an edge  Let (v1, v2) is an edge
 v1 and v2 are endpoints  v1 is initial vertex
 v1 is adjacent to v2  v2 is terminal (end)
 Also mean vertex
“v2 is adjacent to v1”  v1 is adjacent to v2
since (v1, v2) = (v2, v1)  v2 is adjacent from v1
 Do not mean
“v2 is adjacent to v1”

v1 v2 v1 v2

Adjacent / Neighbor
 e incidents with v1 and v2 e
v1 v2
 e connects v1 and v2

 Example:
a & b are adjacent w is adjacent to z
b & a are adjacent z is not adjacent to w
a d w x

c z

b e y v

10
Neighbor Set
 Neighbor Set N(v) contains all adjacent
vertices of v
 For example: N(c) = {a,b,d}
N(z) = {y}

a d w x

c z

b e y v

11

Degree
 Undirected graph  Directed graph
 Degree: number of edges  In-Degree: in-bound edge
containing that vertex number
(Adjacent vertex number)  Out-Degree: out-bound
 Isolated vertex: deg = 0 edge number
 Pendant vertex: deg = 1  E.g. deg-(z) = 2
 E.g. deg(c) = 3 deg+(z) = 1

a d w x

c z

b e y v
12
Degree: Example
 What are the degrees of the following
vertices?
deg(a) = 2 deg-(g) = 2 deg+(g) = 0 a c e

deg(b) = 3 deg-(h) = 0 deg+(h) = 3


b d f
deg(c) = 2 deg-(i) = 1 deg+(i) = 1
deg(d) = 2 deg-(j) = 1 deg+(j) = 1 g i k
deg(e) = 0 deg-(k) = 0 deg+(k) = 0
deg(f) = 1 deg-(l) = 1 deg+(l) = 0 h j l

13

Degree Sequence
 A degree sequence is a monotonic
nonincreasing sequence of the degrees of
vertices in an undirected graph.
2 2 0
a c e (2,3,2,2,0,1) Not monotonic nonincreasing

3 2 1 (3,2,2,2,1,0) Degree sequence


b d f

2 2
a d
3
(3,3,2,2,2) Degree sequence
c
3 2
b e
14
Degree

Handshaking Theorem 1
 For any undirected graph G = (V, E),

 Twice number of edges = sum of degrees


|E| = 5
 Each edge maps to two a c e
deg(a) = 2
vertices (start & end) deg(b) = 3
b d f deg(c) = 2
 It also applies to
deg(d) = 2
multiple edges and loop deg(e) = 0
deg(f) = 1
15

Degree: Handshaking Theorem 1

Example 1
 How many edges are there in a graph with 10
vertices each of degree six?

 Total degree = 10 x 6 = 60
 According to Handshaking Theorem

 |E| = 60/2 = 30

16
Degree: Handshaking Theorem 1

Example 1
 Is there a graph with degree sequence…
 (2,2,2)? Yes  (3,3,3,3)? Yes
a a

d
b c b c

 (2,2,1)? No,  (5,5,4)? Yes


a not even a

??
b c b c
17

Degree

Handshaking Theorem 2
 Undirected graph has an even number of
vertices of odd degree

a c e a a

d
b d f b c
b c
vertices of odd degree vertices of odd degree vertices of odd degree
2 4 2

18
Degree

Handshaking Theorem 2
 Proof
 Let Vo and Ve be the set of vertices of odd and
even degree

even also be even Must be even

 As summation of even degree (2nd term) is even


 Summation of odd degree (1st term) is also even
 As is odd for
 The number of must be even for
19

Degree

Handshaking Theorem 3
 For any directed graph G = (V, E),

 Each edge maps to a c


|E| = 4
one initial and one end deg+(a) = 2 deg-(a) =0
vertices b d deg+(b) = 0 deg-(b) =3
deg+(c) = 1 deg-(c) = 1
 It also applies to deg+(d) = 1 deg-(d) =0
multiple edges and loop
20
Path
 A sequence of vertices v1, v2, …, vn of length
n-1 with an edge from vi to vi+1 for 1 ≤ i < n
 A path is simple if all vertices on the path are
distinct
Vertex a to e
a d a>b>e Length = 2 Simple
a>c>b>e Length = 3 Simple

c Vertex a to b
a>b Length = 1 Simple

b e a>c>b Length = 2 Simple


a>c>b>e>d>c>b Length = 6
Not Simple
21

Cycle (Circuit)
 A path connects vi to itself
 A cycle is simple if the path is simple, except
the first and last vertices are the same

a d a>c>b>a Simple Cycle

b>e>d>c>b Simple Cycle


c
b>e>d>c>b>a>c>b Cycle

b e

22
Acyclic
 A graph without cycle is called acyclic
 A directed graph without cycles is called a
Directed Acyclic Graph (DAG)

a a a
b c b c b c

d e f d e f d e f
Not a DAG Not a DAG
Undirected Acyclic

23

Connectedness
 Vertices v, w are connected if and only if
there is a path starting at v and ending at w
 Every graph consists of separate connected
pieces called connected components

Are a and e connected? Yes


a d
Are a and d connected? No

c How many connected components?


2
b e

24
Connectedness
 Undirected graph  Directed graph
 Connected: if there is at  Weakly connected: Directed
least one path from any graph without considering
vertex to any other directions is connected
(Only one connected  Strongly connected: Directed
component) graph with considering
direction is connected

a d a d a d a d
c c c c
b e b e b e b e
Connected Not Connected Strongly Weakly
Connected Connected

25

Maximal/Minimal graph
 A graph G is said to be a maximal graph
(minimal graph) with respect to a property P if
G has property P and no proper supergraph
(subgraph) of G has the property P
b d b d
a f a f
b d c e c e
a f maximal cycle
c e b d
b d
a f a f
c e c e
minimal connected graph
26
Graph Operation
 Given G1=(V1,E1) and G2=(V2,E2)
 Complement G1
(V1, {uv | u ≠ v, uv E1} )
 Intersection G1 ∩ G2
(V1 ∩ V2 , E1 ∩ E2 )
 Union G1 U G2
(V1 U V2 , E1 U E2 )
 Join G1 + G2 if V1 ∩ V2 =
(V1 U V2 , E1 U E2 U {uv | uV1 and vV2})
 Cartesian Product G1  G2
(V1  V2 , {(u1, u2), (v1, v2) |
(u1 = v1 and {u2, v2}E2) or
(u2 = v2 and {u1, v1}E1)})
27

Graph Operation

Example 1
a b a a b a a b
c d c c c d c
A B A A∩ B AU B
y 1 2

z 3 y 1 2 y1 y2 y3
C D z 3 z1 z2 z3

C+D CD

28
Graph Operation

Example 2
a b a
c d c
b1 b2 b3
A B
b
y 1 2 a1 a2 a3
a 1 2
z 3
c 3 c1 c2 c3
C D
A+D AD

29

Graph Operation

Example 3
a c
e
b d

W4

a c a c

b d b d

K4 C4

30
Graph Operation

Example 4
1 a
b
2 c

K2 K3

1 a
b
2 c

K2 K3
31

Graph Operation

Example 5
1a 1b 1c

2a 2b 1c

K2  K3

1a 1b 1c

2a 2b 1c

K2  K3
32
Type of Graph

Complete Graph
 Complete graph Kn if there is an edge
between every pair of vertices, where n is the
number of vertices
 Complete Undirected  Complete Directed
Graph Graph
a c a c
K4
b d b d

33

Type of Graph

Cycle Graph
 Cycle graph Cn is a circular graph with V =
{0,1,2,…,n-1} where vertex i is connected to
(i+1) mod n and to (i-1) mod n
 like a polygon

a a c
a a b
b c b d

C1 C2 C3 C4

34
Type of Graph

Wheel Graph
 Wheel graph Wn is a cycle graph with an
extra vertex in the middle which contact to
each of other vertices

b a a c
a a c b d e
b c b d

W1 W2 W3 W4

35

Type of Graph

Cube
 n-cube Qn is defined recursively.
 Q0 is just a vertex
 Qn+1 is gotten by taking 2 copies of Qn and
joining each vertex v of Qn with its copy v’
e' g'
e g
a' c'
a a a a' a c
a'
a c
c'

b' d' f' f h h'


a' b b' b d b d
b' d'

Q0 Q1 Q2 Q3 Q4
36
Type of Graph

Bipartite Graph
 A graph is bipartite if all vertices can be
separated into two partitions, (i.e. V = V1  V2
and Ø = V1  V2) so that any two adjacent
vertices are in different partitions
 (V1, V2) is called a bipartition of V of G
a b a b a b
e f e f f e

g h g h d c

c d c d g h

37

Type of Graph

Bipartite Graph: 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
a b a b
e f f e

g h d c

c d g h

38
Type of Graph

Bipartite Graph: Example 1


 Is Cn (Cycle graph) bipartite?
 When n is even, Yes
 All odd vertices are in a color and
all vertices numbers are in another
color
 All vertices are only adjacent to
opposite color
 When n is odd, No (except n = 1)
 Both n and 1 are odd, but nth
vertex is next to the 1st vertex

39

Type of Graph

Bipartite Graph: Example 2


 Is the given graph bipartite?

b d
a f
c e

 NO
 For example, consider a, b, and c. There is
two adjacent vertices are assigned the same
color if only two colors are allowed

40
Type of Graph

Complete Bipartite Graph


 When all possible edges exist in a simple
bipartite graph with m and n vertices in two
partitions, the graph is called complete
bipartite Km,n
b
b a
d
a d f
e
f e i
c
c g
h
K2,4 K4,5
41

Subgraph
 Let G = (V,E) and H = (W,F) be graphs. H is
a subgraph of G, if W  V and F  E
 Subgraph is a graph inside another group
 A subgraph H of G is a proper subgraph of G
if H ≠ G a d
Subgraph c a d Subgraph
a d d
b c
c a
b e c
Not
b e c Subgraph Subgraph Not
Subgraph
b e
42
Subgraph: Example
 How many different Q2 subgraphs does Q3
have?
6
b d
a c
Q3
f h b d
e g a c
f h
a c e g
Q2
b d

43

Tree
 Tree is an undirected, connected and acyclic graph
 n vertices has (n-1) edges
a c a a
b
a b b b c
d
d d e d e f

 Forest is an undirected, disconnected, acyclic graph


 Disjoint collection of trees
a b c a b
d e c d
f g h
e
44
Tree

Theorem 1
 A tree with at least two vertices has at least two leaves
a
 Assume P is a longest path in a tree T b c
 Prove its endpoints are leaves d e f
 Suppose v is not a leaf, g
then v has at least two neighbors, x and y
 One of them (say x) must not in P, otherwise a cycle
 Let P’ be the path that begins at x followed by P
 This is a longer path than P which is contradict to the
assumption

45

Tree

Theorem 2
 A tree on n vertices has n − 1 edges

 For N(1)
 If n = 1, then T has no edges
 Assume N(k) is true
 T with n vertices has exactly n − 1
 Show N(k+1)

46
Tree

Theorem 2
 Show T with n+1 vertices has exactly n − 1

 Since T is a tree, T has at least two leaves


(Theorem 1)
 Let T’ be the graph created by deleting a leaf in T
 Note that T’ is a tree with n vertices, since:
 T’ is connected and acyclic
 T’ has one less vertex than T
 According to N(k), T’ has n − 1 edges
 Since T’ has one less edge than T, T has K edges

47

Tree

Theorem 3
 Let G be a graph with n vertices. Then the
following are equivalent:
1. G is a tree
2. G is a maximal acyclic graph
3. G is a minimal connected graph
4. G is acyclic and it has n − 1 edges
5. G is connected and it has n − 1 edges
6. Between any two distinct vertices of G there
exists a unique path

48
Tree

Spanning Tree
 Spanning Tree in a connected graph G is a sub-
graph H of G that includes all the vertices of G and
is also a tree
b d b d
a f a f
c e c e
b d Not Spanning Tree Spanning Tree
(not all vertices)
a f
c e b d b d
a f a f
c e c e
Not Spanning Tree Spanning Tree
(not a tree)
49

Tree

Minimum Spanning Tree


 Minimum Spanning Trees (MST) is a spanning tree
with the minimal cost to call all the vertices
4 4
b d 9 b d 9
a 5 6 f a 5 6 f
5 c e 2 5 c e 2
4 8 8
b d 9 Not Spanning Tree Spanning Tree
(not all vertices) MST (22)
a 5 6 f
4 4
5 c e 2 b d 9 b d 9
8
a 5 6 f a 5 6 f
5 c e 2 5 c e 2
8 8
Not Spanning Tree Spanning Tree
(not a tree) Not MST (31)
50
Graph Isomorphism
c b
 Is the following graphs
the same as Graph A? a Graph A
d e f

1 3 a 1
d
4 5
c b 2 6
5 2 6 e f 3 4
Yes Yes Yes
Different Labels Different Positions Different Label
and Positions

51

Graph Isomorphism
 Two graphs, A and B, which contain the same
number of graph vertices connected in the
same way are said to be isomorphic, A  B
 Applications
 Checking fingerprint
 Testing molecules

52
Graph Isomorphism
 If G1  G2, do they have
 the same number of vertices? Yes |V| = 6
|E| = 4
 the same number of edges? Yes
2, 2, 2, 1, 1, 0
 the same degree sequence? Yes a b c

 Are G1  G2, if they have d e f

 the same number of vertices? No a b c

 the same number of edges? No d e f

 the same degree sequence? No |V| = 6


|E| = 4
2, 2, 2, 1, 1, 0
53

Graph Isomorphism
 G1 = (V1,E1)  G2 = (V2,E2) if there is a
bijective function f : V1  V2 such that for all
(u, v)  E1:
(u, v)  E1 iff (f (u), f (v))  E2

 It is edge-preserving vertex matching


 If there is an edge in the original graph, there is
an edge after the mapping; vice versa.

54
Graph Isomorphism (u, v)  E1 iff (f (u), f (v))  E2
Example
f (a) = 3 a 1
f (b) = 1 b 2
a b 1 c 3
f (c) = 2
d 4
2 f (d) = 4 one-to-one
onto
c d 3 4
(a,b)  ( f (a), f (b)) = (3,1)
G1 = (V1, E1) G2 = (V2, E2)
(a,c)  ( f (a), f (c)) = (3,2)
V1 = {a, b, c, d} V2 = {1, 2, 3, 4}
E1 = {(a,b), (a,c), E2 = {(1,3), (1,4), (a,d)  ( f (a), f (d)) = (3,4)
(a,d), (b,d), (2,3), (2,4),
(b,d)  ( f (b), f (d)) = (1,4)
(c,d)} (3,4)}
(c,d)  ( f (c), f (d)) = (2,4)

55

Graph Isomorphism

Self-complementary
 A graph G is called self-complementary
if G  G
 C5 and C5 are self-complementary (C5  C5)

a c a c
C4 C4
b d b d

a a
C5 b c C5 b c

d e d e
56
Graph Isomorphism
 Showing Isomorphism is not easy
 No general method which is more efficient than
trying all possibilities

57

Graph Isomorphism
 Showing non-isomorphic is simpler
 Violate any isomorphic-preserving property
 Example: Are they isomorphism? NO
Degree is 2

a b 1

2
c d 3 4
Degree is 2 Degree of all nodes is 3

58
Discrete Mathematic

Chapter 9: Graphs
9.5
Euler and Hamilton
Paths
9.7
Planar Graphs
9.8
Graph Coloring
Yan Huang
School of Computer Science and Engineering
South China University of Technology

Thanks to Dr Patrick Chan for providing the Slides

Agenda
 Euler Path
 Hamilton Path
 Planar Graph
 Coloring

Ch. 9.5, 9.7 & 9.8 2


Euler and Hamiltonian Path
 Euler Path
a path visits every edge exactly once
 Hamiltonian Path
a path visits every vertex exactly once

Ch. 9.5, 9.7 & 9.8 3

Euler Path
 Seven Bridges of Königsberg
 Königsberg is built on both banks of the Preger river
 Now a city in Russia called Kaliningrad
 Is it possible to walk through the city that would
cross each of bridges once

C
A D
B
Ch. 9.5, 9.7 & 9.8 4
Euler Path
 Leonhard Euler, the Swiss mathematician,
was also unable to find such a route
 Euler figured out how to
show for certain that
no such route existed

Ch. 9.5, 9.7 & 9.8 5

Euler Path
 Euler Path: a path visits every edge exactly once
 Euler Cycle: Euler path which starts and stops at
the same vertex
 A connected graph G is called Eulerian if it contains
an Euler path
c b c b c b c b
a a a a
d f d f d f d f
Euler Path YES Euler Path YES Euler Path NO Euler Path YES
Euler Cycle YES Euler Cycle NO Euler Cycle NO Euler Cycle NO
Eulerian YES Eulerian YES Eulerian NO Eulerian YES
Ch. 9.5, 9.7 & 9.8 6
Euler Path
 Observation from an Euler path,
a>c>a>d>e>a>b a>c>b>a>d>f>a
Euler Path Euler Cycle

c b c b
a a
d e d e
Starting / Intermediate deg(a) = 5 (O) deg(a) = 4 (E) Starting / Intermediate / End

End deg(b) = 1 (O) deg(b) = 2 (E) Intermediate

Intermediate deg(c) = 2 (E) deg(c) = 2 (E) Intermediate

Intermediate deg(d) = 2 (E) deg(d) = 2 (E) Intermediate

Intermediate deg(e) = 2 (E) deg(e) = 2 (E) Intermediate

Ch. 9.5, 9.7 & 9.8 7

Euler Path
 Observation from an Euler path,
 Intermediate vertex
 Degree must be even
(Entrance and exist connection)
 Starting and end vertices
 If the same (cycle),
degree are even
 If different (non-cycle),
degrees are odd (in or out)

Ch. 9.5, 9.7 & 9.8 8


Euler Path
 Theorem 1
A connected multigraph with at least two
vertices has an Euler circuit if and only if
each of its vertices has even degree
 Theorem 2
A connected multigraph has Euler path but
not an Euler circuit if and only if it has exactly
two vertices of odd degree

Ch. 9.5, 9.7 & 9.8 9

Euler Path
2 2 2 1 2 1 2 2
c b c b c b c b
4 a 5 a 5 a 5 a

2 d 2 f 2 d 2 f 1 d 1 f 3d 2 f
Euler Path YES Euler Path YES Euler Path NO Euler Path YES
Euler Cycle YES Euler Cycle NO Euler Cycle NO Euler Cycle NO
Eulerian YES Eulerian YES Eulerian NO Eulerian YES

Odd Degree 0 Odd Degree 2 Odd Degree 4 Odd Degree 2


Even Degree 5 Even Degree 3 Even Degree 1 Even Degree 3

Starting/End Starting/End
Vertices must be Vertices must be
a and c a and d

Ch. 9.5, 9.7 & 9.8 10


Euler Path
 How to identify an Euler Path / Cycle?
 Euler Path
Fleury’s Algorithm
 Euler Cycle
Hierholzer’s Algorithm

Ch. 9.5, 9.7 & 9.8 11

Euler Path

Fleury’s Algorithm
 Identify Euler Path
1. If there are 0 odd vertices, start anywhere. If
there are 2 odd vertices, start at one of them
2. Follow edges one at a time. If you have a
choice between a bridge and a non-bridge,
always choose the non-bridge
3. Stop when you run out of edges
 “Don’t burn bridges” so that we can come
back to a vertex and traverse remaining
edges
Ch. 9.5, 9.7 & 9.8 12
Euler Path a

Fleury’s Algorithm
d f
b
c e
The bridge will be burnt
if we choose this! g
a a a a a a a
d f d f d f d f d f d f d f
b b b b b b b
c e c e c e c e c e c e c e
g g g g g g g
burn
a a a a a a a a bridge

d f d f d f d f d f d f d f d f
b b b b b b b b
c e c e c e c e c e c e c e c e
g g g g g g g g

Ch. 9.5, 9.7 & 9.8 13

Euler Path

Hierholzer’s Algorithm
 Identify Euler Cycle
1. Select a node v as a starting node
2. Form a cycle using non-traveled edges and end
at v (remove the visited edges)
3. While all edges have been traversed, stop
a) Find a node u on the previous cycles that’s
connected to a non-traveled edge
b) Form a cycle using non-traveled edges and
end at u (remove the visited edges)
c) Merge both tours at the node u

Ch. 9.5, 9.7 & 9.8 14


Euler Path 1. Select a node v as a starting node

Hierholzer’s Algorithm
2. Form a cycle using non-traveled edges and end at v
(remove the visited edges)
3. While all edges have been traversed, stop
a) Find a node v on the previous cycles that’s connected
to a non-traveled edge
a b) Form a cycle using non-traveled edges and end at v
(remove the visited edges)
c) Merge both tours at the node v
d f
a > d> f > a
b d > b> e> c > d
c e f > b> c > g> e> f
g
a >d >f >a

d >b >e >c >d f >b >c >g >e >f

a > d >b >e >c >d > f >b >c >g >e >f >a
Ch. 9.5, 9.7 & 9.8 15

Euler Path

Example 1
 Is it possible to begin in a room or the outside
and take a walk that goes through each door
exactly once? If yes, how?

Ch. 9.5, 9.7 & 9.8 16


Euler Path

Example 1
4 4
d
A B
a 7 b
c e
D
f g
h i j

C
5

A B A B A B

D D D

C C C

Ch. 9.5, 9.7 & 9.8 17

Euler Path

Example 2
 Is it possible to walk through and around this
building passing through each and every
doorway exactly once?

Ch. 9.5, 9.7 & 9.8 18


Euler Path

Example 2
3
OUT

4 5 4
EW ME EE

2 CW CE
2

2 12 2
WW GH EW

C1 C2 C3 C4
2 4 4 2

Ch. 9.5, 9.7 & 9.8 19

Euler Path

Example 2
OUT OUT

EW ME EE EW ME EE

CW CE CW CE

WW GH EW WW GH EW

3
C1 C2 C3 C4 C1 C2 C3 C4

OUT

4 5 4 OUT OUT

EW ME EE
EW ME EE EW ME EE

2 CW CE 2 CW CE CW CE

WW GH EW WW GH EW

2 WW GH
12 EW
2
C1 C2 C3 C4 C1 C2 C3 C4

C1 C2 C3 C4 OUT OUT

2 4 4 2 EW ME EE EW ME EE

CW CE CW CE

WW GH EW WW GH EW

C1 C2 C3 C4 C1 C2 C3 C4

Ch. 9.5, 9.7 & 9.8 20


Hamiltonian Path
 Icosian game
 Invented by an Irishman named Sir William
Rowan Hamilton (1805-1865)
 Is there a cycle in the dodecahedron puzzle
that passes through each vertex exactly once?

Dodecahedron puzzle
Ch. 9.5, 9.7 & 9.8 21

Hamilton Path
 Hamilton Path: a path visits every vertex exactly
once
 Hamilton Cycle: Hamilton path which starts and
stops at the same vertex
 Self-loop and multiple edges can be ignored
b b c c b a b
c a
a d c d
e d
d e d f e f
Ham. Path YES Ham. Path YES Ham. Path NO Ham. Path YES
Ham. Cycle YES Ham. Cycle YES Ham. Cycle NO Ham. Cycle NO

Ch. 9.5, 9.7 & 9.8 22


Hamilton Path

Dirac’s Theorem
 Theorem: If each vertex of a simple graph with n
vertices and n  3 has degree  n/2, there is
Hamilton circuit

2 3 3 1 3 1 2
2 b 2 b c c b a b
c a 4 2 1 3
2 a d c d
2
e d 3 3 1 2 2 3
d e d f e f
Ham. Path YES Ham. Path YES Ham. Path NO Ham. Path YES
Ham. Cycle YES Ham. Cycle YES Ham. Cycle NO Ham. Cycle NO

Vertex # (n) 5 Vertex # (n) 5 Vertex # (n) 5 Vertex # (n) 6


All Deg  n/2? NO All Deg  n/2? YES All Deg  n/2? NO All Deg  n/2? NO

Ch. 9.5, 9.7 & 9.8 23

Hamilton Path

Ore’s Theorem
 Theorem: If every pair of non-adjacent vertices u
and v in a simple graph with n vertices and n  3
has deg(u)+deg(v)  n, there is a Hamilton circuit

2 3 3 1 3 1 2
2 b 2 b c c b a b
c a 4 2 1 3
2 a d c d
2
e d 3 3 1 2 2 3
d e d f e f
Ham. Path YES Ham. Path YES Ham. Path NO Ham. Path YES
Ham. Cycle YES Ham. Cycle YES Ham. Cycle NO Ham. Cycle NO

All deg(u)+deg(v)  n All deg(u)+deg(v)  n All deg(u)+deg(v)  n All deg(u)+deg(v)  n


NO YES NO NO

Ch. 9.5, 9.7 & 9.8 24


Hamilton Path

Dirac’s and Ore’s Theorem


 Be noted Dirac’s and Ore’s Theorem is a
sufficient condition but not necessary one
 A graph with a vertex degree < n/2 may have a
Hamilton circuit
 A graph with a pair of non-adjacent vertices
deg(u)+deg(v) < n may have a Hamilton circuit

Ch. 9.5, 9.7 & 9.8 25

Hamilton Path
 Unfortunately, no good algorithm to find the
Hamilton path or cycle
 Just “trial and error” (and good luck!)

Ch. 9.5, 9.7 & 9.8 26


Euler Path VS Hamilton Path
 Euler Path  Hamilton Path
 a path uses every  a path uses every
edge exactly once vertex exactly once
 Euler Cycle  Hamilton Cycle
 Euler path which  Hamilton path which
starts and stops at starts and stops at
the same vertex the same vertex

Ch. 9.5, 9.7 & 9.8 27

Planar Graph
 Planar Graph is a graph can be drawn in the plane
without edges crossing
 A planar graph drawn in the plane without edges
crossing is called Plane Graph
 Plane graph is also called a planar representation
Isomorphism
b d a c
b d b d
a b c
a c
f h f h
f h
e g e g d e f
Planar Graph  Planar Graph  Planar Graph  Planar Graph 
Plane Graph
 Plane Graph
 Plane Graph
 Plane Graph

Ch. 9.5, 9.7 & 9.8 28
Planar Graph
 A graph that is drawn in the plane is also said to be
embedded (or imbedded) in the plane
 A planar graph can generate different plane graphs
 Application: Circuit Layout Problems

a d a d a d

b e b e b e
Embedding
c f c f c f

Not Plane Graph Plane Graph Plane Graph


Planar Graph Planar Graph Planar Graph

Ch. 9.5, 9.7 & 9.8 29

Planar Graph: Region


 A plane graph splits the plane into regions
 Including the unbounded (exterior) region

(unbounded)
a R3 a (unbounded)

R1 R1 (unbounded) a R4 e
b e b e R1
b e b R3
R2
R2
c d c d c d a

r=3 r=1 r=4

Ch. 9.5, 9.7 & 9.8 30


Planar Graph: Region
 The vertices and edges of G that are incident
with a region R form a subgraph of G called
the boundary of R
Boundary of R1 Boundary of R3
a e
a R4 e
b e b
R1
b e b R3
R2 c a
c d a
Boundary of R2 Boundary of R4
a e
e
b e b
c d
c d a
Ch. 9.5, 9.7 & 9.8 31

Planar Graph: Region


 Observation on boundary
 Cycle edge belongs to the boundary of two
regions
 Bridge is on the boundary of only one region
(unbounded region)
a > b > c > e > a is a cycle
a R4 e (a,b), (b,c), (a,e) belongs to R1 and R4
b R1 e b R3 (c,e) belongs to R1 and R2
R2
c d a (e,b) is not a cycle, just a bridge
(e,b) belongs to R4 only

Ch. 9.5, 9.7 & 9.8 32


Planar Graph a b c
 Is K3,3 a planar graph? Not planar
d e f
1) Focus on a, e, d, b 2) c connect to d, e, f 3) f connects to a, b, c

a e f in R11: cross when connect to b


C in R1 R11
c R2 f in R12: cross when connect to a
a e R12 f in R2: cross when connect to c
d b
R1 R2 a e f in R1: cross when connect to c
d b R22
R1 f in R21: cross when connect to a
C in R2 R21 f in R22: cross when connect to b
d b
c
Ch. 9.5, 9.7 & 9.8 33

Planar Graph

Euler’s Formula
 If G be a connected planar simple graph with
e edges, v vertices, and r regions, then
r=e–v+2

 MI is used in the proof

Ch. 9.5, 9.7 & 9.8 34


Planar Graph r=e–v+2
Euler’s Formula: Proof
 For a connected planar graph G
 Let a sequence of subgraphs G1, G2, …Gi, …,
Ge of G, and Ge = G,
 G1  G2  …  G e
 Gi contains i edges
 Gn is obtained from Gn-1 by arbitrarily adding an
edge
 Be noted that all Gi are planar (as subgraph of
planar graph must be planar)

Ch. 9.5, 9.7 & 9.8 35

Planar Graph r=e–v+2


Euler’s Formula: Proof
 For G1,
 e1 = 1
a1 b1
 v1 = 2
 r1 = 1
 Therefore, r1 = e1 – v1 + 2

 Assume rn = en – vn + 2 is true

Ch. 9.5, 9.7 & 9.8 36


Planar Graph r=e–v+2
Euler’s Formula: Proof
 Let {an+1, bn+1} be the edge that is added to
Gn to obtain Gn+1
 Case 1: an+1, bn+1 are in Gn
 Case 2: one of an+1 and bn+1 is not in Gn
Case 1 Case 2

a a a
Add {an+1, bn+1}
b

b b

Gn Gn+1
Ch. 9.5, 9.7 & 9.8 37

Planar Graph r=e–v+2


Euler’s Formula: Proof
 Case 1: an+1 and bn+1 are in Gn
 en+1 = en + 1 , and vn+1 = vn
 If an+1 and bn+1 are not on the boundary an+1 and bn+1 are
not on the
of a common region R, two edges cross. boundary of a
This violates Gn+1 is planar common region R

 Therefore, an+1 and bn+1 must be an+1 and bn+1 are


on the boundary
on the boundary of a common region R of a common
region R
 The new edge splits R into two regions
 rn+1 = rn + 1
a
 Given rn = en – vn + 2
(rn+1 – 1) = (en+1 – 1) – (vn+1) + 2
b
rn+1 = en+1 – vn+1 + 2

Ch. 9.5, 9.7 & 9.8 38


Planar Graph r=e–v+2
Euler’s Formula: Proof
 Case 2: one of an+1 and bn+1 is not in Gn
 en+1 = en + 1
 vn+1 = vn + 1
 No new region is generated, rn+1 = rn
 Given rn = en – vn + 2
(rn+1) = (en+1 – 1) – (vn+1 – 1) + 2
rn+1 = en+1 – vn+1 + 2
a
b

Ch. 9.5, 9.7 & 9.8 39

Planar Graph

Euler’s Formula: Example


 Suppose that a connected planar simple
graph has 20 vertices, each of degree 3. Into
how many regions does a representation of
this planar graph slit the plane?

 v = 20
 Sum of degree = 20 x 3 = 60 = 2e
 e = 30
 r = e – v + 2 = 30 – 20 + 2 = 12
Ch. 9.5, 9.7 & 9.8 40
Planar Graph

Euler’s Formula: Corollary


 If a connected planar simple graph, then G has a
vertex of degree not exceeding 5.

 If a connected planar simple graph has e edges and


v vertices with v ≥ 3, then e ≤ 3v – 6

 If a connected planar simple graph has e edges and


v vertices with v ≥ 3 and no circuits of length three,
then e ≤ 2v – 4

Ch. 9.5, 9.7 & 9.8 41

Planar Graph

Euler’s Formula: Example 1


 Show that K5 is nonplanar e
a d

b c
 K5 has circuit of length three, 5 vertices and
10 edges
 As e = 10 and 3v – 6 = 9, e ≤ 3v – 6 is false
 Therefore, K5 is nonplanar
If a connected planar simple graph has e edges
and v vertices with v ≥ 3, then e ≤ 3v – 6

Ch. 9.5, 9.7 & 9.8 42


Planar Graph

Euler’s Formula: Example 2


 Show that K3,3 is nonplanar a b c

d e f
 K3,3 has no circuit of length three, 6 vertices
and 9 edges
 As e = 9 and 2v – 4 = 8, e ≤ 2v – 4 is false
 Therefore, K3,3 is nonplanar
If a connected planar simple graph has e edges and
v vertices with v ≥ 3 and no circuits of length three,
then e ≤ 2v – 4
Ch. 9.5, 9.7 & 9.8 43

Planar Graph

Homeomorphic
 The graphs are called homeomorphic if they
can be obtained from the same graph by a
sequence of elementary subdivision
 If a graph is planar, it will be any graph obtained
by removing an edge {u,v} and adding a new
vertex w with edges {u,w} and {w,v}

Obtain G from H
a d a d
f Remove {a, b}, Add {a, e}, {e, b}
e g Remove {a, c}, Add {a, f}, {f, c}
b c b c Remove {f, c}, Add {f, c}, {c, g}
G H
Ch. 9.5, 9.7 & 9.8 44
Planar Graph

Kuratowski’s Theorem
 A graph is not planar if it contains a non-
planar subgraph
 Kuratowski’s Theorem
A graph is nonplanar iif it contains a
subgraph homeomorphic to K3,3 or K5
 Proof is neglected
e
a b c
a d

b c d e f
Ch. 9.5, 9.7 & 9.8 45

Planar Graph: Kuratowski’s Theorem

Example
H and K5 are homeomorphic
 Determine whether the H can be obtained from K5
following graph is planar by removing {g,e} and
adding {g,f} and {f,e}
j i
g g g
k f h f
a e a e a e
d
b c b c b c
G HG H K5
 As G contains a subgraph (H) homeomorphic to
K5, it is not planar
Ch. 9.5, 9.7 & 9.8 46
Coloring
 Two regions sharing a border
are assigned different colors
 Represent a map by
a graph (called Dual Graph)
 Vertex: Region c
 Edge: Constraint a d
 the color cannot be the
same for adjacent regions b

Ch. 9.5, 9.7 & 9.8 47

Map Coloring
 What is the largest complete graph
represented by a map?
Planar Planar Planar Planar nonPlanar

a c a c a
a a c b e
b b d c d

Ch. 9.5, 9.7 & 9.8 48


Coloring
 Graph Coloring Problem
Given a graph, assign all the vertices with the
minimum number of colors so that no two
adjacent vertices gets the same color

b c

d a
4 3 2 1
max min

Ch. 9.5, 9.7 & 9.8 49

Coloring
 Chromatic number ( (G) )
The smallest number of colors needed to
produce a proper coloring of G

Ch. 9.5, 9.7 & 9.8 50


Coloring: Example
 Cycle Graph (C)  Wheel Graph (W)
a b a a b a
b e e b e
f
c d c d c d c d

(Ceven)= 2 (Codd)=3 (Weven)= 3 (Wodd)= 4

Ch. 9.5, 9.7 & 9.8 51

Coloring: Example
 Complete Graph (K)  Tree (T)
a
a
a b b c d
b e
e f g h i
c d c d j k l m
n
(T)= 2

(Keven)= n (Kodd)= n

Ch. 9.5, 9.7 & 9.8 52


Coloring: Example
 Bipartite Graph
 Recall… a graph is bipartite if all vertices can
be partitioned into two partitions, so that any
two adjacent vertices are in different partitions
 Obviously,  = 2

a b a b a b a b
e f f e f e e f
g h d c d c g h
c d g h g h c d

Ch. 9.5, 9.7 & 9.8 53

Coloring
 No formula for Chromatic number 
 Discussion
 Given a graph of size k
  > k: not possible
  = k: for a complete graph
  < k: other graphs except the complete one
 Analyzing a subgraph of a graph may be helpful
 If a subgraph is complete of size k,  ≥ k

=4 =5 =4 =4


Ch. 9.5, 9.7 & 9.8 54
Coloring: Application 1
T1 T2 T3 T4 T5 T6

 A flight need a gate in an airport FA


FB
 How many gates needed for this
FC
flight schedule? 3
FD
Vertex: Flight FE
Edge: Share the same time slot FF

C
Bipartite
C C
B D E E
B B
A E A D A D

F F F
Ch. 9.5, 9.7 & 9.8 55

Coloring: Application 2
 Examination of subject S1 S2 S3 S4

conflicts if student takes AI


C++
both subjects DisMaths

 How many different time slots? Java

Vertex: Course
3
Edge: a student take
the two courses

A C A C A C

D J D J D J
Bipartite

Ch. 9.5, 9.7 & 9.8 56


Coloring: Application 3
 Suppose an university offers seven courses. Students can
take more than one course.
 Pairings of courses: 1
 Course 1 : 2, 3, 4, 7
 Course 1 has a student 7 2
in common with courses 2, 3, 4, 7
 Course 2 : 3, 4, 5, 7
6 3
 Course 3 : 4, 6, 7
 Course 4 : 5, 6 5 4
 Course 5 : 6, 7
 Course 6 : 7
 Find the fewest number of final exam slots that are needed
to avoid any conflicts

Ch. 9.5, 9.7 & 9.8 57

Coloring: Application 3
1 1

7 2 7 2

6 3 6 3

5 4 5 4
1
K4
1 1
7 2
7 2 7 2

6 3
6 3 6 3

5 4 5 4 5 4

Bipartite 1

Answer is 4 7 2
7 is not
connected to 4 6 3

5 4
Ch. 9.5, 9.7 & 9.8 58

You might also like