0% found this document useful (0 votes)
3 views695 pages

Discrete Structures

The document outlines a course on Discrete Structures for Computing, detailing its description, course outline, and learning outcomes. It covers various topics including logic, set theory, graph theory, and applications in computer science. The course aims to provide a mathematical foundation for computational science and includes practical exercises and evaluations.

Uploaded by

khuong.a.do
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)
3 views695 pages

Discrete Structures

The document outlines a course on Discrete Structures for Computing, detailing its description, course outline, and learning outcomes. It covers various topics including logic, set theory, graph theory, and applications in computer science. The course aims to provide a mathematical foundation for computational science and includes practical exercises and evaluations.

Uploaded by

khuong.a.do
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/ 695

Introduction

Huynh Tuong Nguyen,


Nguyen Ngoc Le,

Chapter 0 Nguyen Tien Thinh

Introduction
Discrete Structures for Computing on January 5, 2022
Course description
Course outline
Document

Some applications

Huynh Tuong Nguyen, Nguyen Ngoc Le, Nguyen Tien Thinh


Faculty of Computer Science and Engineering
University of Technology - VNUHCM
[email protected]
0.1
Introduction

Huynh Tuong Nguyen,


Nguyen Ngoc Le,
Nguyen Tien Thinh

1 Course description
Course outline Course description

Document Course outline


Document

Some applications

2 Some applications

0.2
Introduction
Context
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Nguyen Tien Thinh

Global
• 12 principal chapters on 45 hours for courses & exercises.
• 10 Homeworks (0%), 1 Assignment (20%) Course description
Course outline
• 2 evaluations: mid-exam (MCQ - 90 minutes - 30%) + final Document

exam (MCQ + writing - 90 minutes - 50%) Some applications

Aims
The content of this subject is mainly a great part of logic, set
theory and graph theory.
This is the mathematical base for many topics of Computational
Science

0.3
Introduction
Subjects in general discrete mathematics course
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Nguyen Tien Thinh

+ Logic
+ Set theory
+ Number theory Course description
Course outline
+ Combinatorics: enumerative combinatorics, graph theory Document

Some applications
+ Algorithmics
+ Information theory
+ Complexity theory
+ Probability theory
+ Proof
+ Counting and Relations

0.4
Introduction
Topics relational to discrete mathematics
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Nguyen Tien Thinh

1 Theoretical computer science


2 Information theory
3 Logic
4 Set theory
5 Combinatorics
Course description
6 Graph theory Course outline
Document
7 Probability
Some applications
8 Number theory
9 Algebra
10 Calculus of finite differences, discrete calculus or discrete analysis
11 Geometry
12 Topology
13 Operations research: scheduling
14 Game theory, decision theory, utility theory, social choice theory
15 Discretization
16 Discrete analogues of continuous mathematics
17 . . .

0.5
Introduction
Context
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Nguyen Tien Thinh

Course outline
• Proof methods
• modular arithmetic over integers.
• induction, contradiction.
Course description
• Set theory Course outline
Document
• relations, functions, cardinalities, relation, equivalence equation, partial order
• combinatorics: counting, principles of sum, multiplication, division, inclusion Some applications

and exclusion.
• Graph theory
• directed, undirected, isomorphism
• weighted graphs, algorithm for finding shortest paths
• trees: features, binary trees, minimum spanning trees in connected and
weighted graphs
• flows network

• Probabilistics Modelling
• introductory random variables.

0.6
Introduction
Course learning outcomes
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Nguyen Tien Thinh

L.O.1 Understanding of logic and discrete structures


L.O.1.1 Describe definition of propositional and predicate logic
L.O.1.2 Define basic discrete structures: set, mapping, graphs, ..

L.O.2 Modeling and formulating generic problems in real life by Course description
the discrete mathematical structures and languages Course outline

L.O.2.1 Logically describe some problems arising in Computing Document

L.O.2.2 Use proving methods: direct, contrapositive, induction Some applications

L.O.2.3 Explain problem modeling using discrete structures

L.O.3 Applying learnt knowledge to compute quantities of


discrete structures and probabilities
L.O.3.1 Operate/ compute on some basic problems which were
modeled by discrete structures (set, graph, tree,..)
L.O.3.2 Calculate optimally based for solving basic problems in graph
theory (shortest path, minimum spanning tree,..)

0.7
Introduction
Document
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Nguyen Tien Thinh

Book Course description


Course outline
• Discrete mathematics and applications - Kenneth H. Rosen. (Vietnamese Document

translation - NXB KHKT 1997) Some applications


• Discrete mathematics - Richard Johnsonbaugh, Willey, 1997
• Discrete mathematics with algorithms - Micheal O. Albertson & Joan P.
Hutchinson, Willey, 1998

0.8
Introduction
Application
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Nguyen Tien Thinh

• it concerns a wide range of disciplines in various areas: science, technology,


business and commerce.
• applied mathematicians are engaged in the creation, study and application of
advanced mathematical methods relevant to specific problems.
• applied mathematics has assumed a much broader meaning and embraces such Course description
diverse fields as communication theory, optimization, game theory and numerical Course outline
analysis. Document

• today there is a remarkable variety of applications of mathematics in industry and Some applications
government, such as materials processing, design, medical diagnosis, development
of financial products, network management, weather prediction, etc.

Science Engineering

Engineers use technology, mathematics and Technology


scientific knowledge to solve practical
problems. (wikipedia.org)

0.9
Introduction
Computing of algorithm complexity
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Nguyen Tien Thinh

Know results
Course description
Size Approximating of computational time
Course outline
n O(log n) O(n) O(n log n) O(n2 ) O(2n ) O(n!) Document
10 3.10−9 s −8
10 s −8
3.10 s −7
10 s 10−6 s 3.10−3 s Some applications
102 7.10−9 s 10−7 s 7.10−7 s 10−5 s 4.1013 y *
103 10−8 s 10−6 s 10−5 s 10−3 s * *
104 1, 3.10−8 s 10−5 s 10−4 s 10−1 s * *
105 1, 7.10−8 s 10−4 s 2.10−3 s 10s * *
106 2.10−8 s 10−3 s 2.10−2 s 17m * *

0.10
Introduction
Mathematical model
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Nguyen Tien Thinh

Solver
• Simplex, GLPK
• CPLEX, MPL
Course description
• Excel, Mathlab, etc. Course outline
Document

Some applications

0.11
Introduction
Mathematical model
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Nguyen Tien Thinh

Exercise - Carpenter’s decision

• A carpenter makes tables and chairs.


• Each table can be sold for a profit of $30 and each chair for a
profit of $10. Course description

• The carpenter can afford to spend up to 40 hours per week Course outline
Document

working. Some applications

• He takes 7 hours to make a table and 3 hours to make a chair.


• It requires that he makes at least 3 times as many chairs as
tables.
• Tables take up 4 times as much storage space as chairs and
there is room for at most 4 tables each week.
Question.
How many tables and chairs could be produced to maximize
weekly profit?

0.12
Introduction
Mathematical model
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Nguyen Tien Thinh

Exercise - Bookseller’s decision


• A bookseller A buys books from two publishers B, and C.
• Publisher B offers a package of 6 mysteries and 8 romance
novels for $40. Course description

• Publisher C offers a package of 5 mysteries and 10 romance Course outline


Document

novels for $100. Some applications

• The bookseller A wants to buy at least 2.000 mysteries and


3.000 romance novels,
• A has promised C (who has influence on the Senate Textbook
Committee) that at least 30% of the total number of books
he purchases will come from publisher C.
Question.
How many packages should A order from each publisher in order
to minimize his cost and satisfy C ?

0.14
Introduction
Graph
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Nguyen Tien Thinh

• Shortest path problem


• Min cut and maximum flow
• Vehicle Routing Problem

Course description
Course outline
Document

Some applications

0.16
Introduction
Scheduling
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Nguyen Tien Thinh

Course description
Course outline
Document

Some applications

0.17
Introduction
Scheduling
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Nguyen Tien Thinh

Exercise
Problem 1||Tmax .
Given 8 jobs with processing times and due dates as follows: Course description
Course outline
Job J1 J2 J3 J4 J5 J6 J7 J8 Document

Some applications
pi 1 2 2 3 3 4 4 3

di 25 16 19 7 18 22 27 8
Let Ci be completion time of job Ji and let Ti = max(0, Ci − di ) its
tardiness.
Question. How to minimize Tmax = maxi Ti ? What is the minimum value of
Tmax ?

0.18
Introduction
Timetabling
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Nguyen Tien Thinh

Example
In the bipartite graph below, the vertices P1 , . . . , P6 represent workers and
edges J1 , . . . , J6 of jobs. An edge connects a worker with a job if the worker
has the necessary qualifications to occupy this job. Here, all the edges have an
unit weight 1, mean that Pi has the skill(competence) to operate Jj if there is
an edge between Pi and Jj . Course description
Course outline
Document
P1 P2 P3 P4 P5 P6
Some applications

J1 J2 J3 J4 J5 J6

0.19
Introduction
Game and simulation
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Sally Salon Game Nguyen Tien Thinh

Course description
Course outline
Document

Some applications

0.20
Introduction
Probabilistics Modelling
Huynh Tuong Nguyen,
Nguyen Ngoc Le,
Nguyen Tien Thinh

Calculating of Pi
Course description
Using a Monte-Carlo method to determine an approximate value Course outline
Document
of π : Some applications
randomly draw a great number of points in a square of side 2, and
determine the ratio C/N where N is the total number of points,
and C the number of points whose distance to the center of the
square is ≤ 1).

0.21
Logics

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

Chapter 1 Ngoc Le

Logics
Discrete Structures for Computing
Contents

Propositional Logic

Logical Equivalences

Exercise

Huynh Tuong Nguyen, Tran Tuan Anh, Nguyen Ngoc Le


Faculty of Computer Science and Engineering
University of Technology - VNUHCM
{htnguyen;trtanh}@hcmut.edu.vn
1.1
Logics
Contents
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

1 Propositional Logic
Contents

Propositional Logic

Logical Equivalences

Exercise
2 Logical Equivalences

3 Exercise

1.2
Logics
Course outcomes
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Course learning outcomes

L.O.1 Understanding of logic and discrete structures


L.O.1.1 – Describe definition of propositional and predicate logic
L.O.1.2 – Define basic discrete structures: set, mapping, graphs
Contents
L.O.2 Represent and model practical problems with discrete structures Propositional Logic
L.O.2.1 – Logically describe some problems arising in Computing Logical Equivalences
L.O.2.2 – Use proving methods: direct, contrapositive, induction
Exercise
L.O.2.3 – Explain problem modeling using discrete structures

L.O.3 Understanding of basic probability and random variables


L.O.3.1 – Define basic probability theory
L.O.3.2 – Explain discrete random variables

L.O.4 Compute quantities of discrete structures and probabilities


L.O.4.1 – Operate (compute/ optimize) on discrete structures
L.O.4.2 – Compute probabilities of various events, conditional
ones, Bayes theorem

1.3
Logics
Logic
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition (Averroes)
The tool for distinguishing between the true and the false.
Contents

Propositional Logic
Definition (Penguin Encyclopedia) Logical Equivalences

The formal systematic study of the principles of valid inference Exercise

and correct reasoning.

Definition (Discrete Mathematics - Rosen)


Rules of logic are used to distinguish between valid and invalid
mathematical arguments.

1.4
Logics
Applications in Computer Science
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• Design of computer circuits


Contents
• Construction of computer programs Propositional Logic

• Verification of the correctness of programs Logical Equivalences

Exercise
• Constructing proofs automatically
• Artificial intelligence
• Many more...

1.5
Logics
Propositional Logic
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
A proposition is a declarative sentence that is either true or false,
Contents
but not both.
Propositional Logic

Logical Equivalences
Examples
Exercise

• Hanoi is the capital of Viet Nam.


• New York City is the capital of USA.
• 1+1=2
• 2+2=3

1.6
Logics
Examples
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Examples (Which of these are propositions?)

• How easy is logic!


Contents
• Read this carefully. Propositional Logic

• H1 building is in Ho Chi Minh City. Logical Equivalences

Exercise
• 4>2
• 2n ≥ 100
• The sun circles the earth.
• Today is Thursday.
• Proposition only when the time is specified

1.7
Logics
Notations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Propositional Logic
• Propositions are denoted by p, q, . . .
Logical Equivalences
• The truth value (”chân trị”) is true (T) or false (F) Exercise

1.8
Logics
Operators
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Negation - ”Phủ định”: ¬p


Contents

Propositional Logic

Bảng: Truth Table for Negation Logical Equivalences

Exercise

p ¬p
T F
F T

1.9
Logics
Operators
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Conjunction - ”Hội”: p ∧ q Disjunction - ”Tuyển”: p ∨ q


“p and q” “p or q”
Contents

Propositional Logic
p q p∧q p q p∨q Logical Equivalences

T T T T T T Exercise

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

I’m teaching DM1 and it is We need students who have


raining today. experience in Java or C++.
Tomorrow, I will eat Pho or Bun
bo.

1.10
Logics
Operators
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Exclusive OR - Tuyển loại: p ⊕ q Implication - Kéo theo: p → q


“p or q (but not both)” “if p, then q” Contents

Propositional Logic

Logical Equivalences
p q p⊕q p q p→q
Exercise

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

If it rains, the pavement will be


wet.

1.11
Logics
More Expressions for Implication p → q
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• if p, then q
• p implies q
Contents
• p is sufficient for q Propositional Logic

• q if p Logical Equivalences

Exercise
• p only if q
• q unless ¬p
• If you get 100% on the final, you will get 10 grade.
• If you feel asleep this afternoon, then 2 + 3 = 5.

1.12
Logics
Conditional Statements From p → q
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents
• q → p (converse - đảo) Propositional Logic

• ¬q → ¬p (contrapositive - phản đảo) Logical Equivalences

• Prove that only contrapositive have the same truth table with Exercise

p→q

phan dao có Truth Table nhu thuan

1.13
Logics

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

Exercise
What are the converse and contrapositive of the following
conditional statement Contents

“If he plays online games too much, his girlfriend leaves him.” Propositional Logic

Logical Equivalences

• Converse: If his girlfriend leaves him, then he plays online Exercise

games too much.


• Contrapositive: If his girlfriend does not leave him, then he
does not play online games too much.

1.14
Logics
Biconditionals
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

p↔q
“p if and only if q”

Contents
p q p↔q
Propositional Logic

T T T Logical Equivalences

T F F Exercise

F T F
F F T

• “p is necessary and sufficient for q”. p->q thi p la du


• “if p then q, and conversely”. q->p thi p la dk can
• “p iff q”.

1.15
Logics
Translating Natural Sentences
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Exercise
I will buy a new phone only if I have enough money to buy iPhone Contents

4 or my phone is not working. Propositional Logic

Logical Equivalences

• p: I will buy a new phone Exercise

• q: I have enough money to buy iPhone 4


• r: My phone is working

• p → (q ∨ ¬r)

1.16
Logics
Translating Natural Sentences
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Exercise not A B C Propositional Logic

Logical Equivalences
He will not run the red light if he sees the police unless he is too Exercise
risky.

not C -> (B-> not A)


(B and not C) -> not A

1.17
Logics
Construct Truth Table
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Exercise
Construct the truth table of the compound proposition
(p ∨ ¬q) → (p ∧ q). Contents

Propositional Logic

Logical Equivalences

Exercise
p q ¬q p ∨ ¬q p∧q (p ∨ ¬q) → (p ∧ q)
T T F T T T
T F T T F F
F T F F F T
F F T T F F

1.18
Logics
Exercise - Truth table
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

¬p → (¬q ∨ r)

p q r ¬p ¬q ¬q ∨ r ¬p → (¬q ∨ r)
T T T F F T T
T T F F F F F
T F T F T T T
T F F F T T T Contents

F T T T F T T Propositional Logic
F T F T F F F Logical Equivalences
F F T T T T T
Exercise
F F F T T T T

a) (p ∧ q) → ¬q
b) (p ∨ r) → (r ∨ ¬p)
c) (p → q) ∨ (q → p)
d) (p ∨ ¬q) ∧ (¬p ∨ q)
e) (p → ¬q) ∨ (q → ¬p)
f) ¬(¬p ∧ ¬q)
g) (p ∨ q) → (p ⊕ q)
h) (p ∧ q) ∨ (r ⊕ q)

1.19
Logics
Applications
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• System specifications Contents

• “When a user clicked on Help button, a pop-up will be shown Propositional Logic

up” Logical Equivalences

• Boolean search Exercise

• type “dai hoc bach khoa” in Google


• means “dai AND hoc AND bach AND khoa”

1.20
Logics
Applications (cont.)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• Logic puzzles
• There are two kinds of inhabitants on an island, knights, who Contents

Propositional Logic
always tell the truth, and their opposites, knaves, who may
Logical Equivalences
lie. You encounter two people A and B. What are A and B if
Exercise
A says “B is a knight” and B says ”The two of us are
opposite types”?
• Bit operations
• 101010011 is a bit string of length nine.

Q is true => P is true => B is Knight and A is knaves


Q is false => B is knaves & A knaves
cach 2 : lap bang tran tri

1.21
Logics
Tautology and Contradiction
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
A compound proposition that is always true (false) is called a
tautology - hằng đúng (contradiction - hằng sai). Contents

• Tautology: hằng đúng Propositional Logic

Logical Equivalences
• Contradiction: mâu thuẫn
Exercise

Example

• p ∨ ¬p (tautology)
• p ∧ ¬p (contradiction)

1.22
Logics
Question
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Hãy chỉ ra hằng đúng trong các dạng mệnh đề sau:


Gợi ý: dùng bảng chân trị.
Contents
a) (p ∨ q) → (p ∧ q) Propositional Logic

b) (p ∧ q) → (p ∨ q) Logical Equivalences

Exercise
c) p → (¬q → p)
d) p → (p → q)
e) p → (p → p)
f) (p → q) → [(p → r) → (q → r)]

1.23
Logics
Proposition? Truth value?
Huynh Tuong Nguyen,
a) “Phan-xi-păng là ngọn núi cao nhất Việt Nam.” Tran Tuan Anh, Nguyen
Ngoc Le
b) “Hai số nguyên tố cùng nhau chỉ có ước chung là 1.”
c) “Tích 3 số nguyên liên tiếp thì chia hết cho 3.”
d) “Hãy đứng lên!”
e) “x+1=0”
f) “Hình lập phương có 8 đỉnh.”
g) “0 là một số dương.”
Contents
h) “Phương trình: x2 + 5x + 6 = 0 vô nghiệm.”
Propositional Logic
i) “2 có phải là số nguyên tố không?”
Logical Equivalences
2
j) “Phương trình mx + 2x − 1 = 0 có nghiệm duy nhất khi và chỉ khi m=-1.”
Exercise
k) “Có số nguyên tố là số chẵn.”
l) “x2 + 1 > 0.”
m) “Bao giờ lớp mình đi dã ngoại?”
n) “Thủy ngân không phải là kim loại.”
o) “320 > 230 .”
p) “Máy bay là phương tiện di chuyển nhanh nhất”
q) “Năm 2002 là năm nhuận.”
r) “Có vô số số nguyên tố.”
s) “210 − 1 chia hết cho 11.”
t) “Cấm hút thuốc lá nơi công cộng.”
u) “Mọi số nguyên dương chẵn lớn hơn 2 là tổng của hai số nguyên tố.”
v) “Số x là nguyên tố nếu nó không có ước số khác 1 và x.”
1.24
Logics
Logical Equivalences
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition Contents

The compound compositions p and q are called logically equivalent Propositional Logic

if p ↔ q is a tautology, denoted p ≡ q. Logical Equivalences

Exercise

Example
Show that ¬(p ∨ q) and ¬p ∧ ¬q are logically equivalent.

1.25
Logics
Logical Equivalences
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

p∧T ≡ p Identity laws


p∨F ≡ p Luật đồng nhất Contents

p∨T ≡ T Domination laws Propositional Logic

Logical Equivalences
p∧F ≡ F Luật nuốt
Exercise

p∨p ≡ p Idempotent laws


p∧p ≡ p Luật lũy đẳng
¬(¬p) ≡ p Double negation law
Luât phủ định kép

1.26
Logics
Logical Equivalences
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

p∨q ≡ q∨p Commutative laws


p∧q ≡ q∧p Luật giao hoán
(p ∨ q) ∨ r ≡ p ∨ (q ∨ r) Associative laws Contents

(p ∧ q) ∧ r ≡ p ∧ (q ∧ r) Luật kết hợp Propositional Logic

Logical Equivalences

p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r) Distributive laws Exercise

p ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r) Luật phân phối


¬(p ∧ q) ≡ ¬p ∨ ¬q De Morgan’s law
¬(p ∨ q) ≡ ¬p ∧ ¬q Luật De Morgan
p ∨ (p ∧ q) ≡ p Absorption laws
p ∧ (p ∨ q) ≡ p Luật hút thu

1.27
Logics
Logical Equivalences
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Equivalence hoc
p ∨ ¬p ≡ T Contents
p ∧ ¬p ≡ F Propositional Logic
p→q ≡ ¬p ∨ q Logical Equivalences

(p → q) ∧ (p → r) ≡ p → (q ∧ r) Exercise

(p → r) ∧ (q → r) ≡ (p ∨ q) → r
(p → q) ∨ (p → r) ≡ p → (q ∨ r)
(p → r) ∨ (q → r) ≡ (p ∧ q) → r
p↔q ≡ (p → q) ∧ (q → p)
p↔q ≡ (¬p ∨ q) ∧ (p ∨ ¬q)

4.

1.28
Logics
Constructing New Logical Equivalences
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Show that ¬(p ∨ (¬p ∧ q)) and ¬p ∧ ¬q are logically equivalent by
developing a series of logical equivalences.

Solution Contents

Propositional Logic

¬(p ∨ (¬p ∧ q)) ≡ ¬p ∧ ¬(¬p ∧ q) by the second De Morgan law Logical Equivalences

Exercise
≡ ¬p ∧ [¬(¬p) ∨ ¬q] by the first De Morgan law

≡ ¬p ∧ (p ∨ ¬q) by the double negation law

≡ (¬p ∧ p) ∨ (¬p ∧ ¬q) by the second distributive law

≡ F ∨ (¬p ∧ ¬q) because ¬p ∧ p ≡ F

≡ ¬p ∧ ¬q by the identity law for F

Consequently, ¬(p ∨ (¬p ∧ q)) and ¬p ∧ ¬q are logically equivalent.

1.29
Logics
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Lấy phủ định rồi đơn giản mệnh đề phủ định đó.
Example
p → (¬q ∧ r)
Bằng cách sử dụng bảng chân trị, ta dễ dàng chứng minh được Contents

hai dạng mệnh đề p → q và ¬p ∨ q là tương đương logic. Propositional Logic

Phủ định: ¬(p → (¬q ∧ r)) Logical Equivalences

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

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

a) p ∧ (q ∨ r) ∧ (¬p ∨ ¬q ∨ r)
b) (p ∧ q) → r lam lai
c) p ∨ q ∨ (¬p ∧ ¬q ∧ r)
d) [[[(p ∧ q) ∧ r] ∨ [(p ∧ r) ∧ ¬r]] ∨ ¬q] → s

1.30
Logics
Exercise lam lai 1 va 2 Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Chứng minh các mệnh đề sau đây là tương đương logic.


Gợi ý: dùng bảng chân trị hoặc dùng các phép biến đổi tương
đương logic mệnh đề. Contents

Propositional Logic
a) ¬(p ↔ q) và ¬p ↔ q Logical Equivalences

b) (p → q) ∧ (p → r) và p → (q ∧ r) Exercise

c) (p → r) ∧ (q → r) và (p ∨ q) → r
d) (p → q) ∨ (p → r) và p → (q ∨ r)
e) ¬p → (q → r) và q → (p ∨ r)
f) p ↔ q và (p → q) ∧ (q → p)

1.32
Logics
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Các cặp mệnh đề được cho dưới đây có tương đương logic nhau
không? Chứng minh?
a) p ∧ (p → q) và p ∧ q Contents

Propositional Logic
b) p → q và ¬p ∨ (p ∧ q) Logical Equivalences

c) p → q và ¬p ∨ ¬q Exercise

d) ¬p và ¬(p ∨ q) ∨ (¬p ∧ q)
e) [(p ↔ q) ∧ (q ↔ r) ∧ (r ↔ p)] và
[(p → q) ∧ (q → r) ∧ (r → p)]
f) [(p ∧ q) ∨ (q ∧ r) ∨ (r ∧ p)] và [(p ∨ q) ∧ (q ∨ r) ∧ (r ∨ p)]

1.33
Logics
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Xác định chân trị của các mệnh đề sau. Hãy phát biểu mệnh đề
đảo và phản đảo của chúng.
a) “Nếu ABCD là hình chữ nhật thì AB vuông góc CD.”
b) “Nếu 14 là số lẻ thì 15 chia hết cho 4.” Contents
c) “Hai tam giác bằng nhau thì có diện tích bằng nhau.” Propositional Logic
d) “Phương trình bậc hai ax2 + bx + c = 0 có tích a.c < 0 thì nó có nghiệm.” Logical Equivalences
e) “Nếu hai số x và y đều chia hết cho n thì (x + y) cũng chia hết cho n.” Exercise

f) “Nếu 45 tận cùng là 5 thì 45 chia hết cho 5.”


√ √ √
g) “Nếu 2 là số vô tỷ thì 2. 2 là số vô tỷ.”
h) “Nếu Pythagore là người Pháp thì Việt Nam thuộc về châu Á.”
i) “Nếu 3n + 2 là số nguyên lẻ thì n là số nguyên lẻ.”
j) “Nếu 8 < 9 thì 5 là một số nguyên tố.”
k) “Một tứ giác là hình thoi khi nó có 2 đường chéo vuông góc.”
l) “Nếu 5 < 3 thì 7 là một số nguyên tố.”

1.35
Logics
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Cho p và q là hai mệnh đề với:


• p: "Hùng thích đọc sách"
• q: "Hùng học giỏi"
Mệnh đề biểu diễn "Nếu Hùng thích đọc sách thì Hùng học giỏi,
Contents
và ngược lại, Hùng học giỏi thì Hùng thích đọc sách" là: Propositional Logic

Logical Equivalences

Exercise

A) (p ∧ q) → r
B) p→q
C) p∨q
D) p∧q
E) p↔q
F) ¬p → ¬q
G) ¬p ∨ (p ∧ q)
H) Các chọn lựa khác đều sai.
1.37
Logics
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Gọi P , Q, R là các mệnh đề: Ngoc Le

• P : “Bình đang học Toán”.


• Q: “Bình đang học Tin học”.
• R: “Bình đang học Anh Văn”.
Hãy viết lại các mệnh đề dưới đây dưới dạng hình thức trong đó Contents
sử dụng các phép nối. Propositional Logic

Logical Equivalences
Example Exercise

Bình đang học Toán và Anh Văn nhưng không học Tin học:
P ∧ R ∧ ¬Q

a) Bình đang học Toán và Tin học nhưng không học cùng một
lúc Tin học và Anh Văn.
b) Không đúng là Bình đang học Anh Văn mà không học Toán.
c) Không đúng là Bình đang học Anh Văn hay Tin học mà
không học Toán.
d) Bình không học Tin học lẫn Anh Văn nhưng đang học Toán.
1.38
Logics
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Xác định các phát biểu sai trong các phát biểu bên dưới.
a) x ∈ {x}

b) {x} ⊆ {x}
c) {x} ∈ {x} Contents

Propositional Logic
d) {x} ∈ {{x}} Logical Equivalences

e) ∅ ⊆ {x} Exercise

A) a
B) b
C) c
D) d
E) Các đáp án khác đều sai.

1.39
Logics
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Mệnh đề nào bên dưới dùng là hằng đúng. Contents

Propositional Logic
A) (p ∨ ¬q) → q
Logical Equivalences
B) p → (p ∧ q) Exercise

C) ¬p → (p → q)
D) ¬(p → q) → q
E) Các chọn lựa khác đều sai.

1.40
Logics
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Cho các mệnh đề sau Ngoc Le

• p: “ABC là một tam giác cân”.


• q: “ABC là một tam giác đều”.
• r: “Tam giác ABC có một góc bằng 60o ”.
Contents

Propositional Logic

Logical Equivalences
Mệnh đề phức hợp nào sau đây biểu diễn định lý: “Nếu tam giác Exercise
ABC là tam giác cân và có một góc bằng 60o thì tam giác đó là
tam giác đều” ?

A) (p ∧ q) → r
B) (p ∧ r) → q
C) (p ∧ r) ∨ q
D) q → (p ∨ r)
E) Các đáp án khác đều sai.
1.41
Logics
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Sáu đội bóng A, B, C, D, E, F tham dự một giải vô địch. Dưới đây


là 5 khẳng định khác nhau về 2 đội có mặt trong trận chung kết:
a. A và C Contents

Propositional Logic
b. B và E
Logical Equivalences
c. B và F Exercise

d. A và F
e. A và D
Biết rằng có 4 khẳng định đúng một nửa và một khẳng định sai
hoàn toàn. Hãy cho biết 2 đội nào được thi đấu trong trận chung
kết?

1.42
Exercise Logics

Tìm chân trị các mệnh đề sau (có giải thích ngắn gọn): Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
a) “∀x ∈ N, x2 + 5x + 6 không phải là số nguyên tố.” Ngoc Le

b) “∃x ∈ R, x2 + x + 1 ≤ 0”
c) “∃n ∈ N, (n3 − n) không là bội của 3.”
d) “∀n ∈ N ∗, n2 − 1 là bội của 3.”
e) “∀x, ∀y ∈ R, x2 + y 2 > 2xy”
f) “∃r ∈ Q, 3 < r < π”
g) “∃n ∈ N, n2 + 1 chia hết cho 8” Contents

h) “∀x ∈ R, |x| < 3 ⇔ x2 < 9” Propositional Logic

i) “∃a, b ∈ R, (a + b)2 > 2(a2 + b2 )” Logical Equivalences

Exercise
j) “Mọi số thực đều dương.”
k) “Có kim loại ở thể lỏng.”
l) “Mọi tam giác đều đều bằng nhau.”
m) “Tất cả các chất khí đều không dẫn điện.”
n) “Có những tứ giác không có đường tròn ngoại tiếp.”
o) “Tồn tại số tự nhiên n, để với mọi số thực x, ta có f (x) = x2 − 2x + n nhận giá
trị không âm.”
p) “Với mọi số nguyên dương x, với mọi số nguyên dương y ta đều có x ≤ y.”
q) “Với mọi số nguyên dương x, có một số nguyên dương y để x ≤ y.”
r) “Có ít nhất một số nguyên dương x để với mọi số nguyên dương y ta đều có
x ≤ y.”
s) “Tồn tại số nguyên dương x và một số nguyên dương y để có x ≤ y.”

1.44
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

Chapter 2 Ngoc Le

Logics (cont.)
Discrete Structures for Computing
Contents

Predicate Logic

Exercise

Huynh Tuong Nguyen, Tran Tuan Anh, Nguyen Ngoc Le


Faculty of Computer Science and Engineering
University of Technology - VNUHCM
{htnguyen;trtanh}@hcmut.edu.vn
2.1
Logics (cont.)
Contents
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents
1 Predicate Logic Predicate Logic

Exercise

2 Exercise

2.2
Logics (cont.)
Course outcomes
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Course learning outcomes

L.O.1 Understanding of logic and discrete structures


