0% found this document useful (0 votes)
18 views69 pages

Seminar 4 - Introduction To Graphs

The document provides an introduction to graphs and graph traversal algorithms as part of the FIT2004 course at Monash University. It covers key concepts such as graph definitions, types of graphs, properties, and applications, along with traversal algorithms like Breadth-First Search (BFS) and Depth-First Search (DFS). Additionally, it discusses graph representation methods and the importance of connected components in practical applications.

Uploaded by

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

Seminar 4 - Introduction To Graphs

The document provides an introduction to graphs and graph traversal algorithms as part of the FIT2004 course at Monash University. It covers key concepts such as graph definitions, types of graphs, properties, and applications, along with traversal algorithms like Breadth-First Search (BFS) and Depth-First Search (DFS). Additionally, it discusses graph representation methods and the importance of connected components in practical applications.

Uploaded by

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

22/03/2025

Faculty of Information Technology,


Monash University

FIT2004: Algorithms and Data


Structures

Week 4: Introduction to Graphs

1
22/03/2025

Overview

Divide and Greedy Dynamic Data


Network flow
conquer algorithms programming structures
(W 8-9)
(W 1-3) (W 4-5) (W 6-7) (W 10-11)

 Today’s lecture
 Introduction to Graphs
 Graph Traversal Algorithms
 The idea
 Breadth-First Search (BFS)
 Depth-First Search (DFS)
 Applications

FIT2004: Seminar 4 - Introduction to Graphs

Outline
1. Introduction to Graphs
2. Graph Traversal Algorithms
A. The idea

B. Breadth-First Search (BFS)

C. Depth-First Search (DFS)

D. Applications

FIT2004: Seminar 4 - Introduction to Graphs

2
22/03/2025

Graphs
 A graph is simply a way of encoding pairwise relationships among a
set of objects.

 Each object is called a vertex or node.

 Each edge of the graph “connects” two nodes.

FIT2004: Seminar 4 - Introduction to Graphs

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

FIT2004: Seminar 4 - Introduction to Graphs

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
 …

FIT2004: Seminar 4 - Introduction to Graphs

Graphs – Formal notations


 A graph G = (V, E) is defined using a set of vertices V and a set of
edges E.

 An edge e is represented as e = (u, v) where u and v are two


vertices

 For undirected graphs, (u, v) = (v, u) because there is no sense of


direction. For a directed graph, (u, v) represents an edge from u to
v and (u, v) ≠ (v, u).

 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

Graphs – Formal notations


 A weighted graph is represented as G = (V, E) and each edge (u, v)
has an associated weight w.

 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

 In this unit, we focus on simple graphs with a finite number of


vertices.

FIT2004: Seminar 4 - Introduction to Graphs

Graphs – Connected Components


 A vertex v is reachable from u if there is a path in the graph that
starts in u and ends v.
 In an undirected graph, reachability is an equivalence relation:
 Reflexive: each u node is reachable from itself.
 Symmetric: if v is reachable from u, then u is reachable from v.
 Transitive: if v is reachable from u, and u is reachable from w, then v is reachable
from w. u
v
w

 The set of vertices reachable from u defines the connected


component of G containing u.

 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

Graphs – Connected Components


 An undirected graph with 3 connected components:

FIT2004: Seminar 4 - Introduction to Graphs

Graphs – Connected Components


 An undirected graph is connected if all vertices are part of a single
connected component.

 In other words, for any pair of vertices u and v, there is a path


between them.

1 3

4 5

FIT2004: Seminar 4 - Introduction to Graphs

6
22/03/2025

Graphs – Connected Components


 Why is that an important concept in practice?
 Example: Airlines normally want their air routes to form a connected graph.

 Getting a connected graph is often an important consideration when designing


communication and transportation networks.
 Hubs: Often it is not viable to have pairwise connections between all nodes; but
one still wants to have paths, without many intermediary nodes, between every
pair of nodes.

