0% found this document useful (0 votes)
16 views293 pages

Mid II

Uploaded by

Muhammad Kumail
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)
16 views293 pages

Mid II

Uploaded by

Muhammad Kumail
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/ 293

Chapter 9

Mr. SHOAIB RAZA


Chapter Summary
 Relations and Their Properties
 Representing Relations
 Equivalence Relations
 Partial Orderings
Section 9.1
Section Summary
 Relations and Functions
 Properties of Relations
 Reflexive Relations
 Symmetric Relations
 Antisymmetric Relations
 Transitive Relations
 Irreflexive Relations
 Asymmetric Relations
 Combining Relations
Recall, Function is…
 Let A and B be nonempty sets Function f from A to B is an
assignment of exactly one element of B to each element of A.
 By defining using a relation, a function from A to B
contains unique ordered pair (a, b) for every element a ∈ A.
What is Relation?
Binary Relations
Definition: A binary relation R from a set A to a set B is a
subset R ⊆ A × B.
 Recall, for example:
A = {a1, a2} and B = {b1, b2, b3}
A x B = { (a1, b1), (a1, b2), (a1, b3),
(a2, b1), (a2, b2), (a2, b3)}
 Ordered pairs, which
 First element comes from A
 Second element comes from B
 aRb: (a, b) ∈ R
 : (a, b) ∉ R
Moreover, when (a, b) belongs to R, a is said to be related to b
by R.
Binary Relations
Example:
 Let A = {0,1,2} and B = {a,b}
 {(0, a), (0, b), (1,a) , (2, b)} is a relation from A to B.
 We can represent relations from a set A to a set B
graphically or using a table:
Binary Relations
EXAMPLE:
 Let A = {eggs, milk, corn} and B = {cows, goats, hens}
Define a relation R from A to B by (a, b) ∈R iff a is
produced by b.
 Then R = {(eggs, hens), (milk, cows), (milk, goats)}
 Thus, with respect to this relation eggs R hens , milk R
cows, etc.
Binary Relations
EXAMPLE #1:
Domain and Range of a Relation
DOMAIN OF A RELATION:
The domain of a relation R from A to B is the set of all first
elements of the ordered pairs which belong to R denoted
by Dom(R).
Symbolically, Dom (R) = {a ∈A | (a, b) ∈R}
RANGE OF A RELATION:
The range of a relation R from A to B is the set of all
second elements of the ordered pairs which belong to R
denoted Ran(R).
Symbolically, Ran(R) = {b ∈B | (a, b) ∈ R}
Domain and Range of a Relation
EXERCISE:
Let A = {1, 2}, B = {1, 2, 3},
Define a binary relation R from A to B as follows:
R = {(a, b) ∈A × B | a < b} Then
 a. Find the ordered pairs in R.
 b. Find the Domain and Range of R.
 c. Is 1R3, 2R2?
SOLUTION:
Given A = {1, 2}, B = {1, 2, 3},
A × B = {(1,1), (1,2), (1,3), (2,1), (2,2), (2,3)}
 a. R = {(a, b) ∈A × B | a < b}
R = {(1,2), (1,3), (2,3)}
Domain and Range of a Relation
Given A = {1, 2}, B = {1, 2, 3},
A × B = {(1,1), (1,2), (1,3), (2,1), (2,2), (2,3)}
 b. Find the Domain and Range of R.
Solution:
Dom(R) = {1,2} and Ran(R) = {2, 3}

 c. Is 1R3, 2R2?
Solution:
c. Since (1, 3)∈R so 1R3.
Since (2, 2)∈R so 2R2.
Representing Relations Using Digraphs
Definition: A directed graph, or digraph, consists of a set V of
vertices (or nodes) together with a set E of ordered pairs of
elements of V called edges (or arcs). The vertex a is called the
initial vertex of the edge (a,b), and the vertex b is called the
terminal vertex of this edge.
 An edge of the form (a,a) is called a loop.

Example: A drawing of the directed graph with vertices a, b, c,


and d, and edges (a, b), (a, d), (b, b), (b, d), (c, a), (c, b), and
(d, b) is shown here.
Representing Relations Using Matrices
 A relation between finite sets can be represented using a
zero-one matrix.
 Suppose R is a relation from A = {a1, a2, …, am} to
B = {b1, b2, …, bn}.
 The elements of the two sets can be listed in any particular
arbitrary order. When A = B, we use the same ordering.
 The relation R is represented by the matrix
MR = [mij], where

 The matrix representing R has a 1 as its (i,j) entry when ai is


related to bj and a 0 if ai is not related to bj.
Examples of Representing
Relations Using Matrices
Example 1: Suppose that A = {1,2,3} and B = {1,2}. Let
R be the relation from A to B containing (a,b) if a ∈ A,
b ∈ B, and a > b. What is the matrix representing R
(assuming the ordering of elements is the same as the
increasing numerical order)?
Solution: Because R = {(2,1), (3,1),(3,2)}, the matrix is
Binary Relations
EXAMPLE #1: (cont.)
Binary Relation on a Set
Definition: A binary relation R on a set A is a subset of
A × A or a relation from A to A.
Example:
 Suppose that A = {a,b,c}. Then R = {(a,a),(a,b), (a,c)} is a
relation on A.
 Let A = {1, 2, 3, 4}. The ordered pairs in the relation
R = {(a,b) | a divides b} are
{(1,1), (1, 2), (1,3), (1, 4), (2, 2), (2, 4), (3, 3), and (4, 4)}.
REMARK:
For any set A
1. A × A is known as the universal relation.
2. ∅ is known as the empty relation.
Binary Relation on a Set
Binary Relation on a Set (cont.)
Question: How many different relations are there on a
set A with n elements?
Solution:
 Suppose A has n elements
 Recall, a relation on a set A is a subset of A x A.
 A x A has 𝑛2 elements.
 If a set has m element, its has 2𝑚 subsets.
𝑛2
 Therefore, the answer is 2 .
Reflexive Relations
Definition: R is reflexive iff (a,a) ∊ R for every element
a ∊ A. Written symbolically, R is reflexive if and only if
∀a[a∊U ⟶ (a,a) ∊ R]

If A = ∅ then the empty


relation is reflexive
vacuously. That is the
empty relation on an
empty set is reflexive!
Reflexive Relations
EXAMPLE: Let A = {1, 2, 3, 4} and determine whether
relations R1, R2, R3, and R4 are Reflexive?
R1 = {(1, 1), (3, 3), (2, 2), (4, 4)}
R2 = {(1, 1), (1, 4), (2, 2), (3, 3), (4, 3)}
R3 = {(1, 1), (1, 2), (2, 1), (2, 2), (3, 3), (4, 4)}
R4 = {(1, 3), (2, 2), (2, 4), (3, 1), (4, 4)}
Solution:
 R1 is reflexive, since (a, a) ∈R1 for all a ∈A.
 R2 is not reflexive, because (4, 4) ∉R2.
 R3 is reflexive, since (a, a) ∈R3 for all a ∈A.
 R4 is not reflexive, because (1, 1) ∉R4, (3, 3) ∉R4.
Symmetric Relations
Definition: R is symmetric iff (b,a) ∊ R whenever (a,b) ∊ R
for all a,b ∊ A. Written symbolically, R is symmetric if and
only if
∀a∀b [(a,b) ∊R ⟶ (b,a) ∊ R]

 More precisely, M is a
 symmetric matrix i.e. M = M t
Accordingly, R is symmetric if the
elements in the ith row are the
same as the elements in the ith
column of the matrix M
representing R. More precisely, M
is a symmetric matrix
i.e. M = 𝑀𝑡
Symmetric Relations
EXAMPLE: Let A = {1, 2, 3, 4} and determine whether
relations R1, R2, R3, and R4 are Symmetric?
R1 = {(1, 1), (1, 3), (2, 4), (3, 1), (4,2)}
R2 = {(1, 1), (2, 2), (3, 3), (4, 4)}
R3 = {(2, 2), (2, 3), (3, 4)}
R4 = {(1, 1), (2, 2), (3, 3), (4, 3), (4, 4)}
Solution:
 R1 is Symmetric, since (a, b) and (b, a) ∈R1 for all (a, b) ∈A.
 R2 is also symmetric. We say it is vacuously true.
 R3 is not symmetric, because (2,3) ∈ R3 but (3,2) ∉ R3.
 R4 is not symmetric because (4,3) ∈ R4 but (3,4) ∉ R4.
Transitive Relations
Definition: A relation R on a set A is called transitive if
whenever (a,b) ∊ R and (b,c) ∊ R, then (a,c) ∊ R, for all a,b,c
∊ A. Written symbolically, R is transitive if and only if
∀a∀b ∀c[(a,b) ∊R ∧ (b,c) ∊ R ⟶ (a,c) ∊ R ]

For a transitive
directed graph,
whenever there is
an arrow going
from one point
to the second, and
from the second
to the third, there
is an arrow going
directly from the
first to the third.
Transitive Relations
EXAMPLE: Let A = {1, 2, 3, 4} and determine whether
relations R1, R2 and R3 are Transitive?
R1 = {(1, 1), (1, 2), (1, 3), (2, 3)}
R2 = {(1, 2), (1, 4), (2, 3), (3, 4)}
R3 = {(2, 1), (2, 4), (2, 3), (3,4)}
Solution:
 R1 is transitive because (1, 1), (1, 2) are in R, then to be
transitive relation(1,2) must be there and it belongs to R.
 R2 is not transitive since (1,2) and (2,3) ∈ R2 but (1,3) ∉ R2.
 R3 is transitive.(check by definition)
Irreflexive Relations
Definition: R is irreflexive iff for all a∈A,(a,a) ∉R. That is,
R is irreflexive if no element in A is related to itself by R.
Written symbolically, R is irreflexive if and only if
∀a [(a ∊A) ⟶ (a , a)∉ R]
R is not irreflexive
iff there is an
element a∈A such
that (a,a) ∈R.
Irreflexive Relations
EXAMPLE: Let A = {1, 2, 3, 4} and determine whether
relations R1, R2 and R3 are Irreflexive?
R1 = {(1,3), (1,4), (2,3), (2,4), (3,1), (3,4)}
R2 = {(1,1), (1,2), (2,1), (2,2), (3,3), (4,4)}
R3= {(1,2), (2,3), (3,3), (3,4)}
Solution:
 R1 is irreflexive since no element of A is related to itself in
R1. i.e. (1,1)∉ R1, (2,2) ∉ R1, (3,3) ∉ R1,(4,4) ∉ R1.
 R2 is not irreflexive, since all elements of A are related to
themselves in R2.
 R3 is not irreflexive since (3,3) ∈R3. Note that R3 is not
reflexive.
Antisymmetric Relations
Definition: A relation R on a set A such that for all a,b ∊ A
if (a,b) ∊ R and (b,a) ∊ R, then a = b is called
antisymmetric. Written symbolically, R is antisymmetric if
and only if ∀a∀b [(a,b) ∊R ∧ (b,a) ∊ R ⟶ a = b]
Note: (a,a) may be an element in R. Let R be an anti-
symmetric relation on a
set A = {a1, a2, …, an}.
Then if (ai, aj) ∈R for
i ≠ j then (ai, aj) ∉R.
Thus in the matrix
representation of R
there is a 1 in the ith row
and jth column
iff the jth row and ith
column contains 0 vice
versa.
Antisymmetric Relations
EXAMPLE: Let A = {1, 2, 3, 4} and determine whether relations R1,
R2, R3, and R4 are Antisymmetric?
R1 = {(1,1),(2,2),(3,3)}
R2 = {(1,2),(2,2), (2,3), (3,4), (4,1)}
R3={(1,3),(2,2), (2,4), (3,1), (4,2)}
R4={(1,3),(2,4), (3,1), (4,3)}
Solution:
 R1 is anti-symmetric and symmetric.
 R2 is anti-symmetric but not symmetric because (1,2) ∈ R2 but