L.O.1.1 – Describe definition of propositional and predicate logic
L.O.1.2 – Define basic discrete structures: set, mapping, graphs
Contents
L.O.2 Represent and model practical problems with discrete structures Predicate Logic
L.O.2.1 – Logically describe some problems arising in Computing Exercise
L.O.2.2 – Use proving methods: direct, contrapositive, induction
L.O.2.3 – Explain problem modeling using discrete structures

L.O.3 Understanding of basic probability and random variables


L.O.3.1 – Define basic probability theory
L.O.3.2 – Explain discrete random variables

L.O.4 Compute quantities of discrete structures and probabilities


L.O.4.1 – Operate (compute/ optimize) on discrete structures
L.O.4.2 – Compute probabilities of various events, conditional
ones, Bayes theorem

2.3
Logics (cont.)
Limits of Propositional Logic
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

• x>3 Predicate Logic

Exercise
• All square numbers are not prime numbers. 100 is a square
number. Therefore 100 is not a prime number.

2.4
Logics (cont.)
Predicates
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
A predicate (vị từ) is a statement containing one or more Contents
variables. If values are assigned to all the variables in a predicate, Predicate Logic

the resulting statement is a proposition (mệnh đề ). Exercise

Example: co gia tri


• x > 3 (predicate)
• 5 > 3 (proposition)
• 2 > 3 (proposition)

2.5
Logics (cont.)
Predicates
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

• x > 3 → P (x) Predicate Logic

• 5 > 3 → P (5) Exercise

• A predicate with n variables P (x1 , x2 , ..., xn )

2.6
Logics (cont.)
Truth value
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents
• x > 3 is true or false?
Predicate Logic
• 5>3 Exercise

• For every number x, x > 3 holds


• There is a number x such that x > 3

2.7
Logics (cont.)
Quantifiers
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• ∀: Universal – Với mọi Contents

• ∀xP (x) = P (x) is T for all x Predicate Logic

Exercise
• ∃: Existential – Tồn tại
• ∃xP (x) = There exists an element x such that P (x) is T
• We need a domain of discourse for variable

mien

2.8
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

Example
Let P (x) be the statement “x < 2”. What is the truth value of the
quantification ∀xP (x), where the domain consists of all real
number?
Contents
• P (3) = 3 < 2 is false Predicate Logic

• ⇒ ∀xP (x) is false Exercise

• 3 is a counterexample (phản ví dụ) of ∀xP (x)

Example
What is the truth value of the quantification ∃xP (x), where the
domain consists of all real number?

2.9
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

Example
Express the statement “Some student in this class comes from
Central Vietnam.”

Solution 1 Contents

Predicate Logic
• M (x) = x comes from Central Vietnam
Exercise
• Domain for x is the students in the class
• ∃xM (x)

Solution 2
• Domain for x is all people
• .S(x)
. . : x is the student in the class
ton tai x, S(x) and M(x)
thay Some thanh All => Voi moi x, S(x) -> M(x)

2.10
Logics (cont.)
Negation of Quantifiers
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Statement Negation Equivalent form


∀xP (x) ¬(∀xP (x)) ∃x¬P (x)
∃xP (x) ¬(∃xP (x)) ∀x¬P (x) Contents

Predicate Logic

Exercise
Example

• All CSE students study Discrete Math 1


• Let C(x) denote “x is a CSE student”
• Let S(x) denote “x studies Discrete Math 1”
• ∀x : C(x) → S(x)
• ∃x : ¬(C(x) → S(x)) ≡ ∃x : C(x) ∧ ¬S(x)
• There is a CSE student who does not study Discrete Math 1.

2.11
Logics (cont.)
Another Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Translate these:
• All lions are fierce.
• Some lions do not drink coffee. Contents

• Some fierce creatures do not drink coffee. Predicate Logic

Exercise

Solution
Let P (x), Q(x) and R(x) be the statements “x is a lion”, “x is
fierce” and “x drinks coffee”, respectively.
• ∀x(P (x) → Q(x)).
• ∃x(P (x) ∧ ¬R(x)).
• ∃x(Q(x) ∧ ¬R(x)).

2.12
Logics (cont.)
The Order of Quantifiers
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
tru TH cung la voi moi hoac ton tai
• The order of quantifiers is important, unless all the quantifiers
are universal quantifiers or all are existential quantifiers
• Read from left to right, apply from inner to outer
Contents
Example Predicate Logic

∀x ∀y (x + y = y + x) Exercise

T for all x, y ∈ R phai xac dinh bien nao phu thuoc luong tu nao

Example
∀x ∃y (x + y = 0) is T,
while
∃y ∀x (x + y = 0) is F

2.13
Logics (cont.)
Translating Nested Quantifiers
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
∀x (C(x) ∨ ∃y (C(y) ∧ F (x, y)) )
Provided that: Contents

• C(x): x has a computer, Predicate Logic

Exercise
• F (x, y): x and y are friends,
• x, y ∈ all students in your school.

Answer
For every student x in your school, x has a computer or there is a
student y such that y has a computer and x and y are friends.

2.14
Logics (cont.)
Translating Nested Quantifiers
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
∃x∀y∀z (((F (x, y) ∧ F (x, z) ∧ (y 6= z)) → ¬F (y, z)))
Provided that: Contents

Predicate Logic
• F (x, y): x, y are friends
Exercise
• x, y, z ∈ all students in your school.

Answer
There is a student x, so that for every student y, every student z
not the same as y, if x and y are friends, and x and z are friends,
then y and z are not friends.

2.15
Logics (cont.)
Translating into Logical Expressions
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example

1 “There is a student in the class has visited Hanoi”.


2 “Every students in the class have visited Nha Trang or Vung
Tau”. Contents

Predicate Logic

Exercise
Answer
Assume:
C(x) : x has visited Hanoi
D(x) : x has visited Nha Trang
E(x) : x has visited Vung Tau
We have:
1 ∃xC(x)
2 ∀x(D(x) ∨ E(x))

2.16
Logics (cont.)
Translating into Logical Expressions
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Every people has only one best friend. Contents

Predicate Logic

Exercise
Solution
Assume:
• B(x, y) : y is the best friend of x
We have:
∀x∃y∀z(B(x, y) ∧ ((y 6= z) → ¬B(x, z)))

2.17
Logics (cont.)
Translating into Logical Expressions
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
If a person is a woman and a parent, then this person is mother of
some one. Contents

Predicate Logic
Solution Exercise

We define:
• W (x) : x is woman
• P (x) : x is a parent
• M (x, y): x is mother of y
We have:
∀x((W (x) ∧ P (x)) → ∃yM (x, y))

2.18
Logics (cont.)
Inference
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example

• If I have a girlfriend, I will take her to go shopping.


• Whenever I and my girlfriend go shopping and that day is a Contents

Predicate Logic
special day, I will surely buy her some expensive gift.
Exercise
• If I buy my girlfriend expensive gifts, I will eat noodles for a
week.
• Today is March 8.
• March 8 is such a special day.
• Therefore, if I have a girlfriend,...
• I will eat noodles for a week.

chi xet cac TH gia thuyet deu dung, neu co 1 TH ra ket luan sai thi do la
suy dien sai, dung het thi dung

2.19
Logics (cont.)
Propositional Rules of Inferences
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Rule of Inference Name


p
p→q
∴q Modus ponens Contents

Predicate Logic
¬q Exercise
p→q
∴ ¬p Modus tollens
p→q
q→r
Hypothetical syllogism
∴p→r
(Tam đoạn luận giả định)
p∨q
¬p
Disjunctive syllogism
∴q
(Tam đoạn luận tuyển)

2.20
Logics (cont.)
Propositional Rules of Inferences
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Rule of Inference Name


p
Addition
∴p∨q
(Quy tắc cộng ) Contents

Predicate Logic
p∧q
Simplification Exercise
∴p
(Rút gọn)
p
q
Conjunction
∴p∧q
(Kết hợp)
p∨q
¬p ∨ r
Resolution
∴q∨r
(Phân giải)

2.21
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le
Example
If it rains today, then we will not have a barbecue today. If we do
not have a barbecue today, then we will have a barbecue
tomorrow. Therefore, if it rains today, then we will have a
barbecue tomorrow. Contents

Predicate Logic
Solution Exercise

• p: It is raining today
• q: We will not have a barbecue today
• r: We will have barbecue tomorrow
p→q
q→r
∴p→r
Hypothetical syllogism

2.22
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

Example
1. ¬p ∧ q Hypothesis
• It is not sunny this afternoon
(¬p) and it is colder than 2. ¬p Simplification using (1)
yesterday (q)
Contents
• We will go swimming (r) only if 3. r → p Hypothesis Predicate Logic
it is sunny Exercise
4. ¬r Modus tollens using (2) and (3)
• If we do not go swimming, then
we will take a canoe trip (s) 5. ¬r → s Hypothesis
• If we take a canoe trip, then we
will be home by sunset (t) 6. s Modus ponens using (4) and (5)
• We will be home by sunset (t)
7. s → t Hypothesis

8. t Modus ponens using (6) and (7)

2.23
Logics (cont.)
Fallacies
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
Fallacies (ngụy biện) resemble rules of inference but are based on
contingencies rather than tautologies. Contents

Predicate Logic

Exercise
Example
If you do correctly every questions in mid-term exam, you will get
10 grade. You got 10 grade.
Therefore, you did correctly every questions in mid-term exam.
Is [(p → q) ∧ q] → p a tautology?

2.24
Logics (cont.)
Rules of Inference for Quantified Statements
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Rule of Inference Name


∀xP (x)
Universal instantiation Contents
∴ P (c)
(Cụ thể hóa phổ quát) Predicate Logic

Exercise
P (c)for an arbitrary c
Universal generalization
∴ ∀xP (x)
(Tổng quát hóa phổ quát)
∃xP (x)
Existential instantiation
∴ P (c)for some element c
(Cụ thể hóa tồn tại)
P (c)for some element c
Existential generalization
∴ ∃xP (x)
(Tổng quát hóa tồn tại)

2.25
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

Example

• A student in this class has not gone to class


• Everyone in this class passed the first exam
Contents
• Someone who passed the first exam has not gone to class
Predicate Logic

Exercise

Hint
• C(x): x is in this class
• B(x): x has gone to class
• P (x): x passed the first exam
• Premises???

2.26
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

1. ∃x(C(x) ∧ ¬B(x)) Premise


2. C(a) ∧ ¬B(a) Existential instantiation from (1)
3. C(a) Simplification from (2) Contents
4. ∀x(C(x) → P (x)) Premise Predicate Logic

5. C(a) → P (a) Universal instantiation from (4) Exercise

6. P (a) Modus ponens from (3) and (5)


7. ¬B(a) Simplification from (2)
8. P (a) ∧ ¬B(a) Conjunction from (6) and (7)
9. ∃x(P (x) ∧ ¬B(x)) Existential generalization from (8)

2.27
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

Xét vị từ p(x) :00 x2 − 3x + 2 = 000 . Cho biết chân trị các mệnh đề
sau:
a) p(0)
b) p(1)
c) p(2) Contents

Predicate Logic
d) ∃x, p(x) Exercise

e) ∀x, p(x)
a) F
b) T
c) T
d) T
e) F

2.28
Xét vị từ theo 2 biến nguyên dương: p(x, y): “x là ước của y” Logics (cont.)

Hãy xác định chân trị các mệnh đề sau: Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
a) p(2, 3) Ngoc Le

b) p(2, 6)

c) ∀y, p(1, y)

d) ∀x, p(x, x)

e) ∀x∃y, p(x, y) Contents

f) ∃y∀x, p(x, y) Predicate Logic

Exercise
g) ∀x∀y, (p(x, y) ∧ p(y, x)) → (x = y)

h) ∀x∀y∀z(p(x, y) ∧ p(y, z)) → (p(x, z))

a) F
b) T
c) T
d) T
e) T
f) F
g) T
h) T 2.29
Logics (cont.)

• F (x, y) : x là cha của y, Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
• M (x, y) : x là mẹ của y, Ngoc Le

• S(x, y) : x là chị em gái của y,


• B(x, y) : x là anh em trai của y,
• H(x, y) : x là người hôn phối (chồng hoặc vợ) của y,
• O(x, y) : x lớn tuổi hơn y. Contents

Predicate Logic

Xác định các biểu thức luận lý vị từ để diễn đạt các câu sau Exercise

a) ‘Anh ta có chị gái và em trai’.


b) ‘Tất cả anh em trai của cô ta đều nhỏ tuổi hơn cô ấy’.
c) ‘Thuyên chỉ có duy nhất một người chồng’.
d) ‘Một trong những chị em gái của anh ta thì nhỏ tuổi hơn anh
ấy’.
e) ‘Mọi người đều có một ông nội, một ông ngoại, một bà nội
và một bà ngoại’.
f) ‘Bất cứ người nào là cha thì sẽ không thể là mẹ được’

2.30
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

Lời giải - Xác định các biểu thức luận lý vị từ để diễn đạt các câu
sau
a) ∃x∃y(S(x, m) ∧ O(x, m) ∧ B(y, m) ∧ ¬O(y, m)).
Contents
b) ∀x(B(x, m) → ¬O(x, m)).
Predicate Logic
c) ∃x∀y H(x, Thuyen) ∧ H(y, Thuyen) → (x = y) Exercise

hoặc ∃x∀y H(x, Thuyen) ∧ (x 6= y) → ¬H(y, Thuyen).


d) ∃x∀y(S(x, m) ∧ ¬O(x, m) ∧ S(y, m) ∧ (x 6= y) → O(y, m)).
e) ∀x∃y∃z∃y1 ∃y2 ∃z1 ∃z2
(F (y, x)∧M (z, x)∧F (y1 , y)∧M (y2 , y)∧F (z1 , z)∧M (z2 , z)).
f) ∃x∃y∀z(F (x, y) → ¬M (x, z)).

2.31
Logics (cont.)

Huynh Tuong Nguyen,


Diễn dịch những mệnh đề luận lý vị từ sau Tran Tuan Anh, Nguyen
Ngoc Le

a) B(c, m) ∧ (O(c, m) ∨ O(m, c)).


b) B(c, m) ∧ F (a, m) → O(a, c) ∧ F (a, c).
c) ∀x∀y(S(x, m) ∧ B(c, y) → x = y).
d) ∃x((S(x, m) ∨ H(c, x)) ∨ ∃x(H(x, m) ∧ O(x, m))).
Contents
e) ∀x∀y(S(x, m) ∧ S(y, m) → O(x, y) ∨ O(y, x)) Predicate Logic

Exercise

a) c hoặc là anh hoặc là em trai của m.


b) Nếu c là anh em trai của m và a là cha của m, thì a lớn tuổi
hơn c và a là cha của c.
c) Bất cứ ai là chị em của m, thì c cũng là anh em của người đó.
d) Có một người là chị của m hoặc c là chồng chị ta, hoặc có
một người là chồng của m và lớn tuổi hơn m.
e) Tất cả các chị em của m đều lớn tuổi hoặc nhỏ tuổi hơn nhau.

2.32
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Cho vị từ N (x) “x đã từng đi chơi Đà Lạt” với tập vũ trụ là toàn Ngoc Le

bộ sinh viên trong lớp Toán. Hãy phát biểu các vị từ sau:
a) ∃xN (x)
b) ∀xN (x)
c) ¬∃xN (x)
Contents
d) ∃x¬N (x) Predicate Logic

e) ¬∀xN (x) Exercise

f) ∀x¬N (x)

a) Có sinh viên trong lớp Toán chưa đi chơi Đà Lạt.


b) Tất cả sinh viên trong lớp Toán đều đã từng đi chơi Đà Lạt.
c) Không tồn tại sinh viên nào trong lớp Toán đã từng đi chơi Đà Lạt.
d) Có một sinh viên trong lớp Toán chưa từng đi chơi Đà Lạt.
e) Không phải tất cả sinh viên trong lớp Toán đều đã từng đi chơi Đà Lạt.
f) Tất cả sinh viên trong lớp Toán đều chưa từng đi chơi Đà Lạt.

2.33
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

Cho vị từ N (x) “x học ở lớp hơn 5 giờ mỗi ngày trong tuần” với
tập vũ trụ là toàn bộ sinh viên trong lớp Toán. Hãy phát biểu các
vị từ sau:
a) ∃xN (x)
Contents
b) ∀xN (x) Predicate Logic

c) ∃x¬N (x) Exercise

d) ∀x¬N (x)
a) Có một sinh viên học ở lớp hơn 5 giờ mỗi ngày trong tuần.
b) Mọi sinh viên đều học ở lớp hơn 5 giờ mỗi ngày trong tuần.
c) Có một sinh viên không học ở lớp hơn 5 giờ mỗi ngày trong tuần.
d) Không có sinh viên nào học ở lớp hơn 5 giờ mỗi ngày trong tuần.

2.34
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

Hãy cho biết công thức vị từ của đoạn mã giả (pseudo code) sau:

for (i = 0; i<numObjects; i++) {


Object x = Objects(i); Contents
if isMushroom(x) Predicate Logic
if isPoisonous(x) && isPurple(x) Exercise

return false;
}
return true;

• There are no mushrooms that are poisonous and purple.


• ∀xM ushroom(x) → ¬(P oisonous(x) ∧ P urple(x))

2.35
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

Hãy cho biết công thức vị từ của đoạn mã giả (pseudo code) sau:

for (i=0; i<numObjects; i++) {


Object x = Objects(i); Contents

if isMushroom(x) && isPoisonous(x) && isPurple(x) Predicate Logic

Exercise
return true;
}
return false;

• There is a mushroom that is purple and poisonous.


• ∃xM ushroom(x) ∧ P oisonous(x) ∧ P urple(x)

2.36
Logics (cont.)

Huynh Tuong Nguyen,


Cho đoạn mã giả (pseudo code) sau: Tran Tuan Anh, Nguyen
Ngoc Le

//— Look for first match


for (x=0; x<numKids; x++)
if isParent(Peter, kids[x])
match1Found = true; Contents

Predicate Logic
//— Now look for a second match Exercise
for (y=0; (y<numKids)&&(y!=x); y++)
if isParent(Peter, kids[y])
match2Found = true;

return match1Found && match2Found;


Biết rằng: Mảng kids gồm 3 phần tử: { Alice, Bob, Charles } và
Peter chỉ có 1 con là Alice.
Hãy cho biết công thức vị từ của câu "Peter có ít nhất 2 con".
∃x∃y(P arentOf (P eter, x) ∧ P arentOf (P eter, y) ∧ ¬(x = y))

2.37
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Cho P(x) là câu "x nói được tiếng Nga" và Q(x) là câu "x biết Ngoc Le

ngôn ngữ Java".


Hãy diễn đạt các câu sau bằng cách dùng P(x), Q(x), các lượng
từ và các liên từ logic.
Cho không gian đối với các lượng từ là tập hợp tất cả sinh viên ở
trường bạn.
Contents
a) Có một sinh viên ở trường bạn nói được tiếng Nga và biết Java.
Predicate Logic
b) Có một sinh viên ở trường bạn nói được tiếng Nga nhưng không biết Exercise
Java.
c) Mọi sinh viên ở trường bạn đều nói được tiếng Nga hoặc biết Java.
d) Không có một sinh viên nào ở trường bạn nói được tiếng Nga hoặc biết
Java.

a) ∃x(P (x) ∧ Q(x))


b) ∃x(P (x) ∧ ¬Q(x))
c) ∀x(P (x) ∨ Q(x))
d) ∀x¬(P (x) ∧ Q(x))

2.38
Cho L(x,y) là câu "x yêu y", với không gian của cả x và y là tập Logics (cont.)

hợp mọi người trên thế giới. Hãy dùng các lượng từ để diễn đạt Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
các câu sau Ngoc Le
a) Mọi người đều yêu Jerry.
b) Mọi người đều yêu một ai đó.
c) Có một người mà tất cả mọi người đều yêu.
d) Không có ai yêu tất cả mọi người.
e) Có một người mà Lydia không yêu.
f) Có một người mà không ai yêu.
g) Có đúng một người mà tất cả mọi người đều yêu. Contents

h) Có đúng hai người mà Lynn yêu. Predicate Logic


i) Mọi người đều yêu chính mình. Exercise
j) Có một người nào đó không yêu ai ngoài chính mình.
a) ∀xL(x, Jerry)
b) ∀x∃yL(x, y)
c) ∃y∀xL(x, y)
d) ¬(∃x(∀yL(x, y)), ¬(∃x(∧y L(x, y)) hoặc ∀x¬(∧y L(x, y))
e) ∃x¬L(Lydia, x)
f) ∃x∀y¬L(x, y)
g) ∃x(∀yL(x, y) ∧ ∀z((∀wL(w, z)) → z = x))
h) ∃x∃y(x 6= y ∧ L(Lynn, x) ∧ L(Lynn, y) ∧ ∀z(L(Lynn, z) →
(z = x ∨ z = y)))
i) ∀xL(x, x)
j) ∃x∀y(L(x, y) → x = y) 2.39
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Cho các vị từ: Ngoc Le

. -P(x): "x là một vấn đề toán học".


. -Q(x): "x khó" (theo một tiêu chuẩn được định nghĩa tốt
nào đó).
. -R(x): "x dễ" (theo một tiêu chuẩn được định nghĩa tốt nào
đó - giống như trên). Contents
. -S(x): "x không thể giải được". Predicate Logic

Diễn giải những công thức vị từ sau dưới dạng ngôn ngữ tự nhiên Exercise

a) ∀x(P (x) → (Q(x) ⇐⇒ ¬R(x)))


b) ∃x(S(x) ∧ ¬P (x))
Vì có nhiều cách diễn đạt một công thức vị từ dưới dạng ngôn
ngữ tự nhiên và sau đây là một cách
a) Nếu x là một vấn đề toán học thì x là bài toán khó tương
đương với x không dễ.
b) Có vấn đề không toán học không thể giải được

2.40
Logics (cont.)

Huynh Tuong Nguyen,


Dịch các bản mô tả sau đây sang tiếng Việt trong đó F (p) là Tran Tuan Anh, Nguyen
Ngoc Le
“Máy in p bị hỏng”,
B(p) là “Máy in p đang bận in tài liệu khác”,
L(j) là “Việc in j đã bị mất”,
và Q(j) là “Việc in j đang trong hàng đợi.”
a) ∃p(F (p) ∧ B(p)) → ∃jL(j)
Contents
b) ∀pB(p) → ∃jQ(j) Predicate Logic

c) ∃j(Q(j) ∧ L(j)) → ∃pF (p) Exercise

d) (∀pB(p) ∧ ∀jQ(j)) → ∃jL(j)


a) Nếu có một máy in nào đó bị hỏng trong khi đang bận in tài liệu thì
chắc chắn sẽ có tài liệu bị mất.
b) Nếu tất cả máy in đều đang bận in tài liệu thì sẽ có tài liệu đang đợi
được in.
c) Nếu một tài liệu đang được đợi in nhưng lại bị mất, chứng tỏ có máy in
nào đó đã bị hỏng.
d) Khi tất cả các máy in đều đang bận in tài liệu khác, mà tất cả các công
việc in vẫn phải chờ, thì sẽ có một việc in nào đó bị mất.

2.41
Logics (cont.)

Chuyển các câu sau sang vị từ, lượng từ và toán tử logic: Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
a) Không có ai là hoàn hảo. Ngoc Le

b) Không phải mọi người đều hoàn hảo.


c) Tất cả bạn bè của bạn đều hoàn hảo.

d) Ít nhất có một đứa bạn của bạn là hoàn hảo.


e) Mọi người đều là bạn của bạn và họ hoàn hảo.
f) Không phải tất cả mọi người là bạn của bạn hoặc có ai đó không hoàn Contents

hảo. Predicate Logic

Exercise
C(x): x là hoàn hảo.
D(x): x là bạn của bạn.
E(x): x là bất kỳ người nào khác.
a) ¬∃xC(x)
b) ∃x¬C(x)
c) ∀xD(x) → C(x)
d) ∃x∀y(D(x) ∧ D(y) ∧ (x 6= y)) → C(x) =⇒ ∃y(D(y) → C(y))
e) ∀x(D(x) ∧ C(x))
f) ∀x∃y(D(y) ∧ (y 6= x) ∨ ¬C(y)) =⇒ ¬∀xD(x) ∨ ∃yC(y)

2.42
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le
Cho các vị từ sau:
- P(x): Chương trình x đáp ứng chuẩn ABET.
- Q(x,y): Chương trình x có mục tiêu đào tạo giống chương
trình y.
- R(x): Kết quả đầu ra của chương trình x là có thể kiểm
chứng được. Contents

Predicate Logic
Mệnh đề vị từ nào sau biểu diễn câu : "Mọi chương trình đào tạo
Exercise
nếu có mục tiêu giống một chương trình khác đã đáp ứng chuẩn
ABET và kết quả đầu ra có thể kiểm chứng được thì cũng tuân
theo chuẩn ABET"
A) ∀x(P (x) ∧ ¬Q(x)) → ∃x(R(x))
B) ∀x(∃y(Q(x, y) ∧ P (y) ∧ R(x)) → P (x))
C) ∀x(∃y(Q(x, y) ∧ P (y) ∧ R(x)) → P (x) ∨ R(x))
D) ∀x(∀y(Q(x, y) ∧ P (y) ∨ R(x)) → P (x))

2.43
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

Trong câu hỏi này giả sử các vị từ:


- P (x, y): x là phụ huynh của y.
- M (x): x là nam giới .
Nếu ta cho: Contents

F (v, w) = M (v) ∧ ∃x∃y(P (x, y) ∧ P (x, v) ∧ (y 6= v) ∧ P (y, w)), Predicate Logic

thì F (v, w) có nghĩa là Exercise

A) v là anh của w
B) v là cháu trai của w
C) v là chú của w
D) v là ông của w

2.44
Logics (cont.)

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Chuyển các câu sau sang vị từ, lượng từ và toán tử logic: Ngoc Le

a) Khi ổ cứng còn ít hơn 30 GB, một thông điệp cảnh báo sẽ
được gửi tới mọi người dùng.
b) Không được sao lưu hệ thống tập tin nếu có một người nào
đó đang đăng nhập vào hệ thống.
c) Đoạn phim Youtube sẽ được buffer khi còn ít nhất 8 MB bộ Contents

nhớ và tốc độ đường tuyền tối thiểu là 56 kbits/s. Predicate Logic

Exercise
d) Chỉ vài sinh viên máy tính lập trình tốt.
e) Không một sinh viên máy tính nào không cần cù.
f) Không phải tất cả các sinh viên máy tính đều thông minh.
g) Tất cả người Pompei hoặc trung thành với Caesar hoặc ghét
ông ta.
h) Mỗi người đều trung thành với một người nào đó.
i) Người ta chỉ muốn ám sát những nhà cầm quyền mà người ta
không trung thành.

2.45
Proving methods

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

Chapter 3 Ngoc Le

Proving methods
Discrete Structures for Computing
Contents

Proving Methods

Exercise

Huynh Tuong Nguyen, Tran Tuan Anh, Nguyen Ngoc Le


Faculty of Computer Science and Engineering
University of Technology - VNUHCM
{htnguyen;trtanh}@hcmut.edu.vn
3.1
Proving methods
Contents
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents
1 Proving Methods Proving Methods

Exercise

2 Exercise

3.2
Proving methods
Course outcomes
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Course learning outcomes

L.O.1 Understanding of logic and discrete structures


L.O.1.1 – Describe definition of propositional and predicate logic
L.O.1.2 – Define basic discrete structures: set, mapping, graphs
Contents
L.O.2 Represent and model practical problems with discrete structures Proving Methods
L.O.2.1 – Logically describe some problems arising in Computing Exercise
L.O.2.2 – Use proving methods: direct, contrapositive, induction
L.O.2.3 – Explain problem modeling using discrete structures

L.O.3 Understanding of basic probability and random variables


L.O.3.1 – Define basic probability theory
L.O.3.2 – Explain discrete random variables

L.O.4 Compute quantities of discrete structures and probabilities


L.O.4.1 – Operate (compute/ optimize) on discrete structures
L.O.4.2 – Compute probabilities of various events, conditional
ones, Bayes theorem

3.3
Proving methods
Introduction
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents
Definition
Proving Methods
A proof is a sequence of logical deductions from Exercise
- axioms, and
- previously proved theorems
that concludes with a new theorem.

3.4
Proving methods
Terminology
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Proving Methods

Exercise

• Theorem (định lý ) = a statement that can be shown to be


true
• Axiom (tiên đề ) = a statement we assume to be true
• Hypothesis (giả thiết) = the premises of the theorem

3.5
Proving methods

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Proving Methods

Exercise
• Lemma (bổ đề ) = less important theorem that is helpful in
the proofs of other results
• Corollary (hệ quả ) = a theorem that can be established
directly from a proved theorem
• Conjecture (phỏng đoán) = statement being proposed to be
true, when it is proved, it becomes theorem

3.6
Proving methods
Proving a Theorem
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Many theorem has the form ∀xP (x) → Q(x) Contents

Proving Methods
Goal:
Exercise
• Show that P (c) → Q(c) is true with arbitrary c of the domain
• Apply universal generalization
⇒ How to show that conditional statement p → q is true.

3.7
Proving methods
Methods of Proof
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents
• Direct proofs (chứng minh trực tiếp)
Proving Methods
• Proof by contraposition (chứng minh phản đảo) Exercise

• Proof by contradiction (chứng minh phản chứng )


• Mathematical induction (quy nạp toán học)

3.8
Proving methods
Direct Proofs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
A direct proof shows that p → q is true by showing that if p is
Contents
true, then q must also be true. Proving Methods

Exercise
Example

Ex.: If n is an odd integer, then n2 is odd.


Pr.: Assume that n is odd. By the definition, n = 2k + 1, k ∈ Z.
n2 = (2k + 1)2 = 4k 2 + 4k + 1 = 2(2k 2 + 2k) + 1 is an odd
number.

3.9
Proving methods
Proof by Contraposition
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
p → q can be proved by showing (directly) that its contrapositive,
¬q → ¬p, is true. Contents

Proving Methods

Exercise
Example

Ex.: Given an integer n, show that if 3n + 2 is odd, then n is odd.


Pr.: Assume that “n is even”, so n = 2k, k ∈ Z. Substituting
3n + 2 = 3(2k) + 2 = 6k + 2 = 2(3k + 1) is even. Because
the negation of the conclusion of the conditional statement
implies that the hypothesis is false, Q.E.D.

3.10
Proving methods
Proofs by Contradiction
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
p is true if if can show that ¬p → (r ∧ ¬r) is true for some
proposition r.
Contents

Example Proving Methods

√ Exercise
Ex.: Prove that 2 is irrational.

Pr.: Let p is the proposition
√ “ 2 is irrational”. Suppose
√ ¬p is true,
which means 2 is rational. If so, ∃a, b ∈ Z, 2 = a/b, a, b
have no common factors. Squared, 2 = a2 /b2 , 2b2 = a2 , so
a2 is even, and a is even, too. Because of that a = 2c, c ∈ Z.
Thus, 2b2 = 4c2 , or b2 = 2c2 , which means b2 is even and so
is b. That means 2 divides both a and b, contradict with the
assumption.

3.11
Proving methods
Problem
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Proving Methods

Exercise

Assume that we have an infinite domino string, we want to know


whether every dominoes will fall, if we only know two things:
1 We can push the first domino to fall
2 If a domino falls, the next one will be fall
We can! Mathematical induction.

3.12
Proving methods
Mathematical Induction
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition (Induction)
To prove that P (n) is true for all positive integers n, where P (n)
Contents
is a propositional function, we complete two steps:
Proving Methods
• Basis Step: Verify that P (1) is true. Exercise