FIT2004: Seminar 4 - Introduction to Graphs

Graphs – Connected Components


 In this graph the edge (g, h) is quite critical as any problem in the network that
eliminates this edge would break the connected graph into “large” disjoint
connected components.
 This can be quite bad in networks. Some kind of redundancy
is often desirable.

 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.

FIT2004: Seminar 4 - Introduction to Graphs

7
22/03/2025

Some Graph Properties


Let G be a graph.
 The minimum number of edges in a connected undirected
graph
 ???
 The maximum number of edges in an undirected graph
 ???

FIT2004: Seminar 4 - Introduction to Graphs

Some Graph Properties


Let G be a graph.
 The minimum number of edges in a connected undirected
graph
 V-1 = O(V)
 The maximum number edges in an undirected graph
 V(V - 1)/2 = O(V2)

 A graph is called sparse if E << V2 (<< means significantly smaller than)

 A graph is called dense if E ≈ V2

FIT2004: Seminar 4 - Introduction to Graphs

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.

 In other words, if any of the above conditions is satisfied for an


undirected graph G, then G is a tree (and all the other
conditions will also hold).

FIT2004: Seminar 4 - Introduction to Graphs

Graphs – Connected Components


 In directed graphs, reachability is reflexive and transitive, but not
guaranteed to be symmetric (i.e., possibly there could be a path
from u to v, but no path from v to u).

 Vertices u and v are called mutually reachable if there are paths


from u to v and from v to u.

 Mutual reachability is an equivalence relation and decomposes the


graph into strongly-connected components (for any two vertices u
and v, their strong components are either identical or disjoint).

FIT2004: Seminar 4 - Introduction to Graphs

9
22/03/2025

Graphs – Connected Components


A directed graph with 3 strongly-connected components:

 A directed graph is strongly connected if for every pair of vertices u


and v of G, there are paths from u to v and from v to u.

 I.e., the graph only has one strongly-connected component.

FIT2004: Seminar 4 - Introduction to Graphs

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

FIT2004: Seminar 4 - Introduction to Graphs

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

FIT2004: Seminar 4 - Introduction to Graphs

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

FIT2004: Seminar 4 - Introduction to Graphs

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

FIT2004: Seminar 4 - Introduction to Graphs

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.

The numbers in parentheses correspond to the weights.


2 5
1 2 (2) 3 (7) 4 (4) 2
2 1 (2) 3 (5) 5 (6) 1 3
7
3
1 (7) 2 (5) 5 (3)
6
4 1 (4) 5 (8) 4 3

5
2 (6) 3 (3) 4 (8) 4 8 5

FIT2004: Seminar 4 - Introduction to Graphs

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

2 1 (2) 3 (5) 5 (6) 1 3


7
3
5 (3)
6
4 3
4 5 (8)
5 4 8 5
FIT2004: Seminar 4 - Introduction to Graphs

Outline
1. Introduction to Graphs
2. Graph Traversal Algorithms
A. The idea

B. Breadth-First Search (BFS)

C. Depth-First Search (DFS)

D. Applications

FIT2004: Seminar 4 - Introduction to Graphs

13
22/03/2025

Graph Traversal
Graph traversal algorithms traverse (visit) all nodes of a graph.

They are very important in the design of numerous algorithms.

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)

Both of them visit the vertices exactly once.

They visit vertices in different orders. D B E H

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.

FIT2004: Seminar 4 - Introduction to Graphs

Graph Traversal - BFS


 Breadth-First Search (BFS)
 Traverses the graph uniformly from the source vertex

 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 Konrad Zuse, computer


science pioneer

FIT2004: Seminar 4 - Introduction to Graphs

14
22/03/2025

Graph Traversal - BFS


 Breadth-First Search (BFS)
 Traverses the graph uniformly from the source vertex

 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

FIT2004: Seminar 4 - Introduction to Graphs

Graph Traversal - BFS


 Breadth-First Search (BFS)
 Traverses the graph uniformly from the source vertex

 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