(2,1) ∉ R2.
 R3 is not anti-symmetric since (1,3) & (3,1) ∈ R3 but 1 ≠ 3. Note
that R3 is symmetric.
 R4 is neither anti-symmetric because (1,3) & (3,1) ∈ R4 but 1 ≠ 3
nor symmetric because (2,4) ∈ R4 but (4,2) ∉R4.
Asymmetric Relations
Definition: R is Asymmetric iff for all (a,b) ∊R than (b ,a)∉R.
Written symbolically, R is Asymmetric if and only if
∀a ∀b[((a,b) ∊R)) ⟶ ((b , a)∉ R)]
Note:(a,a) cannot be an element in R.

Asymmetry =
Antisymmetry +
Irreflexivity
Asymmetric Relations
 EXAMPLE: Let A = {1, 2, 3, 4} and determine whether
relations R1, R2 and R3 are Asymmetric?
R1 = {(1,1), (1,2), (2,1), (2,2), (3,4), (4,1), (4,4)}
R2 = {(1,2),(2,3),(3,4)}
R3 = {(2,3),(3,3),(3,4)}
Solution:
 R1 is not Asymmetric since R1 is neither Antisymmetric
nor Irreflexive.
 R2 is Asymmetric since R2 is both Antisymmetric and
Irreflexive.
 R3 is not Asymmetric since it is Antisymmetric but not
irreflexive.
Activity Time
Consider the following relations on {1, 2, 3, 4}:

R1 = {(1, 1), (1, 2), (2, 1), (2, 2), (3, 4), (4, 1), (4, 4)},
R2 = {(1, 1), (1, 2), (2, 1)},
R3 = {(1, 1), (1, 2), (1, 4), (2, 1), (2, 2), (3, 3), (4, 1), (4, 4)},
R4 = {(2, 1), (3, 1), (3, 2), (4, 1), (4, 2), (4, 3)},
R5 = {(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)},
R6 = {(3, 4)}.

Determine which of these relation are Reflexive, Symmetric, Transitive,


Antisymmetric, Irreflexive and Asymmetric.
Combining Relations
As R is a subsets of A x B, the set operations can be
applied
 Union (U)
 Intersection (∩)
 Difference (-)
 Symmetric Complement (⊕)
Given two relations R1 and R2, we can combine them
using basic set operations to form new relations such as
R1 ∪ R2, R1 ∩ R2, R1 − R2, R2 − R1 and R1 ⊕ R2.
Combining Relations
Given, A = {1,2,3}, B = {1,2,3,4}
R1 = {(1,1),(2,2),(3,3)},
R2 = {(1,1),(1,2),(1,3),(1,4)}
 R1 U R2 = {(1,1),(1,2),(1,3),(1,4),(2,2),(3,3)}
 R1 ∩ R2 = {(1,1)}
 R1 - R2 = {(2,2),(3,3)}
 R2 - R1 = {(1,2),(1,3),(1,4)}
 R1 ⊕ R2 = {(1,2),(1,3),(1,4),(2,2),(3,3)}
Composition of Relations
Definition: Suppose
 R1 is a relation from a set A to a set B.
 R2 is a relation from B to a set C.
Then the composition (or composite) of R2 with R1, is a
relation from A to C where
 if (x,y) is a member of R1 and (y,z) is a member of R2,
then (x,z) is a member of R2∘ R1.
Representing the Composition of a
Relation
R1 m R2 w
a
x
n
b
y
o
c
p z

R1∘ R2 = {(b,D),(b,B)}
Composition of Relations
What is the composite of the relations R and S, where
 R is the relation from {1,2,3} to {1,2,3,4} with
R = {(1,1),(1,4),(2,3),(3,1),(3,4)}
 S is the relation from {1,2,3,4} to {0,1,2} with
S = {(1,0),(1,2),(2,0),(3,1),(3,2),(4,1)}?
 S o R = {(1,0),(1,2),(1,1), (2,2),(2,1), (3,0),(3,2),(3,1)}
INVERSE OF A RELATION
Let R be a relation from A to B. The inverse relation
𝑅−1 from B to A is defined as:
𝑅−1 = {(b,a) ∈B×A | (a,b) ∈R}
More simply, the inverse relation 𝑅−1 of R is obtained by
interchanging the elements of all the ordered pairs in R.
 Example
X = {a, b, c} and Y={1, 2}
R = {(a, 1), (b, 2), (c, 1)}
 𝑅 −1 = {(1, a), (2, b), (1, c)}
INVERSE OF A RELATION
The relation
R = {(2,2), (2,6), (2,8), (3,6), (4,8)} is
represented by the arrow diagram.

Then inverse of the above relation


can be obtained simply changing
the directions of the arrows and
hence the diagram is
Equivalence Relations
Definition 1: A relation on a set A is called an
equivalence relation if it is reflexive, symmetric, and
transitive.

Definition 2: Two elements a, and b that are related


by an equivalence relation are called equivalent. The
notation a ∼ b is often used to denote that a and b are
equivalent elements with respect to a particular
equivalence relation.
Strings
Example:
Suppose that R is the relation on the set of strings of
English letters such that aRb if and only if l(a) = l(b), where
l(x) is the length of the string x. Is R an equivalence
relation?

Solution: Show that all of the properties of an equivalence


relation hold.
 Reflexivity: Because l(a) = l(a), it follows that aRa for all
strings a.
 Symmetry: Suppose that aRb. Since l(a) = l(b), l(b) = l(a)
also holds and bRa.
 Transitivity: Suppose that aRb and bRc. Since l(a) = l(b),
and l(b) = l(c), l(a) = l(a) also holds and aRc.
Congruence Modulo m
Example: Let m be an integer with m > 1. Show that the relation
R = {(a,b) | a ≡ b (mod m)}
is an equivalence relation on the set of integers.

Solution: Recall that a ≡ b (mod m) if and only if m divides a − b.


 Reflexivity: a ≡ a (mod m) since a − a = 0 is divisible by m since
0 = 0 ∙ m.
 Symmetry: Suppose that a ≡ b (mod m). Then a − b is divisible by m,
and so a − b = km, where k is an integer. It follows that b − a = (− k) m,
so b ≡ a (mod m).
 Transitivity: Suppose that a ≡ b (mod m) and b ≡ c (mod m). Then m
divides both a − b and b − c. Hence, there are integers k and l with
a − b = km and b − c = lm. We obtain by adding the equations:
a − c = (a − b) + (b − c) = km + lm = (k + l) m.
Therefore, a ≡ c (mod m).
Divides
Example: Show that the “divides” relation on the set of
positive integers is not an equivalence relation.
Solution: The properties of reflexivity, and transitivity do
hold, but there relation is not transitive. Hence, “divides” is
not an equivalence relation.
 Reflexivity: a ∣ a for all a.
 Not Symmetric: For example, 2 ∣ 4, but 4 ∤ 2. Hence, the
relation is not symmetric.
 Transitivity: Suppose that a divides b and b divides c.
Then there are positive integers k and l such that b = ak
and c = bl. Hence, c = a(kl), so a divides c. Therefore, the
relation is transitive.
Partial Orderings
Definition 1: A relation R on a set S is called a partial
ordering, or partial order, if it is reflexive,
antisymmetric, and transitive.
A set together with a partial ordering R is called a
partially ordered set, or poset, and is denoted by (S, R).
Members of S are called elements of the poset.
Partial Orderings (continued)
Example 1: Show that the “greater than or equal”
relation (≥) is a partial ordering on the set of integers.
 Reflexivity: a ≥ a for every integer a.
 Antisymmetry: If a ≥ b and b ≥ a , then a = b.
 Transitivity: If a ≥ b and b ≥ c , then a ≥ c.

These properties all follow from the order axioms for the integers.
(See Appendix 1).
Partial Orderings (continued)
Example 2: Show that the divisibility relation (∣) is a
partial ordering on the set of integers.
 Reflexivity: a ∣ a for all integers a. (see Example 9 in
Section 9.1)
 Antisymmetry: If a and b are positive integers with a | b
and b | a, then a = b. (see Example 12 in Section 9.1)
 Transitivity: Suppose that a divides b and b divides c.
Then there are positive integers k and l such that b = ak
and c = bl. Hence, c = a(kl), so a divides c. Therefore, the
relation is transitive.
 (Z+, ∣) is a poset.
Partial Orderings (continued)
Example 3: Show that the inclusion relation (⊆) is a
partial ordering on the power set of a set S.
 Reflexivity: A ⊆ A whenever A is a subset of S.
 Antisymmetry: If A and B are positive integers with
A ⊆ B and B ⊆ A, then A = B.
 Transitivity: If A ⊆ B and B ⊆ C, then A ⊆ C.

The properties all follow from the


definition of set inclusion.
Section Summary
 Sequences.
 Examples: Geometric Progression, Arithmetic
Progression
 Recurrence Relations
 Example: Fibonacci Sequence
 Summations
Introduction
 Sequences are ordered lists of elements.
 EXAMPLES:
 1, 2, 3, 5, 8
 1, 3, 9, 27, 81, …….
 1, 2, 3, 4, 5, …
 4, 8, 12, 16, 20,…
 2, 4, 8, 16, 32, …
 1, 1/2, 1/3, 1/4, 1/5, …
 1, 4, 9, 16, 25, …
 1, -1, 1, -1, 1, -1, …
 Sequences arise throughout mathematics, computer science,
and in many other disciplines, ranging from botany to music.
 We will introduce the terminology to represent sequences and
sums of the terms in the sequences.
SEQUENCES IN COMPUTER
PROGRAMMING:
 An important data type in computer programming
consists of finite sequences known as one-
dimensional arrays; a single variable in which a
sequence of variables may be stored.
EXAMPLE:
 The names of k students in a class may be
represented by an array of k elements “name”
as:
name [0], name[1], name[2], …, name[k-1]
Sequences
Definition: A sequence is a function from a subset of
the integers (usually either the set {0, 1, 2, 3, 4, …..} or
{1, 2, 3, 4, ….} ) to a set S.
 The notation an is used to denote the image of the
integer n. We can think of an as the equivalent of
f(n) where f is a function from {0,1,2,…..} to S. We call
an a term of the sequence.
OR
A sequence is just a list of elements usually written in a
row.
Sequences
Example: Consider the sequence where
Sequences
EXAMPLE:
Write the first four terms of the sequence defined by the
formula: bj = 1 + 2𝑗 , for all integers j ≥ 0
SOLUTION:
 b0 = 1 + 20 = 1 + 1 = 2
 b1 = 1 + 21 = 1 + 2 = 3
 b2 = 1 + 22 = 1 + 4 = 5
 b3 = 1 + 23 = 1 + 8 = 9
REMARK:
The formula bj = 1 + 2𝑗 , for all integers j ≥ 0 defines an
infinite sequence having infinite number of values.
Sequences
 EXERCISE:
Compute the first six terms of the sequence defined by the formula
 Cn = 1+ (−1)𝑛 for all integers n ≥ 0
SOLUTION :
 C0 = 1 + (−1)0 = 1 + 1 = 2
 C1 = 1 + (−1)1 = 1 + (-1) = 0
 C2 = 1 + (−1)2 = 1 + 1 = 2
 C3 = 1 + (−1)3 = 1 + (-1) = 0
 C4 = 1 + (−1)4 = 1 + 1 = 2
 C5 = 1 + (−1)5 = 1 + (-1) = 0
REMARK:
1) If n is even, then Cn = 2 and if n is odd, then Cn = 0. Hence, the
sequence oscillates endlessly between 2 and 0.
2) An infinite sequence may have only a finite number of values.
Sequences
Sequences
Sequences
Arithmetic Progression OR Sequences
 A sequence in which every term after the first is
obtained from the preceding term by adding a
constant number is called an arithmetic sequence or
arithmetic progression (A.P.)
 The constant number, being the difference of any
two consecutive terms is called the common
difference of A.P., commonly denoted by “d”.
EXAMPLES:
1. 5, 9, 13, 17, … (common difference = 4)
2. 0, -5, -10, -15, … (common difference = -5)
3. x + a, x + 3a, x + 5a, … (common difference =
2a)
Arithmetic Sequences
GENERAL TERM OF AN ARITHMETIC
SEQUENCE:
Let a be the first term and d be the common difference of
an arithmetic sequence. Then the sequence is:
a, a+d, a+2d, a+3d, …
If a , for i ≥ 1, represents the terms of the sequence then
i

a1 = first term = a = a + (1-1) d


a2 = second term = a + d = a + (2-1) d
a3 = third term = a + 2d = a + (3 -1) d
By symmetry
an = nth term = a + (n - 1)d for all integers n ≥1.
Arithmetic Sequences
Examples:
1. Let a = −1 and d = 4:

2. Let a = 7 and d = −3:

3. Let a = 1 and d = 2:
Arithmetic Sequences
EXAMPLE:
Find the 20th term of the arithmetic sequence
3, 9, 15, 21, …
SOLUTION:
 Here a = first term = 3
 d = common difference = 9 - 3 = 6
 n = term number = 20
 a20 = value of 20th term = ?
 Since a = a + (n - 1) d; n ≥1
n

∴ a20 = 3 + (20 - 1) 6
= 3 + 114
= 117
Arithmetic Sequences
EXERCISE:
Find the 36th term of the arithmetic sequence whose 3rd term is
7 and 8th term is 17.
SOLUTION:
Let a be the first term and d be the common difference of the
arithmetic sequence.
Then an = a + (n - 1)d n ≥ 1
⇒ a3 = a + (3 - 1) d and a8 = a + (8 - 1) d
Given that a = 7 and a = 17. Therefore
3 8

7 = a + 2d……………………(1) and 17 = a +
7d………….………..(2)
Subtracting (1) from (2), we get,
10 = 5d ⇒d=2
Substituting d = 2 in (1) we have
7 = a + 2(2) which gives a = 3
Arithmetic Sequences
Thus, an = a + (n - 1) d
an = 3 + (n - 1) 2 (using values of a and d)
Hence the value of 36th term is
a36 = 3 + (36 - 1) 2
= 3 + 70
a36 = 73
Geometric Progression OR Sequence
 A sequence in which every term after the first is
obtained from the preceding term by multiplying it
with a constant number is called a geometric
sequence or geometric progression (G.P.)
 The constant number, being the ratio of any two
consecutive terms is called the common ratio of the
G.P. commonly denoted by “r”.
 EXAMPLE:
1. 1, 2, 4, 8, 16, … (common ratio = 2)
2. 3, - 3/2, 3/4, - 3/8, … (common ratio = - 1/2)
3. 0.1, 0.01, 0.001, 0.0001, … (common ratio = 0.1 =
1/10)
GENERAL TERM OF A GEOMETRIC SEQUENCE:
Geometric Progression
Examples:
1. Let a = 1 and r = −1. Then:

2. Let a = 2 and r = 5. Then:

3. Let a = 6 and r = 1/3. Then:


Geometric Sequence
Geometric Sequence
Useful Sequences
SERIES
SUMMATIONS
SUMMATIONS
ARITHMETIC SERIES:
ARITHMETIC SERIES:
GEOMETRIC SERIES:
Strings
Definition: A string is a finite sequence of characters
from a finite set (an alphabet).
 Sequences of characters or bits are important in
computer science.
 The empty string is represented by λ.
 The string abcde has length 5.
Recurrence Relations
Definition: A recurrence relation for the sequence {an}
is an equation that expresses an in terms of one or
more of the previous terms of the sequence, namely,
a0, a1, …, an-1, for all integers n with n ≥ n0, where n0 is a
nonnegative integer.
 A sequence is called a solution of a recurrence relation
if its terms satisfy the recurrence relation.
 The initial conditions for a sequence specify the terms
that precede the first term where the recurrence
relation takes effect.
Questions about Recurrence Relations
Example 1: Let {an} be a sequence that satisfies the
recurrence relation an = an-1 + 3 for n = 1,2,3,4,…. and
suppose that a0 = 2. What are a1 , a2 and a3?
[Here a0 = 2 is the initial condition.]

Solution: We see from the recurrence relation that


a1 = a0 + 3 = 2 + 3 = 5
a2 = 5 + 3 = 8
a3 = 8 + 3 = 11
Questions about Recurrence Relations
Example 2: Let {an} be a sequence that satisfies the
recurrence relation an = an-1 – an-2 for n = 2,3,4,…. and
suppose that a0 = 3 and a1 = 5. What are a2 and a3?
[Here the initial conditions are a0 = 3 and a1 = 5. ]

Solution: We see from the recurrence relation that


a2 = a1 - a0 = 5 – 3 = 2
a3 = a2 – a1 = 2 – 5 = –3
Solving Recurrence Relations
 Finding a formula for the nth term of the sequence
generated by a recurrence relation is called solving the
recurrence relation.
 Such a formula is called a closed formula.
 Various methods for solving recurrence relations will
be covered in Chapter 8 where recurrence relations
will be studied in greater depth.
 Here we illustrate by example the method of iteration
in which we need to guess the formula. The guess can
be proved correct by the method of induction
(Chapter 5).
Fibonacci Sequence
Definition: Define the Fibonacci sequence, f0 ,f1 ,f2,…, by:
 Initial Conditions: f0 = 0, f1 = 1
 Recurrence Relation: fn = fn-1 + fn-2

Example: Find f2 ,f3 ,f4 , f5 and f6 .

Answer:
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.
Iterative Solution Example
Method 1: Working upward, forward substitution
Let {an} be a sequence that satisfies the recurrence relation
an = an-1 + 3 for n = 2,3,4,…. and suppose that a1 = 2.
a2 = 2 + 3
a3 = (2 + 3) + 3 = 2 + 3 ∙ 2
a4 = (2 + 2 ∙ 3) + 3 = 2 + 3 ∙ 3
.
.
.
an = an-1 + 3 = (2 + 3 ∙ (n – 2)) + 3 = 2 + 3(n – 1)
Iterative Solution Example
Method 2: Working downward, backward substitution
Let {an} be a sequence that satisfies the recurrence relation
an = an-1 + 3 for n = 2,3,4,…. and suppose that a1 = 2.

an = an-1 + 3
= (an-2 + 3) + 3 = an-2 + 3 ∙ 2
= (an-3 + 3 )+ 3 ∙ 2 = an-3 + 3 ∙ 3
.
.
.

= a2 + 3(n – 2) = (a1 + 3) + 3(n – 2) = 2 + 3(n – 1)


Some Useful Summation Formulae
Geometric Series: We
just proved this.

Later we
will prove
some of
these by
induction.

Proof in text
(requires calculus)
Chapter 10
Chapter 10
Chapter Summary
 Graphs and Graph Models
 Graph Terminology and Special Types of Graphs
 Representing Graphs and Graph Isomorphism
 Connectivity
 Euler and Hamiltonian Graphs
 Shortest-Path Problems
 Planar Graphs
 Graph Coloring
Section 10.1
Section Summary
 Introduction to Graphs
 Graph Taxonomy
 Graph Models
Chapter 10 Graphs
Definition: A graph G = (V, E) consists of a
nonempty set V of vertices (or nodes) and a set E of
edges. Each edge has either one or two vertices
associated with it, called its endpoints. An edge is
said to connect its endpoints.
Basic Types of GRAPH
• Simple Graph

• Multi Graph

• Pseudo Graph

• Null Graph

• Complete Graph

• Regular Graph
Some Terminology
 In a simple graph each edge connects two different vertices and no two
edges connect the same pair of vertices.
 Multigraphs may have multiple edges connecting the same two vertices.
When m different edges connect the vertices u and v, we say that {u,v} is
an edge of multiplicity m.
 An edge that connects a vertex to itself is called a loop.
 A pseudograph may include loops, as well as multiple edges connecting
the same pair of vertices.

Example: a b Remark: There is no standard


This pseudograph terminology for graph theory. So, it is
has both multiple crucial that you understand the
edges and a loop. terminology being used whenever you
read material about graphs.
c
Directed Graphs
Definition: An directed graph (or digraph)
G = (V, E) consists of a nonempty set V of
vertices (or nodes) and a set E of directed
edges (or arcs). Each edge is associated with
an ordered pair of vertices. The directed
edge associated with the ordered pair (u,v) is
said to start at u and end at v.

Remark:
 Graphs where the end points of an edge are not
ordered are said to be undirected graphs.
Some Terminology (continued)

 A simple directed graph has no loops and no multiple edges.


a b
Example:
This is a directed graph with
three vertices and four edges.
c

 A directed multigraph may have multiple directed edges. When there


are m directed edges from the vertex u to the vertex v, we say that (u,v)
is an edge of multiplicity m.

Example:
In this directed multigraph the a b
multiplicity of (a,b) is 1 and the
multiplicity of (b,c) is 2.
c
Graph Terminology: Summary

 To understand the structure of a graph and to build a graph


model, we ask these questions:
• Are the edges of the graph undirected or directed (or both)?
• If the edges are undirected, are multiple edges present that connect
the same pair of vertices? If the edges are directed, are multiple
directed edges present?
• Are loops present?
Other Applications of Graphs
 We will illustrate how graph theory can be used
in models of:
 Social networks
 Communications networks
 Information networks
 Software design
 Transportation networks
 Biological networks
 It’s a challenge to find a subject to which graph
theory has not yet been applied. Can you find
an area without applications of graph theory?
Graph Models: Computer Networks
 When we build a graph model, we use the appropriate type of graph to capture
the important features of the application.
 We illustrate this process using graph models of different types of computer
networks. In all these graph models, the vertices represent data centers and the
edges represent communication links.
 To model a computer network where we are only concerned whether two data
centers are connected by a communications link, we use a simple graph. This is
the appropriate type of graph when we only care whether two data centers are
directly linked (and not how many links there may be) and all communications
links work in both directions.
Graph Models:
Computer Networks (continued)
 To model a computer network where we care about the