• Inductive Step: Show that the conditional statement


P (k) → P (k + 1) is true for all positive integers k
Logic form:
[P (1) ∧ ∀kP (k) → P (k + 1))] → ∀nP (n)

What is P (n) in domino string case?

3.13
Proving methods
Example on Induction
Huynh Tuong Nguyen,
Example Tran Tuan Anh, Nguyen
Ngoc Le

Show that if n is a positive integer, then

n(n + 1)
1 + 2 + ... + n = .
2
Contents

Solution Proving Methods

Let P (n) be the proposition that sum of first n is n(n + 1)/2 Exercise

• Basis Step: P (1) is true, because 1 = 1(1+1)


2
• Inductive Step:
k(k+1)
Assume that 1 + 2 + . . . + k = 2
.
Then:
k(k + 1)
1 + 2 + . . . + k + (k + 1) = + (k + 1)
2
k(k + 1) + 2(k + 1)
=
2
(k + 1)(k + 2)
=
2

shows that P (k + 1) is true under the assumption that P (k) is true.


3.14
Proving methods
Example on Induction
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Prove that n < 2n for all positive integers n.

Solution Contents

Let P (n) be the proposition that n < 2n . Proving Methods

Exercise
• Basis Step: P (1) is true, because 1 < 21 = 2
• Inductive Step:
Assume that P (k) is true for the positive k, that is, k < 2k .
Add 1 to both side of k < 2k , note that 1 ≤ 2k .

k + 1 < 2k + 1 ≤ 2k + 2k = 2 · 2k = 2k+1 .

shows that P (k + 1) is true, namely, that k + 1 < 2k+1 ,


based on the assumption that P (k) is true.

3.15
Proving methods
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Hãy chứng minh rằng nếu n là số nguyên dương và 7n + 9 là số Contents

chẵn thì n là số lẻ bằng cách: Proving Methods

Exercise
1 Chứng minh trực tiếp.
2 Chứng minh gián tiếp (phản đảo).
3 Chứng minh phản chứng.

3.16
Proving methods
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Chứng minh trực tiếp


Giả sử: 7n + 9 là số chẵn. Do đó, 7n + 9 = 2k, (k ∈ Z)
Ta suy ra: n = 2k − 6n − 9 = 2k − 6n − 10 + 1 = 2(k − 3n − 5) + 1
Nghĩa là n là số lẻ. Vậy suy ra đpcm.

Chứng minh gián tiếp (phản đảo) Contents

Proving Methods
Để chứng minh phát biểu trên là đúng. Ta viết phát biểu này dưới dạng phép kéo theo
p → q với p = 7n + 9 là số chẵn và q = n là số lẻ. Exercise
Đảo đề của nó là "Nếu n không là số lẻ thì 7n + 9 không là số chẵn". Và ta có thể
chứng minh phát biểu này đúng như sau:
Nếu n không là số lẽ, nghĩa là n chia hết cho 2. Do đó, n = 2k, (k ∈ Z)
Ta suy ra: 7n + 9 = 7(2k) + 9 = 14k + 9 = 2(7k + 4) + 1
Nghĩa là: 7n + 9 không là số chẵn. Vì mệnh đề phản đảo là đúng nên do chứng minh đảo
đề, ta suy ra p → q cũng đúng.

Chứng minh phản chứng


Giả sử 7n + 9 là số chẵn và n không là số lẻ tức là n là số chẵn.
Bởi vì n là số chẵn. Do đó, n = 2k, (k ∈ Z)
Ta suy ra: 7n + 9 = 7(2k) + 9 = 14k + 9 = 2(7k + 4) + 1
Nghĩa là: 7n + 9 là số lẻ. Ta có thể chỉ ra rằng nếu n là số chẵn thì 7n + 9 là số lẻ. Điều
này mâu thuẫn với giả thuyết 7n + 9 là số chẵn. Vậy suy ra đpcm.

3.17
Proving methods
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Kiểu chứng minh nào đã được sử dụng trong chứng minh bên dưới
đây
Để chứng minh “Nếu m và n là những số nguyên và mn là chẵn, thì
Contents
hoặc là m chẵn, hoặc là n chẵn”, người ta suy diễn như sau:
Proving Methods
Giả sử m và n là số lẻ. Khi đó có thể biểu diễn m = 2k + 1 và
Exercise
n = 2l + 1. Như vậy tích mn = (2k + 1)(2l + 1) = 2(2kl + k + l) + 1 là
số lẻ. Sai giả thiết. Kết luận, hoặc là m chẵn, hoặc là n chẵn.

A) Chứng minh trực tiếp (direct proof )


B) Chứng minh phản chứng (contradiction proof ) hoặc phản
đảo (contra-positive proof )
C) Chứng minh quy nạp (inductive proof )
D) Các chọn lựa khác đều sai

3.18
Proving methods

Huynh Tuong Nguyen,


Điều gì sai trong chuỗi lý luận dưới đây rằng tất cả các bông hoa Tran Tuan Anh, Nguyen
Ngoc Le
đều có cùng một màu?
(1) Đặt P (n) là một mệnh đề rằng tất cả các bông hoa trong
một tập n bông đều có cùng màu.
(2) Ta thấy rõ ràng, P (1) luôn đúng.
(3) Nếu giả sử P (n) đúng. Nghĩa là, giả sử rằng tất cả các bông Contents

hoa trong một tập n bông hoa bất kỳ đều có cùng màu sắc. Proving Methods

Exercise
(4) Xét một tập bất kỳ n + 1 bông; được đánh số lần lượt là
1, 2, 3, . . . , n, (n + 1).
(5) Dựa theo giả định trên, chuỗi n bông hoa đầu tiên của những
bông hoa này có cùng màu, và chuỗi n bông hoa sau cũng sẽ
có cùng một màu.
(6) Do hai tập hợp bông hoa này có sự giao thoa n − 1 bông,
nên tất cả n + 1 bông hoa này phải cùng một màu.
(7) Điều này chứng minh rằng P (n + 1) là đúng và được chứng
minh bằng phương pháp qui nạp.

3.19
Proving methods

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

Xét một tập con D (N × N ) được đinh nghĩa đệ quy như sau:
(i) (n, 0) ∈ D.
(ii) nếu (n, m) ∈ D, thì (n, n + m) ∈ D. Contents

Hãy Proving Methods

Exercise
1) xác định vài phần tử của D.
2) chứng minh bằng phép quy nạp trên k rằng ’nếu m = k.n thì
’(n, m) ∈ D’.
3) chứng minh rằng nếu (n, m) ∈ D, thì chúng ta sẽ có m = kn
với k ∈ N .

3.20
Proving methods

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

a) Chứng minh rằng ∀n ∈ N +


(n + 1)2 − (n + 2)2 − (n + 3)2 + (n + 4)2 = 4.
Contents
b) Từ đó suy ra rằng với mọi số tự nhiên m, tồn tại n nguyên Proving Methods

dương để có thể viết m dưới dạng tổng bình phương của 12 , Exercise

22 , . . ., n2 , nghĩa là: ∀m ∈ N + , ∃n ∈ N + ,
∃ε1 , . . . , εn ∈ {−1, 1}, m = ε1 12 + ε2 22 + . . . + εn n2 .
(Gợi ý: thử biểu diễn các giá trị m ∈ {1, 2, 3, 4, 5, 6})

3.21
Proving methods

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

Hãy chứng minh các đẳng thức sau bằng cách sử dụng phương
pháp chứng minh quy nạp:
Contents
1 1.2 + 2.5 + 3.8 + . . . + n.(3n − 1) = n2 (n + 1), n ≥ 1 Proving Methods
1 1 1 2n
2 1 + 1+2 + 1+2+3 + ... + 1+2+3+...+n = n+1 , n ≥1 Exercise

Pn i−1 n
3 i=1 2 =2 −1
4 Chứng minh log5 (2) là số vô tỷ.

3.22
Proving methods
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Hãy sử dụng phương pháp chứng minh quy nạp để chứng minh:
1 Chứng minh rằng với mọi số nguyên n ≥ 1, 32n−1 + 1 chia
hết cho 4. Contents

2 Chứng minh rằng với mọi số nguyên n ≥ 1, 6n − 1 đều chia Proving Methods

hết cho 5. Exercise

2n−1
3 Chứng minh rằng với mọi số nguyên n ≥ 1, 5 + 1 đều
chia hết cho 6.
4 Chứng minh rằng với mọi số nguyên n ≥ 1, 8n − 1 đều chia
hết cho 7.
5 Chứng minh rằng với mọi số nguyên n ≥ 1, 4n + 15n − 1 đều
chia hết cho 9.

3.24
Proving methods

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

Hãy chứng minh các bất đẳng thức sau bằng cách sử dụng
Contents
phương pháp chứng minh quy nạp: Proving Methods

1 Chứng minh rằng với mọi số nguyên n ≥ 1, 3n > n2 . Exercise

n
2 Chứng minh rằng với mọi số nguyên n ≥ 4, n! > 2 .

3.26
Proving methods

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le

Contents
Chứng minh rằng số lượng đường chéo trong một đa giác lồi với n Proving Methods
đỉnh là 12 n(n − 3), với n ≥ 4 Exercise

3.36
Sets

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

Chapter 4
Ngoc Le

Sets
Discrete Structures for Computing
Contents

Sets

Set Operation

Huynh Tuong Nguyen, Tran Tuan Anh, Nguyen Ngoc Le


Faculty of Computer Science and Engineering
University of Technology - VNUHCM
{htnguyen;trtanh}@hcmut.edu.vn
4.1
Sets
Contents
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents
1 Sets Sets

Set Operation

2 Set Operation

4.2
Sets
Course outcomes
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Course learning outcomes

L.O.1 Understanding of logic and discrete structures


L.O.1.1 – Describe definition of propositional and predicate logic
L.O.1.2 – Define basic discrete structures: set, mapping, graphs
Contents
L.O.2 Represent and model practical problems with discrete structures Sets
L.O.2.1 – Logically describe some problems arising in Computing Set Operation
L.O.2.2 – Use proving methods: direct, contrapositive, induction
L.O.2.3 – Explain problem modeling using discrete structures

L.O.3 Understanding of basic probability and random variables


L.O.3.1 – Define basic probability theory
L.O.3.2 – Explain discrete random variables

L.O.4 Compute quantities of discrete structures and probabilities


L.O.4.1 – Operate (compute/ optimize) on discrete structures
L.O.4.2 – Compute probabilities of various events, conditional
ones, Bayes theorem

4.3
Sets
Set Definition
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
• Set is a fundamental discrete structure on which all discrete
structures are built
• Sets are used to group objects, which often have the same
properties
Contents
Example
Sets

• Set of all the students who are currently taking Discrete Set Operation

Mathematics 1 course.
• Set of all the subjects that K2011 students have to take in
the first semester.
• Set of natural numbers N

Definition
A set is an unordered collection of objects.
The objects in a set are called the elements (phần tử ) of the set.
A set is said to contain (chứa) its elements.

4.4
Sets
Notations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
• a ∈ A: a is an element of the set A
• a∈/ A: a is not an element of the set A Contents

Sets

Set Operation
Definition (Set Description)

• The set V of all vowels in English alphabet, V = {a, e, i, o, u}


• Set of all real numbers greater than 1???
{x | x ∈ R, x > 1}
{x | x > 1}
{x : x > 1}

4.5
Sets
Equal Sets
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
Two sets are equal iff they have the same elements. Contents

Sets

• (A = B) ↔ ∀x(x ∈ A ↔ x ∈ B) Set Operation

Example

• {1, 3, 5} = {3, 5, 1}
• {1, 3, 5} = {1, 3, 3, 3, 5, 5, 5, 5}

4.6
Sets
Venn Diagram
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• John Venn in 1881


• Universal set (tập vũ trụ) is Contents

Sets
represented by a rectangle
Set Operation
• Circles and other
geometrical figures are used
to represent sets
• Points are used to represent
particular elements in set

Tap vu tru = U

4.7
Sets
Special Sets
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents
• Empty set (tập rỗng ) has no elements, denoted by ∅, or {}
Sets
• A set with one element is called a singleton set Set Operation

• What is {∅}?
• Answer: singleton

4.8
Sets
Subset
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
The set A is called a subset (tập con) of B iff every element of A
is also an element of B, denoted by A ⊆ B.
Contents

If A 6= B, we write A ⊂ B and say A is a proper subset (tập con Sets

thực sự) of B. Set Operation

A = B => ONLY use A (- B

• ∀x(x ∈ A → x ∈ B)
• For every set S,
(i) ∅ ⊆ S, (ii) S ⊆ S.

4.9
Sets
Cardinality
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
If S has exactly n distinct elements where n is non-negative
integers, S is finite set (tập hữu hạn), and n is cardinality (bản
số ) of S, denoted by |S|.
Contents
Example Sets

• A is the set of odd positive integers less than 10. |A| = 5. Set Operation

• S is the letters in Vietnamese alphabet, |S| = 29.


• Null set |∅| = 0.

Definition
A set that is infinite if it is not finite.

Example

• Set of positive integers is infinite

4.10
Sets
Power Set
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
Given a set S, the power set (tập lũy thừa) of S is the set of all
subsets of the set S, denoted by P (S).
Contents

Sets
Example
Set Operation

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}}

Example

• What is the power set of the empty set? = { {} }


• What is the power set of the set {∅} = { {}, { {} } }

4.11
Sets
Power Set
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents
Theorem Sets

If a set has n elements, then its power set has 2n elements. Set Operation

Prove using induction!

power Of A = power of B => A = B

4.12
Sets
Ordered n-tuples
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
The ordered n-tuple (dãy sắp thứ tự) (a1 , a2 , . . . , an ) is the
ordered collection that has a1 as its first element, a2 as its second
Contents
element, . . ., and an as its nth element. Sets

Set Operation
Definition
Two ordered n-tuples (a1 , a2 , . . . , an ) = (b1 , b2 , . . . , bn ) iff ai = bi ,
for i = 1, 2, . . . , n.

Example
2-tuples, or ordered pairs (cặp), (a, b) and (c, d) are equal iff
a = c and b = d

4.13
Sets
Cartesian Product
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• René Descartes (1596–1650)

Definition
Let A and B be sets. The Cartesian product (tích Đề-các) of A
and B, denoted by A × B, is the set of ordered pairs (a, b), where Contents

a ∈ A and b ∈ B. Hence, Sets

Set Operation

A × B = {(a, b) | a ∈ A ∧ b ∈ B}
U x {} = {}

Example
Cartesian product of A = {1, 2} and B = {a, b, c}. Then

A × B = {(1, a), (1, b), (1, c), (2, a), (2, b), (2, c)}

Show that A × B 6= B × A

4.14
Sets
Cartesian Product
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition

A1 ×A2 ×· · ·×An = {(a1 , a2 , . . . , an ) | ai ∈ Ai for i = 1, 2, . . . , n} Contents

Sets

Set Operation

Example
A = {0, 1}, B = {1, 2}, C = {0, 1, 2}. What is A × B × C?

A×B×C = {(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)}

4.15
Sets
Union
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
The union (hợp) of A and B

A ∪ B = {x | x ∈ A ∨ x ∈ B}
Contents

Sets

A∪B Set Operation

A B

• Example:
• {1,2,3} ∪ {2,4} = {1,2,3,4}
• {1,2,3} ∪ ∅ = {1,2,3}

4.16
Sets
Intersection
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
The intersection (giao) of A and B

A ∩ B = {x | x ∈ A ∧ x ∈ B}
Contents

Sets

A∩B Set Operation

A B

Example:
• {1,2,3} ∩ {2,4} = {2}
• {1,2,3} ∩ N = {1,2,3}

4.17
Sets
Union/Intersection
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents
n
Sets
[
Ai = A1 ∪ A2 ∪ ... ∪ An = {x | x ∈ A1 ∨ x ∈ A2 ∨ ... ∨ x ∈ An } Set Operation
i=1

n
\
Ai = A1 ∩ A2 ∩ ... ∩ An = {x | x ∈ A1 ∧ x ∈ A2 ∧ ... ∧ x ∈ An }
i=1

4.18
Sets
Difference
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
The difference (hiệu) of A and B

A − B = {x | x ∈ A ∧ x ∈
/ B}
Contents

Sets

A−B Set Operation

A B

Example:
• {1,2,3} - {2,4} = {1,3}
• {1,2,3} - N = ∅

4.19
Sets
Complement
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
The complement (phần bù) of A

A = {x | x ∈A}
/
Contents

Sets

Set Operation
Example:
• A = {1,2,3} then A = ???
• Note that A - B = A ∩ B

4.20
Sets
Set Identities
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

A∪∅ = A Identity laws


A∩U = A Luật đồng nhất Contents

A∪U = U Domination laws Sets

A∩∅ = ∅ Luật nuốt Set Operation

A∪A = A Idempotent laws


A∩A = A Luật lũy đẳng
(Ā) = A Complementation law
Luật bù

4.21
Sets
Set Identities
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

A∪B = B∪A Commutative laws


A∩B = B∩A Luật giao hoán Contents

A ∪ (B ∪ C) = (A ∪ B) ∪ C Associative laws Sets

A ∩ (B ∩ C) = (A ∩ B) ∩ C Luật kết hợp Set Operation

A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C) Distributive laws
A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) Luật phân phối
A∪B = A∩B De Morgan’s laws
A∩B = A∪B Luật De Morgan

4.22
Sets
Method of Proofs of Set Equations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

To prove A = B, we could use Contents

• Venn diagrams Sets

Set Operation
• Prove that A ⊆ B and B ⊆ A
• Use membership table
• Use set builder notation and logical equivalences

4.23
Sets
Example (1)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Sets

Set Operation

Example
Verify the distributive rule P ∪ (Q ∩ R) = (P ∪ Q) ∩ (P ∪ R)

4.24
Sets
Example (2)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Prove: A ∩ B = A ∪ B
Contents
(1) Show that A ∩ B ⊆ A ∪ B Sets

Suppose that x ∈ A ∩ B Set Operation

By the definition of complement, x ∈


/ A∩B
So, x ∈
/ A or x ∈/B
Hence, x ∈ Ā or x ∈ B̄
We conclude, x ∈ A ∪ B
Or, A ∩ B ⊆ A ∪ B
(2) Show that A ∪ B ⊆ A ∩ B

4.25
Sets
Example (3)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Prove: A ∩ B = A ∪ B
Contents

A B A∩B A∩B Ā ∪ B̄ Sets

Set Operation
1 1 1 0 0
1 0 0 1 1
0 1 0 1 1
0 0 0 1 1

4.26
Sets
Example (4)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Prove: A ∩ B = A ∪ B
A∩B = {x|x 6∈ A ∩ B} Contents

= {x|¬(x ∈ A ∩ B)} Sets

= {x|¬(x ∈ A ∧ x ∈ B)} Set Operation

= {x|¬(x ∈ A) ∨ ¬(x ∈ B)}


= {x|x 6∈ A ∨ x 6∈ B}
= {x|x ∈ A ∨ x ∈ B}
= {x|x ∈ A ∪ B}

4.27
Functions

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

Chapter 5 Ngoc Le

Functions
Discrete Structures for Computing
Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

Huynh Tuong Nguyen, Tran Tuan Anh, Nguyen Ngoc Le


Faculty of Computer Science and Engineering
University of Technology - VNUHCM
{htnguyen;trtanh}@hcmut.edu.vn
5.1
Functions
Contents
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

1 Functions

Contents

Functions
2 One-to-one and Onto Functions One-to-one and Onto
Functions

Sequences and
Summation

3 Sequences and Summation Recursion

4 Recursion

5.2
Functions
Course outcomes
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Course learning outcomes

L.O.1 Understanding of logic and discrete structures


L.O.1.1 – Describe definition of propositional and predicate logic
L.O.1.2 – Define basic discrete structures: set, mapping, graphs
Contents
L.O.2 Represent and model practical problems with discrete structures Functions
L.O.2.1 – Logically describe some problems arising in Computing One-to-one and Onto
L.O.2.2 – Use proving methods: direct, contrapositive, induction Functions

L.O.2.3 – Explain problem modeling using discrete structures Sequences and


Summation

L.O.3 Understanding of basic probability and random variables Recursion

L.O.3.1 – Define basic probability theory


L.O.3.2 – Explain discrete random variables

L.O.4 Compute quantities of discrete structures and probabilities


L.O.4.1 – Operate (compute/ optimize) on discrete structures
L.O.4.2 – Compute probabilities of various events, conditional
ones, Bayes theorem

5.3
Functions
Introduction
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• Each student is assigned a grade from set


Contents
{0, 0.1, 0.2, 0.3, . . . , 9.9, 10.0} at the end of semester Functions
• Function is extremely important in mathematics and One-to-one and Onto
computer science Functions

Sequences and
• linear, polynomial, exponential, logarithmic,... Summation

• Don’t worry! For discrete mathematics, we need to Recursion

understand functions at a basic set theoretic level

5.4
Functions
Function
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Definition Ngoc Le

Let A and B be nonempty sets. A function f from A to B is an


assignment of exactly one element of B to each element of A.
• f :A→B
tu 1A -> duy nhat 1 B
• A: domain (miền xác định) of f
Contents
• B: codomain (miền giá trị) of f Functions
• For each a ∈ A, if f (a) = b One-to-one and Onto
• b is an image (ảnh) of a Functions

• a is pre-image (nghịch ảnh) of f (a) Sequences and


Summation
• Range of f is the set of all images of elements of A Recursion

• f maps (ánh xạ) A to B

f
a b = f (a)

A B
f
5.5
Functions
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example:
• y is an image of d
• c is a pre-image of z Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

5.6
Functions
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
What are domain, codomain, and range of the function that
assigns grades to students includes: student A: 5, B: 3.5, C: 9, D:
5.2, E: 4.9? Contents

Functions

One-to-one and Onto


Example Functions

Let f : Z → Z assign the the square of an integer to this integer. Sequences and
Summation
What is f (x)? Domain, codomain, range of f ? Recursion

• f (x) = x2
• Domain: set of all integers
• Codomain: Set of all integers
• Range of f : {0, 1, 4, 9, . . .}

5.7
Functions
Add and multiply real-valued functions
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
Let f1 and f2 be functions from A to R. Then f1 + f2 and f1 f2
are also functions from A to R defined by
(f1 + f2 )(x) = f1 (x) + f2 (x) Contents

Functions
(f1 f2 )(x) = f1 (x)f2 (x) One-to-one and Onto
Functions

Sequences and
Summation
Example Recursion

Let f1 (x) = x2 and f2 (x) = x − x2 . What are the functions


f1 + f2 and f1 f2 ?
(f1 + f2 )(x) = f1 (x) + f2 (x) = x2 + x − x2 = x
(f1 f2 )(x) = f1 (x)f2 (x) = x2 (x − x2 ) = x3 − x4

5.8
Functions
Image of a subset
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
Let f : A → B and S ⊆ A. The image of S:

f (S) = {f (s) | s ∈ S} Contents

Functions

One-to-one and Onto


Functions

f ({a, b, c, d}) = {x, y, z} Sequences and


Summation

Recursion

5.9
Functions
One-to-one
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
A function f is one-to-one or injective (đơn ánh) if and only if
Contents
∀a∀b (f (a) = f (b) → a = b) Functions

One-to-one and Onto


Functions

Sequences and
Summation

• Is f : Z → Z, f (x) = x + 1 Recursion

one-to-one?
• Is f : Z → Z, f (x) = x2
one-to-one?

5.10
Functions
Onto
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
f : A → B is onto or surjective (toàn ánh) if and only if
Contents
∀b ∈ B, ∃a ∈ A : f (a) = b Functions

One-to-one and Onto


Functions

Sequences and
Summation

• Is f : Z → Z, f (x) = x + 1 Recursion

onto?
• Is f : Z → Z, f (x) = x2
onto?

5.11
Functions
One-to-one and onto (bijection)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
f : A → B is bijective (one-to-one correspondence) (song ánh) if
Contents
and only if f is injective and surjective
Functions

One-to-one and Onto


Functions

• Let f be the function from Sequences and


Summation
{a, bc, d} to {1, 2, 3, 4} with Recursion
f (a) = 4, f (b) = 2,
f (c) = 1, f (d) = 3. Is f a
bijection?

5.12
Functions
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

5.13
Functions
Inverse function (Hàm ngược)
Huynh Tuong Nguyen,
Definition Tran Tuan Anh, Nguyen
Ngoc Le

Let f : A → B be a bijection then the inverse of f is the function


f − : B → A defined by

if f (a) = b then f − (b) = a

A one-to-one correspondence is call invertible (khả nghịch) Contents

because we can define the inverse of this function. Functions

One-to-one and Onto


Functions

Sequences and
f (a) Summation

Recursion

a = f −1 (b) f −1 (b) b = f (a)

A f B

f −1
5.14
Functions
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
A = {a, b, c} and B = {1, 2, 3} with
Contents
f (a) = 2 f (b) = 3 f (c) = 1 Functions

One-to-one and Onto


f is invertible and its inverse is Functions

Sequences and
Summation
−1 −1 −1
f (1) = c f (2) = a f (3) = b Recursion

Example
Let f : R → R with f (x) = x2 . If f invertible?

5.15
Functions
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

f :R→R
Contents

Functions
f (x) = 2x + 1 One-to-one and Onto
Functions

Sequences and
f −1 : R → R Summation

Recursion

x−1
f −1 (x) =
2

5.16
Functions
Function Composition
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
Given a pair of functions g : A → B and f : B → C. Then the Contents

Functions
composition (hợp thành) of f and g, denoted f ◦ g is defined by
One-to-one and Onto
Functions
f ◦g :A→C Sequences and
Summation

Recursion
f ◦ g(a) = f (g(a))

5.17
Functions
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

5.18
Functions
Graphs of Functions
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
The graph of f (x) = x2 from Z to Z.

(−3, 9) (3, 9)
Contents

Functions

One-to-one and Onto


(−2, 4) (2, 4) Functions

Sequences and
Summation

(−1, 1) (1, 1) Recursion

(0, 0)

Definition
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}.

5.19
Functions
Important Functions
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Definition Ngoc Le

Floor function (hàm sàn) of x (bxc): the largest integer ≤ x


b 12 c = 0, b3.1c = 3, b7c = 7
Ceiling function (hàm trần) of x (dxe): the smallest integer ≥ x
d 21 e = 1, d3.1e = 4, d7e = 7
Contents

Functions
Bảng: Properties (n is an integer, x is a real number) One-to-one and Onto
Functions

Sequences and
(1a) bxc = n iff n≤x<n+1 Summation

(1b) dxe = n iff n−1<x≤n Recursion

(1c) bxc = n iff x−1<n≤x


(1d) dxe = n iff x≤n<x+1
(2) x − 1 < bxc ≤ dxe < x + 1
(3a) b−xc = −dxe
(3b) d−xe = −bxc
(4a) bx + nc = bxc + n
(4b) dx + ne = dxe + n
5.20
Functions
Sequences
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

What are the rule of these sequences (dãy )?


Example
1, 3, 5, 7, 9, . . . an = 2n − 1
Arithmetic sequence (cấp số cộng ) Contents

Functions

One-to-one and Onto


Functions
Example
Sequences and

1, 12 , 14 , 81 , 16
1 1 Summation
,... an = 2n−1
Recursion
Geometric sequence (cấp số nhân)

Example
{an } 5, 11, 17, 23, 29, 35, 41, 47, . . . an = 6n − 1
{bn } 1, 7, 25, 79, 241, 727, 2185, . . . bn = 3n − 2

5.21
Functions
Recurrence Relations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Example
{an } 5, 11, 17, 23, 29, 35, 41, 47, . . .
an = an−1 + 6 for n = 2, 3, 4, . . . and a1 = 5
Recurrence relations: công thức truy hồi

Contents

Functions
Definition (Fibonacci Sequence)
One-to-one and Onto
Functions
Initial condition: f0 = 0 and f1 = 1
Sequences and
fn = fn−1 + fn−2 for n = 2, 3, 4, . . . Summation

Recursion

Example
Find the Fibonacci numbers f2 , f3 , f4 , f5 and f6
f2 = f1 + f0 =1+0=1
f3 = f2 + f1 =1+1=2
f4 = f3 + f2 =2+1=3
f5 = f4 + f3 =3+2=5
f6 = f5 + f4 =5+3=8
5.22
Functions

Exercise (1) Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Ngoc Le
Initial deposit: $10,000
Interest: 11%/year, compounded annually (lãi suất kép)
After 30 years, how much do you have in your account?
Solution:
Let Pn be the amount in the account after n years. The sequence Contents

{Pn } satisfies the recurrence relation Functions

Pn = Pn−1 + 0.11Pn−1 = (1.11)Pn−1 . One-to-one and Onto


Functions
The initial condition is P0 = 10, 000 Sequences and
Summation
Step 1. Solve the recurrence relation (iteration technique) Recursion
P1 = (1.11)P0
P2 = (1.11)P1 = (1.11)2 P0
P3 = (1.11)P2 = (1.11)3 P0
..
.
Pn = (1.11)Pn−1 = (1.11)n P0 .
Step 2. Calculate
P30 = (1.11)30 10, 000 = $228, 922.97.
5.23
Functions

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Exercise (2) Ngoc Le

What is the 2012th number in the sequence {xn }: 1, 2, 2, 3, 3, 3,


4, 4, 4, 4, 5, 5, 5, 5, 5, 6,. . .
Solution:
In this sequence, integer 1 appears once, the integer 2 appears
twice, the integer 3 appears three times, and so on. Therefore Contents

Functions
integer n appears n times in the sequence.
One-to-one and Onto
Functions
We can prove that (try it!)
n Sequences and
X n(n + 1) Summation
i = 1 + 2 + 3 + ... + n = Recursion
i=1
2
and can easily calculate that
X62
i = 1953
i=1
so the next 63 numbers (until 2016) is 63.
Therefore, 2012th number in the sequence is 63.

5.24
Functions

Theorem Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
If a and r are real numbers and r 6= 0, then Ngoc Le

n  ar n+1 −a
X if r 6= 1
arj = r−1
j=0
(n + 1)a if r = 1.

Contents

Chứng minh. Functions


Pn j One-to-one and Onto
Let Sn = j=0 ar . Functions

Sequences and
Pn Summation
rSn = r j=0 arj Recursion
Pn
= j=0 arj+1
Pn+1
=  k=1 ark 
Pn k
= k=0 ar + (arn+1 − a)
= Sn + (arn+1 − a)

ar n+1 −a
Solving for Sn shows that if r 6= 1, then Sn = r−1
Pn
If r = 1, then Sn = j=0 a = (n + 1)a

5.25
Functions
Recursion
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Definition (Recurrence Relation) Ngoc Le

An equation that recursively defines a sequence.

Definition (Recursion (đệ quy))


The act of defining an object (usually a function) in terms of that Contents

object itself. Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

5.26
Functions
Recursive Algorithms
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
An algorithm is called recursive if it solves a problem by reducing
it to an instance of the same problem with smaller input.

Contents

Example Functions

One-to-one and Onto


Give a recursive algorithm for computing n!, where n is a Functions

nonnegative integer. Sequences and


Summation

Recursion
Solution. We base on the recursive definition of n!:
n! = n · (n − 1)! and 0! = 1.
procedure factorial (n: nonnegative integer)
if n = 0 then return 1
else return n· factorial (n - 1)
{output is n!}

5.27
Functions
Algorithms for Fibonacci Numbers
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Recursive Algorithm Ngoc Le

procedure fibonacci(n: nonnegative integer)


if n = 0 then return 0
else if n = 1 then return 1
else return fibonacci(n-1) + fibonacci(n-2)
{output is fibonacci(n)} Contents