FIT2004: Seminar 4 - Introduction to Graphs

15
22/03/2025

Graph Traversal - BFS


 Breadth-First Search (BFS)
 Traverses the graph uniformly from the source vertex

 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

FIT2004: Seminar 4 - Introduction to Graphs

Graph Traversal - BFS


 Breadth-First Search (BFS)
 Traverses the graph uniformly from the source vertex

 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

FIT2004: Seminar 4 - Introduction to Graphs

16
22/03/2025

Graph Traversal - DFS


 Depth-First Search (DFS)
 A version of DFS was investigated by the 19th century French
mathematician Charles Pierre Trémaux to solve mazes.
 Traverses the graph as deeply as possible before backtracking and traversing
other nodes
 In the graph below, one possible DFS order is: A, B, F, G, H, E, C, D
Is A, B, E, H, F, G, C, D a possible DFS order?

C A F G

D B E H

FIT2004: Seminar 4 - Introduction to Graphs

Graph Traversal - DFS


 Depth-First Search (DFS)
 Traverses the graph as deeply as possible before backtracking and traversing
other nodes
 In the tree, one possible DFS order is: A, B, F, G, H, E, C, D

Is A, B, E, H, F, G, C, D as possible DFS order?


No!

C A F G

D B E H

FIT2004: Seminar 4 - Introduction to Graphs

17
22/03/2025

Graph Traversal - DFS


 Depth-First Search (DFS)
 Traverses the graph as deeply as possible before backtracking and traversing
other nodes
 In the tree, one possible DFS order is: A, B, F, G, H, E, C, D

Is A, B, E, H, F, G, C, D as possible DFS order?


No!

C A F G

D B E H

FIT2004: Seminar 4 - Introduction to Graphs

Graph Traversal - DFS


 Depth-First Search (DFS)
 Traverses the graph as deeply as possible before backtracking and traversing
other nodes
 In the tree, one possible DFS order is: A, B, F, G, H, E, C, D

Is A, B, E, H, F, G, C, D as possible DFS order?


No!

C A F G

D B E H

FIT2004: Seminar 4 - Introduction to Graphs

18
22/03/2025

Graph Traversal - DFS


 Depth-First Search (DFS)
 Traverses the graph as deeply as possible before backtracking and traversing
other nodes
 In the tree, one possible DFS order is: A, B, F, G, H, E, C, D

Is A, B, E, H, F, G, C, D as possible DFS order?


No!

C A F G

D B E H

FIT2004: Seminar 4 - Introduction to Graphs

Graph Traversal - DFS


 Depth-First Search (DFS)
 Traverses the graph as deeply as possible before backtracking and traversing
other nodes
 In the tree, one possible DFS order is: A, B, F, G, H, E, C, D

Is A, B, E, H, F, G, C, D as possible DFS order?


No!

C A F G

D B E H

FIT2004: Seminar 4 - Introduction to Graphs

19
22/03/2025

Graph Traversal - DFS


 Depth-First Search (DFS)
 Traverses the graph as deeply as possible before backtracking and traversing
other nodes
 In the tree, one possible DFS order is: A, B, F, G, H, E, C, D

Is A, B, E, H, F, G, C, D as possible DFS order?


No!

C A F G

D B E H

FIT2004: Seminar 4 - Introduction to Graphs

Graph Traversal - DFS


 Depth-First Search (DFS)
 Traverses the graph as deeply as possible before backtracking and traversing
other nodes
 In the tree, one possible DFS order is: A, B, F, G, H, E, C, D

Is A, B, E, H, F, G, C, D as possible DFS order?


No!

C A F G

D B E H

FIT2004: Seminar 4 - Introduction to Graphs

20
22/03/2025

Graph Traversal - DFS


 Depth-First Search (DFS)
 Traverses the graph as deeply as possible before backtracking and traversing
