Chapter 3
Chapter 3
DISCRETE
C h a p t e
MATHEMATICS r 3
Chapter 3: GRAPHS AND
TREES
3.1 Derive concept of Graphs
3.1.1 Define graph
3.1.2 Identify the graph terminology
3.1.2a Simple graph
3.1.2b Multigraphs
3.1.2c Pseudograph
3.1.3 Explain the properties of graph.
3.1.4 Construct graph representations.
3.1.4a Simple graph
3.1.4b Directed graph
3.1.4c Weighted graph
3.1.4d Connected graph
Chapter 3: GRAPHS AND
TREES
3.1.5 Compare the different types of graphs:
3.1.5a Discrete graph
3.1.5b Complete graph
3.1.5c Linear graph
3.1.5d Bipartite graph
3.1.6 Explain paths, cycles and planarity in graphs
3.1.7 Ascertain the isomorphic graphs
3.1.8 Construct the Euler paths and Euler circuits
in graphs.
3.1.1 DEFINE GRAPH
• Problem can be modeled as a graph. Since
graphs are drawn with dots and lines, they
look like road maps.
• Computer network can be modeled
using a graph in which the vertices of
the graph represent the data centers
and the edges represent
communication links.
An undirected graph G consists of set V A directed graph G (also known as
of vertices and set E of edges such that digraph) is a graph in which edge of the
each edge is associated with an graph has a direction. Such edge is
unordered pair of vertices. known as directed edge.
IMPORTANT: CONCEPT OF GRAPHS
VERTICES/NODES EDGES
A set of 𝑽 = 𝑽 (𝑮) whose elements are A set of 𝑬 = 𝑬 (𝑮) whose elements
called vertices, points or nodes. are called edges.
Vertices, V Edges, E =
={v1, v2, v3, {e1, e2, e3,
v4, v5} e4, e5}
IMPORTANT: CONCEPT OF GRAPHS
INCIDENT ON A VERTEX INCIDENT ON AN EDGE
If edge e is associated with vertices v If edge e is associated with vertices v
and w, e is said to be incident on v and w, v and w are said to be
and w. incident on e.
DEGREE ON A VERTEX
()
1 1 1
1 0 1
1 1 1
EXAMPLE 1 EXAMPLE 2
IMPORTANT: CONCEPT OF GRAPHS
LOOP ISOLATED VERTEX
An edge incident on a single vertex A VERTEX THAT IS NOT INCIDENT ON
Example: (e is a loop) ANY EDGE EXAMPLE: (W: ISOLATED
VERTEX)
3.1.2 IDENTIFY THE GRAPH
TERMINOLOGY
1. A simple graph is an
• Unweighted
• Undirected graph
• Containing no loops
• No multiple edges/parallel edges
EXAMPLE
• Vertices, 𝑉 = A, B, C, D
• Edges, E: 𝐸 = {𝑒₁, 𝑒₂, 𝑒₃, 𝑒₄, 𝑒₅} = {{𝐴, 𝐵} , {𝐴, 𝐶} ,
{𝐵, 𝐶} , {𝐵, 𝐷} , {𝐶, 𝐷}}
• Vertices A and B are said to be adjacent if there is
an edge 𝑒 = 𝐴, 𝐵 . We say that 𝑒₁ is an incident of A
and B.
• Adjacent edges are edges that originate from one
same vertex. Example 𝑒₁ and 𝑒₂.
3.1.2 IDENTIFY THE GRAPH
TERMINOLOGY
2. A multigraph is a graph that may EXAMPLE
contain
• multiple edges/parallel edges
(definition: two or more edges
connecting the same two vertices)
a)
b)
Exercise A
2. Find the number of vertices, the number of
edges; identify all parallel edges, loops and
isolated vertices for the following graph.
a)
b)
3.1.3 PROPERTIES OF GRAPH
• The ordered pair (a, b) is a pair of objects, for
example graph G = G (V, E), V = {a, b, c, d}, the order
pair for the vertices are E = {(a, b), (a, d), (b,
d), (a, c), (c, d)}
• The size of a graph is the number of its
edges.
• Degree of a vertex written 𝒅𝒆𝒈 (𝒗) = the
number of edges which are incident on 𝑣. The
vertex 𝑣 is said to be even or odd (parity)
according as 𝑑𝑒𝑔 (𝑣) is even or odd.
• A vertex 𝒗 is isolated if it is does not belong
to any edge.
EXAMPLE
( )
0110
1001
1001
0110
with respect to the ordering of vertices a,b,c and d.
2. Use an adjacency matrix to represent the
graph shown below.
Exercise 3C
3. From the graph shown below,
a) List down in a table form the in degree and
the
out degree of each vertices
b) Determine the parity (even or odd) for each
vertex and
c) Determine the sum of degrees
3.1.3 PROPERTIES OF GRAPH
• A vertex with degree 1 is called a leaf vertex
and the incident edge is referred to as a
pendant edge.
• 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.
3.1.3 PROPERTIES OF GRAPH
• The length of the path is the number 𝒏 of
edges that it contains.
• The distance between two vertices is
described by the length of the shortest
path that joins them.
• A closed path/circuit is a path that starts
and ends at the same vertex.
• A cycle is a closed path with at least 3
edges and there are no other vertices or
edges repeated except the starting
vertex/node.
EXAMPLE
• The length of path 𝑃 = 1, the length of path 𝑄 = 4 and the length of path 𝑅 = 2
• The distance from A to B (𝒅 𝑨, 𝑩 ) is 1 (shortest path)
• Closed path: 𝑆 = (𝐴, 𝐵, 𝐹, 𝐴) ; 𝑇 = (𝐵, 𝐶, 𝐷, 𝐸, 𝐴, 𝐹, 𝐵)
• Cycle: Path S and T
3.1.4 GRAPH
REPRESENTATIONS
1) A simple graph is an
• Unweighted
• Undirected graph
• Containing no loops
• No multiple edges/parallel edges.
EXAMPLE
• Vertices, 𝑉 = A, B, C, D
• Edges, E: 𝐸 = {𝑒1, 𝑒2, 𝑒3, 𝑒4, 𝑒5} = {(𝐴, 𝐵) , (𝐴, 𝐶) , (𝐵, 𝐶) , (𝐵, 𝐷) , (𝐶, 𝐷)}
• Vertices A and B are said to be adjacent if there is an edge 𝑒 = 𝐴, 𝐵 . We say
that 𝑒1 is an incident of A and B.
• Adjacent edges are edges that originate from one same vertex. Example 𝑒1
and 𝑒2.
3.1.4 GRAPH
REPRESENTATIONS
2) Digraphs (directed graph)
• defined by 𝐷 = 𝑉, 𝐸 where 𝑉 is the set of
vertex and 𝐸 is the set of directed edges.
EXAMPLE
• Vertices, 𝑉 = A, B, C, D, E
{𝐴, 𝐵} ≠ {𝐵, 𝐴}
• Edges, E: 𝐸 = 𝑒1, 𝑒2, 𝑒3, 𝑒4, 𝑒5 = 𝐴, 𝐵 , 𝐴, 𝐶 , 𝐶, 𝐷 , 𝐵, 𝐸 , 𝐸, 𝐷
•
• For and edge 𝐴, 𝐶 vertex 𝑨 is called the head of edge and vertex 𝑪 is the tail of
edge.
• The head vertex is said to be adjacent to the tail but not vice versa. Example
edge {𝐴, 𝐵} , A is adjacent to B BUT vertex B is NOT ADJACENT to A.
3.1.4 GRAPH
REPRESENTATIONS
3) Weighted graph
• a number (weight) is assigned to each
edge.
• Weights are usually real numbers, such as
costs, lengths or capacities, etc. depending on
the problem at hand.
EXAMPLE
3.1.4 GRAPH
REPRESENTATIONS
4) Connected Graph
• An undirected graph is called connected if
there is a path between every pair of
distinct vertices of the graph.
⚬ * Path: path is a sequence of edges that
begins at a vertex of a graph and travels
from vertex to vertex along edges ofthe
graph.
EXAMPLE
3.1.5 DIFFERENT TYPES OF
GRAPHS
1) COMPLETE GRAPH
• All nodes are connected to every other node in
the graph
• The complete graph that has 𝑛 nodes is
written as 𝐾ₙ
• Example: A complete graph with 3 nodes is
written as 𝐾₃
• Formula to calculate the edge in complete
graph:
• The number of edges =
EXAMPLE
3.1.5 DIFFERENT TYPES OF
GRAPHS
2) BIPARTITE GRAPH
• A bipartite graph has vertex that can be divided
into two subsets M and N. Each edge connects a
vertex in M to a vertex in N.
• Represented by 𝐾ₘ.ₙ where 𝑚 is a number of
vertices in M and 𝑛 is the number of vertices in N.
(M ≤ N)
EXAMPLE
Example complete bipartite graph 𝐾₂,₄ has 2 × 4 = 8 edges
3.1.5 DIFFERENT TYPES OF
GRAPHS
3) DISCRETE GRAPH
• A discrete graph consists of only certain
discrete points. Or in other word, the value
of y is independent to x
• Example: If you graphed y = x2 for all integer
values of x, then you would only have certain
values: (0,0) (1,1) (2,4) (3,9) etc. (-1,1) (-2,4) (-
3,9) etc. You do NOT connect the dots in
this case. Therefore, it is called discrete
graph
EXAMPLE
3.1.5 DIFFERENT TYPES OF
GRAPHS
4) LINEAR GRAPH
• A continuous graph contains all points
within a given interval or perhaps the entire
number line. Or in other word, the value of y
is dependent to x
• Example: If you graphed y = x² for all values of
x, it would be a continuous graph from one
side of the graph to the other
EXAMPLE
Exercise 3D
1) Calculate the number of edges in complete
graph K₈.
• 𝑨, 𝑩 and 𝑪 are path because there exists an edge that connects every
• 𝑨 and 𝑩 also be called trails because each edge is only traversed once. (trails =
simple path)
• Path 𝑨 and 𝑩 are also be called simple path where the vertex are only
passed through once.
3.1.6 PATHS, CYCLES AND
PLANARITY IN GRAPH
CIRCUIT & CYCLE
• A closed path/circuit is a path that starts
and ends at the same vertex.
• A cycle is a closed path with at least 3
edges and there are no other vertices or
edges repeated except the starting
vertex/node.
EXAMPLE
(b)
(c)
SUMMARY OF GRAPH
TERMINOLOGY IN GRAPH THEORY
SUMMARY OF GRAPH
TERMINOLOGY IN GRAPH THEORY
3.1.7 ISOMORPHIC GRAPHS
DEFINITION:
• Two or more graphs that have the same
⚬ number of vertices
⚬ number of edges
⚬ degrees for each distinct vertices
⚬ the graphs have bijective function*
(* a function giving an exact pairing of the
elements of two sets, which it is one-to-one & onto
function)
EXAMPLE
4.
f(a)=1 f(b)=2 f(c)=3 f(d)=4 f(e)=5
Exercise E
Determine whether the graphs below are isomorphic
or not.
a)
b)
Exercise E
Determine whether the graphs below are isomorphic
or not.
c)
d)
3.1.8 EULER PATHS & EULER
Euler circuit in a graph G
CIRCUITS
Euler path in G
• An Euler circuit is a circuit that • An Euler path is a path that uses
uses every edge in a graph every edge in a graph with no
with no repeats. Being a circuit, it repeats. Being a path, it does not
must start and end at the same have to return to the starting
vertex. vertex.
• A connected multigraph has an • A connected multigraph has an
Euler circuit if and only if every Euler path but not an Euler
vertex have even degree. circuit if and only if it has exactly
two vertices of odd degree.
EXAMPLE
Solution:
a) Euler circuit – all the vertices have an even degree.
b) Neither Euler circuit nor Euler path.
c) Euler path – two of the vertices have an odd degree.
d) Euler path – two of the vertices have an odd degree.
Exercise F
1. Give the characteristic of Euler path and Euler
circuit.
2. Which of the following graphs G1, G2 and G3 has
Euler path or Euler circuit? If any, list down the
Euler path or Euler circuit.
Chapter 3: GRAPHS AND
TREES
3.1.9 Construct the Hamilton paths and Hamilton circuits in
graphs
3.1.10 Apply graphs theories in Travelling Salesman
Problem (TSP)
3.2 Follow concept of trees
3.2.1 Describes trees
3.2.2 Identify the properties of trees
3.2.3 Describe the terminology and characteristics of trees
3.2.4 Sketches the spanning trees
3.2.4 Construct the minimal spanning trees using
3.1.4a Prim’s Algorithm
3.1.4b Kruskal’s Algorithm
3.1.9 HAMILTON PATHS &
HAMILTON
Hamilton circuit in G
CIRCUITS
Hamilton Path in G
• A Hamilton circuit is a simple • A Hamilton path is a simple path
circuit in a graph G that passes in a graph G that passes through
through every vertex exactly every vertex exactly once.
once, and starts and finish at
the same vertex.
EXAMPLE
Solution:
a) Hamilton circuit (a, b, d, e, c, a) & Hamilton path (a, b,
c, d, e)
b) Hamilton path (a, b, c, d)
c) Hamilton path (a, c, d, b, e)
d) Hamilton circuit (a, b, e, d, a) & Hamilton path (b, a, e,
Exercise G
1. Which of the following graphs G1, G2 and G3 has
Hamilton path or Hamilton circuit? If any,
list down the Hamilton path or Hamilton circuit.
DIFFERENCES BETWEEN HAMILTON
PATH AND EULER PATH
Hamilton Path Euler Path
• Covers all the vertices of a graph • Covers all the edges of a graph
exactly once. It can contain some exactly once. It can contain some
edge multiple times vertex multiple times.
3.1.10 APPLY GRAPHS THEORIES IN
TRAVELLING SALESMAN PROBLEM
(TSP)
Traveling Salesman Problem (TSP) is to find the circuit with minimum total
weight in a weighted, complete, undirected graph that visits each vertex exactly
once and returns to its starting point. It is equivalent to Hamilton circuit concept
STEPS IN SOLVING TSP
EXAMPLE
Find the shortest route that the salesperson could use if he travels to each
city in the map below exactly once, starting and ending in Detroit.
Solution
The possible route:
• Minimum Weight for each of the spanning tree : Figure A=14; Figure B=11 and
Figure C=15.
• Therefore, Figure B has the minimum spanning tree because it has
the smallest sum of weight.
3.2.5 CONSTRUCT MINIMAL
Prim’s Algorithm
SPANNING TREES
• It has a starting point.
The steps for implementing Prim's algorithm are as
follows: Kruskal’s Algorithm
• Initialize the minimum spanning tree with a vertex • start with the
chosen at random. minimum weight of
• Find all the edges that connect the tree to an edge
new vertices, find the minimum and add it to
the tree
• Keep repeating step 2 until we get a minimum
spanning tree
PRIM’S ALGORITHM
PRIM’S ALGORITHM
SUMMARY OF PRIM’S ALGORITHM
(VERTEX C AS THE STARTING
POINT)
KRUSKAL’S ALGORITHM
KRUSKAL’S ALGORITHM
SUMMARY OF KRUSKAL’S
ALGORITHM
Exercise J
a) Based on the following figure,
Tips:
Arrange the data items which
in left and right of the root,
apply the alphabetical order.
EXAMPLE
Construct a BST for the given arithmetic expression : 𝐴 + 𝐵 ∗ 𝐶 − 𝐷/𝐸
Solution:
(𝐴 + 𝐵 ∗ 𝐶 − 𝐷 / 𝐸)
Label each word given according to the sequences.
1 2 3 4 5 6 7 8 9
The first number will become the root. You need to follow the
steps.
Tips:
Arrange the data items where the
internal vertices correspond to the
operators and the terminal vertices
correspond to the operands.
Exercise K
Which of the following binary trees are BSTs? If a
tree is not a BST, why?
a) b)
c) d)
Exercise L
1. Create a BST from the following set of data, with
the first number is the root
a) 14 4 17 19 15 7 9 3 16 10
b) 9 23 17 4 30 7 14 16 15 19
c) 19 27 16 15 14 30 39 17 9 3
Example:
Pre-order: F, B, A, D, C, E, G,
I, H
Example:
Pre-order: A, B, D, E, C, F, G
Pre-order: 1, 2, 3
IN-ORDER TRAVERSAL
Example:
In-order: A, B, C, D, E, F, G,
H, I
Example:
In-order: D, B, E, A, F, C, G
In-order: 1, 2, 3
POST-ORDER TRAVERSAL
Example:
Post-order: A,C,E,D,B,H,I,G,F
Example:
Post-order: D, E, B, F, G, C, A
Post-order: 1, 2, 3
EXAMPLE
Find the tree traversals (pre-order, in-order & post-order) of the following
tree:
Solution:
Pre-order : 54,27,13,1,44,37,89,71,64,92
In-order : 1,13,27,37,44,54,64,71,89,92
Post-order :1,13,37,44,27,64,71,92,89,54
Exercise M
Find the tree traversals (pre-order, in-order & post-
order) of the following tree:
a)
b)
Exercise M
c)
3.2.8 DISTINGUISH FULL
BINARY TREES AND
COMPLETE BINARY TREES
• A full binary tree is a tree in which every vertex
other than the leaves has two children.
• Parent = A
• Left child of vertex A = vertex B
• Right child of vertex A = vertex C
EXAMPLE 1 ( full binary tree)
EXAMPLE 2 (complete binary tree)
The leftmost side of the leaf node must always be filled first.
It isn’t necessary for the last leaf node to have a right sibling.
EXAMPLE 3 (neither complete nor
full)
Exercise I
Draw a rooted tree having the following properties:
a) Full binary tree, four internal vertices and five
terminal vertices (leaves)
b) Full binary tree, six internal vertices and seven
terminal vertices
3.2.9 TRAVELLING SALESMAN
PROBLEM (TSP) IN TREE THEORY
Apply tree theories in searching and Travelling Salesman Problem (TSP) solving.
Normally it will use Prim’s Algorithm.
EXAMPLE
From the map below, find the shortest route that the salesperson could use to
travel to each city exactly once starting at Manhattan.
Solution:
By using Prim’s Algorithm, we will
get: