Chapter Six
Chapter Six
6.0 INTRODUCTION
Graph theory is an important branch of discrete mathematics concerned with the mathematical
representation and analysis of relationships, connections, networks, and structures.
Computer networks
Social networks
Road networks
Communication systems
Web pages and hyperlinks
Software dependencies
Database relationships
Transportation systems
Electrical networks
Network routing
For example, a computer network can be represented using vertices to represent computers or
routers and edges to represent communication links.
Graph theory provides the mathematical foundation for many computing techniques, including:
Network routing
Shortest-path algorithms
Search algorithms
Network optimization
Web crawling
Recommendation systems
Social network analysis
Minimum spanning trees
Scheduling
Artificial intelligence
A particularly important type of graph is a tree. Trees are widely used in computer science for
representing hierarchical relationships and organizing information.
Examples include:
File systems
[Link] 1/31
8/11/26, 11:13 AM Course Outline Generation
Decision trees
Binary search trees
Syntax trees
Database indexes
Network structures
G = (V , E)
where:
G = graph
V = set of vertices
E = set of edges
For example:
V = {A, B, C, D}
and:
[Link] 2/31
8/11/26, 11:13 AM Course Outline Generation
1. Vertices
A vertex is an individual object or point in a graph.
Computers
People
Cities
Routers
Web pages
Software modules
Vertices are also called nodes.
2. Edges
An edge represents a relationship or connection between two vertices.
For example:
A−B
V = {A, B, C, D}
and:
A
/ \
B C
\ /
D
A, B, C, D
The edges are:
1. Undirected graphs
2. Directed graphs
3. Weighted graphs
4. Unweighted graphs
5. Simple graphs
6. Complete graphs
If:
A−B
Mathematically:
AB = BA
Example
A friendship network can be represented as an undirected graph because friendship is generally
considered a mutual relationship.
If:
A→B
However:
A→B
B→A
Applications
Directed graphs are useful for representing:
Website links
Social media following
One-way roads
Task dependencies
Data flow
Software dependencies
For example:
5
A
B
Distance
Cost
Time
Bandwidth
Energy consumption
Network latency
Example
Suppose:
[Link] 5/31
8/11/26, 11:13 AM Course Outline Generation
A − B = 10 km
B − C = 15 km
The graph can represent distances between locations.
For example:
A−B−C −D
Unweighted graphs are commonly used when the existence of a relationship is more important
than its cost.
A→A
A simple graph does not contain such an edge.
Kn
n(n − 1)
2
Worked Example 1
[Link] 6/31
8/11/26, 11:13 AM Course Outline Generation
K5
Solution
n(n − 1)
E=
2
Substitute:
5(5 − 1)
E=
2
5(4)
=
2
= 10
Therefore:
K5 has 10 edges
deg(v)
Consider:
B
|
A------C------D
|
E
Therefore:
deg(C) = 4
Vertex A has:
deg(A) = 1
[Link] 7/31
8/11/26, 11:13 AM Course Outline Generation
In-degree
The number of edges entering a vertex.
Represented as:
deg− (v)
Out-degree
The number of edges leaving a vertex.
Represented as:
deg+ (v)
Worked Example 2
Consider:
A→B
C→B
B→D
For vertex B :
Incoming edges:
A→B
and:
C→B
Therefore:
deg− (B) = 2
Outgoing edges:
B→D
Therefore:
deg+ (B) = 1
[Link] 8/31
8/11/26, 11:13 AM Course Outline Generation
∑ deg(v) = 2∣E∣
v∈V
This means that the sum of all vertex degrees is twice the number of edges.
Each edge contributes two to the total degree because it connects two vertices.
Worked Example 3
Consider a graph with 5 edges.
Using:
∑ deg(v) = 2∣E∣
we obtain:
2(5) = 10
Therefore:
10
6.15 PATHS
A path is a sequence of vertices connected by edges.
For example:
A−B−C −D
is a path from A to D .
Therefore:
A−B−C −D
has:
3
[Link] 9/31
8/11/26, 11:13 AM Course Outline Generation
edges.
Hence:
Path length = 3
6.16 WALK
A walk is a sequence of vertices and edges in which vertices or edges may be repeated.
For example:
A−B−C −B−D
6.17 TRAIL
A trail is a walk in which no edge is repeated.
6.19 CYCLES
A cycle is a path that begins and ends at the same vertex without repeating intermediate vertices.
For example:
A−B−C −A
forms a cycle.
[Link] 10/31
8/11/26, 11:13 AM Course Outline Generation
Circular dependencies
Network loops
Deadlocks
Routing loops
Dependency cycles
For example:
A---B---C
|
D
Example:
A---B C---D
1. Adjacency matrix
2. Adjacency list
[Link] 11/31
8/11/26, 11:13 AM Course Outline Generation
Suppose:
V = {A, B, C}
and edges are:
AB, AC
The adjacency matrix is:
A B C
A 0 1 1
B 1 0 0
C 1 0 0
A value of:
A value of:
0
means no edge exists.
Disadvantages
Requires:
O(V 2 )
memory.
Can waste memory for sparse graphs.
[Link] 12/31
8/11/26, 11:13 AM Course Outline Generation
For:
A − B, A − C
the adjacency list is:
A → B, C
B → A
C → A
[Link] 13/31
8/11/26, 11:13 AM Course Outline Generation
It typically uses a:
Queue
data structure.
Suppose:
A
/ \
B C
/ \
D E
A, B, C, D, E
The algorithm first visits A, then its immediate neighbours, then the next level.
BFS(Graph, start):
create an empty queue
mark start as visited
add start to queue
[Link] 14/31
8/11/26, 11:13 AM Course Outline Generation
A stack, or
Recursion.
For example:
A
/ \
B C
/ \
D E
A, B, D, E, C
The exact traversal can depend on the order in which neighbours are processed.
DFS(vertex):
mark vertex as visited
process vertex
[Link] 15/31
8/11/26, 11:13 AM Course Outline Generation
For an unweighted graph, BFS can be used to find the shortest path.
For a weighted graph with non-negative edge weights, Dijkstra's algorithm is commonly used.
[Link] 16/31
8/11/26, 11:13 AM Course Outline Generation
A --2-- B --3-- D
\ /
5 1
\ /
C
A−B−D
with cost:
2+3=5
Another route:
A−C −B−D
has cost:
5+1+3=9
Therefore:
A−B−D
5
6.38 TREES
A tree is a connected undirected graph that contains no cycles.
n−1
edges.
[Link] 17/31
8/11/26, 11:13 AM Course Outline Generation
Using:
E =V −1
we obtain:
E = 12 − 1
E = 11
Therefore:
11 edges
Example:
A
/ | \
B C D
/ \
E F
Here:
A
is the root.
[Link] 18/31
8/11/26, 11:13 AM Course Outline Generation
Root
The topmost or starting vertex.
Parent
A vertex directly above another vertex.
Child
A vertex directly below another vertex.
Leaf
A vertex with no children.
Internal Vertex
A vertex with at least one child.
Sibling
Vertices having the same parent.
For example:
A Level 0
/ \
B C Level 1
/ \
D E Level 2
Therefore:
A=0
B, C = 1
D, E = 2
The height of a tree is the length of the longest path from the root to a leaf.
A−B−D
has two edges.
Therefore:
Height = 2
Left child
Right child
Example:
10
/ \
5 15
/ \ / \
2 7 12 20
[Link] 20/31
8/11/26, 11:13 AM Course Outline Generation
50
/ \
30 70
/ \ / \
20 40 60 80
60 > 50
so move right.
Then:
60 < 70
so move left.
We find:
60
n−1
edges.
Network design
Telecommunications
Road construction
Electrical networks
Cable installation
[Link] 21/31
8/11/26, 11:13 AM Course Outline Generation
Infrastructure planning
Two common MST algorithms are:
Kruskal's algorithm
Prim's algorithm
AB = 2
AC = 4
BC = 1
BD = 5
CD = 3
Using Kruskal's method, arrange the edges from smallest to largest:
BC = 1
AB = 2
CD = 3
AC = 4
[Link] 22/31
8/11/26, 11:13 AM Course Outline Generation
BD = 5
Select:
BC
then:
AB
then:
CD
At this point all four vertices are connected.
Total cost:
1+2+3=6
Therefore, the minimum spanning tree has total weight:
6
For example:
A→B
may mean:
Dependency relationships
Build order
[Link] 23/31
8/11/26, 11:13 AM Course Outline Generation
Conflicting dependencies
Circular dependencies
For example:
Routers → vertices
Communication links → edges
Transmission cost → edge weights
Graph algorithms can then determine:
Shortest routes
Network connectivity
Redundant links
Minimum-cost infrastructure
Network failures
For example:
Users → vertices
Friendships → edges
Directed graphs can represent following relationships:
A→B
meaning:
A follows B.
Influential users
Communities
Connections
Degrees of separation
Recommendation relationships
For example:
Graph-based models are especially useful where data contains many interconnected relationships.
R1 , R2 , R3 , R4 , R5
with connections:
R1 − R2
R1 − R3
R2 − R4
[Link] 25/31
8/11/26, 11:13 AM Course Outline Generation
R3 − R4
R4 − R5
Questions
1. How many vertices are present?
2. How many edges are present?
3. Is the graph connected?
4. Give one path from R1 to R5 .
Solution
1. Number of vertices
5
2. Number of edges
There are:
5
edges.
3. Connectivity
Every router can be reached from every other router.
Connected
4. Example path
R1 − R2 − R4 − R5
Therefore:
R1 → R2 → R4 → R5
Represent:
[Link] 26/31
8/11/26, 11:13 AM Course Outline Generation
Students as vertices.
Friendships as edges.
Tasks:
1. Draw the graph.
2. Determine the degree of each student.
3. Identify the student with the highest degree.
4. Determine whether the graph is connected.
5. Identify any cycles.
Given:
V = {A, B, C, D, E}
and:
Assignment 4: Trees
Answer the following:
1. Define a tree.
2. State five properties of trees.
3. Explain rooted trees.
4. Define a leaf node.
5. Define a parent and child.
6. Explain binary trees.
7. Explain binary search trees.
8. A tree contains 25 vertices. How many edges does it contain?
9. Explain why trees cannot contain cycles.
[Link] 28/31
8/11/26, 11:13 AM Course Outline Generation
AB = 4, AC = 2, BC = 1
BD = 5, CD = 8, CE = 10
DE = 2
Tasks:
1. Draw the graph.
2. Apply Kruskal's algorithm.
3. Determine the minimum spanning tree.
4. Calculate the total weight.
5. Explain a practical application of MST in computer networking.
Question 1
Define graph theory and explain five applications of graph theory in computer science.
Question 2
Given a graph containing 8 vertices and 12 edges:
Question 3
Differentiate between:
Question 4
A university has six departments connected through a computer network. The network connections
have different costs.
Explain how Dijkstra's algorithm can be used to identify the shortest communication route between
two departments.
Question 5
A network contains 10 routers.
[Link] 30/31
8/11/26, 11:13 AM Course Outline Generation
G = (V , E)
Complete Graph
n(n − 1)
∣E∣ =
2
Handshaking Lemma
∑ deg(v) = 2∣E∣
Tree
∣E∣ = ∣V ∣ − 1
N
⌈ ⌉
k
Students should understand that graph theory is not simply an abstract mathematical topic. It is
directly applicable to real-world computing systems.
For example:
Similarly:
The study of graphs therefore provides an essential foundation for software engineering,
computer science, networking, databases, artificial intelligence, algorithms and computational
mathematics.
[Link] 31/31