other nodes
 In the tree, one possible DFS order is: A, B, F, G, H, E, C, D

Is A, B, E, H, F, G, C, D as possible DFS order?


No!

C A F G

D B E H

FIT2004: Seminar 4 - Introduction to Graphs

Outline
1. Introduction to Graphs
2. Graph Traversal Algorithms
A. The idea

B. Breadth-First Search (BFS)

C. Depth-First Search (DFS)

D. Applications

FIT2004: Seminar 4 - Introduction to Graphs

21
22/03/2025

Breadth-First Search (BFS)

C A F G In Queue:

Finished:

Current:
D B E H

Current: A

Queue:

Finished:

FIT2004: Seminar 4 - Introduction to Graphs

Breadth-First Search (BFS)

C A F G In Queue:

Finished:

Current:
D B E H

Current:

Queue: B C

Finished: A

FIT2004: Seminar 4 - Introduction to Graphs

22
22/03/2025

Breadth-First Search (BFS)

C A F G In Queue:

Finished:

Current:
D B E H

Current: B

Queue: C

Finished: A

FIT2004: Seminar 4 - Introduction to Graphs

Breadth-First Search (BFS)

C A F G In Queue:

Finished:

Current:
D B E H

Current: B

Queue: C

Finished: A

FIT2004: Seminar 4 - Introduction to Graphs

23
22/03/2025

Breadth-First Search (BFS)

C A F G In Queue:

Finished:

Current:
D B E H

Current:

Queue: C E F

Finished: A B

FIT2004: Seminar 4 - Introduction to Graphs

Breadth-First Search (BFS)

C A F G In Queue:

Finished:

Current:
D B E H

Current: C

Queue: E F

Finished: A B

FIT2004: Seminar 4 - Introduction to Graphs

24
22/03/2025

Breadth-First Search (BFS)

C A F G In Queue:

Finished:

Current:
D B E H

Current:

Queue: E F D

Finished: A B C

FIT2004: Seminar 4 - Introduction to Graphs

Breadth-First Search (BFS)

C A F G In Queue:

Finished:

Current:
D B E H

Current: E

Queue: F D

Finished: A B C

FIT2004: Seminar 4 - Introduction to Graphs

25
22/03/2025

Breadth-First Search (BFS)

C A F G In Queue:

Finished:

Current:
D B E H

Current:

Queue: F D G H

Finished: A B C E

FIT2004: Seminar 4 - Introduction to Graphs

Breadth-First Search (BFS)

C A F G In Queue:

Finished:

Current:
D B E H

Current: F

Queue: D G H

Finished: A B C E

FIT2004: Seminar 4 - Introduction to Graphs

26
22/03/2025

Breadth-First Search (BFS)

C A F G In Queue:

Finished:

Current:
D B E H

Current: D

Queue: G H

Finished: A B C E F

FIT2004: Seminar 4 - Introduction to Graphs

Breadth-First Search (BFS)

C A F G In Queue:

Finished:

Current:
D B E H

Current: G

Queue: H

Finished: A B C E F D

FIT2004: Seminar 4 - Introduction to Graphs

27
22/03/2025

Breadth-First Search (BFS)

C A F G In Queue:

Finished:

Current:
D B E H

Current: H

Queue:

Finished: A B C E F D G

FIT2004: Seminar 4 - Introduction to Graphs

Breadth-First Search (BFS)

C A F G In Queue:

Finished:

Current:
D B E H

Current:

Queue:

Finished: A B C E F D G H

FIT2004: Seminar 4 - Introduction to Graphs

28
22/03/2025

Breadth-First Search (BFS)


 Initialize some data structure “queue” and some data structure
“visited”, both empty of vertices
 Put an initial vertex in queue
 Mark the initial vertex as visited
 While queue is not empty
 Get the first vertex, u, from queue

 For each edge (u,v)

 If v is not visited
 Add v to visited

 Add v at the end of queue

What is the time complexity of BFS?