number of links between data centers, we use a multigraph.
Graph Models: Computer
Networks
 To model a computer network with diagnostic links at
data centers, we use a pseudograph, as loops are needed.
Graph Models: Computer
Networks
 To model a network with multiple one-way links, we use a
directed multigraph. Note that we could use a directed
graph without multiple edges if we only care whether there
is at least one link from a data center to another data
center.
Graph Models: Social Networks
 Graphs can be used to model social structures based on
different kinds of relationships between people or groups.
 In a social network, vertices represent individuals or
organizations and edges represent relationships between
them.
 Useful graph models of social networks include:

 friendship graphs - undirected graphs where two


people are connected if they are friends (in the real
world, on Facebook, or in a particular virtual world, and
so on.)
 collaboration graphs - undirected graphs where two
people are connected if they collaborate in a specific way
 influence graphs - directed graphs where there is an
edge from one person to another if the first person can
influence the second person ..
Graph Models: Social Networks
Example: A friendship graph where two people are connected
if they are Facebook friends.
Graph Models: Social Networks
Example: An influence graph
Applications to Information Networks
 Graphs can be used to model different types of networks
that link different types of information.
 In a web graph, web pages are represented by vertices and
links are represented by directed edges.

 A web graph models the web at a particular time.


 We will explain how the web graph is used by search engines in
Section 11.4.
 In a citation network:

 Research papers in a particular discipline are represented by


vertices.
 When a paper cites a second paper as a reference, there is an edge
from the vertex representing this paper to the vertex representing
the second paper.
Transportation Graphs
 Graph models are extensively used in the study of
transportation networks.
 Airline networks can be modeled using directed
multigraphs where
 airports are represented by vertices
 each flight is represented by a directed edge from the vertex
representing the departure airport to the vertex representing
the destination airport
 Road networks can be modeled using graphs where
 vertices represent intersections and edges represent roads.
 undirected edges represent two-way roads and directed
edges represent one-way roads.
Software Design Applications
 Graph models are extensively used in software design. We
will introduce two such models here; one representing the
dependency between the modules of a software application
and the other representing restrictions in the execution of
statements in computer programs.

 When a top-down approach is used to design software, the


system is divided into modules, each performing a specific
task.

 We use a module dependency graph to represent the
dependency between these modules. These dependencies
need to be understood before coding can be done.
Software Design Applications
 In a module dependency graph vertices represent software modules
and there is an edge from one module to another if the second
module depends on the first.

Example: The dependencies between the seven modules in


the design of a web browser are represented by this module
dependency graph.
Software Design Applications
 We can use a directed graph called a precedence graph to
represent which statements must have already been
executed before we execute each statement.

 Vertices represent statements in a computer program


 There is a directed edge from a vertex to a second vertex if the
second vertex cannot be executed before the first

Example: This precedence


graph shows which statements
must already have been
executed before we can
execute each of the six
statements in the program.
Biological Applications
 Graph models are used extensively in many areas of the
biological science. We will describe two such models, one to
ecology and the other to molecular biology.
 Niche overlap graphs model competition between species in an
ecosystem
 Vertices represent species and an edge connects two vertices when they
represent species who compete for food resources.
Example: This is the niche overlap graph for a forest ecosystem with nine species.
Biological Applications
 We can model the interaction of proteins in a cell using a protein
interaction network.
 In a protein interaction graph, vertices represent proteins and
vertices are connected by an edge if the proteins they represent
interact.
 Protein interaction graphs can be huge and can contain more than
100,000 vertices, each representing a different protein, and more
than 1,000,000 edges, each representing an interaction between
proteins
 Protein interaction graphs are often split into smaller graphs, called
modules, which represent the interactions between proteins
involved in a particular function.
Biological Applications
Example: This is a module of the protein interaction graph of
proteins that degrade RNA in a human cell.
Section 10.2
Section Summary
 Basic Terminology
 Some Special Types of Graphs
 Bipartite Graphs
 Bipartite Graphs and Matchings
 Some Applications of Special Types of Graphs
 New Graphs from Old
Basic Terminology
Definition 1. Two vertices u, v in an undirected graph G are
called adjacent (or neighbors) in G if there is an edge e
between u and v. Such an edge e is called incident with the
vertices u and v and e is said to connect u and v.

Definition 2. The set of all neighbors of a vertex v of G = (V,


E), denoted by N(v), is called the neighborhood of v. If A is a
subset of V, we denote by N(A) the set of all vertices in G that
are adjacent to at least one vertex in A. So,

Definition 3. The degree of a vertex in a undirected graph is


the number of edges incident with it, except that a loop at a
vertex contributes two to the degree of that vertex. The degree
of the vertex v is denoted by deg(v).
Degrees and Neighborhoods of Vertices
Example: What are the degrees and neighborhoods of the
vertices in the graphs G and H?

Solution:
G: deg(a) = 2, deg(b) = deg(c) = deg(f ) = 4, deg(d ) = 1, deg(e) = 3, deg(g) = 0.
N(a) = {b, f }, N(b) = {a, c, e, f }, N(c) = {b, d, e, f }, N(d) = {c},
N(e) = {b, c , f }, N(f) = {a, b, c, e}, N(g) =  .
H: deg(a) = 4, deg(b) = deg(e) = 6, deg(c) = 1, deg(d) = 5.
N(a) = {b, d, e}, N(b) = {a, b, c, d, e}, N(c) = {b}, N(d) = {a, b, e},
N(e) = {a, b ,d}.
Handshaking Theorem
Theorem: If G is any graph, then the sum of the degrees of all the
vertices of G equals twice the number of edges of G.
Specifically, if the vertices of G are v1, v2, …, vn, where n is a positive
integer, then
the total degree of G = deg(v1) + deg(v2) + … + deg(vn)
= 2 . (the number of edges of G)
PROOF:
 Each edge “e” of G connects its end points vi and vj. This edge,
therefore contributes 1 to the degree of vi and 1 to the degree of vj.
 If “e” is a loop, then it is counted twice in computing the degree of
the vertex on which it is incident.
 Accordingly, each edge of G contributes 2 to the total degree of G.
Thus, the total degree of G = 2. (the number of edges of G)
COROLLARY: The total degree of G is an even number
Degree of Vertices
Theorem: An undirected graph has an even number of
vertices of odd degree.
Proof: Let V1 be the vertices of even degree and V2 be the
vertices of odd degree in an undirected graph G = (V, E)
with m edges. Then

even

must be even since deg(v) is This sum must be even because 2m is even and
even for each v ∈ V1 the sum of the degrees of the vertices of even
degrees is also even. Because this is the sum of
the degrees of all vertices of odd degree in the
graph, there must be an even number of such
vertices.
Handshaking Theorem
We now give two examples illustrating the usefulness of the
handshaking theorem.
Example: How many edges are there in a graph with 10
vertices of degree six?
Solution: Because the sum of the degrees of the vertices is
6  10 = 60, the handshaking theorem tells us that 2m = 60.
So the number of edges m = 30.

Example: If a graph has 5 vertices, can each vertex have degree


3?
Solution: This is not possible by the handshaking theorem,
because the sum of the degrees of the vertices 3  5 = 15 is odd.
Directed Graphs
Recall the definition of a directed graph.

Definition: An directed graph G = (V, E) consists of V,


a nonempty set of vertices (or nodes), and E, a set of
directed edges or arcs. Each edge is an ordered pair of
vertices. The directed edge (u,v) is said to start at u
and end at v.
Definition: Let (u,v) be an edge in G. Then u is the
initial vertex of this edge and is adjacent to v and v is
the terminal (or end) vertex of this edge and is adjacent
from u. The initial and terminal vertices of a loop are
the same.
Directed Graphs (continued)
Definition:The in-degree of a vertex v, denoted deg−(v), is
the number of edges which terminate at v. The out-degree of v,
denoted deg+(v), is the number of edges with v as their initial
vertex. Note that a loop at a vertex contributes 1 to both the
in-degree and the out-degree of the vertex.
Example: In the graph G we have

deg−(a) = 2, deg−(b) = 2, deg−(c) = 3, deg−(d) = 2, deg−(e) = 3, deg−(f) = 0.

deg+(a) = 4, deg+(b) = 1, deg+(c) = 2, deg+(d) = 2, deg+ (e) = 3, deg+(f) = 0.


Directed Graphs (continued)
Theorem 3: Let G = (V, E) be a graph with directed edges.
Then:

Proof: The first sum counts the number of outgoing edges


over all vertices and the second sum counts the number of
incoming edges over all vertices. It follows that both sums
equal the number of edges in the graph.
Special Types of Simple Graphs:
Complete Graphs
A complete graph on n vertices, denoted by Kn, is the
simple graph that contains exactly one edge between
each pair of distinct vertices.
Special Types of Simple Graphs:
Cycles and Wheels
A cycle Cn for n ≥ 3 consists of n vertices v1, v2 ,⋯ , vn, and
edges {v1, v2}, {v2, v3} ,⋯ , {vn-1, vn}, {vn, v1}.

A wheel Wn is obtained by adding an additional vertex to a cycle


Cn for n ≥ 3 and connecting this new vertex to each of the n
vertices in Cn by new edges.
Special Types of Graphs and Computer
Network Architecture
Various special graphs play an important role in the design of
computer networks.

 Some local area networks use a star topology, which is a complete


bipartite graph K1,n ,as shown in (a). All devices are connected to
a central control device.
 Other local networks are based on a ring topology, where each
device is connected to exactly two others using Cn ,as illustrated
in (b). Messages may be sent around the ring.
 Others, as illustrated in (c), use a Wn – based topology,
combining the features of a star topology and a ring topology.
Special Types of Simple Graphs:
n-Cubes

An n-dimensional hypercube, or n-cube, Qn, is a graph


with 2n vertices representing all bit strings of length n,
where there is an edge between two vertices that differ
in exactly one bit position.
Special Types of Graphs and Computer Network
Architecture
 Various special graphs also play a role in parallel processing where
processors need to be interconnected as one processor may need the
output generated by another.
 The n-dimensional hypercube, or n-cube, Qn, is a common way to
connect processors in parallel, e.g., Intel Hypercube.
 Another common method is the mesh network, illustrated here for 16
processors.
Bipartite Graphs
Definition: A simple graph G is bipartite if V can be
partitioned into two disjoint subsets V1 and V2 such that every
edge connects a vertex in V1 and a vertex in V2. In other words,
there are no edges which connect two vertices in V1 or in V2.

 It is not hard to show that an equivalent definition of a


bipartite graph is a graph where it is possible to color the
vertices red or blue so that no two adjacent vertices are the
same color.
Bipartite Graphs (continued)

Example: Show that C3 is not bipartite.


Solution: If we divide the vertex set of C3 into two
nonempty sets, one of the two must contain two
vertices. But in C3 every vertex is connected to every
other vertex. Therefore, the two vertices in the same
partition are connected. Hence, C3 is not bipartite.
Bipartite Graphs (continued)
Example: Show that C6 is bipartite.
Solution: We can partition the vertex set into
V1 = {v1, v3, v5} and V2 = {v2, v4, v6} so that every edge of C6 connects a
vertex in V1 and V2 .
Bipartite Graphs

G is bipartite H is not bipartite since if we color a red, then


the adjacent vertices f and b must both be blue.
Bipartite Graphs and Matchings
 Bipartite graphs are used to model applications that involve
