Chapter 3
GRAPHS AND
TREES
Azlina Binti Morshidi/JMSK/PKS
Learning Outcomes
Students should be able to:
Derive concept of graphs.
Define graph
Identify the graph terminology
Explain the properties of graph
Construct graph representations
Compare the different types of graphs
Explain paths, cycles and planarity in graphs
Ascertain the isomorphic graphs
Construct the Euler paths and circuits in graphs.
Construct the Hamilton paths and circuits in graphs
Apply graphs theories in travelling Salesman problem(TSP)
Azlina Binti Morshidi/JMSK/PKS
Learning Outcomes
Students should be able to:
Follow concept of trees.
Describes trees
Identify the properties of trees
Describe the terminology and characteristics of trees
Sketch the spanning trees
Construct the minimal spanning trees using
Prim’s algorithm
Kruskal’s algorithm
Build a binary search tree
Organize tree traversals: Pre-order, In-order and Post-order
Distinguish the full binary and complete binary trees
Associate tree theories in searching and travelling salesman
problem solving
Azlina Binti Morshidi/JMSK/PKS
Graphs
Define Graph
A graph G = (V,E) consists of V, a nonempty set of vertices (or
nodes) and E, a set of edges.
Each edge has either one or two vertices associated with it, called
its endpoints.
An edge is said to connect its endpoints.
The set of vertices V of a graph may be infinite. A graph with an
infinite vertex set or an infinite number of edges is called an
infinite graph.
A graph with a finite vertex set and a finite edge set is called a
finite graph.
Azlina Binti Morshidi/JMSK/PKS
Graphs
Define Graph
Graphs can be
Undirected (edges don’t have direction)
Set of objects(called vertices or nodes) that are
connected together, where all the edges are
bidirectional. An undirected graph is sometimes called
an undirected network.
Directed( edges have direction)
Set of objects(called vertices or nodes) that are
connected together, where all the edges are directed
from one vertex to another. A directed graph is
sometimes called a digraph or a directed network.
Azlina Binti Morshidi/JMSK/PKS
Graphs
Graphs Terminology
Simple graph
A graph in which edge connects two different vertices and
where no two edges connect the same pair of vertices is called
a simple graph.
In a simple graph, each edge is associated to an unordered
pair of vertices and no other edge is associated to this same
edge.
When there is an edge of a simple graph associated to {u,v}
we can also say without possible confusion that {u,v} is an
edge of the graph.
Azlina Binti Morshidi/JMSK/PKS
Graphs
Graphs Terminology
Multigraph
Graph that may have multiple edges connecting the same
vertices are called multigraphs.
When there are m different edges associated to the same
unordered pair of vertices {u,v}, we also say that {u,v} is an
edge of multiplicity m.
Azlina Binti Morshidi/JMSK/PKS
Graphs
Graphs Terminology
Pseudograph
A graph that may include loops, and possibly multiple edges
connecting the same pair of vertices or a vertex to itself are
sometimes called pseudograph.
Azlina Binti Morshidi/JMSK/PKS
Graphs
Graphs Terminology
Type Edges Multiple Edges Loops Allowed?
Allowed
Simple graph Undirected No No
Multigraph Undirected Yes No
Pseudograph Undirected Yes Yes
Azlina Binti Morshidi/JMSK/PKS
Graphs
Properties of graphs : Basic Terminologies
Two vertices u and v in an undirected
graph G are called adjacent(or
neighbors) in g if u and v are endpoints
of an edge e of G. Such an edge e is
called incident with the vertices u and v
and e is said to connect u and v.
In graph theory, a loop (also called a
self-loop or a buckle) is an edge that
connects a vertex to itself. A simple
contains no loops.
The degree of a vertex in an undirected
graph is the number of edges incident
with it, except that a loop at a vertex
contributes twice to the degree of that
vertex. The degree of the vertex v is
denoted by deg(v).
Azlina Binti Morshidi/JMSK/PKS
Graphs
Properties of graphs : Basic Terminologies
Once you have degree of the vertex
you can decide if the vertex or node is
even or odd.
If the degree of a vertex is even the
vertex is called an even vertex. On the
other hand, if the degree of the vertex is
odd, the vertex is called an odd vertex.
Vertex Degree Even/Odd
A 4 Even
B 6 Even
C 1 Odd
D 5 Odd
e 6 even
Azlina Binti Morshidi/JMSK/PKS
Graphs f
Properties of graphs : Basic Terminologies
Multiple edges (also called parallel
edges or a multi-edge), two or more
edges that are incident to the same two
vertices.
Isolated vertex – vertex on which no
edges are incident is called isolated.
A vertex is pendant if and only if it has
degree one. Consequently, pendant
vertex is adjacent to exactly one other
vertex. (example Vertex c)
Azlina Binti Morshidi/JMSK/PKS
Graphs
Properties of graphs : Basic Terminologies
Deg(a) = 2 Deg(a) = 4
Deg(b) =4 Deg(b) = 6
Deg(c) = 4 Deg(c) = 1
Deg(d) =1 Deg(d) = 5
Deg(e) = 3 Deg(e) = 6
Deg(f) = 4
Deg(g) = 0 Azlina Binti Morshidi/JMSK/PKS
Graphs
Properties of graphs : Basic Terminologies
In a graph with directed edges the in-degree of a vertex v,
denoted by − , is the number of edges with v as their
terminal vertex.
The out-degree of v, denoted by +( ) is the number of edges
with v as their initial vertex.
Find the in-degree and out-degree of the graph:
Vertex −( ) +( )
a42
b12
C23
D2 2
E33
f 00
Azlina Binti Morshidi/JMSK/PKS
Graphs
Graphs Representations : Simple Graph
Undirected graph that has no loops and no multiple edges.
Azlina Binti Morshidi/JMSK/PKS
Graphs
Graphs Representations : Directed Graph
A directed graph consists of a set of vertices V and a set of edges
E that are ordered pairs of V.
Loops are allowed
Multiple edges are allowed
The edge (v,w) is also denoted by → and v is called the
source of the edge while w is called the target of the edge.
Azlina Binti Morshidi/JMSK/PKS
Graphs
Graphs Representations : Weighted Graph
A weighted graph associates a label(weight) with every edge in
the graph.
Weight are usually real numbers, and often represent a “cost”
associated with the edge, either in terms of the entity that is being
modeled or an optimization problem that is being solved.
Azlina Binti Morshidi/JMSK/PKS
Graphs
Graphs Representations : Connected Graph
A graph is connected if for any two vertices x and y, there is a path
whose endpoints are x and y.
Azlina Binti Morshidi/JMSK/PKS
Graphs
Compare the different types of graph: Discrete Graph
For each integer ≥ 1, we let denote the graph with n vertices
and no edges.
For example the following show 2 5. We call the
discrete graph on a vertices.
2 5
Azlina Binti Morshidi/JMSK/PKS
Graphs
Types of graph: Complete Graph
A complete graph on n vertices, denoted by is a simple graph
that contains exactly one edge between each pair of distinct
vertices.
The graph 6 for n = 1,2,3,4,5,6 are displayed below.
A simple graph for which there is at least one pair of distinct vertex
not connected by an edge is called non-complete.
Azlina Binti Morshidi/JMSK/PKS
Graphs
Types of graph: Linear Graph
Given a graph G, its line graph L(G) is a graph such that each
vertex of L(G) represents an edge of G and two vertices of L(G)
are adjacent if and only if their corresponding edges share a
common endpoint (“are incident”) in G.
2 5
Azlina Binti Morshidi/JMSK/PKS
Graphs
Types of graph: Bipartite Graph
A simple graph G is called bipartite if its vertex set V can be
partitioned into two disjoint set 1 2 such that every edge in
the graph connects a vertex in 1and a vertex in 2 (so that no
edge in G connects either two vertices in 1 or two vertices in 2).
Azlina Binti Morshidi/JMSK/PKS
Graphs
Types of graph: Bipartite Graph
Are the graph G and H bipartite?
Azlina Binti Morshidi/JMSK/PKS
Graphs
Types of graph: Bipartite Graph
A complete bipartite graph . is a graph that has its vertex set
partitioned into two subsets of m and n vertices, respectively with
an edge between two vertices if and only if one vertex is in the
first subset and the other vertex is in the second subset.
Azlina Binti Morshidi/JMSK/PKS
Graphs
Explain Path and cycle(circuit) in graph
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 edges.
If a path begins and ends at the same vertex, the path is also
called a circuit.
Azlina Binti Morshidi/JMSK/PKS
Graphs
Planarity in graphs
A graph is planar if it can be drawn in the plane(2D surface)
without its edges crossing(without edge intersections)
It can be drawn on the plane in such a way that its edges intersect
only at their endpoints.
Informally, a graph is planar if the nodes of the graph can be
arranged (without breaking or adding edges) so that no edges of
the graph cross.
A BA B
CD C D
A(A,B),(A,C),(A,D)
B(B,A),(B,C),(B,D)
C(C,A),(C,B),(C,D)
D(D,A),(D,C),(C,B)
Azlina Binti Morshidi/JMSK/PKS
Graphs C
Planarity in graphs
B
A
DE 3,3
5 Planar or
not?
B
AC
DE
Azlina Binti Morshidi/JMSK/PKS
Graphs A(A,B),(A,C),(A,D),(A,E)
B(B,A),(B,C),(B,D),(B,E)
Planarity in graphs C(C,A),(C,B),(C,D),(C,E)
D(D,A)(D,B),(D,C),(D,E)
B E(E,A),(E,B),(E,C),(E,D)
A
DE
5
B Planar or
A not?
C
E
D
Azlina Binti Morshidi/JMSK/PKS
Graphs
Planarity in graphs A(A,B),(A,C),(A,D),(A,E)
B(B,A),(B,D),(B,E)
B C(C,A),(C,D),(C,E)
D(D,A)(D,B),(D,C),(D,E)
AC E(E,A),(E,B),(E,C),(E,D)
DE B
E
A Planar or
not?
CD
Azlina Binti Morshidi/JMSK/PKS
Graphs
Planarity in graphs
A BC
DE F
Planar or
not?
Azlina Binti Morshidi/JMSK/PKS
Graphs
Isomorphic Graphs
An isomorphic graphs is a graph that can exist in different forms
having the same number of vertices, edges and also the same
edge connectively.
Two graphs G1 and G2 are isomorphic if there exists a matching
between their vertices so that two vertices are connected by an
edge in G1 if and only if corresponding vertices are connected by
and edge in G2.
A property preserved by isomorphic graphs are:
Must have the same number of vertices
Must have the same number of edges.
The degrees of the vertices must be the same
Both graph is a bijective mapping.
Azlina Binti Morshidi/JMSK/PKS
Graphs
Isomorphic Graphs
Azlina Binti Morshidi/JMSK/PKS
Graphs
Isomorphic Graphs
Isomorphic
Properties Graph G1 Graph G2
No. of veGrtices 5 5
No. of edges H 5
No. of degree
5 deg(p) = 2
Vertices in G1 correspond deg(q) = 2
with the vertices in G2 deg(a) = 2 deg(r) = 2
(Bijective mapping) deg(b) = 2 deg(s) = 2
deg(c) = 2 deg(t) = 2
deg(d) = 2
deg(e) = 2
a=p
b=q
c=r
d=s
e=t
Azlina Binti Morshidi/JMSK/PKS
Graphs Properties Graph G1 Graph G2
No. of vertices
Isomorphic Graphs No. of edges 55
No. of degree
Not Isomorphic 66
Vertices in G is
not corresponding deg(a) = 2 deg(a) = 4
with the vertices deg(b) = 2 deg(b) = 2
in H deg(c) = 3 deg(c) = 3
(No bijective map deg(d) = 2 deg(d) = 2
ping) deg(e) = 3 deg(e) = 1
However, G and H are not isomorphic
because there are no vertices in H corr
espond with the vertices in G.
Azlina Binti Morshidi/JMSK/PKS
Graphs Not Isomorphic
Isomorphic Graphs
Properties Graph G1 Graph G2
No. of vertices
No. of edges 8 8
No. of degree
10 10
Deg(a)= 2 deg(a) = 4
Deg(b) = 2 deg(b) = 2
Deg(c) = 3 deg(c) = 3
Deg(d) = 2 deg(d) = 2
Deg(e) = 3 deg(e) = 1
Deg(f) = 3
Deg(g) = 2
Deg(h) = 3
Vertices in G is However, G and H are not isomorphic beca
not corresponding use there are no vertices in H correspond
with the vertices with the vertices in G.
in H Azlina Binti Morshidi/JMSK/PKS
Graphs Not Isomorphic
Isomorphic Graphs
Azlina Binti Morshidi/JMSK/PKS
Graphs
Euler Path and Euler Circuit in graphs
Euler Path : A continuous path that passes through every edge
once and only once. If a graph G has an Euler Path, then it must
have exactly two odd vertices.
Euler Circuit : When a Euler path begins and ends at the same
vertex. If the graph has an Euler Circuit, then each vertex of the
graph has even degree.
Number of Odd Euler Path Euler Circuit
Vertices
No Yes
0 (all even) Yes No
2 (others even)
Azlina Binti Morshidi/JMSK/PKS
Graphs
Euler Path and Euler Circuit in graphs
Determine whether the given graph has an Euler Circuit.
Construct such a circuit when on exists. If no Euler circuit
exists, determine whether the graph has an Euler path and
construct such a path if one exists.
Let’s Try!
Remember to
check the
degree..
Azlina Binti Morshidi/JMSK/PKS
Graphs
Euler Path and Euler Circuit in graphs
Let’s Try!
Remember to
check the
degree..
Azlina Binti Morshidi/JMSK/PKS
Graphs Let’s Try!
Remember to
Euler Path and Euler Circuit in graphs
check the
3 odd vertices degree..
X Euler Path
X Euler Circuit
All even vertices
Euler Circuit
Construct the circuit….
A->B->C->F->I->H->
F->E->B->D->E->
H->G->D->A
Azlina Binti Morshidi/JMSK/PKS
Graphs
Euler Path and Euler Circuit in graphs
2 odd vertices
Euler Path
Construct the circuit….
A->E->B->A->C->E->
D->B->E->C->D
Let’s Try!
Remember to
check the
degree..
Azlina Binti Morshidi/JMSK/PKS
Graphs
Euler Path and Euler Circuit in graphs
Let’s Try!
Remember to
check the
degree..
Azlina Binti Morshidi/JMSK/PKS
Graphs
Euler Path and Euler Circuit in graphs
Let’s Try!
Remember to
check the
degree..
Azlina Binti Morshidi/JMSK/PKS
Graphs
Euler Path and Euler Circuit in graphs
Let’s Try!
Remember to
check the
degree..
Azlina Binti Morshidi/JMSK/PKS
Graphs
Hamilton Path and Hamilton Circuit in graphs
Hamilton Path in a graph is a path that includes each vertex of
the graph once and only once.
Hamilton Circuit is a circuit that includes each vertex of the
graph once and only once (At the end the circuit must return to
the starting vertex)
Azlina Binti Morshidi/JMSK/PKS
Graphs Find the
Hamilton
Hamilton Path and Hamilton Circuit in graphs path or
circuit..
has a Hamilton circuit :a, b, c, d, e, a
There is no Hamilton circuit in but have a Hamilton
path a, b, c, d
has neither a Hamilton circuit nor a Hamilton path
because any path containing all vertices must contain
one of the edges {a, b}, {e, f} and {c, d} more than once.
Azlina Binti Morshidi/JMSK/PKS
Graphs
Hamilton Path and Hamilton Circuit in graphs
Determine whether the given graph has a Hamilton circuit. If it
does, find such a circuit. If it does not, give an argument to show
why no such circuit exists.
A. E.
Let’s Try!
F.
B. C.
D.
Azlina Binti Morshidi/JMSK/PKS
Graphs
Hamilton Path and Hamilton Circuit in graphs
Let’s Try!
Azlina Binti Morshidi/JMSK/PKS
Graphs
Hamilton Path and Hamilton Circuit in graphs
Let’s Try!
Azlina Binti Morshidi/JMSK/PKS
Graphs
Hamilton Path and Hamilton Circuit in graphs
Let’s Try!
Azlina Binti Morshidi/JMSK/PKS