Functions

Iterative Algorithm One-to-one and Onto


Functions

procedure iterative fibonacci(n: nonnegative integer) Sequences and


Summation
if n = 0 then return 0 Recursion
else
x := 0
y := 1
for i := 1 to n − 1
z := x + y
x := y
y := z
return y
{output is the nth Fibonacci number}
5.28
Functions
Tower of Hanoi
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

There is a tower in Hanoi that has three pegs mounted on a board


together with 64 gold disks of different sizes.
Initially, these disks are placed on the first peg in order of size,
Contents
with the largest on the borrom. Functions

The rules: One-to-one and Onto


Functions

1 Move one at a time from one peg to another Sequences and


Summation

2 A disk is never placed on top of a smaller disk Recursion

Goals: all the disks on the third peg in order of size.

The myth says that the world will end when they finish the
puzzle.

5.29
Functions
Tower of Hanoi – 64 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

1
2
3
4
? Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
5 Recursion

5.30
Functions
Tower of Hanoi – 1 Disc
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

5.31
Functions
Tower of Hanoi – 1 Disc
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

Moved disc from peg 1 to peg 3.

5.32
Functions
Tower of Hanoi – 1 Disc
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

OK Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

5.33
Functions
Tower of Hanoi – 2 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
1 Recursion

5.34
Functions
Tower of Hanoi – 2 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

2 1

Moved disc from peg 1 to peg 2.

5.35
Functions
Tower of Hanoi – 2 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

1 2

Moved disc from peg 1 to peg 3.

5.36
Functions
Tower of Hanoi – 2 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
1 Recursion

Moved disc from peg 2 to peg 3.

5.37
Functions
Tower of Hanoi – 2 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

OK Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
1 Recursion

5.38
Functions
Tower of Hanoi – 3 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions
1 Sequences and
Summation
2 Recursion

5.39
Functions
Tower of Hanoi – 3 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
2 Recursion

3 1

Moved disc from peg 1 to peg 3.

5.40
Functions
Tower of Hanoi – 3 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

3 2 1

Moved disc from peg 1 to peg 2.

5.41
Functions
Tower of Hanoi – 3 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
1 Recursion

3 2

Moved disc from peg 3 to peg 2.

5.42
Functions
Tower of Hanoi – 3 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
1 Recursion

2 3

Moved disc from peg 1 to peg 3.

5.43
Functions
Tower of Hanoi – 3 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

1 2 3

Moved disc from peg 2 to peg 1.

5.44
Functions
Tower of Hanoi – 3 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
2 Recursion

1 3

Moved disc from peg 2 to peg 3.

5.45
Functions
Tower of Hanoi – 3 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions
1 Sequences and
Summation
2 Recursion

Moved disc from peg 1 to peg 3.

5.46
Functions
Tower of Hanoi – 3 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

OK 1
Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
2 Recursion

5.47
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

1 One-to-one and Onto


Functions
2 Sequences and
Summation
3 Recursion

5.48
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions
2 Sequences and
Summation
3 Recursion

4 1

Moved disc from peg 1 to peg 2.

5.49
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
3 Recursion

4 1 2

Moved disc from peg 1 to peg 3.

5.50
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
3 1 Recursion

4 2

Moved disc from peg 2 to peg 3.

5.51
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
1 Recursion

4 3 2

Moved disc from peg 1 to peg 2.

5.52
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
1 Recursion

4 3 2

Moved disc from peg 3 to peg 1.

5.53
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
1 2 Recursion

4 3

Moved disc from peg 3 to peg 2.

5.54
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions
1 Sequences and
Summation
2 Recursion

4 3

Moved disc from peg 1 to peg 2.

5.55
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions
1 Sequences and
Summation
2 Recursion

3 4

Moved disc from peg 1 to peg 3.

5.56
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
2 1 Recursion

3 4

Moved disc from peg 2 to peg 3.

5.57
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
1 Recursion

2 3 4

Moved disc from peg 2 to peg 1.

5.58
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
1 Recursion

2 3 4

Moved disc from peg 3 to peg 1.

5.59
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
1 3 Recursion

2 4

Moved disc from peg 2 to peg 3.

5.60
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
3 Recursion

2 1 4

Moved disc from peg 1 to peg 2.

5.61
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

One-to-one and Onto


Functions
2 Sequences and
Summation
3 Recursion

1 4

Moved disc from peg 1 to peg 3.

5.62
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Functions

1 One-to-one and Onto


Functions
2 Sequences and
Summation
3 Recursion

Moved disc from peg 2 to peg 3.

5.63
Functions
Tower of Hanoi – 4 Discs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

OK 1
2
Contents

Functions

One-to-one and Onto


Functions

Sequences and
Summation
3 Recursion

5.64
Functions
Tower of Hanoi
Huynh Tuong Nguyen,
Algorithm Tran Tuan Anh, Nguyen
Ngoc Le

procedure hanoi(n, A, B, C)
if n = 1 then move the disk from A to C
else
call hanoi(n − 1, A, C, B)
move disk n from A to C Contents
call hanoi(n − 1, B, A, C) Functions

One-to-one and Onto


Functions
Recurrence Relation Sequences and
Summation

1 if n = 1 Recursion
H(n) =
2H(n − 1) + 1 if n > 1.

Recurrence Solving
H(n) = 2n − 1
If one move takes 1 second, for n = 64

264 − 1 ≈ 2 × 1019 sec


≈ 500 billion years!.
5.65
Relations

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

Chapter 6 Ngoc Le

Relations
Discrete Structures for Computing
Contents

Properties of Relations

Combining Relations

Representing Relations

Closures of Relations

Types of Relations

Huynh Tuong Nguyen, Tran Tuan Anh, Nguyen Ngoc Le


Faculty of Computer Science and Engineering
University of Technology - VNUHCM
{htnguyen;trtanh}@hcmut.edu.vn
6.1
Relations
Contents
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

1 Properties of Relations

Contents
2 Combining Relations
Properties of Relations

Combining Relations

Representing Relations
3 Representing Relations
Closures of Relations

Types of Relations

4 Closures of Relations

5 Types of Relations

6.2
Relations
Course outcomes
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Course learning outcomes

L.O.1 Understanding of logic and discrete structures


L.O.1.1 – Describe definition of propositional and predicate logic
L.O.1.2 – Define basic discrete structures: set, mapping, graphs
Contents
L.O.2 Represent and model practical problems with discrete structures Properties of Relations
L.O.2.1 – Logically describe some problems arising in Computing Combining Relations
L.O.2.2 – Use proving methods: direct, contrapositive, induction
Representing Relations
L.O.2.3 – Explain problem modeling using discrete structures
Closures of Relations

Types of Relations
L.O.3 Understanding of basic probability and random variables
L.O.3.1 – Define basic probability theory
L.O.3.2 – Explain discrete random variables

L.O.4 Compute quantities of discrete structures and probabilities


L.O.4.1 – Operate (compute/ optimize) on discrete structures
L.O.4.2 – Compute probabilities of various events, conditional
ones, Bayes theorem

6.3
Relations
Introduction
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Properties of Relations

Combining Relations

Representing Relations

Closures of Relations

Types of Relations

Function?
6.4
Relations
Relation
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
Let A and B be sets. A binary relation (quan hệ hai ngôi) from a
set A to a set B is a set Contents

Properties of Relations
R⊆A×B Combining Relations

Representing Relations

Closures of Relations
• Notations:
Types of Relations
(a, b) ∈ R ←→ aRb

• n-ary relations?
(a1,a2,a3,...an) thuoc R

6.5
Relations
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Let A = {a, b, c} be the set of students, B = {l, c, s, d} be the set
of the available optional courses. We can have relation R that
consists of pairs (a, b), where a is a student enrolled in course b.
Contents

Properties of Relations

Combining Relations

Representing Relations

R = {(a, l), (a, s), (a, g), (b, c), Closures of Relations

Types of Relations
(b, s), (b, g), (c, l), (c, g)}

R l c s g
a x x x
b x x x
c x x

6.6
Relations
Functions as Relations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents
• Is a function a relation?
Properties of Relations
• Yes! Combining Relations

• f : A→B Representing Relations

Closures of Relations
R = {(a, b) | b = f (a)} Types of Relations

6.7
Relations
Functions as Relations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
vi moi A co the co 2 anh tren B Ngoc Le

=> vo ly vi ham chi dung khi moi A -> 1 B


• Is a relation a function?
• No

Contents

Properties of Relations

Combining Relations

Representing Relations

Closures of Relations

Types of Relations

• Relations are a generalization of functions

6.8
Relations
Relations on a Set
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
A relation on the set A is a relation from A to A.

Example
Contents
Let A be the set {1, 2, 3, 4}. Which ordered pairs are in the Properties of Relations
relation R = {(a, b) | a divides b} (a là ước số của b)? Combining Relations

Representing Relations
Solution:
Closures of Relations

Types of Relations
R = {(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 4), (3, 3), (4, 4)}

R 1 2 3 4
1 x x x x
2 x x
3 x
4 x

6.9
Relations
Properties of Relations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Reflexive xRx, ∀x ∈ A
(phản xạ) Contents

Symmetric xRy → yRx, ∀x, y ∈ A Properties of Relations

(đối xứng ) Combining Relations

Antisymmetric (xRy ∧ yRx) → x = y, ∀x, y ∈ A Representing Relations

Closures of Relations
(phản đối xứng )
Types of Relations
Transitive (xRy ∧ yRz) → xRz, ∀x, y, z ∈ A
(bắc cầu)
bac cau co 2 cach cm : cm k co ele de bac cau V cm tung ele bac cau

6.10
Relations
Example de xet co the tinh chat ko co 2 cach :
Huynh Tuong Nguyen,
C1 : ve hinh Tran Tuan Anh, Nguyen
Ngoc Le
C2 : cm dk luon sai => P->Q dung
C3 : cm dk dung, kl sai => sai
Example C4 : chi ra 1 VD dk dung, kl sai
Consider the following relations on {1, 2, 3, 4}:
R1 = {(1, 1), (1, 2), (2, 1), (2, 2), (3, 4), (4, 1), (4, 4)},
R2 = {(1, 1), (1, 2), (2, 1)}, Contents

R3 = {(1, 1), (1, 2), (1, 4), (2, 1), (2, 2), (3, 3), (4, 1), (4, 4)}, Properties of Relations

R4 = {(2, 1), (3, 1), (3, 2), (4, 1), (4, 2), (4, 3)}, Combining Relations

Representing Relations
R5 = {(3, 4)}
Closures of Relations

Solution: Types of Relations

• Reflexive: R3
• Symmetric: R2 , R3 -ve hinh giong phan duoi de giai
• Antisymmetric: R4 , R5 -khi ve chi xet nhung ele co quan he
trong set
• Transitive: R4 , R5
- de thoa man tinh chat thi tat ca cac
ele deu phai thoa man
R5 la bac cau vi :
P->Q voi P la xRy va yRx ; Q là bac cau => P sai ne P->Q dung
6.11
Relations
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
What is the properties of the divides (ước số ) relation on the set
of positive integers? Contents

Properties of Relations

Solution: Combining Relations

• ∀a ∈ Z+ , a | a: reflexive Representing Relations

Closures of Relations
• 1 | 2, but 2 - 1: not symmetric Types of Relations

• ∃a, b ∈ Z+ , (a | b) ∧ (b | a) → a = b: antisymmetric
• a | b ⇒ ∃k ∈ Z+ , b = ak; b | c ⇒ ∃l ∈ Z+ , c = bl. Hence,
c = a(kl) ⇒ a | c: transitive

6.12
Relations
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example Contents

What are the properties of these relations on the set of integers: Properties of Relations

Combining Relations
R1 = {(a, b) | a ≤ b} px,pdx, bc Representing Relations
pdx,bc
R2 = {(a, b) | a > b} Closures of Relations

Types of Relations
R3 = {(a, b) | a = b or a = −b} pdx,dx,px,bc

6.13
Relations
Combining Relations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Because relations from A to B are subsets of A × B, two


relations from A to B can be combined in any way two sets can
be combined.
Example
Contents
Let A = {1, 2, 3} and B = {1, 2, 3, 4}. List the combinations of Properties of Relations
relations R1 = {(1, 1), (2, 2), (3, 3)} and Combining Relations
R2 = {(1, 1), (1, 2), (1, 3), (1, 4)}. Representing Relations

Closures of Relations
Solution: R1 ∪ R2 , R1 ∩ R2 , R1 − R2 and R2 − R1 . Types of Relations

Example
Let A and B be the set of all students and the set of all courses at
school, respectively. Suppose R1 = {(a, b) | a has taken the course
b} and R2 = {(a, b) | a requires course b to graduate}. What are
the relations R1 ∪ R2 , R1 ∩ R2 , R1 ⊕ R2 , R1 − R2 , R2 − R1 ?

6.14
Relations
Composition of Relations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
Let R be relations from A to B and S be from B to C. Then the
composite (hợp thành) of S and R is Contents

Properties of Relations

S ◦ R = {(a, c) ∈ A × C | ∃b ∈ B (aRb ∧ bSc)} Combining Relations

Representing Relations

Closures of Relations

Types of Relations
Example
R = {(0, 0), (0, 3), (1, 2), (0, 1)} (0,0) thuoc R bac cau voi (0,0) thuoc
S = {(0, 0), (1, 0), (2, 1), (3, 1)} S => (0,0)
S ◦ R = {(0, 0), (0, 1), (1, 1)} (0,3) thuoc R bac cau (3,1) thuoc S
=> (0,1)

6.15
Relations
Power of Relations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
Let R be a relation on the set A. The powers (lũy thừa)
Rn , n = 1, 2, 3, . . . are defined recursively by

R1 = R and Rn+1 = Rn ◦ R. Contents

Properties of Relations

Combining Relations

Representing Relations
Example
Closures of Relations
Let R = {(1, 1), (2, 1), (3, 2), (4, 3)}. Find the powers Types of Relations
Rn , n = 2, 3, 4, . . ..
Solution:
R2 = {(1, 1), (2, 1), (3, 1), (4, 2)}
R3 = {(1, 1), (2, 1), (3, 1), (4, 1)}
R4 = {(1, 1), (2, 1), (3, 1), (4, 1)}
···

6.16
Relations
Representing Relations Using Matrices
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Definition Ngoc Le

Suppose R is a relation from A = {a1 , a2 , . . . , am } to


B = {b1 , b2 , . . . , bn }, R can be represented by the matrix
MR = [mij ], where

1 if(ai , bj ) ∈ R
mij = Contents
0 if(ai , bj ) ∈
/R Properties of Relations

Combining Relations

Representing Relations
Example Closures of Relations

R is relation from A = {1, 2, 3} to B = {1, 2}. Let Types of Relations

R = {(2, 1), (3, 1), (3, 2)}, the matrix for R is


 
0 0
MR =  1 0 
1 1

Determine whether the relation has certain properties (reflexive,


symmetric, antisymmetric,...)
6.17
Relations
Representing Relations Using Digraphs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
Suppose R is a relation in A = {a1 , a2 , . . . , am }, R can be
represented by the digraph (đồ thị có hướng ) G = (V, E), where Contents

Properties of Relations
V =A Combining Relations
(ai , aj ) ∈ E if (ai , aj ) ∈ R Representing Relations

Closures of Relations

Types of Relations
Example
Given a relation on A = {1, 2, 3, 4},
R = {(1, 1), (1, 3), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (4, 1)}
Draw corresponding digraph.

6.18
Relations
Resulting digraph
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

1
Contents

Properties of Relations

Combining Relations

Representing Relations

2 4 Closures of Relations

Types of Relations

6.19
Relations
Closure
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
The closure (bao đóng ) of relation R with respect to property P Contents

Properties of Relations
is the relation S that
3 thuoc tinh da hoc o tren Combining Relations
i. contains R
(ko bao gom phan doi xung) Representing Relations

ii. has property P Closures of Relations

Types of Relations
iii. is contained in any relation satisfying (i) and (ii).

S is the “smallest” relation satisfying (i) & (ii)

6.20
Relations
Reflexive Closure
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example

Let R = {(a, b), (a, c), (b, d), (d, c)}


The reflexive closure of R Contents

Properties of Relations
{(a, b), (a, c), (b, d), (d, c), (a, a), (b, b), (c, c), (d, d)} Combining Relations

Representing Relations

Closures of Relations
R∪∆ Types of Relations

where
∆ = {(a, a) | a ∈ A}
diagonal relation (quan hệ đường chéo).

6.21
Relations
Reflexive Closure
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Properties of Relations

Combining Relations

Representing Relations

Closures of Relations

Types of Relations

6.22
Relations
Symmetric Closure
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example

Let R = {(a, b), (a, c), (b, d), (c, a), (d, e)}
The symmetric closure of R Contents

Properties of Relations
{(a, b), (a, c), (b, d), (c, a), (d, e), (b, a), (d, b), (e, d)} Combining Relations

Representing Relations

Closures of Relations
R ∪ R− Types of Relations

where
R−1 = {(b, a) | (a, b) ∈ R}
inverse relation (quan hệ ngược).

6.23
Relations
Symmetric Closure
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Properties of Relations

Combining Relations

Representing Relations

Closures of Relations

Types of Relations

6.24
Relations
Transitive Closure
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Contents
Let R = {(a, b), (a, c), (b, d), (d, e)}
Properties of Relations
The transitive closure of R Combining Relations

{(a, b), (a, c), (b, d), (d, e), (a, d), (b, e), (a, e)} Representing Relations

Closures of Relations

Types of Relations

∪∞
n=1 R
n

6.25
Relations
Transitive Closure
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Properties of Relations

Combining Relations

Representing Relations

Closures of Relations

Types of Relations

6.26
Relations
Equivalence Relations
Huynh Tuong Nguyen,
Definition Tran Tuan Anh, Nguyen
Ngoc Le

A relation on a set A is called an equivalence relation (quan hệ


tương đương ) if it is reflexive, symmetric and transitive.

Example (1)
The relation R = {(a, b)|a and b are in the same provinces} is an Contents

equivalence relation. a is equivalent to b and vice versa, denoted Properties of Relations

a ∼ b. Combining Relations

Representing Relations

Example (2) Closures of Relations

Types of Relations

R = {(a, b) | a = b ∨ a = −b}
R is an equivalence relation.

Example (3)

R = {(x, y) | |x − y| < 1}
Is R an equivalence relation? NO do ko co bc
6.27
Relations
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example (Congruence Modulo m - Đồng dư modulo m) Contents

Properties of Relations
Let m be a positive integer with m > 1. Show that the relation Combining Relations

Representing Relations
R = {(a, b) | a ≡ b (mod m)} Closures of Relations

Types of Relations
is an equivalence relation on the set of integers.

6.28
Relations
Equivalence Classes
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
Let R be an equivalence relation on the set A. The set of all
elements that are related to an element a of A is called the
Contents
equivalence class (lớp tương đương ) of a, denoted by
Properties of Relations

Combining Relations
[a]R = {s | (a, s) ∈ R}
Representing Relations

Closures of Relations

Types of Relations

Example
The equivalence class of “Thủ Đức” for the equivalence relation “in
the same provinces” is { “Thủ Đức”, “Gò Vấp”, “Bình Thạnh”,
“Quận 10”,. . .}

6.29
Relations
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
What are the equivalence classes of 0, 1, 2, 3 for congruence Contents
modulo 4? Properties of Relations

Combining Relations
Solution:
Representing Relations
[0]4 = {..., −8, −4, 0, 4, 8, ...} Closures of Relations

[1]4 = {..., −7, −3, 1, 5, 9, ...} Types of Relations

[2]4 = {..., −6, −2, 2, 6, 10, ...}


[3]4 = {..., −5, −1, 3, 7, 11, ...}

6.30
Relations
Equivalence Relations and Partitions
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Theorem
Contents
Let R be an equivalence relation on a set A. These statements for Properties of Relations
elements a and b of A are equivalent: Combining Relations

i aRb Representing Relations

Closures of Relations
ii [a] = [b] => co it nhat 2 pt giong nhau => giao khac rong Types of Relations

iii [a] ∩ [b] 6= ∅

6.31
Relations
Example 1
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Suppose that S = {1, 2, 3, 4, 5, 6}. The collection of sets
A1 = {1, 2, 3}, A2 = {4, 5}, and A3 = {6} forms a partition of S, Contents
because these sets are disjoint and their union is S Properties of Relations

Combining Relations

Representing Relations

The equivalence classes of an equivalence relation R on a set S Closures of Relations

Types of Relations
form a partition of S.

Every partition of a set can be used to form an equivalence


relation.

6.32
Relations
Example 2
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Example
Divides set of all cities and towns
in Vietnam into set of 64
provinces. We know that:
• there are no provinces with Contents
no cities or towns Properties of Relations

• no city is in more than one Combining Relations

province Representing Relations

Closures of Relations
• every city is accounted for Types of Relations

Definition
A partition of a Vietnam is a
collection of non-overlapping
non-empty subsets of Vietnam
(provinces) that, together, make
up all of Vietnam.

6.33
Relations
Relation in a Partition
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• We divided based on
relation Contents

Properties of Relations

R = {(a, b)|a and b are in the same provinces


Combining Relations

Representing Relations

Closures of Relations

Types of Relations

• “Thủ Đức” is related


(equivalent) to “Gò Vấp”
• “Đà Lạt” is not related (not
equivalent) to ”Long Xuyên”

6.34
Relations
Partial Order Relations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• Order words such that x comes before y in the dictionary


• Schedule projects such that x must be completed before y
• Order set of integers, where x < y
Contents
Definition
Properties of Relations
A relation R on a set S is called a partial ordering (có thứ tự bộ Combining Relations
phận) if it is reflexive, antisymmetric and transitive. A set S Representing Relations
together with a partial ordering R is called a partially ordered set, Closures of Relations

or poset (tập có thứ tự bộ phận), and is denoted by (S, R) or Types of Relations

(S, 4).

Example

• (Z, ≥) is a poset
• Let S a set, (P (S), ⊆) is a poset

6.35
Relations
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Properties of Relations

Combining Relations

Representing Relations

Closures of Relations

Types of Relations

6.36
Relations
Totally Order Relations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
In the poset (Z+ , |), 3 and 9 are comparable (so sánh được),
because 3 | 9, but 5 and 7 are not, because 5 - 7 and 7 - 5.
Contents
→ That’s why we call it partially ordering. Properties of Relations

Combining Relations

Definition Representing Relations

Closures of Relations
If (S, 4) is a poset and every two elements of S are comparable, S
Types of Relations
is called a totally ordered (có thứ tự toàn phần). A totally
ordered set is also called a chain (dây xích).

Example
The poset (Z, ≤) is totally ordered.

6.37
Relations
Maximal & Minimal Elements
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
chi xet nhung ele co quan he R => ele lon nhat la max Ngoc Le

=> trong 1 set co the co nhieu max

Definition
• a is maximal (cực đại) in the poset (S, 4) if there is no
b ∈ S such that a ≺ b. Contents

Properties of Relations
• a is minimal (cực tiểu) in the poset (S, 4) if there is no
Combining Relations
b ∈ S such that b ≺ a. Representing Relations

Closures of Relations

Types of Relations
Example
Which elements of the poset
({2, 4, 5, 10, 12, 20, 25}, |) are
minimal and maximal?

6.38
Relations
Greatest Element& Least Element
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
co quan he va lon nhat (ko nhat thiet phai co qhe R voi moi ele
=> chi co 1 greatest va 1 least

Definition
• a is the greatest element (lớn nhất) of the poset (S, 4) if
b 4 a for all b ∈ S. Contents

Properties of Relations
• a is the least element (nhỏ nhất) of the poset (S, 4) if Combining Relations
a 4 b for all b ∈ S. Representing Relations

The greatest and least element are unique if it exists. Closures of Relations

Types of Relations

Example
Let S be a set. In the poset (P (S), ⊆), the least element is ∅ and
the greatest element is S.

6.39
Relations
Upper Bound & Lower Bound
Huynh Tuong Nguyen,
Definition ngoai set && co qhe R voi moi ele trong set && lon hon
Tran Tuan Anh, Nguyen
Ngoc Le

Let A ⊆ (S, 4).


• If u is an element of S such that a 4 u for all elements
a ∈ A, then u is called an upper bound (cận trên) of A.
• If l is an element of S such that l 4 a for all elements a ∈ A,
then l is called a lower bound (cận dưới) of A. Contents

Properties of Relations

Combining Relations

Representing Relations

Closures of Relations

Example Types of Relations

• Subset A does not have


upper bound and lower
bound.
• The upper bound of B are
20, 40 and the lower bound
is 2.

6.40
Counting

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

Chapter 7 Ngoc Le

Counting
Discrete Structures for Computing
Contents

Introduction

Counting Techniques

Pigeonhole Principle

Permutations &
Combinations

Exercises

Huynh Tuong Nguyen, Tran Tuan Anh, Nguyen Ngoc Le


Faculty of Computer Science and Engineering
University of Technology - VNUHCM
{htnguyen;trtanh}@hcmut.edu.vn
7.1
Counting
Contents
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

1 Introduction

Contents
2 Counting Techniques
Introduction

Counting Techniques

Pigeonhole Principle
3 Pigeonhole Principle Permutations &
Combinations

Exercises

4 Permutations & Combinations

5 Exercises

7.2
Counting
Course outcomes
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Course learning outcomes

L.O.1 Understanding of logic and discrete structures


L.O.1.1 – Describe definition of propositional and predicate logic
L.O.1.2 – Define basic discrete structures: set, mapping, graphs
Contents
L.O.2 Represent and model practical problems with discrete structures Introduction
L.O.2.1 – Logically describe some problems arising in Computing Counting Techniques
L.O.2.2 – Use proving methods: direct, contrapositive, induction
Pigeonhole Principle
L.O.2.3 – Explain problem modeling using discrete structures
Permutations &
Combinations
L.O.3 Understanding of basic probability and random variables Exercises
L.O.3.1 – Define basic probability theory
L.O.3.2 – Explain discrete random variables

L.O.4 Compute quantities of discrete structures and probabilities


L.O.4.1 – Operate (compute/ optimize) on discrete structures
L.O.4.2 – Compute probabilities of various events, conditional
ones, Bayes theorem

7.3
Counting
Introduction
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example

• In games: playing card, gambling, dices,...


• How many allowable passwords on a computer system?
Contents
• How many ways to choose a starting line-up for a football Introduction

match? Counting Techniques

Pigeonhole Principle

Permutations &
Combinations

Exercises

• Combinatorics (tổ hợp) is the study of arrangements of


objects
• Counting of objects with certain properties is an important
part of combinatorics

7.4
Counting
Applications of Combinatorics
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• Number theory
• Probability Contents

Introduction
• Statistics Counting Techniques

• Computer science Pigeonhole Principle

Permutations &
• Game theory Combinations

• Information theory Exercises

• ...

7.5
Counting
Problems
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents
• Number of passwords a hacker should try if he wants to use Introduction
brute force attack Counting Techniques

• Number of possible outcomes in experiments Pigeonhole Principle

Permutations &
• Number of operations used by an algorithm Combinations

Exercises

7.6
Counting
Product Rule
Huynh Tuong Nguyen,
Example Tran Tuan Anh, Nguyen
Ngoc Le

There are 32 routers in a computer center. Each router has 24


ports. How many different ports in the center?

Solution
There are two tasks to choose a port: Contents

1 picking a router Introduction

Counting Techniques
2 picking a port on this router
Pigeonhole Principle
Because there are 32 ways to choose the router and 24 ways to Permutations &
Combinations
choose the port no matter which router has been selected, the
Exercises
number of ports are 32 × 24 = 768 ports.

Definition (Product Rule (Luật nhân))


Suppose that a procedure can be broken down into a sequence of
two tasks. If there are n1 ways to do the first task and for each of
these ways of doing the first task, there are n2 ways to do the
second task, then there are n1 × n2 ways to do the procedure.
Can be extended to T1 , T2 , . . ., Tm tasks in sequence.
7.7
Counting
More examples
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example (1)
Two new students arrive at the dorm and there are 12 rooms
available. How many ways are there to assign different rooms to
two students? Contents

Introduction

Counting Techniques

Pigeonhole Principle
Example (2)
Permutations &
Combinations
How many different bit strings of length seven are there?
Exercises

Example (3)
How many one-to-one functions are there from a set with m
elements to one with n elements?

7.8
Counting
Sum Rule
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
A student can choose a project from one of three fields:
Information system (32 projects), Software Engineering (12
Contents
projects) and Computer Science (15 projects). How many ways are
Introduction
there for a student to choose?
Counting Techniques
Solution: 32 + 12 + 15 Pigeonhole Principle

Permutations &
Combinations
Definition (Sum Rule (Luật cộng))
Exercises

If a task can be done either in one of n1 ways or in one of n2


ways, there none of the set of n1 ways is the same as any of the
set of n2 ways, then there are n1 + n2 ways to do the task.
Can be extended to n1 , n2 , . . ., nm disjoint ways.

7.9
Counting
Using Both Rules
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Example
In a computer language, the name of a variable is a string of one
or two alphanumeric characters, where uppercase and lowercase
letters are not distinguished. Moreover, a variable name must
begin with a letter and must be different from the five strings of
two characters that are reserved for programming use. How many Contents

Introduction
different variables names are there in this language?
Counting Techniques

Pigeonhole Principle
Solution Permutations &
Combinations
Let V equal to the number of different variable names.
Exercises
Let V1 be the number of these that are one character long, V2 be
the number of these that are two characters long. Then, by sum
rule, V = V1 + V2 .
Note that V1 = 26, because it must be a letter. Moreover, there
are 26 · 36 strings of length two that begin with a letter and end
with an alphanumeric character. However, five of these are
excluded, so V2 = 26 · 36 − 5 = 931. Hence V = V1 + V2 = 957
different names for variables in this language.
7.10
Counting
Inclusion-Exclusion
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
How many bit strings of length eight either start with a 1 bit or
end with the two bits 00?
Contents

Introduction

Counting Techniques
Solution
Pigeonhole Principle

• Bit string of length eight that begins with a 1 is 27 = 128 Permutations &
Combinations
ways Exercises

• Bit string of length eight that ends with 00 is 26 = 64 ways


• Bit string of length eight that begins with 1 and ends with
00: 25 = 32 ways
Number of satisfied bit strings are 27 + 26 − 25 = 160 ways.

7.11
Counting
Inclusion-Exclusion
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Introduction
|A ∪ B| = |A| + |B| − |A ∩ B| Counting Techniques

Pigeonhole Principle

Permutations &
Combinations

Exercises

7.12
Counting
Inclusion-Exclusion
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents
|A ∪ B ∪ C| =??? Introduction

Counting Techniques

Pigeonhole Principle

Permutations &
Combinations

Exercises

7.13
Counting
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

In a certain survey of a group of students, 87 students indicated Contents

they liked Arsenal, 91 indicated that they liked Chelsea and 91 Introduction

indicated that they liked MU. Of the students surveyed, 9 liked Counting Techniques

only Arsenal, 10 liked only Chelsea, 12 liked only MU and 40 liked Pigeonhole Principle

Permutations &
all three clubs. How many of the student surveyed liked both MU Combinations

and Chelsea but not Arsenal? Exercises

7.14
Counting
Pigeonhole Principle
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Introduction

Counting Techniques

Pigeonhole Principle

Permutations &
Combinations

Exercises

7.15
Counting
Examples
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Example (1) Ngoc Le

Among any group of 367 people, there must be at least two with
the same birthday.
Because there are only 366 possible birthdays.
Contents
Example (2) Introduction

Counting Techniques
In any group of 27 English words, there must be at least two that
Pigeonhole Principle
begin with the same letter.
Permutations &
Combinations
Because there are 26 letters in the English alphabet. Exercises

7.16
Counting
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Prove that if seven distinct numbers are selected from
{1, 2, . . . , 11}, then some two of these numbers sum to 12.
Contents

Solution Introduction

Counting Techniques
1 Pigeons: seven numbers from {1, 2, . . . , 11} Pigeonhole Principle

2 Pigeonholes: corresponding to six sets, {1, 11}, {2, 10}, Permutations &
Combinations
{3, 9}, {4, 8}, {5, 7}, {6} Exercises

3 Assigning rule: selected number gets placed into the


pigeonhole corresponding to the set that contains it.
4 Apply the pigeon hole: seven numbers are selected and
placed in six pigeonholes, some pigeonhole contains two
numbers.

7.17
Counting
Examples – Permutations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
How many ways can we arrange three students to stand in line for
a picture?

Contents

Introduction

Counting Techniques

Pigeonhole Principle

Permutations &
Number of choices: 6 = 3! Combinations

Exercises

7.18
Counting
Permutations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
A permutation (hoán vị) of a set of distinct objects is an ordered
arrangement of these objects.
An ordered arrangement of r elements of a set is called an
r-permutation (hoán vị chập r). Contents

Introduction

n! Counting Techniques
P (n, r) = Pigeonhole Principle
(n − r)!
Permutations &
Combinations

Exercises
Example
How many ways are there to select a first-prize winner, a
second-prize winner, and a third-prize winner from 100 different
people who have entered a contest?

P (100, 3) = 100 · 99 · 98 = 970, 200

7.19
Counting
Examples – Combinations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

How many ways to choose two students from a group of four to


offer scholarship?

Contents

Introduction

Counting Techniques

Pigeonhole Principle

Permutations &
Combinations

Exercises

Number of choices: 6

7.20
Counting
Combinations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition (Combinations)
An r-combination (tổ hợp chập r) of elements of a set is an
unordered selection of r elements from the set. Thus, an
r-combination is simply a subset of the set with r elements.
Contents
 
n n! Introduction
C(n, r) = = Counting Techniques
r r!(n − r)!
Pigeonhole Principle

Permutations &
Combinations

Example Exercises

How many ways are there to select eleven players from a


22-member football team to start up?

22!
C(22, 11) = = 705432
11!11!

7.21
Counting
Exercises – Permutations with Repetition
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

1 Suppose that a salesman has to visit eight different cities. She


must begin her trip in a specified city, but she can visit the Contents
other seven cities in any order she wishes. How many possible Introduction
orders can the salesman use when visiting these cities? Counting Techniques

2 Suppose that there are 9 faculty members in CS department Pigeonhole Principle

Permutations &
and 11 in CE department. How many ways are there to select Combinations
a defend committee if the committee is to consist of three Exercises

faculty members from the CS and four from the CE


department?

7.22
Counting
Permutations with Repetition
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
How many strings of length r can be formed from the English Contents
alphabet? Introduction

By product rule, we see that there are 26r strings of length r. Counting Techniques

Pigeonhole Principle

Permutations &
Combinations

Theorem Exercises

The number of r-permutations of a set of n objects with


repetition allowed is nr .

7.23
Counting
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Question: How many ways we can choose 3 students from the Introduction

Counting Techniques
faculties of Computer Science, Electrical Engineering and
Pigeonhole Principle
Mechanical Engineering?
Permutations &
Combinations

Exercises

7.24
Counting
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

CCC CEM Contents

CCE EEE Introduction

Counting Techniques
CCM EEM
Pigeonhole Principle
CEE EMM
Permutations &
CMM MMM Combinations

Exercises

7.25
Counting
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

CCC CEM ? ? ?|| ?| ? |?


CCE EEE ??|?| |???| Contents

CCM EEM ? ? ||? | ? ?|? Introduction

CEE EMM ?| ? ?| ??? Counting Techniques

CMM MMM ?|| ? ? || ? ?? Pigeonhole Principle

Permutations &
Combinations

Exercises

How many ways to put ? and | ???

7.26
Counting
Combinations with Repetition
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Theorem
There are C(n + r − 1, r) r-combinations from a set with n
Contents
elements when repetition of elements is allowed.
Introduction

Counting Techniques
Example Pigeonhole Principle

How many solutions does the equation Permutations &


Combinations

Exercises
x1 + x2 + x3 = 11

have, where x1 , x2 , and x3 are nonnegative integers?

7.27
Counting
Examples
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Question: How many permutations are there of MISSISSIPPI? Contents

Introduction

Counting Techniques

Pigeonhole Principle

Permutations &
MISSISSIPPI ≡ MISSISSIPPI Combinations

Exercises

7.28
Counting
Permutations with Indistinguishable Objects
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Theorem
The number of different permutations of n objects, where there
are n1 indistinguishable objects of type 1, n2 indistinguishable Contents

objects of type 2, . . . , and nk indistinguishable objects of type k, is Introduction

Counting Techniques

n! Pigeonhole Principle

n1 !n2 ! · · · nk ! Permutations &


Combinations

Exercises

Example
How many permutations are there of MISSISSIPPI?

7.29
Counting
Hãng hàng không
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Ba hãng hàng không có tuyến từ Hà Nội đến TP.HCM và hai


hãng có tuyến từ TP.HCM đến Phú Quốc. Hỏi có tất cả bao nhiêu
cặp hãng hàng không khi mua vé một lộ trình đi từ Hà Nội đến Contents
Phú Quốc mà quá cảnh tại TP.HCM ? Introduction

A) 2 Counting Techniques

Pigeonhole Principle
B) 4
Permutations &
C) 5 Combinations