matching the elements of one set to elements in another,
for example:
 Job assignments - vertices represent the jobs and the
employees, edges link employees with those jobs they have
been trained to do. A common goal is to match jobs to
employees so that the most jobs are done.
Bipartite Graphs and Matchings
Bipartite Graphs and Matchings
Complete Bipartite Graphs
Definition: A complete bipartite graph Km,n is a graph that
has its vertex set partitioned into two subsets V1 of size m and
V2 of size n such that there is an edge from every vertex in V1
to every vertex in V2.
Example: We display four complete bipartite graphs here.
New Graphs from Old
Definition: A subgraph of a graph G = (V,E) is a graph (W,F),
where W ⊂ V and F ⊂ E. A subgraph H of G is a proper subgraph of G
if H ≠ G.
Example: Here we show K5 and one of its subgraphs.

Definition: Let G = (V, E) be a simple graph. The subgraph


induced by a subset W of the vertex set V is the graph (W,F),
where the edge set F contains an edge in E if and only if both
endpoints are in W.
Example: Here we show K5 and the subgraph induced by W =
{a,b,c,e}.
New Graphs from Old (continued)
Definition: The union of two simple graphs G1 = (V1, E1)
and G2 = (V2, E2) is the simple graph with vertex set V1 ⋃ V2
and edge set E1 ⋃ E2. The union of G1 and G2 is denoted by
G1 ⋃ G2.
Example:
Section 10.3
Section Summary
 Adjacency Lists
 Adjacency Matrices
 Incidence Matrices
Representing Graphs: Adjacency Lists
Definition: An adjacency list can be used to represent a graph
with no multiple edges by specifying the vertices that are adjacent
to each vertex of the graph.
Example:
Representing Graphs: Adjacency Lists

Example:
Representation of Graphs:
Adjacency Matrices
Definition: Suppose that G = (V, E) is a simple graph
where |V| = n. Arbitrarily list the vertices of G as
v1, v2, … , vn. The adjacency matrix AG of G, with
respect to the listing of vertices, is the n × n zero-one
matrix with 1 as its (i, j)th entry when vi and vj are
adjacent, and 0 as its (i, j)th entry when they are not
adjacent.
 In other words, if the graphs adjacency matrix is
AG = [aij], then
Adjacency Matrices (continued)
Example: When a graph is sparse,
that is, it has few edges
relatively to the total
number of possible edges,
The ordering
it is much more efficient
of vertices is
a, b, c, d. to represent the graph
using an adjacency list
than an adjacency matrix.
But for a dense graph,
The ordering
which includes a high
of vertices is
a, b, c, d. percentage of possible
edges, an adjacency
matrix is preferable.

Note: The adjacency matrix of a simple graph is symmetric, i.e., aij = aji
Also, since there are no loops, each diagonal entry aij for i = 1, 2, 3, …, n, is 0.
Adjacency Matrices (continued)
 Adjacency matrices can also be used to represent graphs with
loops and multiple edges.
 A loop at the vertex vi is represented by a 1 at the (i, j)th
position of the matrix.
 When multiple edges connect the same pair of vertices vi and
vj, (or if multiple loops are present at the same vertex), the (i,
j)th entry equals the number of edges connecting the pair of
vertices.
 Example: We give the adjacency matrix of the pseudograph
shown here using the ordering of vertices a, b, c, d.
Adjacency Matrices (continued)
 Adjacency matrices can also be used to represent
directed graphs. The matrix for a directed graph G =
(V, E) has a 1 in its (i, j)th position if there is an edge
from vi to vj, where v1, v2, … vn is a list of the vertices.
 In other words, if the graphs adjacency matrix is AG = [aij], then

 The adjacency matrix for a directed graph does not have to be


symmetric, because there may not be an edge from vi to vj, when
there is an edge from vj to vi.
 To represent directed multigraphs, the value of aij is the number
of edges connecting vi to vj.
Representation of Graphs:
Incidence Matrices
Definition: Let G = (V, E) be an undirected graph
with vertices where v1, v2, … vn and edges
e1, e2, … em. The incidence matrix with respect to the
ordering of V and E is the n × m matrix M = [mij],
where
Incidence Matrices (continued)
Example: Simple Graph and Incidence Matrix
The rows going
from top to
bottom
represent v1
through v5 and
the columns
going from left
to right
represent e1
through e6.
Example: Pseudograph and Incidence Matrix
The rows going
from top to
bottom represent
v1 through v5 and
the columns
going from left to
right represent e1
through e8.
Section 10.4
Section Summary
 Paths
 Connectedness in Undirected Graphs
 Connectedness in Directed Graphs
 Counting Paths between Vertices
Paths
Informal Definition: A path is a sequence of edges
that begins at a vertex of a graph and travels from
vertex to vertex along edges of the graph. As the path
travels along its edges, it visits the vertices along this
path, that is, the endpoints of these.
Applications: Numerous problems can be modeled
with paths formed by traveling along edges of graphs
such as:
 determining whether a message can be sent between
two computers.
 efficiently planning routes for mail delivery.
Paths
Definition: Let n be a nonnegative integer and G an
undirected graph. A path of length n from u to v in G is a
sequence of n edges e1, … , en of G for which there exists a
sequence x0 = u, x1, …, xn-1, xn = v of vertices such that ei has,
for i = 1, …, n, the endpoints xi-1 and xi.
 When the graph is simple, we denote this path by its
vertex sequence x0, x1, … , xn(since listing the vertices
uniquely determines the path).
 The path is a circuit if it begins and ends at the same
vertex (u = v) and has length greater than zero.
 The path or circuit is said to pass through the vertices
x1, x2, … , xn-1 and traverse the edges e1, … , en.
 A path or circuit is simple if it does not contain the
same edge more than once.
Paths (continued)

Example: In the simple graph here:


 a, d, c, f, e is a simple path of length 4.
 d, e, c, a is not a path because e is not connected to c.
 b, c, f, e, b is a circuit of length 4.
 a, b, e, d, a, b is a path of length 5, but it is not a simple
path.
Connectedness in Undirected Graphs
Definition: An undirected graph is called connected if there
is a path between every pair of vertices. An undirected graph
that is not connected is called disconnected. We say that we
disconnect a graph when we remove vertices or edges, or both,
to produce a disconnected subgraph.
Example: G1 is connected because there is a path between any
pair of its vertices, as can be easily seen. However G2 is not
connected because there is no path between vertices a and f,
for example.
Section 10.5
Section Summary
 Euler Paths and Circuits
 Hamilton Paths and Circuits
 Applications of Hamilton Circuits
Euler Paths and Circuits
 The town of Kӧnigsberg, Prussia Leonard Euler
(now Kalingrad, Russia) was divided into (1707-1783)
four sections by the branches of the Pregel river. In the 18th century
seven bridges connected these regions.
 People wondered whether whether it was possible to follow a path
that crosses each bridge exactly once and returns to the starting
point.
 The Swiss mathematician Leonard Euler proved that no such path
exists. This result is often considered to be the first theorem ever
proved in graph theory.

Multigraph
Model of the
Bridges of
Kӧnigsberg
The 7 Bridges of Kӧnigsberg
Euler Paths and Circuits
Definition: An Euler circuit in a graph G is a simple circuit containing
every edge of G. An Euler path in G is a simple path containing every
edge of G.
Example: Which of the undirected graphs G1, G2, and G3 has a Euler
circuit? Of those that do not, which has an Euler path?

Solution: The graph G1 has an Euler circuit (e.g., a, e, c, d, e, b, a). But, as can
easily be verified by inspection, neither G2 nor G3 has an Euler circuit. Note
that G3 has an Euler path (e.g., a, c, d, e, b, d, a, b), but there is no Euler path
in G2, which can be verified by inspection.
Necessary Conditions for Euler Circuits and Paths
 An Euler circuit begins with a vertex a and continues with an edge
incident with a, say {a, b}. The edge {a, b} contributes one to
deg(a).
 Each time the circuit passes through a vertex it contributes two to
the vertex’s degree.
 Finally, the circuit terminates where it started, contributing one to
deg(a). Therefore deg(a) must be even.
 We conclude that the degree of every other vertex must also be
even.
 By the same reasoning, we see that the initial vertex and the final
vertex of an Euler path have odd degree, while every other vertex
has even degree. So, a graph with an Euler path has exactly two
vertices of odd degree.
 In the next slide we will show that these necessary conditions are
also sufficient conditions.
Sufficient Conditions for Euler Circuits and Paths
Suppose that G is a connected multigraph with ≥ 2 vertices, all of even
degree. Let x0 = a be a vertex of even degree. Choose an edge {x0, x1}
incident with a and proceed to build a simple path {x0, x1}, {x1, x2}, …, {xn-1,
xn} by adding edges one by one until another edge can not be added.

We illustrate this idea in the graph G here. We


begin at a and choose the edges {a, f}, {f, c}, {c, b},
and {b, a} in succession.

 The path begins at a with an edge of the


form {a, x}; we show that it must terminate
at a with an edge of the form {y, a}. Since each vertex has an even degree,
there must be an even number of edges incident with this vertex. Hence,
every time we enter a vertex other than a, we can leave it. Therefore, the path
can only end at a.
 If all of the edges have been used, an Euler circuit has been constructed.
Otherwise, consider the subgraph H obtained from G by deleting the
edges already used.
In the example H consists of the vertices c, d, e.
Sufficient Conditions for Euler Circuits and Paths

 Because G is connected, H must have at least one vertex in common with the circuit that has been deleted.

In the example, the vertex is c.

 Every vertex in H must have even degree because all the vertices in G have even degree and for
each vertex, pairs of edges incident with this vertex have been deleted. Beginning with the
shared vertex construct a path ending in the same vertex (as was done before). Then splice this
new circuit into the original circuit.
In the example, we end up with the circuit a, f, c, d, e, c, b, a.

 Continue this process until all edges have been used. This produces an Euler circuit. Since every edge
is included and no edge is included more than once.
 Similar reasoning can be used to show that a graph with exactly two vertices of odd degree must have
an Euler path connecting these two vertices of odd degree
Algorithm for Constructing an
Euler Circuits
In our proof we developed this algorithms for
constructing a Euler circuit in a graph with no vertices of
odd degree.
procedure Euler(G: connected multigraph with all vertices of even degree)
circuit := a circuit in G beginning at an arbitrarily chosen vertex with edges
successively added to form a path that returns to this vertex.
H := G with the edges of this circuit removed
while H has edges
subciruit := a circuit in H beginning at a vertex in H that also is
an endpoint of an edge in circuit.
H := H with edges of subciruit and all isolated vertices removed
circuit := circuit with subcircuit inserted at the appropriate vertex.
return circuit{circuit is an Euler circuit}
Necessary and Sufficient Conditions for
Euler Circuits and Paths (continued)
Theorem: A connected multigraph with at least two vertices
has an Euler circuit if and only if each of its vertices has an
even degree and it has an Euler path if and only if it has
exactly two vertices of odd degree.

Example: Two of the vertices in the multigraph model of the


Kӧnigsberg bridge problem have odd degree. Hence, there is
no Euler circuit in this multigraph and it is impossible to
start at a given point, cross each bridge exactly once, and
return to the starting point.
Euler Circuits and Paths
Example:

G1 contains exactly two vertices of odd degree (b and d). Hence it has
an Euler path, e.g., d, a, b, c, d, b.

