0% found this document useful (0 votes)
20 views129 pages

Chapter 3

Uploaded by

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

Chapter 3

Uploaded by

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

DBM20083-

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

Degree of vertex v is 3, because there are 3 edges incident on v


IMPORTANT: CONCEPT OF GRAPHS
ADJACENT VERTICES ADJACENT EDGES
If edge e is associated with vertices v Adjacent edges are edges that originate
and w, v and w are said to be adjacent from one same vertex. If edge e₁ and e₂
(adjoining). is associated with vertices v₁ , e₁ and
e₂ are said to be adjacent
(adjoining)
IMPORTANT: CONCEPT OF GRAPHS
ADJACENCY MATRIX ADJACENT EDGES
A matrix that representing a graph with • Edges that are incident on the
rows and columns labeled by vertices. same pair of vertices.
• Adjacent vertices is labeled 1, 0 if there
no adjacent vertices. Example:
Example: Given coordinates A, B and C (e₁ and e₂ are parallel edges)

()
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)

• IT CONTAINS MULTIPLE EDGES


WHICH CONNECT THE SAME
TWO VERTICES B AND C.
3.1.2 IDENTIFY THE GRAPH
TERMINOLOGY
3. A pseudograph is a non-simple
graph in which
• multiple edges/parallel edges
(definition: two or more edges
connecting the same two vertices)
• loops are permitted.
Exercise A
1. Find the number of vertices, the number of
edges; identify all parallel edges, loops and
isolated vertices for the following graph.

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

𝑑𝑒𝑔 (𝐴) = 2 → A is even vertex


𝑑𝑒𝑔 (𝐵) = 2 → B is even vertex
𝑑𝑒𝑔 (𝐶) = 3 → C is odd vertex
𝑑𝑒𝑔 (𝐷) = 2 → D is even vertex (loop must be counted twice)
𝑑𝑒𝑔 (𝐸) = 3 → D is even vertex

*Sum of degrees = 𝟔 × 𝟐 = 𝟏𝟐 (𝟔 EDGES)


*Isolated vertex = F
Exercise 3B
1. Find the degree of each vertex for the following
graph.

2. Draw a graph having the given properties or


explain why no such graph exists.
a) Six vertices each of degree 3
b) Five vertices each of degree 2
Exercise 3C
1. Draw a graph with the adjacency matrix

( )
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

𝑃 = (𝑒₁) ; 𝑄 = (𝑒₂, 𝑒₃, 𝑒₄, 𝑒₅) ; 𝑅 = (𝑒₇, 𝑒₆)


• From vertex A to B there exists three different paths:

• 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₈.

2) How many vertices and edges are in K₁₀₀?

3) Draw Complete bipartite graph K₃,₃.


3.1.6 PATHS, CYCLES AND
PLANARITY IN GRAPH
PATHS
• 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.
EXAMPLE

• 𝑨, 𝑩 and 𝑪 are path because there exists an edge that connects every

• 𝑫 is not a path because there is no edge that connects 𝑉₅ and 𝑉₃ directly.


consecutives vertex.

• 𝑨 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

𝑃 = (𝑒₁) ; 𝑄 = (𝑒₂, 𝑒₃, 𝑒₄, 𝑒₅) ; 𝑅 = (𝑒₇, 𝑒₆)


• From vertex A to B there exists three different paths:

• Closed path: 𝑆 = (𝐴, 𝐵, 𝐹, 𝐴) ; 𝑇 = (𝐵, 𝐶, 𝐷, 𝐸, 𝐴, 𝐹,


𝐵)
• Cycle: Path S and T
EXAMPLE

Let G be a graph in the figure above. Find :


a) All simple paths from vertex A to vertex Z.
b) d(A, Z)
c) All closed path (circuit) starting from vertex A.
d) k-cycle for k = 3, k = 4, k = 5 and k = 6
Solution
a) (A, B, Z), (A, B, Y, Z), (A, X, Y, Z), (A, X, Y, B, Z)
b) d(A, Z) = 2
c) (A, B, Y, X, A), (A, X, Y, B, A), (A, B, Z, Y, X, A), (A, X, Y, Z, B, A)
d)
⚬ 3-cycle = (B, Y, Z, B), (B, Z, Y, B)
⚬ 4-cycle = (A, B, Y, X, A), (A, X, Y, B, A)
⚬ 5-cycle = (A, B, Z, Y, X, A), (A, X, Y, Z, B, A)
⚬ 6-cycle = no 6-cycle
EXAMPLE