FIT2004: Seminar 4 - Introduction to Graphs

Breadth-First Search (BFS)


 Initialize some data structure “queue” and some data structure
“visited”, both empty of vertices 𝑂(? )
 Put an initial vertex in queue 𝑂(1)
 Mark the initial vertex as visited 𝑂(? )
 While queue is not empty 𝑂(1)
 Get the first vertex, u, from queue 𝑂 𝑉 𝑡𝑜𝑡𝑎𝑙
 For each edge (u,v) 𝑂 𝐸 𝑡𝑜𝑡𝑎𝑙
 If v is not visited 𝑂(? )
 Add v to visited 𝑂(? )
 Add v at the end of queue 𝑂 𝑉 𝑡𝑜𝑡𝑎𝑙

Assuming adjacency list representation.


Time Complexity:
 We look at every edge twice
 For each edge, we do a lookup on visited (with some complexity)
 We insert vertices to visited at most O(V) times
 O(V*insert to visited + E*lookup on visited)
FIT2004: Seminar 4 - Introduction to Graphs

29
22/03/2025

Breadth-First Search (BFS)


 Initialize some data structure “queue” and some data structure
“visited”, both empty of vertices 𝑂(? )
 Put an initial vertex in queue 𝑂(1)
 Mark the initial vertex as visited 𝑂(? )
 While queue is not empty 𝑂(1)
 Get the first vertex, u, from queue 𝑂 𝑉 𝑡𝑜𝑡𝑎𝑙
 For each edge (u,v) 𝑂 𝐸 𝑡𝑜𝑡𝑎𝑙
 If v is not visited 𝑂(? )
 Add v to visited 𝑂(? )
 Add v at the end of queue 𝑂 𝑉 𝑡𝑜𝑡𝑎𝑙

Assuming adjacency list representation.


Time Complexity:
 O(V*insert to visited + E*lookup on visited)
 Visited is just a bit list, indexed by vertex ID
 Lookup and insert are both O(1)

FIT2004: Seminar 4 - Introduction to Graphs

Breadth-First Search (BFS)


 Initialize some data structure “queue” and some data structure
“visited”, both empty of vertices
 Put an initial vertex in queue
 Mark the initial vertex as visited
 While queue is not empty
 Get the first vertex, u, from queue

 For each edge (u,v)

 If v is not visited
 Add v to visited

 add v at the end of queue

Assuming adjacency list representation.


Time Complexity:
 O(V * 1 + E * 1) = O(V+E)

Space Complexity:
 O(V+E)
FIT2004: Seminar 4 - Introduction to Graphs

30
22/03/2025

Breadth-First Search (BFS)

Assuming adjacency list representation.


Time Complexity:
 O(V+E)

Space Complexity:
 O(V+E)

FIT2004: Seminar 4 - Introduction to Graphs

Outline
1. Introduction to Graphs
2. Graph Traversal Algorithms
A. The idea

B. Breadth-First Search (BFS)

C. Depth-First Search (DFS)

D. Applications

FIT2004: Seminar 4 - Introduction to Graphs

31
22/03/2025

Depth-First Search (DFS)

C A F G Visited:

D B E H

Visited is indexed by vertex ID. Normally,


Current: A the IDs are integers from from 0 to V-1 (to
allow O(1) lookup), but letters are used
here for ease of understanding

A B C D E F G H
Visited:
0 0 0 0 0 0 0 0
FIT2004: Seminar 4 - Introduction to Graphs

Depth-First Search (DFS)


1

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

Depth-First Search (DFS)


1

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

Depth-First Search (DFS)


1

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

Depth-First Search (DFS)


1

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

Depth-First Search (DFS)


1

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

Depth-First Search (DFS)


1

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

Depth-First Search (DFS)


1 4

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

Depth-First Search (DFS)


1 4

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

Depth-First Search (DFS)


1 5 4

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

Depth-First Search (DFS)


1 5 4

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