G2 has exactly two vertices of odd degree (b and d). Hence it has an
Euler path, e.g., b, a, g, f, e, d, c, g, b, c, f, d.

G3 has six vertices of odd degree. Hence, it does not have an Euler path.
Applications of Euler Paths and
Circuits
 Euler paths and circuits can be used to solve many practical
problems such as finding a path or circuit that traverses
each
 street in a neighborhood,
 road in a transportation network,
 connection in a utility grid,
 link in a communications network.
 Other applications are found in the
 layout of circuits,
 network multicasting,
 molecular biology, where Euler paths are used in the
sequencing of DNA.
William Rowan
Hamilton
Hamilton Paths and Circuits (1805- 1865)

 Euler paths and circuits contained every edge only once.


Now we look at paths and circuits that contain every vertex exactly once.
 William Hamilton invented the Icosian puzzle in 1857. It consisted of a wooden
dodecahedron (with 12 regular pentagons as faces), illustrated in (a), with a peg
at each vertex, labeled with the names of different cities. String was used to used
to plot a circuit visiting 20 cities exactly once
 The graph form of the puzzle is given in (b).

 The solution (a Hamilton circuit) is given here.


Hamilton Paths and Circuits
Definition: A simple path in a graph G that passes through
every vertex exactly once is called a Hamilton path, and a
simple circuit in a graph G that passes through every vertex
exactly once is called a Hamilton circuit.

That is, a simple path x0, x1, …, xn-1, xn in the graph G = (V,
E) is called a Hamilton path if V = {x0, x1, … , xn-1, xn } and xi
≠ xj for 0≤ i < j ≤ n, and the simple circuit x0, x1, …, xn-1, xn,
x0 (with n > 0) is a Hamilton circuit if x0, x1, … , xn-
1, xn is a Hamilton path.
Hamilton Paths and Circuits
Example: Which of these simple graphs has a
Hamilton circuit or, if not, a Hamilton path?

Solution:
G1 does not have a Hamilton circuit (Why?), but does
have a Hamilton path : a, b ,e , c, d.
G2 has a Hamilton circuit: a, b, c, d, e, a.
G3 has a Hamilton circuit: a,b,e,d,c,a
Necessary Conditions for
Hamilton Circuits Gabriel Andrew Dirac
(1925-1984)
 Unlike for an Euler circuit, no simple necessary and sufficient
conditions are known for the existence of a Hamiton circuit.
 However, there are some useful necessary conditions. We
describe two of these now.
Dirac’s Theorem: If G is a simple graph with n ≥ 3 vertices such
that the degree of every vertex in G is ≥ n/2, then G has a
Hamilton circuit.

Ore’s Theorem: If G is a simple graph with n ≥ 3 vertices such


that deg(u) + deg(v) ≥ n for every pair of nonadjacent vertices,
then G has a Hamilton circuit.
Øysten Ore
(1899-1968)
Applications of Hamilton Paths and
Circuits
 Applications that ask for a path or a circuit that visits each
intersection of a city, each place pipelines intersect in a
utility grid, or each node in a communications network
exactly once, can be solved by finding a Hamilton path in
the appropriate graph.
 The famous traveling salesperson problem (TSP) asks for
the shortest route a traveling salesperson should take to
visit a set of cities. This problem reduces to finding a
Hamilton circuit such that the total sum of the weights of
its edges is as small as possible.
 A family of binary codes, known as Gray codes, which
minimize the effect of transmission errors, correspond to
Hamilton circuits in the n-cube Qn.
Section 10.3
Isomorphism of Graphs
Definition: The simple graphs G1 = (V1, E1) and
G2 = (V2, E2) are isomorphic if there is a one-to-one
and onto function f from V1 to V2 with the property
that a and b are adjacent in G1 if and only if f(a) and
f(b) are adjacent in G2 , for all a and b in V1 . Such a
function f is called an isomorphism. Two simple graphs
that are not isomorphic are called nonisomorphic.
Isomorphism of Graphs
 It is difficult to determine whether two simple graphs are
isomorphic using brute force because there are n! possible
one-to-one correspondences between the vertex sets of two
simple graphs with n vertices.
 The best algorithms for determining weather two graphs
are isomorphic have exponential worst case complexity in
terms of the number of vertices of the graphs.
 Sometimes it is not hard to show that two graphs are not
isomorphic. We can do so by finding a property, preserved
by isomorphism, that only one of the two graphs has. Such
a property is called graph invariant.
 There are many different useful graph invariants that can
be used to distinguish nonisomorphic graphs, such as the
number of vertices, number of edges, and degree sequence
(list of the degrees of the vertices in nonincreasing order).
We will encounter others in later sections of this chapter.
ISOMORPHIC INVARIANT
 A property P is called an isomorphic invariant if, and only if, given any
graphs G and G’,
 If G has property P and G’ is isomorphic to G, then G’ has property P.
THEOREM OF ISOMORPHIC INVARIANT
Each of the following properties is an invariant for graph isomorphism,
where n, m and k are all non-negative integers, if the graph:
1. has n vertices.
2. has m edges.
3. has a vertex of degree k.
4. has m vertices of degree k.
5. has a circuit of length k.
6. has a simple circuit of length k.
7. has m simple circuits of length k.
8. is connected.
9. has an Euler circuit.
10. has a Hamiltonian circuit.
Isomorphism of Graphs (cont.)
Example: Show that the graphs G =(V, E) and
H = (W, F) are isomorphic.

Solution: The function f with


f(u1) = v1,f(u2) = v4, f(u3) = v3, and f(u4) = v2 is a one-to-one
correspondence between V and W.
Note that adjacent vertices in G are
u1 and u2, u1 and u3, u2 and u4,
and u3 and u4. Each of the pairs
f(u1) = v1 and f(u2) = v4, f(u1) = v1
and f(u3) = v3 , f(u2) = v4 and
f(u4) = v2 , and f(u3) = v3 and
f(u4) = v2 consists of two adjacent vertices in H.
Isomorphism of Graphs
Example: Show that the graphs G =(V, E)
and G’ = (W, F) are isomorphic.

Solution: The function f with


f(v1) = v1,f(v2) = v3, f(v3) = v5, f(v4) = v2 and
f(v5) = v4 is a one-to-one correspondence
between V and W.
Note that adjacent vertices in G are v1 and v2,
v2 and v3, v3 and v4, v4 and v5 and v5 and v1.
Each of the pairs f(v1) = v1 and f(v2) = v3,
f(v2) = v3 and f(v3) = v5 , f(v3) = v5 and
f(v4) = v2 , f(v4) = v2 and f(v5) = v4 and f(v5)
= v4 and f(v1) = v1 consists of two adjacent
vertices in H.
Isomorphism of Graphs
EXAMPLE: Determine whether the graph G and G’ given below
are isomorphic.

SOLUTION:
As both the graphs have the same number of vertices. But the
graph G has 7 edges and the graph G’ has only 6 edges. Therefore
the two graphs are not isomorphic.
Note: As the edges of both the graphs G and G’ are not same
then how the one-one correspondence is possible ,that the
reason the graphs G and G’ are not isomorphic.
Isomorphism of Graphs
EXAMPLE: Determine whether the graph G and G’ given
below are isomorphic.

SOLUTION:
Both the graphs have 5 vertices and 7 edges. The vertex q
of G’ has degree 5. However G does not have any vertex of
degree 5 (so one-one correspondence is not possible).
Hence, the two graphs are not isomorphic.
Isomorphism of Graphs
EXAMPLE: Determine whether the graph G and G’ given below
are isomorphic.

SOLUTION:
Clearly the vertices of both the graphs G and G’ have the same
degree (i.e “2”) and having the same number of vertices and
edges but isomorphism is not possible. As the graph G’ is a
connected graph but the graph G is not connected due to have
two components (eca and bdf ). Therefore the two graphs are non
isomorphic.
Isomorphism of Graphs
EXAMPLE: Determine whether the graph G and G’ given below
are isomorphic.

SOLUTION:
Clearly G has six vertices, G’ also has six vertices. And the graph G
has two simple circuits of length 3; one is abca and the other is
defd. But G’ does not have any simple circuit of length 3(as one
simple circuit in G’ is uxwvu of length 4).Therefore the two graphs
are non-isomorphic.
Note: A simple circuit is a circuit that does not have any other
repeated vertex except the first and last.
Isomorphism of Graphs
EXAMPLE: Determine whether the graph G and G’ given below
are isomorphic.

SOLUTION:
Both the graph G and G’ have 8 vertices and 12 edges and both are
also called regular graph(as each vertex has degree 3).The graph G
has two simple circuits of length 5; abcfea(i.e starts and ends at a)
and cdhgfc(i.e starts and ends at c). But G’ does not have any
simple circuit of length 5 (it has simple circuit tyxut,vwxuv of
length 4 etc). Therefore the two graphs are non-isomorphic.
Isomorphism of Graphs
EXAMPLE: Determine whether the given
graph G and H are isomorphic.
Solution:
Solution: The function f with
f(a) = u, f(b) = v, f(c) = y, f(d) = x, f(e) = w and
f(f) = z is a one-to-one correspondence
between G and H.
Note that adjacent vertices in G are a and b, b
and c, c and d, c and f, d and e, e and f and f
and a.
Each of the pairs f(a) = u and f(b) = v, f(b) = v
and f(c) = y , f(c) = y and f(d) = x , f(c) = y and
f(f) = z , f(d) = x and f(e) = w, f(e) = w and f(f)
= z and f(f) = z and f(a) = u consists of two
adjacent vertices in H.
Isomorphism of Graphs (cont.)
Example: Determine whether these two graphs are isomorphic.

Solution: Both graphs have eight vertices and ten edges.


They also both have four vertices of degree two and four of degree three.
However, G and H are not isomorphic. Note that since deg(a) = 2 in G, a must
correspond to t, u, x, or y in H, because these are the vertices of degree 2. But each of
these vertices is adjacent to another vertex of degree two in H, which is not true for a
in G.
Alternatively, note that the subgraphs of G and H
made up of vertices of degree three and the edges
connecting them must be isomorphic.
But the subgraphs, as shown at the right,
are not isomorphic.
Isomorphism of Graphs
Example: Determine whether these two graphs are isomorphic.

Solution: Both graphs have six vertices and seven edges.

They also both have four vertices of degree two and two of degree three.
The subgraphs of G and H consisting of all the vertices of degree two and the
edges connecting them are isomorphic. So, it is reasonable to try to find an
isomorphism f.

We define an injection f from the vertices of G to the vertices of H that


preserves the degree of vertices. We will determine whether it is an
isomorphism.

The function f with f(u1) = v6, f(u2) = v3, f(u3) = v4, and f(u4) = v5 , f(u5) = v1,
and f(u6) = v2 is a one-to-one correspondence between G and H. Showing
that this correspondence preserves edges is straightforward, so we will omit
the details here. Because f is an isomorphism, it follows that G and H are
isomorphic graphs.
Algorithms for Graph Isomorphism
 The best algorithms known for determining whether two
graphs are isomorphic have exponential worst-case time
complexity (in the number of vertices of the graphs).
 However, there are algorithms with linear average-case
time complexity.
 You can use a public domain program called NAUTY to