Let G be a graph in the figure below. Determine whether each


of the following is a closed path(cycle), trail or simple path?
a) (B, A, X, C, B) e)(X, C, A, B, Y)
b) (X, A, B, Y) f) (X, B, A, X, C)
c) (B, X, Y, B) g) (X, B, A, X, B)
d) (B, A, X, C, B, Y) h) (X, C, B, A)
Solution
a) Closed path (cycle) & trail.
b) Simple path & trail.
c) Not a path because there is no edge between X and Y.
d) Trail.
e) Not a path because there is no edge between C and A.
f) Trail.
g) This path is not a simple path, trail or cycle.
h) Simple path & trail.
EXAMPLE

Distance from A to F : d(A, F) = 2 The path: (A, B, F)


Size of the graph: 9
Path from B to C: (B, A, C) @ (B, E, C) @ (B, F, D, C)
Trail from A to D: (A, C, D) @ (A, B, D) @ (A, B, F, D)
Simple cycle start from A: (A, B, D, C, A) @ (A, B, E, F, D, C,
A)
Exercise 3E
4. From the graph,

a) Determine the parity (even or odd) for each


vertex
b) Identify the leaf vertex
c) Identify the pendant edge
d) Identify the distance, D from a to g
e) Identify the size of the graph
f) Find the 2 simple path, P₁, P₂ from a to c
g) Find a trail, T from d to e
h) Find 3 simple cycle, labelled as C₁, C₂ and C₃
3.1.6 PATHS, CYCLES AND
PLANARITY IN GRAPH
PLANAR GRAPH
• A graph is called planar if it can be drawn in
the plane without any edges crossing
each other.
EXAMPLE
Example:

Graph A is planar since no link is overlapping with another.


Graph B is non-planar since many links are overlapping. Also, the
links of graph B cannot be reconfigured in a manner that would
make it planar.
Exercise 3E
Is this a planar graph or not?
(a)