Depth-First Search (DFS)


1 5 4

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

Depth-First Search (DFS)


1 5 4

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

Depth-First Search (DFS)


1 5 4

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

Depth-First Search (DFS)


1 5 4

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

Depth-First Search (DFS)


1 5 4

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

Depth-First Search (DFS)


1 5 4

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

Depth-First Search (DFS)


1 5 4

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

Depth-First Search (DFS)


1 5 4

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

Depth-First Search (DFS)


1 5 4

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

Depth-First Search (DFS)


1 5 4

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

Depth-First Search (DFS)


1 5 4

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

Depth-First Search (DFS)


7 1 5 4

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

Depth-First Search (DFS)


7 1 5 4

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

Depth-First Search (DFS)


7 1 5 4

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

Depth-First Search (DFS)

Assuming adjacency list representation.


Time Complexity:
 Each vertex visited at most once
 Each edge accessed at most twice (once when u is visited once when v is visited)
 Total cost: O(V+E)
Space Complexity:
 O(V+E)

FIT2004: Seminar 4 - Introduction to Graphs

44
22/03/2025

Outline
1. Introduction to Graphs
2. Graph Traversal Algorithms
A. The idea

B. Breadth-First Search (BFS)

C. Depth-First Search (DFS)

D. Applications

FIT2004: Seminar 4 - Introduction to Graphs

Applications of DFS and BFS


The algorithms we saw can also be applied on directed graphs.
BFS and DFS have a wide variety of applications:
 Reachability
 Finding all connected components
 Testing a graph for bipartiteness
 A graph is bipartite when we can divide it into two sets, U and V, with every edge
having one vertex in set U and the other in set V
 Finding cycles
 Shortest paths on unweighted graphs
 Topological sort
 …
More details are given in unit notes and applied classes.

Example of bipartite graph

FIT2004: Seminar 4 - Introduction to Graphs

45
22/03/2025

Shortest Path Problem

Length of a path:

For unweighted graphs, the length of a path is the number of


edges along the path.

For weighted graphs, the length of a path is the sum of weights of


the edges along the path.

FIT2004: Seminar 4 - Introduction to Graphs

Shortest Path Problem

Single source, single target:


Given a source vertex s and a target vertex t, return the shortest
path from s to t.

Single source, all targets:


Given a source vertex s, return the shortest paths to every other
vertex in the graph.

We will focus on single source, all targets problem because the


single source, single target problem is subsumed by it.

FIT2004: Seminar 4 - Introduction to Graphs

46
22/03/2025

Shortest Path Algorithms

 Breadth-First Search – (Single source, unweighted graphs) Today

 Dijkstra’s Algorithm – (Single Source, weighted graphs with non-


negative weights) Week 5

 Bellman-Ford Algorithm – (Single source, weighted graphs


including negative weights) Week 7

 Floyd-Warshall Algorithm– (All pairs, weighted graphs including


negative weights) Week 7

FIT2004: Seminar 4 - Introduction to Graphs

BFS Shortest Path (now with good data


structures)
Dist: 0

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

BFS Shortest Path (now with good data


structures)
Dist: 0

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

BFS Shortest Path (now with good data


structures)
Dist: 0

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0

C A F G
Discovered:

Visited:

D B E H
Dist: 1

Current: A Always keep track of the predecessor nodes.

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2

C A F G
Discovered:

Visited:

D B E H
Dist: 2 Dist: 1 Dist: 2

Current: E Speeding up visualisation…

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2 Dist: 3

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2 Dist: 3

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2 Dist: 3

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2 Dist: 3

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2 Dist: 3

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2 Dist: 3

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2 Dist: 3

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2 Dist: 3

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

BFS Shortest Path (now with good data


structures)
Dist: 1 Dist: 0 Dist: 2 Dist: 3

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

Unweighted Shortest Paths

 Note that distances are stored in an O(1) lookup structure.


 Distances are set by lookup at the distance of the current vertex and adding 1.
 Path from s to v can be found by backtracking from v to s using the array pred.
 Complexity is the same as regular BFS, O(V+E).