determine in less than a second whether two graphs with as
many as 100 vertices are isomoprhic.
 Graph isomorphism is a problem of special interest
because it is one of a few NP problems not known to be
either tractable or NP-complete (see Section 3.3).
Applications of Graph Isomorphism
 The question whether graphs are isomorphic plays an
important role in applications of graph theory. For example,
 chemists use molecular graphs to model chemical compounds.
Vertices represent atoms and edges represent chemical bonds.
When a new compound is synthesized, a database of molecular
graphs is checked to determine whether the graph representing
the new compound is isomorphic to the graph of a compound that
this already known.
 Electronic circuits are modeled as graphs in which the vertices
represent components and the edges represent connections
between them. Graph isomorphism is the basis for
 the verification that a particular layout of a circuit corresponds
to the design’s original schematics.
 determining whether a chip from one vendor includes the
intellectual property of another vendor.
Shortest Paths
Weighted Graphs
Graphs that have a number assigned to
each edge are called weighted graphs. BOS

CHI NY
SF DEN

ATL
LA

MIA
Weighted Graphs
MILES

BOS
2534

1855 722 NY
CHI
SF DEN 908
957

349
ATL
LA 595
MIA
Weighted Graphs
FARES

BOS
$129
$39
$99 $59 NY
CHI
SF DEN
$89

$39
ATL
LA

MIA
Weighted Graphs
FLIGHT
TIMES

BOS
4:05
0:50
2:55 NY
CHI 1:50
SF DEN 2:10
2:20

1:15
ATL
LA

MIA
Weighted Graphs
 A weighted graph is a graph in which each edge (u,
v) has a weight w(u, v). Each weight is a real number.
 Weights can represent distance, cost, time, capacity,
etc.
 The length of a path in a weighted graph is the sum
of the weights on the edges.
 Dijkstra’s Algorithm finds the shortest path between
two vertices.
Dijkstra’s Algorithm
 Dijkstra’s algorithm is used in problems relating to finding the shortest path.

 Each node is given a temporary label denoting the length of the shortest
path from the start node so far.

 This label is replaced if another shorter route is found.

 Once it is certain that no other shorter paths can be found, the temporary
label becomes a permanent label.

 Eventually all the nodes have permanent labels.

 At this point the shortest path is found by retracing the path backwards.
Dijkstra’s algorithm: example
D(v) D(w) D(x) D(y) D(z)
Step N'
0 u 7,u 3,u 5,u ∞ ∞
1 uw 6,w 5,u 11,w ∞
2 uwx 6,w 11,w 14,x
3 uwxv 10,v 14,x
4 uwxvy 12,y
5 uwxvyz x
9

notes: 5
4
7
 construct shortest path tree by
8
tracing predecessor nodes
ties can exist (can be broken 3 w z
 u y
arbitrarily) 2
3
7 4
v
Dijkstra’s algorithm: another example
Step N' D(v) D(w) D(x) D(y) D(z)
0 u 2,u 5,u 1,u ∞ ∞
1 ux 2,u 4,x 2,x ∞
2 uxy 2,u 3,y 4,y
3 uxyv 3,y 4,y
4 uxyvw 4,y
5 uxyvwz
5

v 3 w
2 5
u 2 1 z
3
1 2
x 1
y
Problem: shortest path from a to z

b 5 d 5 f
4 7
4 3 1
a 2 z
3 4
c 5 e 5 g
The Traveling Salesman Problem

The traveling salesman problem is one of the classical problems in


computer science.
A traveling salesman wants to visit a number of cities and then return to his
starting point. Of course he wants to save time and energy, so he wants to
determine the shortest cycle for his trip.
We can represent the cities and the distances between them by a weighted,
complete, undirected graph.
The problem then is to find the shortest cycle (of minimum total weight
that visits each vertex exactly one).
Finding the shortest cycle is different than Dijkstra’s shortest path.
It is much harder too, no polynomial time algorithm exists!
The Traveling Salesman Problem
 Importance:
 Variety of scheduling application can be solved as a
traveling salesmen problem.
 Examples:
 Ordering drill position on a drill press.
 School bus routing.
 The problem has theoretical importance because it
represents a class of difficult problems known as NP-hard
problems.
Travelling Salesman problem
Travelling Salesman problem
The House-and-Utilities Problem
Planar Graphs
 Consider the previous slide. Is it possible to
join the three houses to the three utilities in
such a way that none of the connections
cross?
Planar Graphs
 Phrased another way, this question is equivalent to: Given
the complete bipartite graph K3,3, can K3,3 be drawn in the
plane so that no two of its edges cross?

K3,3
Planar Graphs
 A graph is called planar if it can be drawn
in the plane without any edges crossing.
 A crossing of edges is the intersection of the
lines or arcs representing them at a point
other than their common endpoint.
 Such a drawing is called a planar
representation of the graph.
Example
A graph may be planar even if it is usually
drawn with crossings, since it may be
possible to draw it in another way without
crossings.
Example
A graph may be planar even if it represents a
3-dimensional object.
Planar Graphs
 We can prove that a particular graph is
planar by showing how it can be drawn
without any crossings.
 However, not all graphs are planar.
 It may be difficult to show that a graph is
nonplanar. We would have to show that
there is no way to draw the graph without
any edges crossing.
Regions
 Euler showed that all planar representations
of a graph split the plane into the same
number of regions, including an unbounded
region.

R4 R3 R2

R1
Regions
 In any planar representation of K3,3, vertex
v1 must be connected to both v4 and v5, and
v2 also must be connected to both v4 and v5.
v1 v2 v3

v4 v5 v6
Regions
 The four edges {v1, v4}, {v4, v2}, {v2, v5},
{v5, v1} form a closed curve that splits the
plane into two regions, R1 and R2.

v1 v5

R2 R1

v4 v2
Regions
 Next, we note that v3 must be in either R1 or R2.
 Assume v3 is in R2. Then the edges {v3, v4} and {v4, v5}
separate R2 into two subregions, R21 and R22.

v1 v5 v1 v5

R21

R2 R1 → v3

R22

v4 v2 v4 v2
Regions
 Now there is no way to place vertex v6
without forcing a crossing:
 If v6 is in R1 then {v6, v3} must cross an edge
 If v6 is in R21 then {v6, v2} must cross an edge
 If v6 is in R22 then {v6, v1} must cross an edge
v1 v5

R21

v3 R1

R22

v4 v2
Regions
 Alternatively, assume v3 is in R1. Then the
edges {v3, v4} and {v4, v5} separate R1 into
two subregions, R11 and R12.

v1 v5

R11

R2 R12 v3

v4 v2
Regions
 Now there is no way to place vertex v6 without forcing a
crossing:
 If v6 is in R2 then {v6, v3} must cross an edge
 If v6 is in R11 then {v6, v2} must cross an edge
 If v6 is in R12 then {v6, v1} must cross an edge

v1 v5

R11

R2 R12 v3

v4 v2
Planar Graphs
 Consequently, the graph K3,3 must be
nonplanar.

K3,3
Regions
 Euler devised a formula for expressing the
relationship between the number of vertices,
edges, and regions of a planar graph.
 These may help us determine if a graph can
be planar or not.
Euler’s Formula
 Let G be a connected planar simple graph
with e edges and v vertices. Let r be the
number of regions in a planar
representation of G. Then r = e - v + 2.

R4 R3 R2
# of edges, e = 6
R1 # of vertices, v = 4
# of regions, r = e - v + 2 = 4
Euler’s Formula (Cont.)
 Corollary 1: If G is a connected planar
simple graph with e edges and v vertices
where v  3, then e  3v - 6.
 Is K5 planar?

K5
Euler’s Formula (Cont.)
 K5 has 5 vertices and 10 edges.
 We see that v  3.
 So, if K5 is planar, it must be true that e  3v – 6.
 3v – 6 = 3*5 – 6 = 15 – 6 = 9.
 So e must be  9.
 But e = 10.
 So, K5 is nonplanar.

K5
Euler’s Formula (Cont.)
 Corollary 2: If G is a connected planar
simple graph, then G must have a vertex of
degree not exceeding 5.
If G has one or two vertices, it is true;
thus, we assume that G has at least three vertices.

If the degree of each vertex were at least 6, then by Handshaking Theorem,


2e ≥ 6v, i.e., e ≥ 3v,

but this contradicts the inequality from


Corollary 1: 1 e ≤ 3v – 6.
2e   deg( v )
vV
Euler’s Formula (Cont.)
 Corollary 3: If a connected planar simple
graph has e edges and v vertices with v  3
and no circuits of length 3, then e  2v - 4.
 Is K3,3 planar?
Euler’s Formula (Cont.)
 K3,3 has 6 vertices and 9 edges.
 Obviously, v  3 and there are no circuits of length 3.
 If K3,3 were planar, then e  2v – 4 would have to be true.
 2v – 4 = 2*6 – 4 = 8
 So e must be  8.
 But e = 9.
 So K3,3 is nonplanar.

K3,3
Chapter 11
Chapter Summary
 Introduction to Trees
 Applications of Trees
 Tree Traversal
 Spanning Trees
 Minimum Spanning
Section 11.1
Section Summary
 Introduction to Trees
 Rooted Trees
 Trees as Models
 Properties of Trees
Trees
Definition: A tree is a connected undirected graph with no
simple circuits.

Definition: An undirected graph is a tree if and only if there is a


unique simple path between any two of its vertices. A tree cannot
contain multiple edges or loops.

Definition: An undirected graph is a tree if and only if there is a


unique simple path between any two of its vertices.
Trees
Example: Which of these graphs are trees?

Solution: G1 and G2 are trees - both are connected and have


no simple circuits. G3 is not a tree because e, b, a, d, e is a
simple circuit,. G4 is not a tree because it is not connected.
FOREST
Definition: A forest is a graph that has no simple
circuit, but is not connected. Each of the connected
components in a forest is a tree.
Section 11.2
Trees as Models Arthur Cayley
(1821-1895)

 Trees are used as models in computer science, chemistry,


geology, botany, psychology, and many other areas.

 Trees were introduced by the mathematician Cayley in 1857


in his work counting the number of isomers of saturated
hydrocarbons. The two isomers of butane are: .
Trees as Models
 The organization of a computer file system into directories,
subdirectories, and files is naturally represented as a tree.
Trees as Models
 Trees are used to represent the structure of organizations.
Applications of Trees
 Game Trees
Trees can be used to analyze certain types of games
such as tic-tac-toe, nim, checkers, and chess.
Game Tree for Tic-Tac-Toe
Universal Address Systems
Prefix code
Definition: A code that has the property that
the code of a character is never a prefix of the
code of another character.

 A prefix code can be represented using a


binary tree, where the characters are the labels
of the leaves in the tree.
 The edges of the tree are labeled so that an
edge leading to a left child is assigned a 0 and
an edge leading to a right child is assigned a 1.
 The bit string used to encode a character is the
sequence of labels of the edges in the unique
path from the root to the leaf that has this
character as its label.
 For instance, the tree in Figure 5 represents
the encoding of e by 0, a by 10, t by 110, n by
1110, and s by 1111.
Decision Trees
Definition: A rooted tree where each vertex represents a
possible outcome of a decision and the leaves represent the
possible solutions of a problem.
 Rooted trees can be used to model problems in which a
series of decisions leads to a solution.
 The possible solutions of the problem correspond to the