(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

For each of the graph,


G = {(e₁, e₂), (e₂, e₃),
(e₃, e₄), (e₄, e₅)} the set
of edges are E = { e₁,
e₂, e₃, e₄, e₅}.
Therefore, Figure A, B and C
are isomorphic graphs.
EXAMPLE

The algorithm to determine isomorphism of 2 or more graphs…


Solution
Step Graph G Graph H
No. of
1. 5 5
vertices
2. No. of edges 6 6
Deg(a) 3 Deg(1) 3
Deg(b) 3 Deg(2) 3
Degree of
3. Deg(c) 2 Deg(3) 2
vertex
Deg(d) 2 Deg(4) 2
Deg(e) 2 Deg(5) 2
Illustrate the graph G & H to ensure it have bijective
function!

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

In the graph shown above, there are several Euler


paths. One such path is (C,A,B,D,C,B). The path
is shown in arrows to the right, with the order of
edges numbered.
EXAMPLE

The graph above has several possible Euler circuits.


Here’s a couple, starting and ending at vertex A:
(A,D,E,A,C,E,F,C,B,A) and (A,E,C,A,B,C,F,E,D,A). The
second is shown in arrows.
EXAMPLE
Determine whether each of the graph has an Euler circuit
or Euler path.

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

We can see that once we travel to vertex E there is


no way to leave without returning to C, so
there is no possibility of a Hamiltonian
circuit. If we start at vertex E we can find
several Hamiltonian paths, such as
(E,C,D,A,B) and (E,C,A,B,D)
EXAMPLE

• One Hamiltonian circuit is shown on the graph above.


• There are several other Hamiltonian circuits possible on this graph. Notice that
the circuit only has to visit every vertex once; it does not need to use every
edge.
• This circuit could be notated by the sequence of vertices visited,
starting and ending at the same vertex: (A,B,F,G,C,D,H,M,L,K,J,E,A).
Notice that the same circuit could be written in reverse order, or
starting and ending at a different vertex.
EXAMPLE
Determine whether each of the graph has a Hamilton path
or Hamilton circuit or both.

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:

By referring to the table in the


slide before,
The shortest route is:
Detroit – Toledo –
Kalamazoo – Grand Rapids
– Saginaw – Detroit
The total distance is 458
3.2.1 DESCRIBES TREES
• A tree is another data structure that you can
use to store pieces of information, or rather,
a bunch of elements.
• A tree is a
⚬ connected
⚬ undirected graph with no simple
circuit
⚬ cannot contain multiple edges or
loops
• Therefore a tree must be a simple graph.
A rooted tree is a tree in which one
• The root is a.
vertex has been designated as the root
• The parent of h,i and j is g.
and every edge is directed away from
• The children of b is c. The
the root.
children of j are l and m.
• h, i and j are a siblings.
• The ancestor of e are c, b and a.
• The descendants of b are c, d and
e.
• The internal vertices are a, b, c,
g, h and j. (vertices that have
children)
• The leaves are d, e, f, i, k, l and
m.(vertices that have no children)
• The level of a vertex v in rooted tree is the
If a is a vertex in a tree, the subtree
length of the unique path from the root to
with a as its root is the subgraph of
this vertex.
the tree consisting of a and its
• The level of the root is defined to be zero.
descendants and
• The height of a rooted tree is the maximum
all edges incident to these
of the levels of vertices.
descendants.
• 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.
3.2.2 THE PROPERTIES OF
• A tree with 𝑛 vertices has 𝒏 − 𝟏 edges. (no. of
TREES
edges = n – 1; n is no. of vertices)
• A full m-ary tree with 𝑖 internal vertices
contains 𝒏 = 𝒎𝒊 + 𝟏 vertices
• A full m-ary tree with:
⚬ 𝑛 vertices has 𝑖 = internal vertices
and 𝑙 = leaves.
⚬ 𝑖 internal vertices has 𝑛 = 𝑚𝑖 + 1 vertices
and 𝑙 = (𝑚 − 1)𝑖 + 1 leaves.
⚬ 𝑙 leaves has 𝑛 = vertices and 𝑖 =
internal vertices.
Question:
Example
a) Which vertex is the root?
b) Which vertices are internal?
c) Which vertices are leaves?
d) Which vertices are children of k?
e) Which vertex is the parent of h?
f) Which vertices are siblings of o?
g) Which vertices are ancestors of m?
h) Which vertices are descendants of b?
i) Is the rooted tree above a full m-ary tree for some
positive integer m?
j) What is the level of each vertex of the rooted tree
above?
k) Draw a subtree that is rooted at d.
Solution: j)
a) A Level 0 = a
Level 1 = b, c, d
b) a, b, c, d, f, h, k, q, t
Level 2 = e, f, g, h, i, j, k
c) e, g, i, j, l, m, n, o, p, r, s, u
Level 3 = l, m, n, o, p, q, r
d) q and r Level 4 = s, t
e) C Level 5 = u
f) P
k)
g) f, b, a
h) e, f, l, m, n
i) No
Example: Solution:
a) How many edges does a tree with a) 10000 – 1 = 9999 edges
10,000 vertices have? b) n = mi + 1 = 5 100 + 1 = 501 vertices
c) n = mi + 1 = 2 1000 + 1 = 2001
b) How many vertices does a full 5-ary vertices
tree with 100 internal vertices have? d) l = = = 67 leaves

c) How many edges does a full binary


tree with 1000 internal vertices have?

d) How many leaves does a full 3-ary


tree with 100 vertices have?
Exercise H
a) How many edges does a full 3-ary tree with 500
internal vertices have?

b) How many leaves does a full binary tree with 1001


vertices have?
3.2.3 DESCRIBE THE
TERMINOLOGY AND
CHARACTERISTICS OF TREES
• Binary trees are among the most
important type of rooted trees.