FIT2004: Seminar 4 - Introduction to Graphs

Directed Acyclic Graph (DAG)


A Directed Acyclic Graph (DAG) is
 Directed
 Acyclic – has no cycles
 Graph
Which of the two graphs is a DAG?

B D B D

A A

C E C E

Graph 1 Graph 2

FIT2004: Seminar 4 - Introduction to Graphs

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

Topological Sort of a DAG


Order of vertices in a DAG
 A < B if A→B.
 Note that if A → B and B→D, we have A < B and B < D which implies that A < D (i.e., transitivity).
 Some vertices may be incomparable (e.g., B and C are incomparable), i.e. A< B
and A < C but we do not know whether C < B or B < C.

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.

FIT2004: Seminar 4 - Introduction to Graphs

61
22/03/2025

Topological Sort of a DAG


 A DAG can have many valid topological sorts, e.g., let u and v be two incomparable
vertices, u may appear before or after v.
Which of these is NOT a valid topological ordering of the DAG?
1. A, B, C, E, D
2. A, C, B, E, D
3. A, C, E, B, D
4. A, B, E, C, D

B D

C E

FIT2004: Seminar 4 - Introduction to Graphs

Depth First Search (DFS)


Assume we call DFS(A), which of the following is NOT a possible order in which vertices are
marked visited?
1. A, B, D, C, E
2. A, C, E, D, B
3. A, C, D, E, B
4. A, C, E, B, D

B D

C E

FIT2004: Seminar 4 - Introduction to Graphs

62
22/03/2025

DFS for Topological Sort

B D

C E

Not accessed:

DFS not finished yet:

Sorted:

Sorted:

FIT2004: Seminar 4 - Introduction to Graphs

DFS for Topological Sort

B D

C E

Not accessed:

DFS not finished yet:

Sorted:

Sorted:

FIT2004: Seminar 4 - Introduction to Graphs

63
22/03/2025

DFS for Topological Sort

B D

C E

Not accessed:

DFS not finished yet:

Sorted:

Sorted:

FIT2004: Seminar 4 - Introduction to Graphs

DFS for Topological Sort

B D

C E

Not accessed:

DFS not finished yet:

Sorted:

Sorted:

FIT2004: Seminar 4 - Introduction to Graphs

64
22/03/2025

DFS for Topological Sort

B D

C E

Not accessed:

DFS not finished yet:

Sorted:

Sorted: D

FIT2004: Seminar 4 - Introduction to Graphs

DFS for Topological Sort

B D

C E

Not accessed:

DFS not finished yet:

Sorted:

Sorted: B D

FIT2004: Seminar 4 - Introduction to Graphs

65
22/03/2025

DFS for Topological Sort

B D

C E

Not accessed:

DFS not finished yet:

Sorted:

Sorted: B D

FIT2004: Seminar 4 - Introduction to Graphs

DFS for Topological Sort

B D

C E

Not accessed:

DFS not finished yet:

Sorted:

Sorted: B D

FIT2004: Seminar 4 - Introduction to Graphs

66
22/03/2025

DFS for Topological Sort

B D

C E

Not accessed:

DFS not finished yet:

Sorted:

Sorted: E B D

FIT2004: Seminar 4 - Introduction to Graphs

DFS for Topological Sort

B D

C E

Not accessed:

DFS not finished yet:

Sorted:

Sorted: C E B D

FIT2004: Seminar 4 - Introduction to Graphs

67
22/03/2025

DFS for Topological Sort

B D

C E

Not accessed:

DFS not finished yet:

Sorted:

Sorted: A C E B D

FIT2004: Seminar 4 - Introduction to Graphs

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

FIT2004: Seminar 4 - Introduction to Graphs

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

FIT2004: Seminar 4 - Introduction to Graphs

69

You might also like