Exercises
D) 6
E) 7
F) Các chọn lựa khác đều sai

7.30
Counting
Quần vợt
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Hai tuyển thủ quần vợt nữ thi đấu cùng nhau trong tối đa 3 hiệp.
Tuyển thủ nào thắng ít nhất hai trận thì sẽ thắng giải. Mỗi hiệp
đấu không bao giờ xảy ra tình huống hai đội hòa nhau. Có bao Contents

nhiêu kịch bản thắng thua cho giải này? Introduction

Counting Techniques
A) 4 Pigeonhole Principle

B) 5 Permutations &
Combinations
C) 6 Exercises

D) 7
E) Các chọn lựa khác đều sai

7.31
Counting
Mã hóa
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Cần bao nhiêu byte để mã hóa n bit dữ liệu, với n = 1001


A) 2
B) 4
C) 10
Contents
D) 236
Introduction
E) 1001 Counting Techniques

F) Các chọn lựa khác đều sai Pigeonhole Principle

Permutations &
Có bao nhiêu chuỗi bit có độ dài 100 với số bit 0 và bit 1 như Combinations

nhau? Exercises

100
A) 2
100

B) 50
50
C) 2
D) 1001
E) Các chọn lựa khác đều sai

7.32
Counting
Bút màu
Huynh Tuong Nguyen,
Trong lúc đến trường, hai bạn An và Bão đếm số lượng bút bi Tran Tuan Anh, Nguyen
Ngoc Le
màu đem theo. Tổng cộng 10 cây viết gồm có hai màu: xanh (X)
và tím (T).
Đặt XA , TA lần lượt biểu diễn số lượng bút xanh và tím của bạn
An.
Và đặt XB , TB lần lượt biểu diễn số lượng bút xanh và bút tím
của bạn Bão. Contents

Bạn An nhận thấy rằng số bút màu xanh của An lớn hơn hoặc Introduction

bằng 2 lần số bút màu tím của anh ta. Bạn Bão có số bút màu Counting Techniques

Pigeonhole Principle
xanh lớn hơn hoặc bằng 2 lần số bút màu xanh của bạn An.
Permutations &
Hỏi có bao nhiêu trường hợp có thể có của bộ giá trị (XA , TA )? Combinations

A) 2 Exercises

B) 4
C) 6
D) 8
E) nhiều hơn 10
F) Các chọn lựa khác đều sai
Bạn Bão không có bút màu tím. Hỏi có bao nhiêu trường hợp có
thể có của bộ giá trị (XA , TA , XB , TB )?
A) 2
B) 5 7.33
Counting
Tiệc
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Một buổi tiệc nhỏ có 2 bàn tròn với số lượng khách là n (n ≥ 3).
Bàn thứ nhất có k ghế ngồi và bàn thứ hai có n − k ghế ngồi
(k ≥ 3, n − k ≥ 3). Các ghế ngồi đều được đánh số khác nhau từ
1, 2, . . . n. Trong n vị khách thì có 3 vị khách đặc biệt là X, Y, Z Contents

cần được sắp xếp ngồi cạnh nhau (theo một thứ tự nào đó). Hỏi Introduction

có bao nhiêu cách sắp xếp hợp lệ các vị khách theo số ghế ? Counting Techniques

Pigeonhole Principle
A) 6k!(k − 3)!(n − k)! + 6(n − k)(n − k − 3)!k!
Permutations &
B) 6(n − k)(n − k − 3)!k! Combinations

Exercises
C) 6k!(k − 3)!(n − k)!
D) 6k!(n − k)!
E) 6n(n − 3)!
F) Các chọn lựa khác đều sai

7.34
Counting
Tiệc
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Một buổi tiệc nhỏ có 2 bàn tròn với số lượng khách là n (n ≥ 3).
Bàn thứ nhất có k ghế ngồi và bàn thứ hai có n − k ghế ngồi
(k ≥ 3, n − k ≥ 3). Các ghế ngồi đều được đánh số khác nhau từ
1, 2, . . . n. Trong n vị khách thì có 3 vị khách đặc biệt là X, Y, Z Contents

cần được sắp xếp ngồi cạnh nhau (theo một thứ tự nào đó). Hỏi Introduction

có bao nhiêu cách sắp xếp hợp lệ các vị khách theo số ghế ? Counting Techniques

Pigeonhole Principle
A) 6k!(k − 3)!(n − k)! + 6(n − k)(n − k − 3)!k!
Permutations &
B) 6(n − k)(n − k − 3)!k! Combinations

Exercises
C) 6k!(k − 3)!(n − k)!
D) 6k!(n − k)!
E) 6n(n − 3)!
F) Các chọn lựa khác đều sai

7.35
Counting
Con rắn
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Một phép hoán vị (x1 , x2 , . . . , xn ) của các số {1, 2, . . . , n} được Ngoc Le
gọi là con rắn (có độ dài n) nếu x1 < x2 > x3 < x4 . . ..
Ví dụ:
n = 2, ta chỉ có 1<2 ,  số con rắn là 1,
n = 3, 1<3>2
, số con rắn là 2,
2<3>1
n = 4, 1<3>2<4 
 Contents
 Introduction
1<4>2<3 

 Counting Techniques
2<3>1<4 , số con rắn là 5. Pigeonhole Principle
2<4>1<3 


 Permutations &
3<4>1<2 Combinations

Exercises
Tìm số con rắn có độ dài 5.
A) 12
B) 14
C) 16
D) 17
E) 20
F) Các chọn lựa khác đều sai
7.36
Counting
Pokemon
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Một người sở hữu 6 Pokemon: Pikachu, Gengar, Meowth, Poliwag,


Spinda và Ditto. Anh ta muốn xếp các Pokemon này trong hai bàn
dài B1 , B2 đặt đối diện nhau để dạy chúng nói tiếng Việt. Mỗi
bàn ngồi được ba Pokemon. Hỏi có bao nhiêu cách sắp xếp chỗ Contents

ngồi sao cho Pikachu và Meowth không ngồi cạnh nhau và cũng Introduction

không ngồi đối diện nhau? Counting Techniques

Pigeonhole Principle
A) 720
Permutations &
B) 522 Combinations

Exercises
C) 480
D) 384
E) 120
F) Các chọn lựa khác đều sai

7.37
Counting
Giải đấu vật
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Trong một giải đấu đấu vật để xác định một nhà vô địch thế giới,
101 vận động viên từ khắp các nước đến tham dự. Hỏi cần tổ chức
bao nhiêu trận đấu biết rằng mỗi trận đấu sẽ phân biệt thắng thua Contents
giữa hai vận động viên và người thua sẽ bị loại ngay khỏi giải đấu? Introduction

A) 1 Counting Techniques

Pigeonhole Principle
B) 70
Permutations &
C) 100 Combinations

Exercises
D) 99
E) Không thể xác định con số chính xác
F) Các chọn lựa khác đều sai

7.38
Counting
Nam nữ
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Lớp của bạn An có 17 học sinh nam và 19 học sinh nữ. Nhân dịp
sinh nhật của bạn An, các bạn trong lớp đều chuẩn bị quà tặng.
Mỗi bạn nam của lớp làm 4 mô hình giấy và mỗi bạn nữ xếp 3
Contents
hoặc 5 ngôi sao may mắn để tặng.
Introduction
Biết số mô hình giấy và số ngôi sao may mắn bằng nhau, hỏi bạn
Counting Techniques
An là nam hay nữ? Pigeonhole Principle

A) Không thể xác định được giới tính Permutations &


Combinations
B) Không tồn tại đáp án Exercises

C) Nam
D) Nữ
E) Các chọn lựa khác đều sai

7.39
Counting
Chia quà
Huynh Tuong Nguyen,
Xét bài toán chia k phần quà hoàn toàn giống nhau cho n sinh Tran Tuan Anh, Nguyen
Ngoc Le
viên CSE-HCMUT sao cho mỗi sinh viên phải được ít nhất một
phần quà và các phần quà phải được phân phối hết.
Với k = 6 và n = 2, hỏi có bao nhiêu cách chia phần quà?
A) 3
B) 4
Contents
C) 5
Introduction
D) 6
Counting Techniques
E) 7 Pigeonhole Principle

F) 20 Permutations &
Combinations
G) Các chọn lựa khác đều sai Exercises

Với k = 13 và n = 3, hỏi có bao nhiêu cách chia phần quà?


A) 45
B) 10
C) 60
D) 66
E) 91
F) 286
G) Các chọn lựa khác đều sai 7.40
Counting
Passphrase
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Việc lưu trữ tài khoản các đồng tiền mã hóa (crypto-currencies) ở
một ví thường được quản lý bới một cơ chế bảo mật Passphrase.
Contents
Với người dùng, thay vì nhớ mã code hoặc mật khẩu của từng tài
Introduction
khoản, thì chỉ cần nhớ một passphrase riêng có thể truy xuất được
Counting Techniques
bất kỳ tài khoản nào trong ví riêng của mình. Pigeonhole Principle
Nhìn góc độ hệ thống, từ mã passphrase, sẽ có một cơ chế xác Permutations &
định số tài khoản của người dùng. Và để đảm bảo đủ tính bảo Combinations

Exercises
mật, trên thực tế, người ta dùng passphrase bao gồm 12 từ tiếng
Anh có nghĩa.
Hỏi có bao nhiêu khả năng xảy ra?

7.41
Discrete Probability

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

Chapter 8 Ngoc Le

Discrete Probability
Discrete Structures for Computing
Contents

Introduction
Randomness

Probability

Probability Rules

Random variables

Probability Models
Geometric Model
Binomial Model

Huynh Tuong Nguyen, Tran Tuan Anh, Nguyen Ngoc Le


Faculty of Computer Science and Engineering
University of Technology - VNUHCM
{htnguyen;trtanh}@hcmut.edu.vn
8.1
Discrete Probability
Contents
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

1 Introduction
Randomness

Contents
2 Probability
Introduction
Randomness

Probability
3 Probability Rules
Probability Rules

Random variables

Probability Models
4 Random variables Geometric Model
Binomial Model

5 Probability Models
Geometric Model
Binomial Model

8.2
Discrete Probability
Course outcomes
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Course learning outcomes

L.O.1 Understanding of logic and discrete structures


L.O.1.1 – Describe definition of propositional and predicate logic
L.O.1.2 – Define basic discrete structures: set, mapping, graphs
Contents
L.O.2 Represent and model practical problems with discrete structures Introduction
L.O.2.1 – Logically describe some problems arising in Computing Randomness

L.O.2.2 – Use proving methods: direct, contrapositive, induction Probability


L.O.2.3 – Explain problem modeling using discrete structures Probability Rules

Random variables
L.O.3 Understanding of basic probability and random variables
Probability Models
L.O.3.1 – Define basic probability theory Geometric Model
L.O.3.2 – Explain discrete random variables Binomial Model

L.O.4 Compute quantities of discrete structures and probabilities


L.O.4.1 – Operate (compute/ optimize) on discrete structures
L.O.4.2 – Compute probabilities of various events, conditional
ones, Bayes theorem

8.3
Discrete Probability
Motivations
Huynh Tuong Nguyen,
• Gambling Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Introduction
Randomness

Probability
• Real life problems
Probability Rules

Random variables

Probability Models
Geometric Model
Binomial Model

• Computer Science: cryptology – deals with encrypting codes


or the design of error correcting codes 8.4
Discrete Probability
Randomness
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Which of these are random phenomena?


• The number you receive when rolling a fair dice
• The sequence for lottery special prize (by law!) Contents

• Your blood type (No!) Introduction


Randomness

• You met the red light on the way to school Probability

• The traffic light is not random. It has timer. Probability Rules

• The pattern of your riding is random. Random variables

Probability Models

So what is special about randomness? Geometric Model


Binomial Model

In the long run, they are predictable and have relative frequency
(fraction of times that the event occurs over and over and over).

8.5
Discrete Probability
Terminology
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Introduction
Randomness

Probability

Probability Rules

Random variables

Probability Models
• Experiment (thí nghiệm): a procedure that yields one of a Geometric Model

given set of possible outcomes. Binomial Model

• Tossing a coin to see the face


• Sample space (không gian mẫu): set of possible outcomes
• {Head, Tail}
• Event (sự kiện): a subset of sample space.
• You see Head after an experiment. {Head} is an event.

8.6
Discrete Probability
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example (1)
Experiment: Rolling a die. What is the sample space?
Answer: {1, 2, 3, 4, 5, 6}
Contents

Introduction

Example (2) Randomness

Probability
Experiment: Rolling two dice. What is the sample space? Probability Rules

Answer: It depends on what we’re going to ask! Random variables

Probability Models
• The total number? Geometric Model

{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} Binomial Model

• The number of each die?


{(1,1), (1,2), (1,3), . . ., (6,6)}

Which is better?
The latter one, because they are equally likely outcomes

8.7
Discrete Probability
The Law of Large Numbers
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
The Law of Large Numbers (Luật số lớn) states that the long-run
relative frequency of repeated independent events gets closer and Contents

closer to the true relative frequency as the number of trials Introduction


Randomness
increases. Probability

Probability Rules
Example Random variables

Probability Models
Do you believe that the true relative frequency of Head when you Geometric Model

toss a coin is 50%? Binomial Model

Let’s try!

8.8
Discrete Probability
Be Careful!
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Don’t misunderstand the Law of Large Numbers (LLN). It can


lead to money lost and poor business decisions.

Example Contents

I had 8 children, all of them are girls. Thanks to LLN (!?), there Introduction
Randomness
are high possibility that the next one will be a boy. Probability
(Overpopulation!!!) Probability Rules

Random variables

Probability Models
Example Geometric Model
Binomial Model

I’m playing Bầu cua tôm cá, the fish has not appeared in recent 5
games, it will be more likely to be fish next game. Thus, I bet all
my money in fish. (Sorry, you lose!)

8.9
Discrete Probability
Probability
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
The probability (xác suất) of an event E of a finite nonempty
sample space of equally likely outcomes S is:
Contents
|E| Introduction
p(E) = . Randomness
|S|
Probability

Probability Rules

Random variables
• Note that E ⊆ S so 0 ≤ |E| ≤ |S|
Probability Models
• 0 ≤ p(E) ≤ 1 Geometric Model
Binomial Model
• 0 indicates impossibility
• 1 indicates certainty

People often say: “It has a 20% probability”

8.10
Discrete Probability
Examples
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Example (1)
What is the probability of getting a Head when tossing a coin?
Answer:
• There are |S| = 2 possible outcomes
• Getting a Head is |E| = 1 outcome, so Contents

Introduction
p(E) = 1/2 = 0.5 = 50% Randomness

Probability

Probability Rules
Example (2)
Random variables

What is the probability of getting a 7 by rolling two dice? Probability Models


Geometric Model

Answer: Binomial Model

• Product rule: There are a total of 36 equally likely possible


outcomes
• There are six successful outcomes:
(1,6), (2,5), (3,4), (4,3), (5,2), (6,1)
• Thus, |E| = 6, |S| = 36, p(E) = 6/36 = 1/6
8.11
Discrete Probability
Examples
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example (3)
We toss a coin 6 times. What is probability of H in 6th toss, if all
the previous 5 are T?
Answer: Contents

Don’t be silly! Still 1/2. Introduction


Randomness

Probability
Example (4) Probability Rules

Random variables
Which is more likely:
Probability Models
• Rolling an 8 when 2 dice are rolled? Geometric Model
Binomial Model
• Rolling an 8 when 3 dice are rolled?

Answer:
Two dice: 5/36 ≈ 0.139
Three dice: 21/216 ≈ 0.097

8.12
Discrete Probability
Formal Probability
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Rule 1 Ngoc Le

A probability is a number between 0 and 1.

0 ≤ p(E) ≤ 1

Contents

Introduction
Rule 2: Something has to happen rule Randomness

The probability of the set of all possible outcomes of a trial must Probability

be 1. Probability Rules

Random variables
p(S) = 1
Probability Models
Geometric Model
Binomial Model

Rule 3: Compliment Rule


The probability of an event occurring is 1 minus the probability
that it doesn’t occur.

p(E) = 1 − p(E)

8.13
Discrete Probability
Examples
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
What is the probability of NOT drawing a heart card from 52 deck
Contents
cards?
Introduction

Answer: Randomness

Probability
Let E be the event of getting a heart from 52 deck cards. We
Probability Rules
have: Random variables
p(E) = 13/52 = 1/4 Probability Models
By the compliment rule, the probability of NOT getting a heart Geometric Model
Binomial Model
card is:
p(E) = 1 − p(E) = 3/4

8.14
Discrete Probability
Formal Probability
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

General Addition Rule


Contents

Introduction
p(E1 ∪ E2 ) = p(E1 ) + p(E2 ) − p(E1 ∩ E2 ) Randomness

Probability

Probability Rules
• If E1 ∩ E2 = ∅: They are disjoint, which means they can’t Random variables
occur together Probability Models

• then, p(E1 ∪ E2 ) = p(E1 ) + p(E2 ) Geometric Model


Binomial Model

8.15
Discrete Probability
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example (1)
If you choose a number between 1 and 100, what is the probability
that it is divisible by either 2 or 5?
Contents
Short Answer: Introduction
50 20 10 3
100 + 100 − 100 = 5 Randomness

Probability

Probability Rules

Example (2) Random variables

Probability Models
There are a survey that about 45% of VN population has Type O Geometric Model

blood, 40% type A, 11% type B and the rest type AB. What is the Binomial Model

probability that a blood donor has Type A or Type B?


Short Answer:
40% + 11% = 51%

8.16
Discrete Probability
Conditional Probability (Xác suất có điều kiện)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• “Knowledge” changes probabilities

Contents

Introduction
Randomness

Probability

Probability Rules

Random variables

Probability Models
Geometric Model
Binomial Model

8.17
Discrete Probability
Conditional Probability
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
p(E | F ) = Probability of event E given that event F has
occurred. Contents

Introduction
Randomness

Probability
General Multiplication Rule
Probability Rules

Random variables

Probability Models

p(E ∩ F ) = p(E) × p(F | E) Geometric Model


Binomial Model

= p(F ) × p(E | F )

8.18
Discrete Probability
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
What is the probability of drawing a red card and then another red
card without replacement (không hoàn lại)? Contents

Introduction
Randomness

Solution Probability

Probability Rules
E: the event of drawing the first red card
Random variables
F : the event of drawing the second red card
Probability Models
p(E) = 26/52 = 1/2 Geometric Model

p(F | E) = 25/51 Binomial Model

So the event of drawing a red card and then another red card is
p(E ∩ F ) = p(E) × p(F | E) = 1/2 × 25/51 = 25/102

8.19
Discrete Probability
Independence
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
Events E and F are independent (độc lập) whenever

p(E | F ) = p(E)
Contents

• The outcome of one event does not influence the probability Introduction
Randomness

of the other. Probability

• Example: p(“Head”|“It’s raining outside”) = p(“Head”) Probability Rules

Random variables
• If E and F are independent
Probability Models
Geometric Model

p(E ∩ F ) = p(E) × p(F ) Binomial Model

Disjoint 6= Independence
Disjoint events cannot be independent. They have no outcomes in
common, so knowing that one occurred means the other did not.

8.20
Discrete Probability
Bayes’s Theorem
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
If we know that the probability that a person has tuberculosis
(TB) is p(TB) = 0.0005. Contents
We also know p(+|TB) = 0.999 and p(−|TB) = 0.99. Introduction

What is p(TB|+) and p(TB|−)? Randomness

Probability

Probability Rules

Random variables
Theorem (Bayes’s Theorem) Probability Models
Geometric Model
Binomial Model

p(E | F )p(F )
p(F | E) =
p(E | F )p(F ) + p(E | F )p(F )

8.21
Discrete Probability
Expected Value: Center
Huynh Tuong Nguyen,
An insurance company charges $50 a year. Can company make a Tran Tuan Anh, Nguyen
Ngoc Le
profit? Assuming that it made a research on 1000 people and have
following table:
Outcome Payroll Probability
x p(X = x)
Contents
1
Death 10,000 1000 Introduction
Randomness
2
Disability 5000 1000 Probability

997 Probability Rules


Neither 0 1000 Random variables

• X is a discrete random variable (biến ngẫu nhiên rời rạc) Probability Models
Geometric Model

The company expects that they have to pay each customer: Binomial Model

1 2 997
E(X) = $10, 000( ) + $5000( ) + $0( ) = $20
1000 1000 1000

Expected value (giá trị kỳ vọng)


P
E(X) = x · p(X = x)
8.22
Discrete Probability
Variance: The Spread
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
• Of course, the expected value $20 will not happen in reality
• There will be variability. Let’s calculate!

• Variance P
(phương sai)
V (X) = (x − E(X))2 · p(X = x) Contents

• V (X) = 99802 ( 1 ) + 49802 ( 2 ) + (−20)2 ( 997 ) = Introduction


1000 1000 1000 Randomness
149, 600 Probability
• Standard deviation (độ lệch chuẩn) Probability Rules
p
SD(X) = V (X) Random variables

• SD(X) = 149, 600 ≈ $386.78 Probability Models
Geometric Model
Binomial Model

Comment
The company expects to pay out $20, and make $30. However,
the standard deviation of $386.78 indicates that it’s no sure thing.
That’s pretty big spread (and risk) for an average profit of $20.

8.23
Discrete Probability
Bernoulli Trials
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Some people madly drink Coca-Cola, hoping to find a ticket to see
Big Bang. Let’s call tearing a bottle’s label trial (phép thử ):
• There are only possible outcomes (congrats or good luck) Contents

Introduction
• The probability of success, p, is the same on every trial, say Randomness

0.06 Probability

Probability Rules
• The trials are independent. Finding a ticket in the first bottle
Random variables
does not change what might happen in the second one. Probability Models
Geometric Model
Binomial Model

• Bernoulli Trials
• Another examples: tossing a coin many times, results of
testing TB on many patients, . . .

8.24
Discrete Probability
Geometric Model (Mô hình hình học)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Question: How long it will take us to achieve a success, given p,


the probability of success?

Contents
Definition (Geometric probability model: Geom(p)) Introduction
Randomness
p = probability of success (q = 1 − p = probability of failure) Probability
X = number of trials until the first success occurs Probability Rules

Random variables
p(X = x) = q x−1 p Probability Models
Geometric Model

1 Binomial Model
Expected value: µ = p q
q
Standard deviation: σ = p2

8.25
Discrete Probability
Geometric Model: Example
Huynh Tuong Nguyen,
Example Tran Tuan Anh, Nguyen
Ngoc Le

If the probability of finding a Sound Fest ticket is p = 0.06, how


many bottles do you expect to open before you find a ticket?
What is the probability that the first ticket is in one of the first
four bottles?
Contents

Solution Introduction
Randomness

Let X = number of trials until a ticket is found Probability


We can model X with Geom(0.06). Probability Rules
1
E(X) = 0.06 ≈ 16.7 Random variables

Probability Models
Geometric Model
Binomial Model
P (X ≤ 4) = P (X = 1) + P (X = 2) + P (X = 3) + P (X = 4)
= (0.06) + (0.94)(0.06) + (0.94)2 (0.06)
+(0.94)3 (0.06)
≈ 0.2193

Conclusion: We expect to open 16.7 bottles to find a ticket.


About 22% of time we’ll find one within the first 4 bottles. 8.26
Discrete Probability
Binomial Model (Mô hình nhị thức)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Previous Question: How long it will take us to achieve a success,


given p, the probability of success?
New Question: You buy 5 Coca-Cola. What’s the probability you
get exactly 2 Sound Fest tickets?
Contents

Introduction

Definition (Binomial probability model: Binom(n, p)) Randomness

Probability
n = number of trials Probability Rules

p = probability of success (q = 1 − p = probability of failure) Random variables

X = number of successes in n trials Probability Models


Geometric Model
  Binomial Model
n x n−x
p(X = x) = p q
x

Expected value: µ = np

Standard deviation: σ = npq

8.27
Discrete Probability
Binomial Model: Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Example Ngoc Le

Suppose you buy 20 Coca-Cola bottles. What are the mean and
standard deviation of the number of winning bottles among them?
What is the probability that there are 2 or 3 tickets?

Solution Contents

Introduction
Let X = number of tickets among n = 20 bottles Randomness

We can model X with Binom(20, 0.06). Probability


E(X) = np = 20(0.06)
p = 1.2 Probability Rules

SD(X) = npq = 20(0.06)(0.94) ≈ 1.96 Random variables

Probability Models

P (X = 2 or 3) = P (X = 2) + P (X = 3) Geometric Model
Binomial Model
   
20 20
= (0.06)2 (0.94)18 + (0.06)3 (0.94)17
2 3
≈ 0.2246 + 0.0860 = 0.3106

Conclusion: In 20 bottles, we expect to find an average of 1.2


tickets, with a sd of 1.06. About 31% of the time we’ll find 2 or 3
tickets among 20 bottles.
8.28
Introduction to Graphs

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

Chapter 9 Ngoc Le

Introduction to Graphs
Discrete Structures for Computing
Contents

Graph definitions
Terminology
Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

Huynh Tuong Nguyen, Tran Tuan Anh, Nguyen Ngoc Le


Faculty of Computer Science and Engineering
University of Technology - VNUHCM
{htnguyen;trtanh}@hcmut.edu.vn
9.1
Introduction to Graphs
Contents
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

1 Graph definitions
Terminology
Special Graphs
Contents

Graph definitions

2 Representing Graphs and Graph Isomorphism Terminology


Special Graphs
Representing Graphs Representing Graphs
Graph Isomorphism and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
3 Exercise Graph

Graph Bipartie graph


Isomorphism
Bipartie graph
Isomorphism

9.2
Introduction to Graphs
Course outcomes
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Course learning outcomes

L.O.1 Understanding of logic and discrete structures


L.O.1.1 – Describe definition of propositional and predicate logic
L.O.1.2 – Define basic discrete structures: set, mapping, graphs
Contents
L.O.2 Represent and model practical problems with discrete structures Graph definitions
L.O.2.1 – Logically describe some problems arising in Computing Terminology