• A rooted binary tree is a rooted tree in


which every vertex has at most two
children.

• Every vertex in a binary tree has at


most two children.

• Each child is either a left child or a right


3.2.4 SPANNING TREES
• A spanning tree of G is a subgraph of G that is a tree containing every
vertex of G.
• A simple graph G with a spanning tree must be connected because there is a
path in the spanning tree between any two vertices.
• Consider the graph below:
3.2.4 SKETCHES MINIMAL
SPANNING TREES
• A minimum spanning tree in a connected weighted graph is a
spanning tree that has the smallest possible sum of weights of its
edges.
• Consider the graph below: (WITH 3 DIFFERENT SPANNING TREE)

• 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,

i. Draw 2 possible spanning trees


ii. Find a minimum spanning tree by using
Kruskal’s and Prim’s algorithms (compare the
findings)
iii. Draw the minimum spanning tree.
Exercise J
b) By referring to the following weighted graph,
find
Exercise J
c) Based on the following graph:

i. Draw the minimum spanning tree by using


Prim’s algorithm.
ii. Calculate the shortest path.
Chapter 3: GRAPHS AND
TREES
3.2.6 Build a binary search tree
3.2.7 Organize tree traversals:
3.1.5a Pre-order traversal
3.1.5b In-order traversal
3.1.5c Post-order traversal
3.2.8 Distinguish the full binary trees and
complete binary trees
3.2.9 Associate tree theories in searching and
Travelling Salesman Problem (TSP) solving.
3.2.6 BUILD A BINARY
SEARCH TREE
A Binary Search Tree (BST) is a tree in which
all the nodes follow the below-mentioned
properties:
• The value of the key of the left sub-tree is
less than the value of its parent (root)
node's key.
• The value of the key of the right sub-tree is
greater than or equal to the value of its
parent (root) node's key.
EXAMPLE
EXAMPLE
Construct a BST from set of data 63 89 72 41 56 95 34.
Solution:
Label each number given according to the sequences.
63 89 72 41 56 95 34
1 2 3 4 5 6 7
The first number will become the root. You need to follow the steps.
EXAMPLE
Construct a BST from the words “LINA MUST BUY FRUITS AND VITAMINS
MONTHLY”.
Solution:
Label each word given according to the sequences.
LINA MUST BUY FRUITS AND VITAMINS MONTHLY
1 2 3 4 5 6 7
The first number will become the root. You need to follow the steps.

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

2. Create a BST from the following sequence of words


(sentence), with the first word is the root.
a) MOST STUDENTS IN THIS CLASS HAS STUDIED
BASIC LOGIC
b) EVERYTHING HAPPENS FOR A REASON, EVEN
WHEN WE ARE NOT WISE TO SEE IT
Exercise L
3. Represent the following arithmetic expression as
binary tree:
a) (A + B) ∗ (C − D)
b) ((A + B ∗ C + D) ∗ E) − ( (A + B) ∗ (C − D))
3.2.7 ORGANIZE TREE
TRAVERSALS
• Tree traversals is a process of visiting
(examining and/or updating) each node in a
trees, exactly once, in a systematic way.
• Method of searching Binary Tree:
⚬ 1) Pre-order traversal
⚬ 2) In-order traversal
⚬ 3) Post-order traversal
PRE-ORDER TRAVERSAL

To traverse a non-empty binary tree in pre-order, perform the following operations


recursively at each node, starting with the root node:
• Visit the root.
• Traverse the left subtree.
• Traverse the right subtree.
Example:

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

To traverse a non-empty binary tree in pre-order, perform the following operations


recursively at each node, starting with the root node:
• Traverse the left subtree.
• Visit the root.
• Traverse the right subtree.
Example:

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

To traverse a non-empty binary tree in pre-order, perform the following operations


recursively at each node, starting with the root node:
• Traverse the left subtree.
• Traverse the right subtree.
• Visit the root.
Example:

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.

• A complete binary tree is a binary tree in which


every level except possibly the last is completely
filled and all nodes are as far left as possible.
EXAMPLE

• 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:

You might also like