Seminar 4 - Introduction To Graphs
Seminar 4 - Introduction To Graphs
1
22/03/2025
Overview
Today’s lecture
Introduction to Graphs
Graph Traversal Algorithms
The idea
Breadth-First Search (BFS)
Depth-First Search (DFS)
Applications
Outline
1. Introduction to Graphs
2. Graph Traversal Algorithms
A. The idea
D. Applications
2
22/03/2025
Graphs
A graph is simply a way of encoding pairwise relationships among a
set of objects.
Graph - Examples
2 2
1 3 1 3
4 5 4 5
Undirected Graph Directed Graph
2 5
2 2 5 2
1
1 3 1 3
7 7
6 6 10 3
4 3 4
4 8 5 4 8 5
Undirected Weighted Graph Directed Weighted Graph
3
22/03/2025
Uses of Graphs
Graphs are extremely useful for modelling. For example:
Transportation networks: map of routes of an airline, rail network, …
Communication networks: the connections between different Internet
service providers, wireless ad-hoc networks, …
Information networks: the connections between different webpages using
links (Google PageRank algorithm for determining the relative importance
of each website), …
Social networks: the persons could be the nodes and the edges represent
friendship (Facebook), or the nodes represent companies and people, and
the edges financial relationships between them, etc. Properties of the
graphs representing social networks are often used to find influencers,
target ads,…
Dependency networks: prerequisites in a course map
…
We will slightly abuse notation and use V (instead of |V|) for the
number of vertices and E (instead of |E|) for the number of edges
when what is meant is clear from the context.
FIT2004: Seminar 4 - Introduction to Graphs
4
22/03/2025
A graph is called a simple graph if it does not have loops AND does
not contain multiple edges between same pair of vertices.
u
Loop z
w
Multiple edges
For any two nodes u and v, their connected components are either
identical or disjoint.
FIT2004: Seminar 4 - Introduction to Graphs
5
22/03/2025
1 3
4 5
6
22/03/2025
Adding edges that join distinct connected components can sometimes also have
bad consequences. E.g., quarantine measures often try to avoid a disease from
reaching a disease-free connected component of a social network.
7
22/03/2025
8
22/03/2025
Tree
Let G=(V, E) be an undirected graph. G is a tree if it satisfies any
of the following equivalent conditions:
G is connected and acyclic (i.e., contains no cycles).
G is connected and has V-1 edges.
G is acyclic and has V-1 edges.
G is acyclic, but a cycle is formed if any edge
is added to G.
G is connected, but would become disconnected
if any single edge is removed from G.
9
22/03/2025
Representing Graphs
Adjacency Matrix (Undirected Graph):
Create a V x V matrix M and store T (true) for M[i][j] if there exists
an edge between i-th and j-th vertex. Otherwise, store F (false).
2
1 2 3 4 5
1 F T T T F 1 3
2 T F T F T
3 T T F F T
4 T F F F T
5 F T T T F 4 5
10
22/03/2025
Representing Graphs
Adjacency Matrix (Undirected Weighted Graph):
Create a V x V matrix M and store weight at M[i][j] only if there
exists an edge between i-th and j-th vertex.
2 5
1 2 3 4 5 2
1 2 7 4
1 3
2 2 5 6 7
3 7 5 3
6
4 4 8 4 3
5 6 3 8
4 8 5
Representing Graphs
Adjacency Matrix (Directed Weighted Graph):
Create a V x V matrix M and store weight at M[i][j] only if there exists an edge from i-th to
j-th vertex.
Space Complexity: O(V2) regardless of the number of edges
Time Complexity of checking if an edge exits: O(1)
Time Complexity of retrieving all neigbhbors (adjacent vertices) of a given vertex:
O(V) regardless of the number of neighbors (unless additional pointers are stored)
2 5
2
1 2 3 4 5
1 3
1 7 4 7
2 2 5 6
6
3 3 4 3
4 8
4 8 5
5
11
22/03/2025
Representing Graphs
Adjacency List (Undirected Graph):
Create an array of size V. At each V[i], store the list of vertices
adjacent to the i-th vertex.
2
1 2 3 4
1 3
2 1 3 5
3
1 2 5
4 1 5
5 4 5
2 3 4
Representing Graphs
Adjacency List (Undirected Weighted Graph):
Create an array of size V. At each V[i], store the list of vertices
adjacent to the i-th vertex along with the weights.
5
2 (6) 3 (3) 4 (8) 4 8 5
12
22/03/2025
Representing Graphs
Adjacency List (Directed Weighted Graph):
Create an array of size V. At each V[i], store the list of vertices adjacent to the i-th vertex along with
the weights.
Space Complexity:
O(V + E)
Time complexity of checking if a particular edge exists:
O(log V) assuming each adjacency list is a sorted array on vertex IDs
Time complexity of retrieving all adjacent vertices of a given vertex:
O(X) where X is the number of adjacent vertices (note: this is output-sensitive complexity)
2 5
1 3 (7) 4 (4) 2
Outline
1. Introduction to Graphs
2. Graph Traversal Algorithms
A. The idea
D. Applications
13
22/03/2025
Graph Traversal
Graph traversal algorithms traverse (visit) all nodes of a graph.
We will look into two algorithms that traverse a connected component from a graph starting from
a source vertex:
Breadth-First Search (BFS)
C A F G
Depth-First Search (DFS)
If a graph has more than one connected component, they can be repeatedly called (on unvisited
nodes) until all graph nodes are marked as visited.
Each one has properties that makes it useful for certain kinds of graph problems.
i.e., all vertices that are k edges away from the source vertex are visited before all vertices that
are k+1 edges away from source
In the graph below, if A is the source, then one possible BFS order is:
A, C, B, D, E, F, G, H
C A F G
14
22/03/2025
i.e., all vertices that are k edges away from the source vertex are visited before all vertices that
are k+1 edges away from source
In the graph below, if A is the source, then one possible BFS order is:
A, C, B, D, E, F, G, H
C A F G
D B E H
i.e., all vertices that are k edges away from the source vertex are visited before all vertices that
are k+1 edges away from source
In the graph below, if A is the source, then one possible BFS order is:
A, C, B, D, E, F, G, H
C A F G
D B E H
15
22/03/2025
i.e., all vertices that are k edges away from the source vertex are visited before all vertices that
are k+1 edges away from source
In the graph below, if A is the source, then one possible BFS order is:
A, C, B, D, E, F, G, H
C A F G
D B E H
i.e., all vertices that are k edges away from the source vertex are visited before all vertices that
are k+1 edges away from source
In the graph below, if A is the source, then one possible BFS order is:
A, C, B, D, E, F, G, H
C A F G
D B E H
16
22/03/2025
C A F G
D B E H
C A F G
D B E H
17
22/03/2025
C A F G
D B E H
C A F G
D B E H
18
22/03/2025
C A F G
D B E H
C A F G
D B E H
19
22/03/2025
C A F G
D B E H
C A F G
D B E H
20
22/03/2025
C A F G
D B E H
Outline
1. Introduction to Graphs
2. Graph Traversal Algorithms
A. The idea
D. Applications
21
22/03/2025
C A F G In Queue:
Finished:
Current:
D B E H
Current: A
Queue:
Finished:
C A F G In Queue:
Finished:
Current:
D B E H
Current:
Queue: B C
Finished: A
22
22/03/2025
C A F G In Queue:
Finished:
Current:
D B E H
Current: B
Queue: C
Finished: A
C A F G In Queue:
Finished:
Current:
D B E H
Current: B
Queue: C
Finished: A
23
22/03/2025
C A F G In Queue:
Finished:
Current:
D B E H
Current:
Queue: C E F
Finished: A B
C A F G In Queue:
Finished:
Current:
D B E H
Current: C
Queue: E F
Finished: A B
24
22/03/2025
C A F G In Queue:
Finished:
Current:
D B E H
Current:
Queue: E F D
Finished: A B C
C A F G In Queue:
Finished:
Current:
D B E H
Current: E
Queue: F D
Finished: A B C
25
22/03/2025
C A F G In Queue:
Finished:
Current:
D B E H
Current:
Queue: F D G H
Finished: A B C E
C A F G In Queue:
Finished:
Current:
D B E H
Current: F
Queue: D G H
Finished: A B C E
26
22/03/2025
C A F G In Queue:
Finished:
Current:
D B E H
Current: D
Queue: G H
Finished: A B C E F
C A F G In Queue:
Finished:
Current:
D B E H
Current: G
Queue: H
Finished: A B C E F D
27
22/03/2025
C A F G In Queue:
Finished:
Current:
D B E H
Current: H
Queue:
Finished: A B C E F D G
C A F G In Queue:
Finished:
Current:
D B E H
Current:
Queue:
Finished: A B C E F D G H
28
22/03/2025
If v is not visited
Add v to visited
29
22/03/2025
If v is not visited
Add v to visited
Space Complexity:
O(V+E)
FIT2004: Seminar 4 - Introduction to Graphs
30
22/03/2025
Space Complexity:
O(V+E)
Outline
1. Introduction to Graphs
2. Graph Traversal Algorithms
A. The idea
D. Applications
31
22/03/2025
C A F G Visited:
D B E H
A B C D E F G H
Visited:
0 0 0 0 0 0 0 0
FIT2004: Seminar 4 - Introduction to Graphs
C A F G Visited:
D B E H
Current: A
A B C D E F G H
Visited:
1 0 0 0 0 0 0 0
FIT2004: Seminar 4 - Introduction to Graphs
32
22/03/2025
C A F G Visited:
D B E H
Current: A
A B C D E F G H
Visited:
1 0 0 0 0 0 0 0
FIT2004: Seminar 4 - Introduction to Graphs
C A F G Visited:
D 2 B E H
Current: B
A B C D E F G H
Visited:
1 1 0 0 0 0 0 0
FIT2004: Seminar 4 - Introduction to Graphs
33
22/03/2025
C A F G Visited:
D 2 B E H
Current: B
A B C D E F G H
Visited:
1 1 0 0 0 0 0 0
FIT2004: Seminar 4 - Introduction to Graphs
C A F G Visited:
D 2 B E H
3
Current: E
A B C D E F G H
Visited:
1 1 0 0 1 0 0 0
FIT2004: Seminar 4 - Introduction to Graphs
34
22/03/2025
C A F G Visited:
D 2 B E H
3
Current: E
A B C D E F G H
Visited:
1 1 0 0 1 0 0 0
FIT2004: Seminar 4 - Introduction to Graphs
C A F G Visited:
D 2 B E H
3
Current: G
A B C D E F G H
Visited:
1 1 0 0 1 0 1 0
FIT2004: Seminar 4 - Introduction to Graphs
35
22/03/2025
C A F G Visited:
D 2 B E H
3
Current: G
A B C D E F G H
Visited:
1 1 0 0 1 0 1 0
FIT2004: Seminar 4 - Introduction to Graphs
C A F G Visited:
D 2 B E H
3
Current: F
A B C D E F G H
Visited:
1 1 0 0 1 1 1 0
FIT2004: Seminar 4 - Introduction to Graphs
36
22/03/2025
C A F G Visited:
D 2 B E H
3
Current: F
A B C D E F G H
Visited:
1 1 0 0 1 1 1 0
FIT2004: Seminar 4 - Introduction to Graphs
C A F G Visited:
• F is a dead end
D 2 B E H • Go back to the last
active node (G)
3
Current: F
A B C D E F G H
Visited:
1 1 0 0 1 1 1 0
FIT2004: Seminar 4 - Introduction to Graphs
37
22/03/2025
C A F G Visited:
D 2 B E H
3
Current: G
A B C D E F G H
Visited:
1 1 0 0 1 1 1 0
FIT2004: Seminar 4 - Introduction to Graphs
C A F G Visited:
D 2 B E H
3
Current: G
A B C D E F G H
Visited:
1 1 0 0 1 1 1 0
FIT2004: Seminar 4 - Introduction to Graphs
38
22/03/2025
C A F G Visited:
D 2 B E H
3
Current: G
A B C D E F G H
Visited:
1 1 0 0 1 1 1 0
FIT2004: Seminar 4 - Introduction to Graphs
C A F G Visited:
D 2 B E H
3 6
Current: H
A B C D E F G H
Visited:
1 1 0 0 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
39
22/03/2025
C A F G Visited:
D 2 B E H
3 6
Current: H
A B C D E F G H
Visited:
1 1 0 0 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
C A F G Visited:
D 2 B E H • H is a dead end
• Speeding up
3 6
visualisation…
Current: H
A B C D E F G H
Visited:
1 1 0 0 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
40
22/03/2025
C A F G Visited:
D 2 B E H
6
Current: G
A B C D E F G H
Visited:
1 1 0 0 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
C A F G Visited:
D 2 B E H
3 6
Current: E
A B C D E F G H
Visited:
1 1 0 0 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
41
22/03/2025
C A F G Visited:
D 2 B E H
3 6
Current: B
A B C D E F G H
Visited:
1 1 0 0 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
C A F G Visited:
D 2 B E H
3 6
Current: A
A B C D E F G H
Visited:
1 1 0 0 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
42
22/03/2025
C A F G Visited:
D 2 B E H
3 6
Current: C
A B C D E F G H
Visited:
1 1 1 0 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
C A F G Visited:
D 2 B E H
3 6
Current: C
A B C D E F G H
Visited:
1 1 1 0 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
43
22/03/2025
C A F G Visited:
8 D 2 B E H
3 6
Current: D
A B C D E F G H
Visited:
1 1 1 1 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
44
22/03/2025
Outline
1. Introduction to Graphs
2. Graph Traversal Algorithms
A. The idea
D. Applications
45
22/03/2025
Length of a path:
46
22/03/2025
C A F G
Discovered:
Visited:
D B E H
Current:
Queue: A
A B C D E F G H
Visited:
1 0 0 0 0 0 0 0
FIT2004: Seminar 4 - Introduction to Graphs
47
22/03/2025
C A F G
Discovered:
Visited:
D B E H
Current: A
Queue:
A B C D E F G H
Visited:
1 0 0 0 0 0 0 0
FIT2004: Seminar 4 - Introduction to Graphs
C A F G
Discovered:
Visited:
D B E H
Dist: 1
Current: A
Queue:
A B C D E F G H
Visited:
1 0 0 0 0 0 0 0
FIT2004: Seminar 4 - Introduction to Graphs
48
22/03/2025
C A F G
Discovered:
Visited:
D B E H
Dist: 1
Queue: B
A B C D E F G H
Visited:
1 1 0 0 0 0 0 0
FIT2004: Seminar 4 - Introduction to Graphs
C A F G
Discovered:
Visited:
D B E H
Dist: 1
Current: A
Queue: B C
A B C D E F G H
Visited:
1 1 1 0 0 0 0 0
FIT2004: Seminar 4 - Introduction to Graphs
49
22/03/2025
C A F G
Discovered:
Visited:
D B E H
Dist: 1
Current: B
Queue: C
A B C D E F G H
Visited:
1 1 1 0 0 0 0 0
FIT2004: Seminar 4 - Introduction to Graphs
C A F G
Discovered:
Visited:
D B E H
Dist: 1 Dist: 2
Current: B
Queue: C
A B C D E F G H
Visited:
1 1 1 0 0 0 0 0
FIT2004: Seminar 4 - Introduction to Graphs
50
22/03/2025
C A F G
Discovered:
Visited:
D B E H
Dist: 1 Dist: 2
Current: B
Queue: C E
A B C D E F G H
Visited:
1 1 1 0 1 0 0 0
FIT2004: Seminar 4 - Introduction to Graphs
C A F G
Discovered:
Visited:
D B E H
Dist: 1 Dist: 2
Current: B
Queue: C E F
A B C D E F G H
Visited:
1 1 1 0 1 1 0 0
FIT2004: Seminar 4 - Introduction to Graphs
51
22/03/2025
C A F G
Discovered:
Visited:
D B E H
Dist: 1 Dist: 2
Current: B
Queue: C E F
A B C D E F G H
Visited:
1 1 1 0 1 1 0 0
FIT2004: Seminar 4 - Introduction to Graphs
C A F G
Discovered:
Visited:
D B E H
Dist: 1 Dist: 2
Current: C
Queue: E F
A B C D E F G H
Visited:
1 1 1 0 1 1 0 0
FIT2004: Seminar 4 - Introduction to Graphs
52
22/03/2025
C A F G
Discovered:
Visited:
D B E H
Dist: 1 Dist: 2
Current: C
Queue: E F
A B C D E F G H
Visited:
1 1 1 0 1 1 0 0
FIT2004: Seminar 4 - Introduction to Graphs
C A F G
Discovered:
Visited:
D B E H
Dist: 2 Dist: 1 Dist: 2
Current: C
Queue: E F D
A B C D E F G H
Visited:
1 1 1 1 1 1 0 0
FIT2004: Seminar 4 - Introduction to Graphs
53
22/03/2025
C A F G
Discovered:
Visited:
D B E H
Dist: 2 Dist: 1 Dist: 2
Current: C
Queue: E F D
A B C D E F G H
Visited:
1 1 1 1 1 1 0 0
FIT2004: Seminar 4 - Introduction to Graphs
C A F G
Discovered:
Visited:
D B E H
Dist: 2 Dist: 1 Dist: 2
Current: E
Queue: F D
A B C D E F G H
Visited:
1 1 1 1 1 1 0 0
FIT2004: Seminar 4 - Introduction to Graphs
54
22/03/2025
C A F G
Discovered:
Visited:
D B E H
Dist: 2 Dist: 1 Dist: 2
Queue: F D
A B C D E F G H
Visited:
1 1 1 1 1 1 0 0
FIT2004: Seminar 4 - Introduction to Graphs
C A F G
Discovered:
Visited:
D B E H
Dist: 2 Dist: 1 Dist: 2 Dist: 3
Current: E
Queue: F D G H
A B C D E F G H
Visited:
1 1 1 1 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
55
22/03/2025
C A F G
Discovered:
Visited:
D B E H
Dist: 2 Dist: 1 Dist: 2 Dist: 3
Current: F
Queue: D G H
A B C D E F G H
Visited:
1 1 1 1 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
C A F G
Discovered:
Visited:
D B E H
Dist: 2 Dist: 1 Dist: 2 Dist: 3
Current: D
Queue: G H
A B C D E F G H
Visited:
1 1 1 1 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
56
22/03/2025
C A F G
Discovered:
Visited:
D B E H
Dist: 2 Dist: 1 Dist: 2 Dist: 3
Current: D
Queue: G H
A B C D E F G H
Visited:
1 1 1 1 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
C A F G
Discovered:
Visited:
D B E H
Dist: 2 Dist: 1 Dist: 2 Dist: 3
Current: G
Queue: H
A B C D E F G H
Visited:
1 1 1 1 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
57
22/03/2025
C A F G
Discovered:
Visited:
D B E H
Dist: 2 Dist: 1 Dist: 2 Dist: 3
Current: G
Queue: H
A B C D E F G H
Visited:
1 1 1 1 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
C A F G
Discovered:
Visited:
D B E H
Dist: 2 Dist: 1 Dist: 2 Dist: 3
Current: H
Queue:
A B C D E F G H
Visited:
1 1 1 1 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
58
22/03/2025
C A F G
Discovered:
Visited:
D B E H
Dist: 2 Dist: 1 Dist: 2 Dist: 3
Current: H
Queue:
A B C D E F G H
Visited:
1 1 1 1 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
C A F G
Discovered:
Visited:
D B E H
Dist: 2 Dist: 1 Dist: 2 Dist: 3
Current:
Queue:
A B C D E F G H
Visited:
1 1 1 1 1 1 1 1
FIT2004: Seminar 4 - Introduction to Graphs
59
22/03/2025
B D B D
A A
C E C E
Graph 1 Graph 2
60
22/03/2025
DAG: Examples
Sub-tasks of a project and which “must finish before”
A → B means task A must finish before task B
so, DAGs useful in project management
Relationships between subjects for your degree -- “is prerequisite for”
A→B means subject A must be completed before enrolling in subject B
People genealogy – “is an ancestor of”
A → B means A is an ancestor of B
Power sets and “is a subset of“
A → B means A is a subset of B
B D
C E
Source: wikipedia
FIT2004: Seminar 4 - Introduction to Graphs
A topological order
is a permutation of the vertices in the original DAG such that
for every directed edge u→v of the DAG B D
u appears before v in the permutation
A
Example: A, B, C, E, D C E
Topological sort of a DAG of “is prerequisite of” example gives an ordering of the
subjects for studying your degree, one at a time, while obeying prerequisite rules.
61
22/03/2025
B D
C E
B D
C E
62
22/03/2025
B D
C E
Not accessed:
Sorted:
Sorted:
B D
C E
Not accessed:
Sorted:
Sorted:
63
22/03/2025
B D
C E
Not accessed:
Sorted:
Sorted:
B D
C E
Not accessed:
Sorted:
Sorted:
64
22/03/2025
B D
C E
Not accessed:
Sorted:
Sorted: D
B D
C E
Not accessed:
Sorted:
Sorted: B D
65
22/03/2025
B D
C E
Not accessed:
Sorted:
Sorted: B D
B D
C E
Not accessed:
Sorted:
Sorted: B D
66
22/03/2025
B D
C E
Not accessed:
Sorted:
Sorted: E B D
B D
C E
Not accessed:
Sorted:
Sorted: C E B D
67
22/03/2025
B D
C E
Not accessed:
Sorted:
Sorted: A C E B D
Reading
Course Notes: Chapter 5
You can also check algorithms’ textbooks for contents related to this
lecture, e.g.:
CLRS: Chapter 22
KT: Chapter 3
Rou: Chapters 7 and 8
68
22/03/2025
Concluding Remarks
Things to do (this list is not exhaustive)
Read more about BFS, DFS and their applications
Implement BFS and DFS
Read course notes
Coming Up Next
Greedy (Graph) Algorithms
69