L.O.2.2 – Use proving methods: direct, contrapositive, induction Special Graphs

L.O.2.3 – Explain problem modeling using discrete structures Representing Graphs


and Graph
Isomorphism
L.O.3 Understanding of basic probability and random variables Representing Graphs
Graph Isomorphism
L.O.3.1 – Define basic probability theory
Exercise
L.O.3.2 – Explain discrete random variables
Graph
Bipartie graph
L.O.4 Compute quantities of discrete structures and probabilities Isomorphism

L.O.4.1 – Operate (compute/ optimize) on discrete structures


L.O.4.2 – Compute probabilities of various events, conditional
ones, Bayes theorem

9.3
Introduction to Graphs
Motivations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

The need of the graph Its applications

• Representation/Storing • Electric circuit/board


Contents
• Searching/sorting • Chemical structure Graph definitions

• Optimization • Networking Terminology


Special Graphs

• Map, geometry, . . . Representing Graphs


and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

• Graph theory is useful for analysing “things that are Exercise


Graph
connected to other things”. Bipartie graph

• Some difficult problems become easy when represented using Isomorphism

a graph.

9.4
Introduction to Graphs
Graph
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

just only one type : or un or directed


Definition
A graph (đồ thị) G is a pair of (V, E), which are:
• V – nonempty set of vertices (nodes) (đỉnh) Contents

• E – set of edges (cạnh) Graph definitions


Terminology
Special Graphs
A graph captures abstract relationships between vertices.
Representing Graphs
and Graph
Isomorphism
2 4 2 4 Representing Graphs
Graph Isomorphism

Exercise
1 3 1 3 Graph
Bipartie graph
Isomorphism

Undirected graph Directed graph

1 -> 2 = 2 -> 1 have direct to connect ele


1->2 != 2->1

9.5
Introduction to Graphs
Undirected Graph (Đồ thị vô hướng)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition (Simple graph (đơn đồ thị))

• Each edge connects two different vertices, and Contents

• No two edges connect the same pair of vertices Graph definitions


Terminology

An edge between two vertices u and v is denoted as {u, v} Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

9.6
Introduction to Graphs
Undirected Graph
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition (Multigraph (đa đồ thị))


Graphs that may have multiple edges connecting the same vertices. Contents

Graph definitions
An unordered pair of vertices {u, v} are called multiplicity m (bội Terminology

m) if it has m different edges between. Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

9.7
Introduction to Graphs
Undirected Graph
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition (Pseudograph (giả đồ thị))


Are multigraphs that have Contents

• loops (khuyên)– edges that connect a vertex to itself Graph definitions


Terminology
Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

9.8
Introduction to Graphs
Directed Graph
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition (Directed Graph (đồ thị có hướng))


A directed graph G is a pair of (V, E), in which:
• V – nonempty set of vertices Contents

• E – set of directed edges (cạnh có hướng, arcs ) Graph definitions


Terminology

A directed edge start at u and end at v is denoted as (u, v). Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

9.9
Introduction to Graphs
Terminologies For Undirected Graph
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Neighborhood
In an undirected graph G = (V, E),
• two vertices u and v ∈ V are called adjacent (liền kề ) if they
are end-points (điểm đầu mút) of edge e ∈ E, and Contents

• e is incident with (cạnh liên thuộc) u and v Graph definitions


Terminology
• e is said to connect (cạnh nối) u and v; Special Graphs

Representing Graphs
and Graph
Isomorphism
The degree of a vertex Representing Graphs
Graph Isomorphism

The degree of a vertex (bậc của một đỉnh), denoted by deg(v) is Exercise
the number of edges incident with it, except that a loop Graph
Bipartie graph
contributes twice to the degree of that vertex. Isomorphism

• isolated vertex (đỉnh cô lập): vertex of degree 0


• pendant vertex (đỉnh treo): vertex of degree 1

loop = twice => caculate the edges in v


9.10
Introduction to Graphs
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Example
What are the degrees and neighborhoods of the vertices in these
graphs?
b c d a b c
Contents

Graph definitions
Terminology
Special Graphs
a f e g e d Representing Graphs
and Graph
G H Isomorphism
Representing Graphs
Graph Isomorphism

Solution Exercise
Graph

In G, deg(a) = 2, deg(b) = deg(c) = deg(f ) = 4, deg(d) = 1, . . . Bipartie graph


Isomorphism
Neiborhoods of these vertices are
N (a) = {b, f }, N (b) = {a, c, e, f }, . . .
In H, deg(a) = 4, deg(b) = deg(e) = 6, deg(c) = 1, . . .
Neiborhoods of these vertices are
N (a) = {b, d, e}, N (b) = {a, b, c, d, e}, . . .
9.11
Introduction to Graphs
Basic Theorems
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Theorem (The Handshaking Theorem)


Let G = (V, E) be an undirected graph with m edges. Then
X Contents

2m = deg(v) Graph definitions


Terminology
v∈V
Special Graphs

Representing Graphs
(Note that this applies even if multiple edges and loops are and Graph
Isomorphism
present.) Representing Graphs
Graph Isomorphism

Exercise
Graph

Theorem Bipartie graph


Isomorphism

An undirected graph has an even number of odd-degree vertices.

9.12
Introduction to Graphs
Prove that ... hw Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

...
If the number of vertices in an undirected graph is an odd number,
then there exists an even-degree vertex. Contents

Graph definitions

... Terminology
Special Graphs

If the number of vertices in an undirected graph is an odd number, Representing Graphs


and Graph
then the number of vertices with even degree is odd. Isomorphism
Representing Graphs
Graph Isomorphism

... Exercise
Graph
If the number of vertices in an undirected graph is an even Bipartie graph

number, then the number of vertices with even degree is even. Isomorphism

9.13
Introduction to Graphs
Terminologies for Directed Graph
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Neighborhood
In an directed graph G = (V, E),
• u is said to be adjacent to (nối tới) v and v is said to be
adjacent from (được nối từ) u if (u, v) is an arc of G, and
• u is called initial vertex (đỉnh đầu) of (u, v) Contents

• v is called terminal (đỉnh cuối) or end vertex of (u, v) Graph definitions


Terminology
• the initial vertex and terminal vertex of a loop are the same. Special Graphs

Representing Graphs
and Graph
Isomorphism
The degree of a vertex Representing Graphs
Graph Isomorphism
In a graph G with directed edges: Exercise

• in-degree (bậc vào) of a vertex v, denoted by deg− (v), is Graph


Bipartie graph

the number of arcs with v as their terminal vertex. Isomorphism

• out-degree (bậc ra) of a vertex v, denoted by deg+ (v), is


the number of arcs with v as their initial vertex.
Note: a loop at a vertex contributes 1 to both the in-degree and
the out-degree of this vertex.
9.17
Introduction to Graphs
Basic Theorem
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Theorem Contents

Let G = (V, E) be a graph with directed edges. Then Graph definitions


Terminology
Special Graphs
X X
deg− (v) = deg+ (v) = |E|. = sum of edges Representing Graphs
and Graph
v∈V v∈V Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

9.18
Introduction to Graphs
Complete Graphs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

A complete graph (đồ thị đầy đủ) on n vertices, Kn , is a simple


graph that contains exactly one edge between each pair of distinct
vertices.
deg(v) always equal n-1 , n>=1
Contents

Graph definitions
Terminology
Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

K5 K4

9.20
Introduction to Graphs
Cycles
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

A cycle (đồ thị vòng ) Cn , n ≥ 3, consists of n vertices


v1 , v2 , . . . , vn and edges {v1 , v2 }, {v2 , v3 }, . . . , {vn−1 , vn }, and
{vn , v1 }. deg always equal 2, has n edges
Contents

Graph definitions
Terminology
Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

C5 C4

9.21
Introduction to Graphs
Wheels
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

We obtain a wheel (đồ thị hình bánh xe) Wn when we add an


additional vertex to a cycle Cn , for n ≥ 3, and connect this new
vertex to each of the n vertices in Cn . deg tt = n
deg xung quanh = 3 Contents
has 2n edges Graph definitions
Terminology
Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

W5 W4

9.22
Introduction to Graphs
n-cube
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

An n-dimensional hypercube (khối n chiều), Qn , is a graph that


has vertices representing the 2n bit strings of length n. Two
vertices are adjacent iff the bit strings that they represent differ in
exactly one bit position.
Contents

110 111 Graph definitions


Terminology
Special Graphs
10 11 100 101 Representing Graphs
and Graph
Isomorphism
Representing Graphs

010 011 Graph Isomorphism

0 1 Exercise
Graph
Bipartie graph
00 01 000 001 Isomorphism

Q1 Q2 Q3
Qn is bipartite

What’s about Q4 ? has 16 vertices and 16*4/2 edges


9.23
Introduction to Graphs
Applications of Special Graphs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• Local networks topologies Contents

Graph definitions
• Star, ring, hybrid Terminology

• Parallel processing Special Graphs

Representing Graphs
• Linear array and Graph
Isomorphism
• Mesh network Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

9.24
Introduction to Graphs
Exercise hw
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Exercise (5)
Give the number of edges in function of number of vertices in a
complete graph Kn . (n-1)*n/2

Exercise (6) Contents

Graph definitions
Give an undirected simple graph G = (V, E) with |V | = n, show Terminology

that Special Graphs

Representing Graphs
a ∀v ∈ V , deg(v) < n, and Graph
Isomorphism
b there does not exist simultaneously both a vertex of degree 0 Representing Graphs
Graph Isomorphism
and a vertex of degree (n − 1) with n ≥ 2, Exercise

c deduce that there are at least two vertices of the same degree. Graph
Bipartie graph

0+1+2+..+n-1 Isomorphism

Exercise (7)
Is it possible that each person has exactly 3 friends in the same
group of 9 people ? 3*9=27 ko chia het cho 2

9.25
Introduction to Graphs
Bipartite Graphs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
A simple graph G is called bipartite (đồ thị phân đôi) if its vertex
set V can be partitioned into two disjoint sets V1 and V2 such that
every edge in the graph connects a vertex in V1 and a vertex in V2
(so that no edge in G connects either two vertices in V1 or two
Contents
vertices in V2 ) neu do thi con la tam giac thi ko phan doi Graph definitions
Terminology
Special Graphs
Example
Representing Graphs
and Graph
C6 is bipartite Cn is bipartie if n is even Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

C6 Cn la do thi vong da hc o tren

9.29
Introduction to Graphs
Complete Bipartite Graphs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
A complete bipartite Km,n is a graph that
• has its vertex set partitioned into two subsets of m and n
vertices, respectively,
Contents
• with an edge between two vertices iff one vertex is in the first Graph definitions
subset and the other is in the second one Terminology
Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

K3,3
moi dinh dc noi voi all dinh o subset kia

9.30
Introduction to Graphs
Bipartite graphs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example (Bipartite graphs?)

• C6
• Cn
• K3 Contents

• Kn Graph definitions
Terminology

• the following graph Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

9.31
Introduction to Graphs
New Graph From Old
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
A subgraph (đồ thị con) of a graph G = (V, E) is a graph
H = (W, F ) where W ⊆ V and F ⊆ E.
Contents
Definition Graph definitions

The union (hợp) of two simple graphs G1 = (V1 , E1 ) and Terminology


Special Graphs
G2 = (V2 , E2 ) is a simple graph with vertex set V1 ∪ V2 and edge Representing Graphs
set E1 ∪ E2 . The union of G1 and G2 is denoted by G1 ∪ G2 . and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

G1 G2 G1 ∪ G2

9.32
Introduction to Graphs
Planar Graphs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Graph definitions
Terminology
Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

9.33
Introduction to Graphs
Planar Graphs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
• A graph is called planar (phẳng ) if it can be drawn in the
plane without any edges crossing.
• Such a drawing is called planar representation (biểu diễn
Contents
phẳng ) of the graph. Graph definitions
Terminology
Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

K4 K4 with no crossing

9.34
Introduction to Graphs
Important Corollaries
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Corollary

• If G is a connected planar simple graph with e edges and v


vertices where v ≥ 3, then e ≤ 3v − 6.
• If G is a connected planar simple graph with e edges and v
vertices where v ≥ 3, and no circuits of length 3, then Contents
e ≤ 2v − 4. Graph definitions
ko chu trinh 3 canh Terminology
Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

K3,3
Non-planar
K5
Non-planar

9.35
Introduction to Graphs
Elementary Subdivision
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
• Given a planar graph G, an elementary subdivision (phân chia
sơ cấp) is removing an edge {u, v} and adding a new vertex
w together with edges {u, w} and {w, v}. Contents

• Graphs G1 = (V1 , E1 ) and G2 = (V2 , E2 ) are called Graph definitions


Terminology

homeomorphic (đồng phôi) if they can obtained from the Special Graphs

same graph by a sequence of elementary subdivisions. Representing Graphs


and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

9.36
Introduction to Graphs
Kuratowski’s Theorem
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Theorem
A graph is nonplanar iff it contains a subgraph homeomorphic to
K3,3 or K5 .
Contents

Graph definitions
Terminology

a Special Graphs

Representing Graphs
and Graph
f f d Isomorphism
c j Representing Graphs
e b
j g Graph Isomorphism

a g Exercise
Graph
i h
e Bipartie graph
Isomorphism
i h
d c

9.37
Introduction to Graphs
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Exercise
• Is K4 planar?
• Is Q3 planar?

110 111 Contents

Graph definitions
Terminology

100 101 Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism
010 011 Exercise
Graph
Bipartie graph
Isomorphism
000 001

K4 Q3

9.38
Introduction to Graphs
Adjacency Lists (Danh sách kề)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Vertex Adjacent vertices Initial vertex Terminal vertices


a b, c, e a b, c, d, e
b a b b, d
c a, d, e c a, c, e Contents

d c, e d c, e Graph definitions
Terminology
e a, c, d e b, c, d Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

9.39
Introduction to Graphs
Adjacency Matrices
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
Adjacency matrix (Ma trận kề ) AG of G = (V, E)
• Dimension |V | × |V |
• Matrixelements
1 if (vi , vj ) ∈ E Contents
aij =
0 otherwise Graph definitions

vo huong : doi xung qua duong cheo Terminology


Special Graphs

chinh Representing Graphs


a b and Graph
Isomorphism
Representing Graphs

 a b c d 
Graph Isomorphism

Exercise
a 0 1 1 1 Graph

b  1 0 1 0  Bipartie graph
  Isomorphism
c  1 1 0 0 
d 1 0 0 0

c d

9.40
Introduction to Graphs
Examples
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Give the graph defined by the following adjacency matrix
Contents

Graph definitions

 A B C D E  Terminology
Special Graphs

Representing Graphs
A 
 0 0 1 1 0 
 and Graph
Isomorphism
B 
 0 1 0 1 0 
 Representing Graphs

C 
 1 0 0 1 0 

Graph Isomorphism

D 
 1 1 1 0 1 

Exercise
Graph
E  0 0 0 1 0  Bipartie graph
Isomorphism

9.41
Introduction to Graphs
Adjacency Matrices
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Give the directed graph defined by the following adjacency matrix
Contents

Graph definitions

 A B C D E  Terminology
Special Graphs

Representing Graphs
A 
 1 0 1 1 0 
 and Graph
Isomorphism
B 
 0 0 0 0 0 
 Representing Graphs

C 
 1 0 0 0 0 

Graph Isomorphism

D 
 1 1 1 0 1 

Exercise
Graph
E  1 0 0 0 0  Bipartie graph
Isomorphism

9.42
Introduction to Graphs
Incidence Matrices
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
Incidence matrix (ma trận liên thuộc) MG of G = (V, E)
• Dimension |V | × |E|
• Matrix 
elements
1 if ej is incident with vi Contents
mij =
0 otherwise Graph definitions
Terminology
Special Graphs

Representing Graphs
a e2 b and Graph
Isomorphism
Representing Graphs

 e1 e2 e3 e4 
Graph Isomorphism

Exercise
a 1 1 1 0 Graph
e1 b  0 1 0 1  Bipartie graph
e4 e3   Isomorphism
c  1 0 0 1 
d 0 0 1 0

c d

9.43
Introduction to Graphs
Examples
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Give incidence matrix according to the following graph
Contents

F Graph definitions
Terminology
Special Graphs
B C Representing Graphs
and Graph
Isomorphism
Representing Graphs
A Graph Isomorphism

Exercise
Graph

E D Bipartie graph
Isomorphism
G

9.44
Introduction to Graphs
Graph Isomorphism
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
G1 = (V1 , E1 ) and G2 = (V2 , E2 ) are isomorphic (đẳng cấu) if
there is a one-to-one function f from V1 to V2 with the property
that a and b are adjacent in G1 iif f (a) and f (b) are adjacent in Contents
G2 , for all a and b in V1 . Such a function f is called an Graph definitions
isomorphism (một đẳng cấu). Terminology
Special Graphs
(i.e. there is a one-to-one correspondence between vertices of the Representing Graphs
two graphs that preserves the adjacency relationship.) and Graph
Isomorphism
Representing Graphs
Graph Isomorphism
u1 u2 v1 v2
Isomorphism function f : U −→ Exercise
Graph

V with Bipartie graph

f (u1 ) = v1 f (u2 ) = v4 Isomorphism

f (u3 ) = v3 f (u4 ) = v2
u3 u4 v3 v4

9.45
Introduction to Graphs
Bipartie graph
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Graph definitions
Terminology
Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

9.46
Introduction to Graphs
Isomorphism ?
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Graph definitions
Terminology
Special Graphs

Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

Exercise
Graph
Bipartie graph
Isomorphism

9.47
Introduction to Graphs
Isomorphism?
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

A
A B Contents

B Graph definitions
Terminology
Special Graphs

C F C Representing Graphs
and Graph
Isomorphism
Representing Graphs
Graph Isomorphism

D E D Exercise
Graph
G2
F E Bipartie graph
Isomorphism
G1

9.48
Introduction to Graphs
Isomorphism?
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

A1 A2 B2

Contents

Graph definitions
Terminology

D1 E1 E2 F2 Special Graphs

Representing Graphs
and Graph
Isomorphism
F1 Representing Graphs
Graph Isomorphism

Exercise
C1 B1 D2 C2 Graph

G1 G2 Bipartie graph
Isomorphism

9.49
Introduction to Graphs
Isomorphism
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Are the simple graphs with the following adjacency matrices


isomorphic ?
   
0 0 1 0 1 1 Contents

1  0 0 1   1 0 0  Graph definitions
Terminology
1 1 0 1 0 0 Special Graphs
    Representing Graphs
0 1 0 1 0 1 1 1 and Graph
 1 0 Isomorphism
0 1   1 0 0 1  Representing Graphs
2    
 0 0 0 1   1 0 0 1  Graph Isomorphism

1 1 1 0 1 1 1 0 Exercise
Graph
   
0 1 1 0 0 1 0 1 Bipartie graph
Isomorphism
 1 0 0 1   1 0 0 0 
3    
 1 0 0 1   0 0 0 1 
0 1 1 0 1 0 1 0

9.50
Introduction to Graphs
Isomorphism
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Determine whether the graphs (without loops) with the incidence


matrices are isomorphic.
Contents
   
1 0 1 1 1 0
Graph definitions
•  0 1 1   1 0 1  Terminology

1 1 0 0 1 1 Special Graphs

    Representing Graphs
1 1 0 0 0 0 1 0 0 1 and Graph
Isomorphism

•  1 0 1 0 1   0 1 1 1 0 
    Representing Graphs
Graph Isomorphism
 0 0 0 1 1   1 0 0 1 0 
Exercise
0 1 1 1 0 1 0 1 0 1 Graph
Bipartie graph
• Extend the definition of isomorphism of simple graphs to Isomorphism

undirected graphs containing loops and multiple edges.


• Define isomorphism of directed graphs

9.51
Graph connectivity

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

Chapter 10 Ngoc Le

Graph connectivity
Discrete Structures for Computing
Contents

Connectivity
Paths and Circuits

Euler and Hamilton


Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

Huynh Tuong Nguyen, Tran Tuan Anh, Nguyen Ngoc Le


Faculty of Computer Science and Engineering
University of Technology - VNUHCM
{htnguyen;trtanh}@hcmut.edu.vn
10.1
Graph connectivity
Acknowledgement
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Connectivity
Some slides about Euler and Hamilton circuits are created by Paths and Circuits

Chung Ki-hong and Hur Joon-seok from KAIST. Euler and Hamilton
Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.2
Graph connectivity
Course outcomes
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Course learning outcomes

L.O.1 Understanding of logic and discrete structures


L.O.1.1 – Describe definition of propositional and predicate logic
L.O.1.2 – Define basic discrete structures: set, mapping, graphs
Contents
L.O.2 Represent and model practical problems with discrete structures Connectivity
L.O.2.1 – Logically describe some problems arising in Computing Paths and Circuits

L.O.2.2 – Use proving methods: direct, contrapositive, induction Euler and Hamilton
Paths
L.O.2.3 – Explain problem modeling using discrete structures
Euler Paths and Circuits
Hamilton Paths and Circuits
L.O.3 Understanding of basic probability and random variables Shortest Path Problem
L.O.3.1 – Define basic probability theory Dijkstra’s Algorithm

L.O.3.2 – Explain discrete random variables Bellman-Ford Algorithm


Floyd-Warshall Algorithm
Ford’s algorithm
L.O.4 Compute quantities of discrete structures and probabilities Others

L.O.4.1 – Operate (compute/ optimize) on discrete structures Graph Coloring


L.O.4.2 – Compute probabilities of various events, conditional
ones, Bayes theorem

10.3
Graph connectivity
Contents
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

1 Connectivity
Paths and Circuits

2 Euler and Hamilton Paths


Contents
Euler Paths and Circuits
Connectivity
Hamilton Paths and Circuits Paths and Circuits

Euler and Hamilton


Paths
3 Shortest Path Problem Euler Paths and Circuits

Dijkstra’s Algorithm Hamilton Paths and Circuits

Shortest Path Problem


Bellman-Ford Algorithm Dijkstra’s Algorithm

Floyd-Warshall Algorithm Bellman-Ford Algorithm


Floyd-Warshall Algorithm
Ford’s algorithm Ford’s algorithm
Others
Others
Graph Coloring

4 Graph Coloring

10.4
Graph connectivity
Paths and Circuits
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
a b c Ngoc Le

Contents
d e f Connectivity
Paths and Circuits

Simple path of length 4 Euler and Hamilton


Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

a b c Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

d e f
Simple circuit of length 4
10.5
Graph connectivity
Path and Circuits
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition (in undirected graph)

• Path (đường đi) of length n from u to v: a sequence of n


edges {x0 , x1 }, {x1 , x2 }, . . . , {xn−1 , xn }, where x0 = u and
xn = v.
• A path is a circuit (chu trình) if it begins and ends at the Contents

same vertex, u = v. Connectivity


Paths and Circuits
• A path or circuit is simple (đơn) if it does not contain the Euler and Hamilton
same edge more than once. Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

a b c a b c Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

d e f d e f
Simple path Not simple path
10.6
Graph connectivity
Path and Circuits
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Definition (in directed graphs) Connectivity


Paths and Circuits

Path is a sequence of (x0 , x1 ), (x1 , x2 ), . . . , (xn−1 , xn ), where Euler and Hamilton


Paths
x0 = u and xn = v. Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.7
Graph connectivity
Connectedness in Undirected Graphs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Definition Ngoc Le

• An undirected graph is called connected (liên thông ) if there


is a path between every pair of distinct vertices of the graph.
• There is a simple path between every pair of distinct vertices
of a connected undirected graph.
Contents
planar : so mien = so canh - so dinh +2 Connectivity
Paths and Circuits

d e f Euler and Hamilton


b Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm

a c h g Others

Graph Coloring

Connected graph
Disconnected graph
Connected components (thành phần liên thông )
10.8
Graph connectivity
How Connected is a Graph?
Huynh Tuong Nguyen,
-A connected graph ‘G’ may have at most (n–2) cut vertices Tran Tuan Anh, Nguyen
Ngoc Le
-a cut edge e G if and only if the edge ‘e’ is not a part of any cycle in G
-the maximum number of cut edges possible is ‘n-1’ (n is num of vertices)
a d f g

Contents

Connectivity
Paths and Circuits

Euler and Hamilton


bb c e h Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

Definition Shortest Path Problem


Dijkstra’s Algorithm

• b is a cut vertex (đỉnh cắt) or articulation point (điểm khớp). Bellman-Ford Algorithm
Floyd-Warshall Algorithm
What else? Ford’s algorithm
Others
• {a, b} is a cut edge (cạnh cắt) or bridge (cầu). What else? Graph Coloring

moi quan he giua canh cat va dinh cat ?


- whenever cut edges exist, cut vertices also exist because at least one vertex of a cut edge
is a cut vertex. cut edge => cut vertex
- cut vertex !=> cut edge
10.9
Graph connectivity
How Connected is a Graph? +For any connected graph G
Huynh Tuong Nguyen,
K(G) (G) (G)
b d Vertex connectivity (K(G)), edgeTran Tuan Anh, Nguyen
Ngoc Le

connectivity ((G)), minimum number of


degrees of G((G)).

g
Contents
a e
Connectivity
Paths and Circuits

Euler and Hamilton


Paths
Definition Euler Paths and Circuits
Hamilton Paths and Circuits
• This graph doesn’t have cut vertices: nonseparable graph (đồ Shortest Path Problem
thị không thể phân tách) Dijkstra’s Algorithm
Bellman-Ford Algorithm

• The vertex cut is {c, f }, so the minimum number of vertices Floyd-Warshall Algorithm
Ford’s algorithm
in a vertex cut, vertex connectivity (liên thông đỉnh) Others

κ(G) = 2. Graph Coloring

• The edge cut is {{b, c}, {a, f }, {f, g}}, the minimum number
of edges in an edge cut, edge connectivity (liên thông cạnh)
λ(G) = 3.
10.10
Graph connectivity
Applications of Vertex and Edge Connectivity
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• Reliability of networks
Contents
• Minimum number of routers that disconnect the network Connectivity
• Minimum number of fiber optic links that can be down to Paths and Circuits

disconnect the network Euler and Hamilton


Paths
• Highway network Euler Paths and Circuits
Hamilton Paths and Circuits
• Minimum number of intersections that can be closed
Shortest Path Problem
• Minimum number of roads that can be closed Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.11
Graph connectivity
Connectedness in Directed Graphs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Definition Ngoc Le

• An directed graph is strongly connected (liên thông mạnh) if


there is a path between any two vertices in the graph (for
both directions).
• An directed graph is weakly connected (liên thông yếu) if
Contents
there is a path between any two vertices in the underlying
Connectivity
undirected graph. Paths and Circuits

Euler and Hamilton


Paths
a b a b Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem

c c Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring
e d e d

Strongly connected Weakly connected


10.12
Graph connectivity
Applications
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Example Ngoc Le

Determine whether the graphs below are isomorphic.

u1 v1

Contents
u6 u2 v6 v2 Connectivity
Paths and Circuits

Euler and Hamilton


Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


u5 u3 v5 v3 Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others
u4 v4
Graph Coloring
G H

Solution
H has a simple circuit of length three, not G.
10.13
Graph connectivity
Applications
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Determine whether the graphs below are isomorphic.

u2 v2
Contents

Connectivity
u1 u3 v1 v3 Paths and Circuits

Euler and Hamilton


Paths
Euler Paths and Circuits
u5 u4 v5 v4 Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm

Solution Bellman-Ford Algorithm


Floyd-Warshall Algorithm

Both graphs have the same vertices, edges, degrees, circuits. They Ford’s algorithm
Others
may be isomorphic. Graph Coloring
To find a possible isomorphism, we can follow paths that go
through all vertices so that the corresponding vertices in the two
graphs have the same degrees.

10.14
Graph connectivity
The Famous Problem of Seven Bridges of Königsberg
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Connectivity
Paths and Circuits

Euler and Hamilton


Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm

• Is there a route that a person crosses all the seven bridges Bellman-Ford Algorithm
Floyd-Warshall Algorithm

once? Ford’s algorithm


Others

Graph Coloring

10.17
Graph connectivity
Euler Solution
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Connectivity
Paths and Circuits

Euler and Hamilton


Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


• Euler gave the solution: It is not possible to cross all the Dijkstra’s Algorithm
Bellman-Ford Algorithm
bridges exactly once. Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.18
Graph connectivity
What is Euler Path and Circuit?
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• Euler Path (đường đi Euler ) is a path in the graph that


passes each edge only once. Contents

The problem of Seven Bridges of Königsberg can be also Connectivity


Paths and Circuits
stated: Does Euler Path exist in the graph?
Euler and Hamilton
Paths
• Euler Circuit (chu trình Euler ) is a path in the graph that Euler Paths and Circuits
Hamilton Paths and Circuits
passes each edge only once and return back to its original Shortest Path Problem
position. Dijkstra’s Algorithm
Bellman-Ford Algorithm
From Definition, Euler Circuit is a subset of Euler Path. Floyd-Warshall Algorithm
Ford’s algorithm

It must be a connected graph before being a Euler path/circuit Others

Graph Coloring

10.19
Graph connectivity
Examples of Euler Path and Circuit
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

4 3 D C

Contents

Connectivity
Paths and Circuits

Euler and Hamilton


Paths
Euler Paths and Circuits

1 2 A B Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Euler Circuit Euler Path Graph Coloring

10.20
Graph connectivity
Conditions for Existence
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

In a connected multigraph, Contents

• Euler Circuit existence: no odd-degree nodes exist in the Connectivity


Paths and Circuits

graph. Euler and Hamilton


Paths
• Euler Path existence: 2 or no odd-degree nodes exist in the Euler Paths and Circuits
Hamilton Paths and Circuits
graph.
Shortest Path Problem
Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.21
Graph connectivity
Back to the Seven Bridges Problem
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Connectivity
Paths and Circuits

Euler and Hamilton


Paths
Euler Paths and Circuits
Hamilton Paths and Circuits
• Four vertices of odd degree
Shortest Path Problem
• No Euler circuit → cannot cross each bridge exactly once, Dijkstra’s Algorithm
Bellman-Ford Algorithm

and return to starting point Floyd-Warshall Algorithm


Ford’s algorithm
• No Euler path, either Others

Graph Coloring

10.22
Graph connectivity
Searching Euler Circuits and Paths – Fleury’s Algorithm
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• Choose a random vertex (if circuit) or an odd degree vertex Contents


(if path) Connectivity
Paths and Circuits
• Pick an edge joined to another vertex so that it is not a cut
Euler and Hamilton
edge unless there is no alternative Paths
Euler Paths and Circuits
• Remove the chosen edge. The above procedure is repeated Hamilton Paths and Circuits

until all edges are covered. Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.23
Graph connectivity
Searching Euler Circuits and Paths – Hierholzer’s Algorithm
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• Choose a starting vertex and find a circuit Contents

• As long as there exists a vertex v that belongs to the current Connectivity


Paths and Circuits

tour but that has adjacent edges not part of the tour, start Euler and Hamilton
Paths
another circuit from v Euler Paths and Circuits
Hamilton Paths and Circuits

More efficient algorithm, O(n). Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.24
Graph connectivity
Exercise
Huynh Tuong Nguyen,
Example Tran Tuan Anh, Nguyen
Ngoc Le

Are these following graph Euler path (circuit)? If yes, find one.

a g f e

Contents
Euler path Connectivity
not Euler circuit Paths and Circuits

Euler and Hamilton


Paths

b c d Euler Paths and Circuits


Hamilton Paths and Circuits

a b Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm
not Euler path or circuit Floyd-Warshall Algorithm
Ford’s algorithm
Others

f c Graph Coloring