paths to the leaves of this rooted tree.
Example : A decision tree that orders the elements of
the list a, b, c.
Rooted Trees
Definition: A rooted tree is a tree in which one vertex has been
designated as the root and every edge is directed away from the
root.
 An unrooted tree is converted into different rooted trees
when different vertices are chosen as the root.
Rooted Tree Terminology
 Terminology for rooted trees is a mix from botany and
genealogy (such as this family tree of the Bernoulli
family of mathematicians).
Rooted Tree Terminology
 If v is a vertex of a rooted tree other than the root, the parent
of v is the unique vertex u such that there is a directed edge
from u to v. When u is a parent of v, v is called a child of u.
Vertices with the same parent are called siblings.
Rooted Tree Terminology
 The ancestors of a vertex are the vertices in the path from
the root to this vertex, excluding the vertex itself and
including the root.
 The descendants of a vertex v are those vertices that have v as
an ancestor. The subtree rooted at u includes all the
descendants of u, and all edges that connect between them.
Rooted Tree Terminology
 A vertex of a rooted tree with no children is called a leaf.
Vertices that have children are called internal vertices.
Terminology for Rooted Trees

Example: In the rooted tree T


(with root a):
(i) Find the parent of c, the
children of g, the siblings
of h, the ancestors of e,
and the descendants of b.

Solution:
(i) The parent of c is b. The
children of g are h, i, and j.
The siblings of h are i and j.
The ancestors of e are c, b,
and a. The descendants of
b are c, d, and e.
Terminology for Rooted Trees
Example: In the rooted
tree T (with root a):
(i) Find all internal
vertices and all leaves.
Solution:
(i) The internal vertices
are a, b, c, g, h, and j.
The leaves are d, e, f, i,
k, l, and m.
Terminology for Rooted Trees
(i) What is the subtree
rooted at g?
Solution:
(i) We display the subtree
rooted at g.
Level of vertices and height of trees
 When working with trees, we often want to have rooted
trees where the sub trees at each vertex contain paths of
approximately the same length.
 To make this idea precise we need some definitions:
 The level of a vertex v in a rooted tree is the length of the
unique path from the root to this vertex.
 The height of a rooted tree is the maximum of the levels of
the vertices.
Level of vertices and height of trees
Example:
(i) Find the level of each vertex in
the tree to the right.
(ii) What is the height of the tree?
Solution:
(i) The root a is at level 0.
Vertices b, j, and k are at level 1.
Vertices c, e, f, and l are at level 2.
Vertices d, g, i, m, and n are at level 3.
Vertex h is at level 4.

(ii) The height is 4, since 4 is the largest level of any vertex.


m-ary Rooted Trees
Definition: A rooted tree is called an m-ary tree if every
internal vertex has no more than m children. The tree is
called a full m-ary tree if every internal vertex has exactly m
children. An m-ary tree with m = 2 is called a binary tree.

Example: Are the following rooted trees full m-ary trees


for some positive integer m?
Solution:
 T1 is a full binary tree because each of its internal
vertices has two children.
 T2 is a full 3-ary tree because each of its internal
vertices has three children.
 In T3 each internal vertex has five children, so T3 is a
full 5-ary tree.
 T4 is not a full m-ary tree for any m because some of
its internal vertices have two children and others
have three children.
Balanced m-Ary Trees
Definition: A rooted m-ary tree of height h is balanced if
all leaves are at levels h or h − 1.
Example: Which of the rooted trees shown below is
balanced?

Solution: T1 and T3 are balanced, but T2 is not because it


has leaves at levels 2, 3, and 4.
Ordered Rooted Trees
Definition: An ordered rooted tree is a rooted tree where the children
of each internal vertex are ordered.
 We draw ordered rooted trees so that the children of each internal
vertex are shown in order from left to right.
Binary Trees
Definition: A binary tree is an ordered rooted where each
internal vertex has at most two children. If an internal vertex
of a binary tree has two children, the first is called the left
child and the second the right child. The tree rooted at the left
child of a vertex is called the left subtree of this vertex, and
the tree rooted at the right child of a vertex is called the right
subtree of this vertex.
Example:
Consider the binary tree T.
(i) What are the left and right children of d?
(ii) What are the left and right sub trees of c?
Solution:
(i) The left child of d is f and the right child is g.
(ii) The left and right subtrees of c are displayed in (b) and (c).
Properties of Trees
 A tree with n vertices has n − 1 edges.
 A full m-ary tree with i internal vertices has n = mi + 1
vertices.
 A full m-ary tree with:
(i) n vertices has i = (n − 1)/m internal vertices and
l = [(m − 1)n + 1]/m leaves,
(ii) i internal vertices has n = mi + 1 vertices and
l = (m − 1)i + 1 leaves,
(iii) l leaves has n = (ml − 1)/(m − 1) vertices and
i = (l − 1)/ (m − 1) internal vertices.
 There are at most 𝑚ℎ leaves in an m-ary tree of height
h.
Binary Search Tree
Definition: A binary tree in which the vertices are labeled
with items so that a label of a vertex is greater than the labels
of all vertices in the left subtree of this vertex and is less than
the labels of all vertices in the right subtree of this vertex.
 Searching for items in a list is one of the most important
tasks that arises in computer science.
 Our primary goal is to implement a searching algorithm
that finds items efficiently when the items are totally
ordered. This can be accomplished through the use of a
binary search tree.
Example : Form a binary search tree for the words mathematics,
physics, geography, zoology, meteorology, geology, psychology,
and chemistry (using alphabetical order).
Section 11.3
Tree Traversal
 Procedures for systematically visiting every vertex of
an ordered tree are called traversals.
 The three most commonly used traversals are preorder
traversal, inorder traversal, and postorder traversal.
Preorder Traversal
Definition: Let T be an ordered rooted tree with root r. If
T consists only of r, then r is the preorder traversal of T.
Otherwise, suppose that T1, T2, …, Tn are the subtrees of r
from left to right in T. The preorder traversal begins by
visiting r, and continues by traversing T1 in preorder, then
T2 in preorder, and so on, until Tn is traversed in preorder.
Preorder Traversal (continued)

procedure preorder
(T: ordered rooted
tree)
r := root of T
list r
for each child c of r
from left to right
T(c) := subtree with
c as root
preorder(T(c))
Inorder Traversal
Definition: Let T be an ordered rooted tree with root r. If
T consists only of r, then r is the inorder traversal of T.
Otherwise, suppose that T1, T2, …, Tn are the subtrees of r
from left to right in T. The inorder traversal begins by
traversing T1 in inorder, then visiting r, and continues by
traversing T2 in inorder, and so on, until Tn is traversed in
inorder.
Inorder Traversal (continued)
procedure
inorder (T: ordered
rooted tree)
r := root of T
if r is a leaf then list r
else
l := first child of r
from left to right
T(l) := subtree with l
as its root
inorder(T(l))
list(r)
for each child c of r
from left to right
T(c) := subtree
with c as root
inorder(T(c))
Postorder Traversal
Definition: Let T be an ordered rooted tree with root r. If
T consists only of r, then r is the postorder traversal of T.
Otherwise, suppose that T1, T2, …, Tn are the subtrees of r
from left to right in T. The postorder traversal begins by
traversing T1 in postorder, then T2 in postorder, and so on,
after Tn is traversed in postorder, r is visited.
Post order Traversal (continued)

procedure
postordered (T:
ordered rooted
tree)
r := root of T
for each child c of
r from left to right
T(c) := subtree
with c as root
postorder(T(c))
list r
Expression Trees
 Complex expressions can be represented using ordered
rooted trees.
 Consider the expression ((x + y) ↑ 2 ) + ((x − 4)/3).
 A binary tree for the expression can be built from the
bottom up, as is illustrated here.
Infix Notation
 An inorder traversal of the tree representing an expression produces
the original expression when parentheses are included except for unary
operations, which now immediately follow their operands.
 We illustrate why parentheses are needed with an example that
displays three trees all yield the same infix representation.
Jan Łukasiewicz
(1878-1956)

Prefix Notation
 When we traverse the rooted tree representation of an
expression in preorder, we obtain the prefix form of the
expression. Expressions in prefix form are said to be in Polish
notation, named after the Polish logician Jan Łukasiewicz.
 Operators precede their operands in the prefix form of an
expression. Parentheses are not needed as the representation is
unambiguous.
 The prefix form of ((x + y) ↑ 2 ) + ((x − 4)/3)
is + ↑ + x y 2 / − x 4 3.
 Prefix expressions are evaluated by working from right to left.
When we encounter an operator, we perform the corresponding
operation with the two operations to the right.
Prefix Notation
 Example: We show the steps used to evaluate a
particular prefix expression:
Postfix Notation
 We obtain the postfix form of an expression by traversing its
binary trees in postorder. Expressions written in postfix
form are said to be in reverse Polish notation.
 Parentheses are not needed as the postfix form is
unambiguous.
 x y + 2 ↑ x 4 − 3 / + is the postfix form of ((x + y) ↑ 2 ) + ((x
− 4)/3).
 A binary operator follows its two operands. So, to evaluate
an expression one works from left to right, carrying out an
operation represented by an operator on its preceding
operands.
Postfix Notation
 Example: We show the steps used to evaluate a
particular postfix expression.
Section 11.4
Section Summary
 Spanning Trees
 Prim’s Algorithm
 Kruskal Algorithm
Spanning Trees
Definition: Let G be a simple graph. A spanning tree of G is a
subgraph of G that is a tree containing every vertex of G.

Example: Find the spanning tree of the simple graph:


Spanning Trees
Solution: The graph is connected, but is not a tree because
it contains simple circuits. Remove the edge {a, e}. Now one
simple circuit is gone, but the remaining subgraph still has
a simple circuit. Remove the edge {e, f} and then the edge
{c, g} to produce a simple graph with no simple circuits. It
is a spanning tree, because it contains every vertex of the
original graph.
Section 11.5
Minimum spanning tree
 A minimum spanning tree in a connected weighted
graph is a spanning tree that has the smallest possible
sum of weights of its edges.
 Example: A company plans to build a
communications network connecting its five
computer centers. Any pair of these centers can be
linked with a leased telephone line. Which links
should be made to ensure that there is a path
between any two computer centers so that the total
cost of the network is minimized?
Minimum spanning tree
 Solution: We can model this problem using the weighted graph shown in
Figure 1, where vertices represent computer centers, edges represent
possible leased lines, and the weights on edges are the monthly lease rates
of the lines represented by the edges. We can solve this problem by finding a
spanning tree so that the sum of the weights of the edges of the tree is
minimized. Such a spanning tree is called a minimum spanning tree.
PRIM’S ALGORITHM
Minimal spanning tree (MST)
Example: Use Prims algorithm
to find a minimal spanning tree
for the graph below. Indicate the
order in which edges are added
to form the tree.

Order of adding the edges:


{a , b}, {b , c}, {c , d}, {d , e}, {e , f}, {f , g}

MST COST = 23
KRUSKAL’S ALGORITHM
Minimal spanning tree (MST)
Example: Use Kruskal’s
algorithm to find a minimal
spanning tree for the graph
below. Indicate the order in
which edges are added to
form the tree.

MST COST = 23
Activity Time
Minimal spanning tree (MST)
Find the minimum spanning tree of the graph given
below using Prims and Kruskal's Algorithm.

You might also like