Chapter 3
Chapter 3
Vertices, V Edges, E =
={v1, v2, v3, {e1, e2, e3,
v4, v5} e4, e5}
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 and w, v and w are said to be incident
w. on e.
Degree of vertex v is 3, because there are 3 edges incident on v
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)
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
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)
1. A simple graph is an
• Unweighted
• Undirected graph
• Containing no loops
• No multiple edges/parallel edges
• 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 𝑒₂.
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)
2. Find the number of vertices, the number of
edges; identify all parallel edges, loops and
isolated vertices for the following graph.
a)
b)
• 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.
• Sum of the degrees = twice the number of edges.
𝑑𝑒𝑔 (𝐴) = 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
*Isolated vertex = F
*Sum of degrees = 𝟔 × 𝟐 = 𝟏𝟐 (𝟔 EDGES)
1. Find the degree of each vertex for the following
graph.
(b)
(c) 𝐾5
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)
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.
The algorithm to determine isomorphism of 2 or more graphs…
Step Graph G Graph H
1. No. of vertices 5 5
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!
b)
Determine whether the graphs below are isomorphic
or not.
c)
d)
Euler circuit in a graph G 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 with every edge in a graph with no
no repeats. Being a circuit, it must repeats. Being a path, it does not
start and end at the same vertex. have to return to the starting
• A connected multigraph has an vertex.
Euler circuit if and only if every • A connected multigraph has an
vertex have even degree. Euler path but not an Euler circuit
if and only if it has exactly two
vertices of odd degree.
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.
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.
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.
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.
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
Hamilton circuit in G Hamilton Path in G
• A Hamilton circuit is a simple • A Hamilton path is a simple path in
circuit in a graph G that passes a graph G that passes through
through every vertex exactly once, every vertex exactly once.
and starts and finish at the same
vertex.
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)
• 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.
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, d)
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.
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.
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
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.
The possible route:
b) a, b, c, d, f, h, k, q, t Level 1 = b, c, d
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
g) f, b, a k)
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
• 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.
Prim’s Algorithm
• 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 new an edge
vertices, find the minimum and add it to the tree
• Keep repeating step 2 until we get a minimum
spanning tree
a) Based on the following figure,
Tips:
Arrange the data items which in
left and right of the root, apply the
alphabetical order.
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.
Which of the following binary trees are BSTs? If a
tree is not a BST, why?
a) b)
c) d)
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
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
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
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
Find the tree traversals (pre-order, in-order & post-
order) of the following tree:
a)
b)
c)
• A full binary tree is a tree in which every vertex other
than the leaves has two children.