e d 10.25
Graph connectivity
Traveling Salesman Problem
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Connectivity
Paths and Circuits

Euler and Hamilton


Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Is there the possible tour that visits each city exactly once? Others

Graph Coloring

10.26
Graph connectivity
What Is A Hamilton Circuit?
Huynh Tuong Nguyen,
K : n vertices , halminton <=> deg cua moi vertex > n/2 Tran Tuan Anh, Nguyen
Ngoc Le

Definition
The circuit that visit each vertex in a graph once
Contents

Connectivity
Paths and Circuits

Euler and Hamilton


Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.27
Graph connectivity
Rules of Hamilton Circuits
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

deg(v) = 2 for ∀v in Hamilton circuit!

Rule 1 if deg(v) = 2, both edge must be used. Contents

Connectivity
Paths and Circuits
v
Euler and Hamilton
Paths
Euler Paths and Circuits

Rule 2 No subcircuit (chu trình con) can be formed. Hamilton Paths and Circuits

Shortest Path Problem


Rule 3 Once two edges at a vertex v is determined, all Dijkstra’s Algorithm
Bellman-Ford Algorithm
other edges incident at v must be removed. Floyd-Warshall Algorithm

v Ford’s algorithm
Others

Graph Coloring

10.28
Graph connectivity
Finding Hamilton Circuits
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

a
Vertices : cities
b c
Edges : possible routes Contents
d
Connectivity
Rule 1 Paths and Circuits
deg(v) = 2 Euler and Hamilton
e Paths

f Rule 3 Euler Paths and Circuits


h Hamilton Paths and Circuits
g Once two edges are determined, Shortest Path Problem
other edges must be removed Dijkstra’s Algorithm
Bellman-Ford Algorithm
i Floyd-Warshall Algorithm
Ford’s algorithm
j k Others

Graph Coloring
We get Hamilton circuit!

10.29
Graph connectivity
Existence of Hamilton Circuit
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

condition : NO SUBcircuit

Hamilton circuit does not exist for all graph. But, there is no
specific way to find whether Hamilton circuit exists or not.
Contents
Simple check by rules of Hamilton circuit Connectivity
Paths and Circuits

Euler and Hamilton


Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm

Violates Rule 2! (No subcircuit) Floyd-Warshall Algorithm


Ford’s algorithm
Others

Graph Coloring

10.30
Graph connectivity

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
We can verify nonexistence of the graph during find Hamilton Ngoc Le

circuit.

a
b c
d Contents

Connectivity
Contradict with Rule 1! Paths and Circuits

e Euler and Hamilton


Paths

f h Euler Paths and Circuits

g Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm

i Bellman-Ford Algorithm
Floyd-Warshall Algorithm

j k Ford’s algorithm
Others

Graph Coloring
Hamilton circuit doesn’t exist!

10.31
Graph connectivity
Application – Gray Code
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
The binary sequence that express consecutive numbers by differing
just one position of sequence.
Contents

Connectivity
Decimal number Binary number Gray code Paths and Circuits

1 = 001 000 Euler and Hamilton


Paths
2 = 010 100 Euler Paths and Circuits

3 = 011 110 Hamilton Paths and Circuits

Shortest Path Problem


4 = 100 010 Dijkstra’s Algorithm

5 = 101 011 Bellman-Ford Algorithm

.. .. .. Floyd-Warshall Algorithm
Ford’s algorithm
. . . Others

Graph Coloring
Used at digital communication for reduce the effect of noise; it
prevents serious changes of information by noise.

10.32
Graph connectivity
Gray Code
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

n-digit gray code can be generated by finding Hamilton circuits of


n-dimensional hypercube! Consider the case n = 3.

5 = 011
7 6 = 111
4
Contents

Connectivity
8 = 001 7 = 101
3 Paths and Circuits

Euler and Hamilton


Paths

4 = 010
6 3 = 110
5 Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm
1 = 000 2 = 100 Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm

Coordinate of each vertex is 3-digit binary sequences. Coordinates Others

of adjacent vertices differ in just on place. Hamilton circuits of a Graph Coloring

cubic graph makes the order of binary sequences!

10.33
Graph connectivity
Weighted Graphs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Dien Bien
HA NOI
296 88

Hai Phong

264605
Contents

1146 Vinh Connectivity


Paths and Circuits

Euler and Hamilton


886 Paths
Da Nang Euler Paths and Circuits

436
Hamilton Paths and Circuits

610 Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Nha Trang Ford’s algorithm
2
32

Others
6

Graph Coloring
12

HO CHI MINH
230

Can Tho

Con Dao
10.37
Graph connectivity
Problem
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

The problem is also sometimes called the single-pair shortest path problem, to
distinguish it from the following generalizations:
• The single-source shortest path problem, in which we have to find Contents

shortest paths from a source vertex v to all other vertices in the graph. Connectivity
Paths and Circuits
• The single-destination shortest path problem, in which we have to find
Euler and Hamilton
shortest paths from all vertices in the graph to a single destination Paths
vertex v. This can be reduced to the single-source shortest path problem Euler Paths and Circuits

by reversing the edges in the graph. Hamilton Paths and Circuits

Shortest Path Problem


• The all-pairs shortest path problem, in which we have to find shortest Dijkstra’s Algorithm
paths between every pair of vertices v, v 0 in the graph. Bellman-Ford Algorithm
Floyd-Warshall Algorithm
These generalizations have significantly more efficient algorithms than the Ford’s algorithm
simplistic approach of running a single-pair shortest path algorithm on all Others

relevant pairs of vertices. Graph Coloring

10.38
Graph connectivity
Dijkstra’s Algorithm
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

procedure Dijkstra(G,a)
// Initialization Step
forall vertices v
Label[v] := ∞
Prev[v] := -1
endfor Contents
Label(a) := 0 // a is the source node Connectivity
S := ∅ Paths and Circuits

Euler and Hamilton


// Iteration Step Paths

while z ∈/ S Euler Paths and Circuits


Hamilton Paths and Circuits
u := a vertex not in S with minimal Label
Shortest Path Problem
S := S ∪ {u} Dijkstra’s Algorithm
forall vertices v not in S Bellman-Ford Algorithm

if (Label[u] + Wt(u,v)) < Label(v) Floyd-Warshall Algorithm


Ford’s algorithm
then begin
Others
Label[v] := Label[u] + Wt(u,v)
Graph Coloring
Pred[v] := u
end
endwhile

10.39
Graph connectivity
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

b 5 d
4 6
8
a 1 2 z
Contents

2 3 Connectivity
Paths and Circuits

c 10 e Euler and Hamilton


Paths
Euler Paths and Circuits

S a b c d e z Hamilton Paths and Circuits

Shortest Path Problem


∅ 0 ∞ ∞ ∞ ∞ ∞ Dijkstra’s Algorithm

a 0 4 2 ∞ ∞ ∞ Bellman-Ford Algorithm
Floyd-Warshall Algorithm
c 0 3 2 10 12 ∞ Ford’s algorithm

b 0 3 2 8 12 ∞ Others

Graph Coloring
d 0 3 2 8 10 14
e 0 3 2 8 10 13

10.40
Graph connectivity
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

b 5 d
4 6
8
a 1 2 z
Contents

2 3 Connectivity
Paths and Circuits

c 10 e Euler and Hamilton


Paths
Euler Paths and Circuits

S a b c d e z Hamilton Paths and Circuits

Shortest Path Problem


∅ 0 ∞ ∞ ∞ ∞ ∞ Dijkstra’s Algorithm

a 0 4 22 ∞ ∞ ∞ Bellman-Ford Algorithm
Floyd-Warshall Algorithm
c 0 33 2 10 12 ∞ Ford’s algorithm

b 0 3 2 88 12 ∞ Others

Graph Coloring
d 0 3 2 8 1010 14
e 0 3 2 8 10 1313

10.41
Graph connectivity
Back tracking procedure
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

How to determine shortest path from a to d according to Dijkstra’s


algorithm?

b 5 d
4 6 Contents

8 Connectivity
a 1 2 z Paths and Circuits

Euler and Hamilton


Paths
2 3 Euler Paths and Circuits
Hamilton Paths and Circuits
c 10 e
Shortest Path Problem
Dijkstra’s Algorithm
Bellman-Ford Algorithm
S a b c d e z Floyd-Warshall Algorithm

∅ 0 ∞ ∞ ∞ ∞ ∞ Ford’s algorithm
Others
a 0 4 2 ∞ ∞ ∞ Graph Coloring
c 0 3 2 10 12 ∞
b 0 3 2 8 12 ∞
d 0 3 2 8 10 14
e 0 3 2 8 10 13
10.42
Graph connectivity
Dijkstra’s Algorithm
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Property Connectivity
Paths and Circuits

Applicable for any G, any length `(vi ) ≥ 0, ∀i; one-to-all; Euler and Hamilton
Paths
complexity O(|V |2 ). Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.43
Graph connectivity
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Example Ngoc Le

Find the shortest path from a to other vertices using Dijkstra’s


algorithm.

b 2
Contents
e
Connectivity
4 9 Paths and Circuits

4 Euler and Hamilton


4 Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

g Shortest Path Problem


6 Dijkstra’s Algorithm
c Bellman-Ford Algorithm
a Floyd-Warshall Algorithm

8 Ford’s algorithm
1 3 Others
1 2 Graph Coloring

d 2 5
h
f
10.44
Graph connectivity
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Find the shortest path from e to other vertices using Dijkstra’s
algorithm.

a 4 b 5 c Contents

Connectivity
Paths and Circuits

4 Euler and Hamilton


1 1 Paths
10 Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


d e Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
5 Ford’s algorithm
10
3 Others

Graph Coloring

h f 10 g

10.45
Graph connectivity
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Find the shortest path from e to other vertices using Dijkstra’s
algorithm.

a 4 b 5 c Contents

Connectivity
Paths and Circuits

2 Euler and Hamilton


1 1 Paths
10 Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


d e Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
5 Ford’s algorithm
10
3 Others

Graph Coloring

h f 10 g

10.46
Graph connectivity
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Find the shortest path from a to other vertices using Dijkstra’s
algorithm. Contents

Connectivity
Paths and Circuits
b 5 d Euler and Hamilton
Paths
5 7 Euler Paths and Circuits
Hamilton Paths and Circuits
8
a 3 3 z Shortest Path Problem
Dijkstra’s Algorithm
Bellman-Ford Algorithm

2 3 Floyd-Warshall Algorithm
Ford’s algorithm
c 10 e Others

Graph Coloring

10.47
Graph connectivity
Dijkstra’s Algorithm Flaw
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Can Dijkstra’s Algorithm be used on...


• ...digraph?
• Yes!
Contents
• ...negative weighted graph? Connectivity

• No! Why? Paths and Circuits

Euler and Hamilton


Paths

b Euler Paths and Circuits


Hamilton Paths and Circuits

2 b 1 c Shortest Path Problem


1 1 d
Dijkstra’s Algorithm
Bellman-Ford Algorithm
a -4 a Floyd-Warshall Algorithm
1 1 Ford’s algorithm
5 -10 Others

Graph Coloring
c e

10.48
Graph connectivity
Bellman-Ford Algorithm
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
procedure BellmanFord(G,a)
// Initialization Step
forall vertices v
Label[v] := ∞
Prev[v] := -1
Label(a) := 0 // a is the source node
// Iteration Step Contents
for i from 1 to size(vertices)-1 Connectivity
forall vertices v Paths and Circuits

if (Label[u] + Wt(u,v)) < Label[v] Euler and Hamilton


Paths
then
Euler Paths and Circuits
Label[v] := Label[u] + Wt(u,v) Hamilton Paths and Circuits
Prev[v] := u Shortest Path Problem
// Check circuit of negative weight Dijkstra’s Algorithm
forall vertices v Bellman-Ford Algorithm
Floyd-Warshall Algorithm
if (Label[u] + Wt(u,v)) < Label(v)
Ford’s algorithm
error "Contains circuit of negative weight" Others

Graph Coloring

Property
any G, any weighted; one-to-all; detect whether there exists a circle of
negative weight; complexity O(|V | × |E|).

10.49
Graph connectivity
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Step a b c d e f
0 0 ∞ ∞ ∞ ∞ ∞
1 0 -2a ∞ ∞ ∞ 3a
2 0 -2 3b ∞ -5b 3 Contents
3 0 -2 3 -4e -5 3 Connectivity
4 0 -2 3 -4 -5 3 Paths and Circuits

Euler and Hamilton


Stop since Step 4 = Step 3. Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

b 5 c Shortest Path Problem


Dijkstra’s Algorithm

-2 -5 Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
a 4 3 d Others

Graph Coloring
1 -3
3 1
f 1 e

10.50
Graph connectivity
Backtracking procedure
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Example
Step a b c d e f
0 0 ∞ ∞ ∞ ∞ ∞
1 0 -2a ∞ ∞ ∞ 3a
2 0 -2 3b ∞ -5b 3
3 0 -2 3 -4e -5 3 Contents

Connectivity
4 0 -2 3 -4 -5 3 Paths and Circuits

Stop since Step 4 = Step 3. Euler and Hamilton


Paths
How to find shortest path from a to d? a → b → e → d Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


b 5 c Dijkstra’s Algorithm
Bellman-Ford Algorithm

-2 -5 Floyd-Warshall Algorithm
Ford’s algorithm
Others
a 4 3 d Graph Coloring

1 -3
3 1
f 1 e

10.51
Graph connectivity
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Example Ngoc Le

Step a b c d e f
0 0 ∞ ∞ ∞ ∞ ∞
1 0 -2a ∞ ∞ ∞ ∞
2 0 -2 -1b ∞ -1b ∞
3 0 -2 -1 -6c -1 -4c
Contents
4 -1f -2 -1 -6 -3f -4 Connectivity
5 -1 -3a -1 -6 -3 -4 Paths and Circuits

6 -1 -3 -2b -6 -3 -4 Euler and Hamilton


Paths
7 -1 -3 -2 -7c -3 -4 Euler Paths and Circuits
Hamilton Paths and Circuits

There exists a circle of negative weight since Step 6 6= Step 5. Shortest Path Problem
Dijkstra’s Algorithm
Bellman-Ford Algorithm
b 1 c Floyd-Warshall Algorithm
Ford’s algorithm
-2 -5 Others

Graph Coloring
a 4 3 d
-3 1
3 1
f 1 e
10.52
Graph connectivity
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

1
Contents
b 1 c Connectivity
Paths and Circuits

Euler and Hamilton


Paths
5 1
a d Euler Paths and Circuits
Hamilton Paths and Circuits

1 -3 Shortest Path Problem


4 3 Dijkstra’s Algorithm
Bellman-Ford Algorithm

f 1 e Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.53
Graph connectivity
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

2
(G1 )
Contents
3
B D Connectivity
Paths and Circuits
4 7 Euler and Hamilton
2 Paths
Euler Paths and Circuits
A 2 3 F Hamilton Paths and Circuits

9 6 5 Shortest Path Problem


Dijkstra’s Algorithm

5 Bellman-Ford Algorithm

C E Floyd-Warshall Algorithm
Ford’s algorithm
12 Others

Graph Coloring

10.54
Graph connectivity
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

1
Contents
b -5 c Connectivity
Paths and Circuits

Euler and Hamilton


Paths
5 1
a d Euler Paths and Circuits
Hamilton Paths and Circuits

1 2 Shortest Path Problem


4 3 Dijkstra’s Algorithm
Bellman-Ford Algorithm

f 1 e Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.55
Graph connectivity
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

A -2 D 3 E
(G7 )
Contents
4
6 -4 -3 Connectivity
4 Paths and Circuits

-5 F 5 Euler and Hamilton


B G Paths
C 2 Euler Paths and Circuits
-2 6 Hamilton Paths and Circuits
-4 3 -1 Shortest Path Problem
Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
I H Ford’s algorithm
Others

Graph Coloring

10.56
Graph connectivity
Floyd-Warshall Algorithm [1962]
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

procedure FloydWarshall ()
for k := 1 to n
Contents
for i := 1 to n
for j := 1 to n Connectivity
Paths and Circuits
path[i,j] = min (path[i,j],
Euler and Hamilton
path[i,k]+path[k,j]); Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


Property
Dijkstra’s Algorithm

any G, any weighted; all-to-all; this is an software algorithm; complexity Bellman-Ford Algorithm

O(|V |3 ).
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.59
Graph connectivity
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
 
00 10 ∞0 40
1
 20 00 −20 ∞0 
L(0) =  
 30 ∞0 00 ∞0 
a
2
b  ∞0 −50 −10 00 
00 10 ∞0 40
(1)
 20 00 − 20 61 
3 -5 L =
  Contents
30 41 00 71 
Connectivity
 ∞0 − 50 − 10 00  Paths and Circuits
-2 4 00 10 −12 40
Euler and Hamilton
c d  20 00 − 20 61  Paths
-1 L (2) =
 
30 41 00 71  Euler Paths and Circuits

Shortest path from b to d


 −32 − 50 −72 Hamilton Paths and Circuits
00 
(53 from L(4) ): 00 10 − 12 40 Shortest Path Problem
bd = bc + cd (3)
 13 00 − 20 53 
Dijkstra’s Algorithm

(53 = −20 + 71 from L(3) ) L =


 3
 Bellman-Ford Algorithm
41 00 71  Floyd-Warshall Algorithm
cd = ca + ad
 −43 − 50 − 72 00  Ford’s algorithm

(71 = 30 + 40 from L(1) ) 00 −14 −34 40 Others

⇒ bd = bc + ca + ad (4)
 13 00 − 20 53  Graph Coloring
L =
 
30 24 00 71 
− 43 − 50 − 72 00

10.60
Graph connectivity
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

2
a 3 b
Contents

1 -4 Connectivity
Paths and Circuits
c
Euler and Hamilton
    Paths
00 20 ∞0 00 20 ∞0 Euler Paths and Circuits

L(0) =  30 00 −40  L(1) =  30 00 −40  Hamilton Paths and Circuits

1 ∞0 00 10 31 00 Shortest Path Problem


 0  Dijkstra’s Algorithm
00 20 −20 Bellman-Ford Algorithm
L(2) =  30 00 −40  Floyd-Warshall Algorithm

10 31 − 12 Ford’s algorithm
Others

Graph Coloring
STOP, there exists a circuit of negative length.

10.61
Graph connectivity
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

b
7 5 Contents

Connectivity

1 2 Paths and Circuits

a c Euler and Hamilton


Paths
Euler Paths and Circuits
-3 Hamilton Paths and Circuits
1 1 Shortest Path Problem
Dijkstra’s Algorithm
d Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.62
Graph connectivity
Ford’s algorithm
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

π(1) = 0
Contents
For each j ∈ V do Connectivity
π(j) = mini∈ρ−1 (π(i)+`[i,j]) Paths and Circuits

j
Euler and Hamilton
End Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

Property Shortest Path Problem


Dijkstra’s Algorithm

G without circle, positive length; one-to-all; rank table definition; complexity Bellman-Ford Algorithm
Floyd-Warshall Algorithm
O(|V |). Ford’s algorithm
Others

Graph Coloring

10.66
Graph connectivity
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

1 1
A B
B
3 4
1 3 4
3
4 0 3 5
Contents
C E 3 5
A C D Connectivity
5 Paths and Circuits
4 2
2 Euler and Hamilton
4 Paths
D Euler Paths and Circuits

E Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm

i Γ−1
i rank(i) Floyd-Warshall Algorithm
Ford’s algorithm
A - 0 Others
B A 1 Graph Coloring
C A, B 2
D B, C , E 3
E A 1

10.67
Graph connectivity
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

5 1
A B C
Contents
7 2
Connectivity
Paths and Circuits

6 1 3 Euler and Hamilton


D E Paths
Euler Paths and Circuits
4 Hamilton Paths and Circuits

3 5
Shortest Path Problem
F 4 Dijkstra’s Algorithm

H Bellman-Ford Algorithm

G 2
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.68
Graph connectivity
Application
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Problem
A young professor in Hue is invited to teach some years in Ho Chi Minh university of
technology. He decides to represent the diverse operations of his transfer by a graph and,
in this purpose, establishes the list of following operations:
A: Find a house in Ho Chi Minh city.
Contents
B: Choose a removal man and sign a contract of move
Connectivity
C: Make pack his furniture by the removal man Paths and Circuits

D: Make transport his furniture towards Ho Chi Minh city Euler and Hamilton
Paths
E: Find an accommodation to HCM (from Hue) Euler Paths and Circuits
F: Transport his family to HCM Hamilton Paths and Circuits

G: Move into his new accommodation Shortest Path Problem


Dijkstra’s Algorithm
H: Register the children to their new school Bellman-Ford Algorithm
I: Look for a temporary work for his wife Floyd-Warshall Algorithm
Ford’s algorithm
J: Fit out the new accommodation and pay this arrangement with the first treatment Others
of his wife
Graph Coloring
K: Find a small bar to celebrate in family the success of the move and express the
enjoyment to live in a good accommodation arrangement

10.69
Graph connectivity
Application
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Considering constraint of posteriority following: A < F ; B < C;


C < D ∧ F ; D < G; E < F ; F < G ∧ H ∧ I; G < K; H < K;
I < J; J < K.
Contents
Approximated task processing times :
Connectivity
Paths and Circuits
A B C D E F G H I J K Euler and Hamilton
10 2 3 4 7 3 5 1 3 8 2 Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


Question Dijkstra’s Algorithm
Bellman-Ford Algorithm
• Determine the minimal duration needed to completed all Floyd-Warshall Algorithm
Ford’s algorithm
tasks. Others

Graph Coloring

10.70
Graph connectivity
Question
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents

Connectivity

How to determine a shortest path from u to v in graph G which Paths and Circuits

Euler and Hamilton


traverses at most ≤ a given constant number of intermediate Paths

vertices. Euler Paths and Circuits


Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.71
Graph connectivity
Other shortest path problems
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• multicriteria shortest path problem


Contents
• linear combination
Connectivity
• ε-constraint approach Paths and Circuits
• lexico-graphical order Euler and Hamilton
Paths
• k shortest paths problem Euler Paths and Circuits

• allowing loop Hamilton Paths and Circuits

• loopless Shortest Path Problem


Dijkstra’s Algorithm

• multi-point shortest path Bellman-Ford Algorithm


Floyd-Warshall Algorithm
• TSP, VRP Ford’s algorithm
Others

Graph Coloring

10.72
Graph connectivity
Traveling Salesman Problem (TSP)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Problem
• Given a set of n customers located in n cities and distances for each pair
of cities, the problem involves finding a round-trip with the minimum Contents
traveling cost. Connectivity
• The vehicle must visit each customer exactly once and return to its point Paths and Circuits

of origin also called depot. Euler and Hamilton


Paths
• The objective function is the total cost of the tour. Euler Paths and Circuits
Hamilton Paths and Circuits
• N P-complete: all known techniques for obtaining an exact solution
Shortest Path Problem
require an exponentially increasing number of steps (computing Dijkstra’s Algorithm
resources) as the problems become larger. Bellman-Ford Algorithm

• TSP is one of the most intensely studied problems in computational Floyd-Warshall Algorithm
Ford’s algorithm
mathematics, yet no effective solution method. Others

Graph Coloring

10.74
Graph connectivity
Traveling Salesman Problem
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
b 3 c Ngoc Le

2 2

3 2 4
a d Contents

Connectivity
Paths and Circuits

3 1 4 3 Euler and Hamilton


Paths
Euler Paths and Circuits

e Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm

• The total number of possible Hamilton circuit is (n − 1)!/2. Floyd-Warshall Algorithm


Ford’s algorithm

• For example, if there are 25 customers to visit, the total Others

Graph Coloring
number of solutions is 24!/2 = 3.1 × 1023 .
• If the depot is located at node 1, then the optimal tour is
1 − 5 − 2 − 3 − 4 − 1 with total cost equal to 11.

10.75
Graph connectivity
Vehicle Routing Problem (VRP)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Problem
• The vehicle routing problem involves finding a set of trips,
one for each vehicle, to deliver known quantities of goods to a Contents

Connectivity
set of customers. Paths and Circuits

• The objective is to minimize the travel costs of all trips Euler and Hamilton
Paths
combined. Euler Paths and Circuits
Hamilton Paths and Circuits
• There may be upper bounds on the total load of each vehicle Shortest Path Problem
and the total duration of its trip. Dijkstra’s Algorithm
Bellman-Ford Algorithm
• The most basic Vehicle Routing Problem (VRP) is the Floyd-Warshall Algorithm
Ford’s algorithm
single-depot capacitate VRP. Others

Graph Coloring

10.76
Graph connectivity
Maps and Graphs
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
• Every map can be represented by a graph. We call it dual
graph.
• Problem of coloring the regions of a map → coloring the Contents

Connectivity
vertices of the dual graph so that no two adjacent vertices Paths and Circuits

have the same color. Euler and Hamilton


Paths
Euler Paths and Circuits
B Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm

A E Floyd-Warshall Algorithm
Ford’s algorithm
C Others

Graph Coloring

10.77
Graph connectivity
Graph coloring
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
• A coloring (tô màu) of a simple graph is the assignment of a
color to each vertex of the graph so that no two adjacent
vertices are assigned the same color.
• The chromatic number (số màu) of a graph, denoted by Contents
χ(G), is the least number of colors needed for a coloring of Connectivity

this graph. Paths and Circuits

Euler and Hamilton


Paths
Euler Paths and Circuits
Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm
a b a b Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

e g e g Graph Coloring

f f

c d c d
10.78
Graph connectivity
Four color theorem
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Theorem (Four color theorem)


Contents
The chromatic number of a planar graph is no greater than four. Connectivity
Paths and Circuits

• Was a conjecture in the 1850s Euler and Hamilton


Paths

• Was not proved completely until 1976 by Kenneth Appel and Euler Paths and Circuits
Hamilton Paths and Circuits

Wolfgang Haken, using computer Shortest Path Problem


Dijkstra’s Algorithm
• No proof not relying on a computer has yet been found Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.79
Graph connectivity
Applications of Graph coloring
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Scheduling Final Exam Ngoc Le

• How can the final exams at a university be scheduled so that


no student has two exams at the same time?
• Suppose we have 7 finals, numbered 1 through 7.
• The pairs of courses have common students are depicted in Contents
the following graph Connectivity
Paths and Circuits

Euler and Hamilton


1 Paths
Euler Paths and Circuits
7 2 Hamilton Paths and Circuits

Shortest Path Problem


Dijkstra’s Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Ford’s algorithm

6 3 Others

Graph Coloring

5 4
10.80
Graph connectivity
Applications of Graph Coloring
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Other Applications

• Frequency Assignments: Television channels 2 through 12 are Contents

Connectivity
assigned to stations in North America so that no two stations Paths and Circuits

within 150 miles can operate on the same channel. How can Euler and Hamilton
Paths
the assignment of channels be modeled by graph coloring? Euler Paths and Circuits

• Index Registers: In an execution of loop, the frequently used Hamilton Paths and Circuits

Shortest Path Problem


variables should be stored in index registers to speed up. How Dijkstra’s Algorithm

many index registers are needed? Bellman-Ford Algorithm


Floyd-Warshall Algorithm
Ford’s algorithm
Others

Graph Coloring

10.81
Graph connectivity
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Determine a shortest path from a to other vertices in the following


graph.
1 Contents

Connectivity
Paths and Circuits
b -5 c Euler and Hamilton
Paths
Euler Paths and Circuits
Hamilton Paths and Circuits
5 1
a d Shortest Path Problem
Dijkstra’s Algorithm
-1 2 Bellman-Ford Algorithm
4 3 Floyd-Warshall Algorithm
Ford’s algorithm

f 1 e Others

Graph Coloring

10.85
Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

Chapter 11
Ngoc Le

Trees
Discrete Structures for Computing
Contents

Introduction
Properties of Trees

Tree Traversal

Applications of Trees
Binary Search Trees
Decision Trees

Spanning Trees

Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm

Huynh Tuong Nguyen, Tran Tuan Anh, Nguyen Ngoc Le


Faculty of Computer Science and Engineering
University of Technology - VNUHCM
{htnguyen;trtanh}@hcmut.edu.vn
11.1
Trees
Contents
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

1 Introduction
Properties of Trees

2 Tree Traversal
Contents

Introduction

3 Applications of Trees Properties of Trees

Tree Traversal
Binary Search Trees
Applications of Trees
Decision Trees Binary Search Trees
Decision Trees

Spanning Trees
4 Spanning Trees Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
5 Minimum Spanning Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.2
Trees
Course outcomes
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Course learning outcomes

L.O.1 Understanding of logic and discrete structures


L.O.1.1 – Describe definition of propositional and predicate logic
L.O.1.2 – Define basic discrete structures: set, mapping, graphs
Contents
L.O.2 Represent and model practical problems with discrete structures Introduction
L.O.2.1 – Logically describe some problems arising in Computing Properties of Trees

L.O.2.2 – Use proving methods: direct, contrapositive, induction Tree Traversal


L.O.2.3 – Explain problem modeling using discrete structures Applications of Trees
Binary Search Trees

L.O.3 Understanding of basic probability and random variables Decision Trees

L.O.3.1 – Define basic probability theory Spanning Trees

L.O.3.2 – Explain discrete random variables Minimum Spanning


Trees
Prim’s Algorithm
L.O.4 Compute quantities of discrete structures and probabilities Kruskal’s Algorithm

L.O.4.1 – Operate (compute/ optimize) on discrete structures


L.O.4.2 – Compute probabilities of various events, conditional
ones, Bayes theorem

11.3
Trees
Introduction
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• Very useful in computer science: search algorithm, game


winning strategy, decision making, sorting, . . .
• Other disciplines: chemical compounds, family trees,
organizational tree, . . . Contents

Introduction
Properties of Trees
/
Tree Traversal

Applications of Trees

home bin tmp Binary Search Trees


Decision Trees

Spanning Trees

Minimum Spanning
tan mail ls junk Trees
Prim’s Algorithm
Kruskal’s Algorithm

music latex scala

11.4
Trees
Tree
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
A tree (cây ) is a connected undirected graph with no simple circuits.
Consequently, a tree must be a simple graph.

Contents

Introduction
Properties of Trees

Tree Traversal

Applications of Trees
Binary Search Trees
Decision Trees

Spanning Trees
G1 G2 G3 G4
Minimum Spanning
circuit exists Trees
Prim’s Algorithm

not connected Kruskal’s Algorithm

Definition
Graphs containing no simple circuits that are not necessarily connected
is forest (rừng ), in which each connected component is a tree.

11.5
Trees
Rooted Trees
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
A rooted tree (cây có gốc) is a tree in which:
• One vertex has been designated as the root and
• Every edge is directed away from the root
Contents

Introduction
Properties of Trees
f g a c Tree Traversal

Applications of Trees
Binary Search Trees
d b a Decision Trees
b c Spanning Trees
d e
Minimum Spanning
e b Trees
Prim’s Algorithm
a f g e d Kruskal’s Algorithm

c f g

11.6
Trees
Terminology
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Definition Ngoc Le

• parent (cha) of v is the unique u such that there is a directed


edge from u to v
• when u is the parent of v, v is called a child (con) of u
• vertices with the same parent are called siblings (anh em) Contents

• the ancestors (tổ tiên) of a vertex are the vertices in the path Introduction
Properties of Trees
from the root to this vertex (excluding the vertex itself)
Tree Traversal
• descendants (con cháu) of a vertex v are those vertices that Applications of Trees

have v as an ancestor Binary Search Trees


Decision Trees

Spanning Trees
a Minimum Spanning
Trees
Prim’s Algorithm

b Kruskal’s Algorithm

c
d

f g e

11.7
Trees
Terminology
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
• a vertex of a tree is called a leaf (lá ) if it has no children
• vertices that have children are called internal vertices (đỉnh
Contents
trong ) Introduction
Properties of Trees

a Tree Traversal

Applications of Trees
Binary Search Trees
Decision Trees
b
Spanning Trees
c
Minimum Spanning
d Trees
Prim’s Algorithm
Kruskal’s Algorithm

f g e

11.8
Trees
Terminology
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
If a is a vertex in a tree, the subtree (cây con) with a as its root is
the subgraph of the tree consisting of a and its descendants and
all edges incident to these descendants. Contents

Introduction
a Properties of Trees

Tree Traversal

Applications of Trees
b Binary Search Trees
c Decision Trees

d Spanning Trees

Minimum Spanning
Trees

f g e Prim’s Algorithm
Kruskal’s Algorithm

11.9
Trees
m-ary tree
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
• m-ary tree (cây m-phân): at most m children on each internal
vertex of a rooted tree. nhieu nhat
• full m-ary tree (cây m-phân đầy đủ): every internal vertex has
exactly m children. tat ca
Contents
• An m-ary tree with m = 2 is called a binary tree (cây nhị phân). Introduction
Properties of Trees

Tree Traversal

Applications of Trees
Binary Search Trees
Decision Trees

Spanning Trees
Full 3-ary tree Minimum Spanning
Trees
Binary tree Prim’s Algorithm
Kruskal’s Algorithm

Full 5-ary tree 3-ary tree

11.10
Trees
Ordered Rooted Trees
Huynh Tuong Nguyen,
Definition Tran Tuan Anh, Nguyen
Ngoc Le

• An ordered rooted tree (cây có gốc có thứ tự) is a rooted tree


where the children of each internal vertex are ordered (e.g. in
order from left to right).
• In an ordered binary tree (cây nhị phân có thứ tự), if an
internal vertex has two children, the first child is called the Contents

left child (con bên trái) and the second is called the right Introduction
Properties of Trees
child (con bên phải). Tree Traversal

a Applications of Trees
Binary Search Trees
Decision Trees

b c Spanning Trees

Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
e
f d

g
Left subtree of a Right subtree of a
11.11
Trees
Properties & Theorems
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Theorem
A tree with n vertices has n − 1 edges.
Contents
Theorem Introduction

A full m-ary tree Properties of Trees

Tree Traversal
(i) n vertices has (n − 1)/m internal vertices and Applications of Trees
[(m − 1)n + 1]/m leaves Binary Search Trees
Decision Trees

(ii) i internal vertices has n = mi + 1 vertices and (m − 1)i + 1 Spanning Trees

leaves Minimum Spanning


Trees
(iii) ` leaves has n = (m` − 1)/(m − 1) vertices and Prim’s Algorithm
Kruskal’s Algorithm
(` − 1)/(m − 1) internal vertices

11.12
Trees
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example (Chain Letter Game)

• Each person who receives the letter is asked to send it on to Contents


four other peoples. Introduction
Properties of Trees
• Some peoples do this, but others do not send any letters.
Tree Traversal
• How many people have seen the letter, including the first Applications of Trees

person, if no one receives more than one letter and if the Binary Search Trees
Decision Trees

chain letter ends after there have been 100 people who read it Spanning Trees

but did not send it out ? Minimum Spanning


Trees
• How many people sent out the letter? Prim’s Algorithm
Kruskal’s Algorithm

11.13
Trees
Level and Height
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Definition Ngoc Le

• The level (mức) of a vertex v in a rooted tree is the length of


the unique path from the root to this vertex.
• The level of the root is defined to be zero.
• The height (độ cao) of a rooted tree is the maximum of the
Contents
levels of vertices (i.e. the length of the longest path from the Introduction
root to any vertex). Properties of Trees

Tree Traversal

a Applications of Trees
Binary Search Trees

Example Decision Trees

b k Spanning Trees
• Level of root a = 0, Minimum Spanning
j b, j, k = 1 and
Trees
Prim’s Algorithm

c f l c, e, f, l = 2 . . . Kruskal’s Algorithm

e
• Because the largest
g level of any vertex is
d i m n
4, this tree has height
4.
h
11.14
Trees
Balanced m-ary Trees
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
A rooted m-ary tree of height h is balanced (cân đối) if all leaves
are at levels h or h − 1.

Contents

Introduction
Properties of Trees

Tree Traversal

Applications of Trees
Binary Search Trees
Decision Trees

T1 Spanning Trees

Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm

T2

11.15
Trees
Balanced m-ary Tree
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Theorem
There are at most mh leaves in an m-ary tree of height h. Contents

Introduction

It can be proved by using mathematical induction on the height. Properties of Trees

Tree Traversal

Corollary Applications of Trees


Binary Search Trees

• If an m-ary tree of height h has ` leaves, then h ≥ dlogm `e. Decision Trees

Spanning Trees
• If the m-ary tree is full and balanced, then h = dlogm `e. Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.16
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Exercise (Chess tournament)


Suppose 1000 people enter a chess tournament. Use a rooted tree
model of the tournament to determine how many games must be
played to determine a champion. If a player is eliminated after one Contents

loss and games are played until only one entrant has not lost. Introduction

(Assume there are no ties) Properties of Trees

Tree Traversal

Applications of Trees
Exercise (Isomorphic) Binary Search Trees
Decision Trees

How many different isomers (đồng phân) do the following Spanning Trees

saturated hydrocarbons have ? Minimum Spanning


Trees
• C3 H8 Prim’s Algorithm
Kruskal’s Algorithm

• C5 H12
• C6 H14

11.17
Trees
Question
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Exercise
Contents
• How many vertices and how many leaves does a complete Introduction
m-ary tree of height h have? Properties of Trees

Tree Traversal
• Show that a full m-ary balanced tree (cây m-phân hoàn hảo)
Applications of Trees
of height h has more than mh−1 leaves. Binary Search Trees
Decision Trees
• How many edges are there in a forest of t trees containing a
Spanning Trees
total of n vertices? Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.18
Trees
Labeling Ordered Rooted Trees
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
• Ordered rooted trees are often used to store information. Ngoc Le

• Need a procedure for visiting each vertex of an ordered rooted tree to


access data.
• Ordering and labeling the vertices is important to traverse them in any
procedure
• Universal address system (hệ địa chỉ phổ dụng )
0 < 1 < 1.1 < 1.1.1 < 1.2 < 1.3 < . . . < 2 < 3 < 3.1 < . . . Contents

Introduction
0 Properties of Trees

Tree Traversal

Applications of Trees
Binary Search Trees
1 2 3 Decision Trees

Spanning Trees

Minimum Spanning
1.1 1.2 1.3 3.1 Trees
Prim’s Algorithm
Kruskal’s Algorithm

1.3.1
1.1.1 1.3.2 3.1.1 3.1.2

1.3.1.1

11.19
Trees
Traversal Algorithms (Thuật toán duyệt cây)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Preorder Traversal (duyệt tiền thứ tự - NLR)
procedure preorder (T : ordered rooted tree)
r := root of T
print r
for each child c of r from left to right
Contents
T (c) := subtree with c as its root
Introduction
preorder (T (c)) Properties of Trees

Tree Traversal

a Applications of Trees
Binary Search Trees
Decision Trees

Spanning Trees

b c Minimum Spanning
Trees
d Prim’s Algorithm
Kruskal’s Algorithm

f g e

a b f g c e d
11.20
Trees
Traversal Algorithms
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Inorder Traversal (Duyệt trung thứ tự - LNR)


Suppose a tree T with root r. If T consists only of r, then r is
inorder traversal of T . Otherwise, suppose r has subtrees T1 , T2 ,
. . . , Tn from left to right, inorder traversal:
T1 → r → T2 → . . . → Tn . Contents

Introduction
Properties of Trees
a Tree Traversal

Applications of Trees
Binary Search Trees
Decision Trees
b c
Spanning Trees
d
Minimum Spanning
Trees
Prim’s Algorithm

f g e Kruskal’s Algorithm

f b g a e c d

11.21
Trees
Traversal Algorithms
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Postorder Traversal (Duyệt hậu thứ tự - LRN)
procedure postorder (T : ordered rooted tree)
r := root of T
for each child c of r from left to right
T (c) := subtree with c as its root
Contents
postorder (T (c))
Introduction
print r Properties of Trees

Tree Traversal
a Applications of Trees
Binary Search Trees
Decision Trees

Spanning Trees

b c Minimum Spanning
d Trees
Prim’s Algorithm
Kruskal’s Algorithm

f g e

f g b e c d a
11.22
Trees
Infix, Prefix and Postfix Notations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• Infix (trung tố ):
((x + y) ↑ 2) + ((x − 4)/3) +
Contents

Introduction
↑ / Properties of Trees

• Prefix (tiền tố ): Tree Traversal

Applications of Trees
+ ↑ +xy2/ − x43 + −
2 3 Binary Search Trees
Decision Trees

Spanning Trees

x y x 4 Minimum Spanning
• Postfix (hậu tố ): Trees
Prim’s Algorithm

x y + 2 ↑ x 4 −3 / + Kruskal’s Algorithm

11.23
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Exercise
Implement postorder, inorder and preorder traversal of the
following tree.
Contents

Introduction
A Properties of Trees

Tree Traversal

Applications of Trees
B C Binary Search Trees
Decision Trees

Spanning Trees

D E F Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
G H I

11.24
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Exercise
Find the ordered rooted tree representing Contents

Introduction

(¬(p ∧ q) ∨ (¬q ∧ r)) → (¬p ∨ ¬r) Properties of Trees

Tree Traversal

Applications of Trees
Then use this rooted tree to find the prefix, postfix and infix forms Binary Search Trees

of this expression Decision Trees

Spanning Trees

Minimum Spanning
Solution Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.27
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Contents
Exercise
Introduction
Determine postorder of a binary tree with inorder D B H E I A F C Properties of Trees

J G K and preorder A B D E H I C F G J K. Tree Traversal

Applications of Trees
Binary Search Trees
Solution Decision Trees

Spanning Trees

Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.28
Trees
Binary Search Trees
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
Binary search tree (cây tìm kiếm nhị phân - BST) is a binary tree
in which the assigned key of a vertex is:
• larger than the keys of all vertices in its left subtree, and
Contents
• smaller than the keys of all vertices in its right subtree. Introduction
Properties of Trees

Tree Traversal

6 Applications of Trees
Binary Search Trees
Decision Trees

2 7 Spanning Trees

Minimum Spanning
Trees
Prim’s Algorithm
1 4 9 Kruskal’s Algorithm

3 5 8

11.32
Trees
Adding and Locating an Item in BST
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example
Form a BST for the words mathematics, physics, geography ,
zoology , meteorology , geology , psychology , chemistry using
alphabetical order.
Contents
mathematics Introduction
Properties of Trees

Tree Traversal
geography physics Applications of Trees
Binary Search Trees
Decision Trees

chemistry geology meteorology zoology Spanning Trees

Minimum Spanning
Trees
Prim’s Algorithm
psychology Kruskal’s Algorithm

Complexity in searching
O(log(n)) vs. O(n) in linear list

11.33
Trees
Decision Trees (Cây quyết định)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Example Contents

Introduction
There are seven coins, all with the same weight, and a counterfeit Properties of Trees

coin that weighs less than the others. How many weighings are Tree Traversal

necessary using a balance scale to determine which of the eight Applications of Trees

coins is the counterfeit one? Give an algorithm for finding this Binary Search Trees
Decision Trees

counterfeit coin. Spanning Trees

Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.34
Trees
Yet Another Application
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Example
If we know that the probability that a person has tuberculosis
(TB) is p(TB) = 0.0005.
We also know p(+|TB) = 0.999 and p(−|TB) = 0.99.
What is p(TB|+) and p(TB|−)? Contents

Introduction

0.999
Properties of Trees
+ TB & + = 0.0004995
Tree Traversal
TB 0.001 Applications of Trees
5
00 − TB & − = 0.0000005 Binary Search Trees

0.0 Decision Trees

Start! 0.99 Spanning Trees


95
0.01
Minimum Spanning
+ TB & + = 0.009995 Trees

TB 0.99 Prim’s Algorithm


Kruskal’s Algorithm

− TB & − = 0.989505

p(TB∩+) 0.0004995
p(TB|+) = p(+) = 0.0004995+0.009995 ≈ 0.0476

11.35
Trees
Problem
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
• A spanning tree (cây khung ) in a graph G is a subgraph of G
Contents
that is a tree which contains all vertices of G. Introduction
Properties of Trees

a b c d Tree Traversal

Applications of Trees
Binary Search Trees
Decision Trees

e g Spanning Trees

f Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.36
Trees
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

End! Start!
1

Contents
2 3 4 Introduction
Properties of Trees

Tree Traversal
Stuck! Stuck! Applications of Trees

5 6 7 8 Binary Search Trees


Decision Trees

Spanning Trees

Minimum Spanning
Property Trees
Prim’s Algorithm

• Go deeper as you can Kruskal’s Algorithm

• Backtrack (quay lui) to possible branch when you are stuck.


• O(e) or O(n2 )

11.37
Trees
Depth-First Search
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Algorithm
procedure DFS (G) Contents

T := tree consisting only vertex v1 Introduction


Properties of Trees
visit(v1 )
Tree Traversal

Applications of Trees
procedure visit(v: vertex of G) /* recursive */ Binary Search Trees
Decision Trees
for each vertex w adjacent to v and not in T Spanning Trees
add w and edge {v, w} to T Minimum Spanning
visit(w) Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.38
Trees
Breadth-First Search (Tìm kiếm ưu tiên chiều rộng)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

vertex L
Start! ∅
1 1 2, 3, 4
2 3, 4, 5, 6 Contents
3 4, 5, 6 Introduction

4 5, 6, 7, 8 Properties of Trees

2 3 4 5 6, 7, 8 Tree Traversal

Applications of Trees
6 7, 8 Binary Search Trees

End! 7 8 Decision Trees

8 ∅ Spanning Trees
5 6 7 8 Minimum Spanning
Trees
Prim’s Algorithm
Property Kruskal’s Algorithm

• O(e) or O(n2 )

11.39
Trees
Breadth-First Search
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Algorithm
procedure BFS (G)
T := tree consisting only vertex v1 Contents

L := empty list Introduction


Properties of Trees
put v1 in the list L of unprocessed vertices Tree Traversal
while L is not empty Applications of Trees

remove the first vertex, v, from L Binary Search Trees


Decision Trees
for each neighbor w of v Spanning Trees
if w is not in L and not in T then Minimum Spanning
Trees
add w to the end of the list L Prim’s Algorithm

add w and edge {v, w} to T Kruskal’s Algorithm

11.40
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Exercise
Find spanning tree in the following graphs.

a b c

Contents

Introduction
d e f g Properties of Trees

Tree Traversal

Applications of Trees
Binary Search Trees
h i j Decision Trees

Spanning Trees
a b c
Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm

d e f g

h i

11.41
Trees
Minimum Spanning Trees
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Definition
• A minimum spanning tree (cây khung nhỏ nhất) in a
connected weighted graph is a spanning tree that has the
smallest possible sum of weights of its edges.
Contents

Introduction
Properties of Trees

Tree Traversal
$2000 Applications of Trees
Binary Search Trees
Decision Trees
$1200 Chicago $1000 New York Spanning Trees
San Francisco $9
00 00
$13
Minimum Spanning
Trees
Denver
$70

00
Prim’s Algorithm

$1600

$8
Kruskal’s Algorithm
0
$140
0
$220 Atlanta
0

11.42
Trees
Prim’s Algorithm (Nearest-Neighbor)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Prim’s Algorithm (1957)


Contents
procedure Prim(G) Introduction
T := a minimum-weight edge Properties of Trees

for i := 1 to n − 2 Tree Traversal

e := an edge of minimum weight incident to a vertex in T Applications of Trees


Binary Search Trees
and not forming a simple circuit in T if added to T Decision Trees

T := T with e added Spanning Trees

return T Minimum Spanning


Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.43
Trees
Prim’s Algorithm (Nearest-Neighbor)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
• Pick a vertex to start from
• Iteratively absorb smallest edge possible

Contents
a 4 b 5 c Introduction
Properties of Trees

Tree Traversal
4 Applications of Trees
1 1 Binary Search Trees
10 Decision Trees

Spanning Trees
d e Minimum Spanning
Trees
Prim’s Algorithm
5 Kruskal’s Algorithm
10
3

f 10 g

11.44
Trees
Kruskal’s Algorithm (Lightest-Edge)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Kruskal’s Algorithm (1958)


Contents
procedure Kruskal (G) Introduction
T := empty graph Properties of Trees

for i := 1 to n − 1 Tree Traversal

e := any edge in G with smallest weight that does not form Applications of Trees
Binary Search Trees
a simple circuit when added to T Decision Trees

T := T with e added Spanning Trees

return T Minimum Spanning


Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.45
Trees
Kruskal’s Algorithm (Lightest-Edge)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

• Iteratively add smallest edge possible

a 4 b 5 c Contents

Introduction
Properties of Trees

4 Tree Traversal
1 1 Applications of Trees
10 Binary Search Trees
Decision Trees

d e Spanning Trees

Minimum Spanning
Trees
5
10 Prim’s Algorithm

3 Kruskal’s Algorithm

f 10 g

11.46
Trees
Exercise
Huynh Tuong Nguyen,
Exercise Tran Tuan Anh, Nguyen
Ngoc Le

By using Prim’s and Kruskal’s algorithm, determine minimum


spanning tree in the following graphs. (and maximum spanning
tree (cây khung cực đại).

a 4 b c
Contents
1 2 5 2 4 1 Introduction
Properties of Trees

d e 2 f g Tree Traversal
1 Applications of Trees
2 3 1 3 1 Binary Search Trees
Decision Trees

h i j Spanning Trees

Minimum Spanning
a 1 b 8 c Trees
Prim’s Algorithm

4 2 4 6 1 2 Kruskal’s Algorithm

d e f g
5 1 3
3 2 7
h i
11.47
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Cho một cây có gốc với n đỉnh. Giả thiết một đỉnh trong tập đỉnh Contents

có bậc là n − 1. Chiều cao của cây là Introduction


Properties of Trees

Tree Traversal
A 1 Applications of Trees

B n−1 Binary Search Trees


Decision Trees

C n Spanning Trees

Minimum Spanning
D 2 Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.48
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Xác định tiền tố (prefix) của cây nhị phân có gốc và có thứ tự
(ordered rooted tree) dùng để biểu diễn Contents

Introduction

(¬(p ∧ q) ∨ (¬q ∧ r)) → (¬p ∨ ¬r) Properties of Trees

Tree Traversal

Applications of Trees
Binary Search Trees
A → ∨¬ ∧ p q ∨ ¬q r ∨ ¬p r Decision Trees

Spanning Trees
B p q ∧ ¬ ∨ q¬r ∧ p¬r∨ →
Minimum Spanning
C p q¬ ∨ q¬ ∧ r → p¬ ∨ r Trees
Prim’s Algorithm

D p q¬ ∨ q¬ ∧ r → p¬ ∨ r Kruskal’s Algorithm

11.49
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Có bao nhiêu cây nhị phân có tiền tố (pre-order traversal) là Contents

Introduction
ABC? Properties of Trees

Tree Traversal
A 1 Applications of Trees

B 3 Binary Search Trees


Decision Trees

C 5 Spanning Trees

Minimum Spanning
D 7 Trees
Prim’s Algorithm
Kruskal’s Algorithm

11.50
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Hãy cho biết hậu tố (post-order traversal) của một cây nhị phân Contents
biết rằng tiền tố (pre-order traversal) là HBGF DECIA và trung Introduction
tố (in-order traversal) là GBF HCEIDA. Properties of Trees

Tree Traversal

Applications of Trees
A GF BCIEADH Binary Search Trees
Decision Trees
B BGF DECIAH
Spanning Trees
C GF BCIEJADH Minimum Spanning
Trees
D GF BHCIEADH Prim’s Algorithm
Kruskal’s Algorithm

11.51
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Cho đồ thị như trong hình vẽ dưới.

8 14

2 32 12 Contents
24 26 Introduction
Properties of Trees

Tree Traversal
18 28 Applications of Trees
Binary Search Trees

16 30 4 Decision Trees

22 Spanning Trees

Minimum Spanning
Trees
10 20 . Prim’s Algorithm
Kruskal’s Algorithm

Cây phủ tối thiểu có tổng trọng số là


A 40
B 60
C 84
D 100 11.52
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le

Cho trước số tự nhiên a > 1, và xét đồ thị đầy đủ K2a+3 . Số lượng Contents
cạnh ta phải xóa khỏi đồ thị K2a+3 để thu được một cây phủ (cây Introduction
khung hay bao trùm, spanning tree) của K2a+3 là bao nhiêu? Properties of Trees

Tree Traversal

Applications of Trees
A 2a + 2 Binary Search Trees

B 2a2 + 3a − 1 Decision Trees

Spanning Trees
C 4a2 + 3a + 1 Minimum Spanning
Trees
D 2a2 + 3a + 1 Prim’s Algorithm
Kruskal’s Algorithm

11.56
Trees - DFS

Huynh Tuong Nguyen,


Nguyen Ngoc Le

Chapter 11
Trees - DFS
Discrete Structures for Computing

Huynh Tuong Nguyen, Nguyen Ngoc Le


Faculty of Computer Science and Engineering
University of Technology - VNUHCM
[email protected];[email protected]
11.1
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Start!
1

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

Stuck!
5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

Stuck!
5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

End!
1

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

Property

• Go deeper as you can


• Backtrack (quay lui) to possible branch when you are stuck.
• O(e) or O(n2 )

11.2
Trees - DFS
Depth-First Search
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Algorithm
procedure DFS (G)
T := tree consisting only vertex v1
visit(v1 )

procedure visit(v: vertex of G) /* recursive */


for each vertex w adjacent to v and not in T
add w and edge {v, w} to T
visit(w)

11.3
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - W • •
6.time=time+1
B - W • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 0

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - W • •
6.time=time+1
B - W • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 0

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG • •
6.time=time+1
B - W • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 0

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG • •
6.time=time+1
B - W • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 1

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B - W • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 1

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A W • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 1

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A W • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 1

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 1

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 2

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 2

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 2

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 2

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 2

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 3

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 3

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E - W • •
F C W • •
G - W • •
H - W • •
B C D
time = 3

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E - W • •
F C W • •
G - W • •
H - W • •
B C D
time = 3

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E - W • •
F C WG • •
G - W • •
H - W • •
B C D
time = 3

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E - W • •
F C WG • •
G - W • •
H - W • •
B C D
time = 4

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E - W • •
F C WG 4 •
G - W • •
H - W • •
B C D
time = 4

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F W • •
F C WG 4 •
G - W • •
H - W • •
B C D
time = 4

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F W • •
F C WG 4 •
G - W • •
H - W • •
B C D
time = 4

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WG • •
F C WG 4 •
G - W • •
H - W • •
B C D
time = 4

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WG • •
F C WG 4 •
G - W • •
H - W • •
B C D
time = 5

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WG 5 •
F C WG 4 •
G - W • •
H - W • •
B C D
time = 5

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WGB 5 •
F C WG 4 •
G - W • •
H - W • •
B C D
time = 5

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WGB 5 •
F C WG 4 •
G - W • •
H - W • •
B C D
time = 6

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WGB 5 6
F C WG 4 •
G - W • •
H - W • •
B C D
time = 6

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WGB 5 6
F C WG 4 •
G - W • •
H - W • •
B C D
time = 6

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WGB 5 6
F C WGB 4 •
G - W • •
H - W • •
B C D
time = 6

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WGB 5 6
F C WGB 4 •
G - W • •
H - W • •
B C D
time = 7

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 7

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 7

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WGB 3 •
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 7

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WGB 3 •
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 8

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WGB 3 8
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 8

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WGB 3 8
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 8

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 • 7.f[v]=time
C B WGB 3 8
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 8

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 • 7.f[v]=time
C B WGB 3 8
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 9

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 9

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 9

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 9

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 9

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 9

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 10

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 10

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D W • •
H - W • •
B C D
time = 10

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D W • •
H - W • •
B C D
time = 10

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG • •
H - W • •
B C D
time = 10

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG • •
H - W • •
B C D
time = 11

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H - W • •
B C D
time = 11

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G W • •
B C D
time = 11

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G W • •
B C D
time = 11

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G WG • •
B C D
time = 11

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G WG • •
B C D
time = 12

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G WG 12 •
B C D
time = 12

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G WGB 12 •
B C D
time = 12

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G WGB 12 •
B C D
time = 13

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G WGB 12 13
B C D
time = 13

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G WGB 12 13
B C D
time = 13

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 •
H G WGB 12 13
B C D
time = 13

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 •
H G WGB 12 13
B C D
time = 14

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 14

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 14

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WGB 10 • A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 14

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WGB 10 • A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 15

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WGB 10 15 A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 15

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WGB 10 15 A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 15

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WGB 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WGB 10 15 A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 15

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WGB 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WGB 10 15 A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 16

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WGB 1 16
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WGB 10 15 A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 16

E F G H
11.4
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

B C D

E F G H

11.5
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

How to apply & modify DFS algorithm to ...

a determine whether there exists a cycle in digraph,


b determine whether a graph is bipartite,
c determine topological order,
d calculate number of connected components,
e identify articulation points,
f determine a longest path in a given digraph...

11.6
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

B C D

E F G H

11.7
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

B C D

E F G H

11.8
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

A B C

D E

F
H
G

11.9
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

A B C

D E

F
H
G

11.10
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

B D I

C H

E F J K

11.11
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

B D I

C H

E F J K

11.12
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

a d f g

b c e h

11.13
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

a d f g

b c e h

11.14
Trees - DFS

Huynh Tuong Nguyen,


Nguyen Ngoc Le

Chapter 11
Trees - DFS
Discrete Structures for Computing

Huynh Tuong Nguyen, Nguyen Ngoc Le


Faculty of Computer Science and Engineering
University of Technology - VNUHCM
[email protected];[email protected]
11.1
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Start!
1

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

Stuck!
5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

Stuck!
5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

End!
1

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

11.2
Trees - DFS
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Nguyen Ngoc Le

2 3 4

5 6 7 8

Property

• Go deeper as you can


• Backtrack (quay lui) to possible branch when you are stuck.
• O(e) or O(n2 )

11.2
Trees - DFS
Depth-First Search
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Algorithm
procedure DFS (G)
T := tree consisting only vertex v1
visit(v1 )

procedure visit(v: vertex of G) /* recursive */


for each vertex w adjacent to v and not in T
add w and edge {v, w} to T
visit(w)

11.3
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - W • •
6.time=time+1
B - W • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 0

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - W • •
6.time=time+1
B - W • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 0

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG • •
6.time=time+1
B - W • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 0

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG • •
6.time=time+1
B - W • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 1

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B - W • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 1

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A W • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 1

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A W • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 1

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 1

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG • • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 2

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C - W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 2

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 2

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B W • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 2

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 2

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG • •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 3

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E - W • •
F - W • •
G - W • •
H - W • •
B C D
time = 3

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E - W • •
F C W • •
G - W • •
H - W • •
B C D
time = 3

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E - W • •
F C W • •
G - W • •
H - W • •
B C D
time = 3

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E - W • •
F C WG • •
G - W • •
H - W • •
B C D
time = 3

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E - W • •
F C WG • •
G - W • •
H - W • •
B C D
time = 4

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E - W • •
F C WG 4 •
G - W • •
H - W • •
B C D
time = 4

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F W • •
F C WG 4 •
G - W • •
H - W • •
B C D
time = 4

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F W • •
F C WG 4 •
G - W • •
H - W • •
B C D
time = 4

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WG • •
F C WG 4 •
G - W • •
H - W • •
B C D
time = 4

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WG • •
F C WG 4 •
G - W • •
H - W • •
B C D
time = 5

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WG 5 •
F C WG 4 •
G - W • •
H - W • •
B C D
time = 5

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WGB 5 •
F C WG 4 •
G - W • •
H - W • •
B C D
time = 5

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WGB 5 •
F C WG 4 •
G - W • •
H - W • •
B C D
time = 6

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WGB 5 6
F C WG 4 •
G - W • •
H - W • •
B C D
time = 6

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WGB 5 6
F C WG 4 •
G - W • •
H - W • •
B C D
time = 6

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WGB 5 6
F C WGB 4 •
G - W • •
H - W • •
B C D
time = 6

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WGB 5 6
F C WGB 4 •
G - W • •
H - W • •
B C D
time = 7

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 7

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WG 3 •
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 7

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WGB 3 •
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 7

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WGB 3 •
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 8

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WGB 3 8
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 8

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WG 2 • 7.f[v]=time
C B WGB 3 8
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 8

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 • 7.f[v]=time
C B WGB 3 8
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 8

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 • 7.f[v]=time
C B WGB 3 8
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 9

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 9

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D - W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 9

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 9

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A W • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 9

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 9

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG • • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 10

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G - W • •
H - W • •
B C D
time = 10

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D W • •
H - W • •
B C D
time = 10

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D W • •
H - W • •
B C D
time = 10

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG • •
H - W • •
B C D
time = 10

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG • •
H - W • •
B C D
time = 11

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H - W • •
B C D
time = 11

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G W • •
B C D
time = 11

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G W • •
B C D
time = 11

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G WG • •
B C D
time = 11

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G WG • •
B C D
time = 12

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G WG 12 •
B C D
time = 12

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G WGB 12 •
B C D
time = 12

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G WGB 12 •
B C D
time = 13

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G WGB 12 13
B C D
time = 13

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WG 11 •
H G WGB 12 13
B C D
time = 13

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 •
H G WGB 12 13
B C D
time = 13

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 •
H G WGB 12 13
B C D
time = 14

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 14

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WG 10 • A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 14

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WGB 10 • A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 14

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WGB 10 • A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 15

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WGB 10 15 A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 15

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WG 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WGB 10 15 A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 15

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WGB 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WGB 10 15 A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 15

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WGB 1 •
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WGB 10 15 A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 16

E F G H
11.4
Trees - DFS
A pseudocode of DFS
Huynh Tuong Nguyen,
Nguyen Ngoc Le

void DFS(G) void DFSVisit (G, v)


1.loop (more vertex v in G) 1.color[v]=Gray
1.color[v]= White 2.time= time+1
2.father[v]=null 3.d[v]= time
2.time=0 4.loop(more u adjacent to v)
3.loop (more vertex v in G ) 1.if(color[u]==White)
1.if (color[v]== White) 1. father[u]=v
1.DFSVisit(G, v) 2.DFSVisit(G, u)
5.color[v]=Black
vertex father color d f
A - WGB 1 16
6.time=time+1
B A WGB 2 9 7.f[v]=time
C B WGB 3 8
D A WGB 10 15 A
E F WGB 5 6
F C WGB 4 7
G D WGB 11 14
H G WGB 12 13
B C D
time = 16

E F G H
11.4
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

B C D

E F G H

11.5
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

How to apply & modify DFS algorithm to ...

a determine whether there exists a cycle in digraph,


b determine whether a graph is bipartite,
c determine topological order,
d calculate number of connected components,
e identify articulation points,
f determine a longest path in a given digraph...

11.6
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

B C D

E F G H

11.7
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

B C D

E F G H

11.8
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

A B C

D E

F
H
G

11.9
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

A B C

D E

F
H
G

11.10
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

B D I

C H

E F J K

11.11
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

B D I

C H

E F J K

11.12
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

a d f g

b c e h

11.13
Trees - DFS
Exercise
Huynh Tuong Nguyen,
Nguyen Ngoc Le

Exercise
Apply DFS into the following graph.

a d f g

b c e h

11.14

You might also like