TCP2101 Algorithm Design and Analysis Lab05 - Graphs
TCP2101 Algorithm Design and Analysis Lab05 - Graphs
Lab 5 Graphs
Learning Outcome
Understand how to represent a graph using adjacency matrix and adjacency list.
Understand how Depth-First Search works.
Understand how Breadth-First Search works.
Understand how Topological SorFng works.
1. (10 min) The following undirected graph is provided. Undirected graph is considered bi-direcFonal.
Write down its adjacency matrix representaFon and adjacency list representaFon.
A
5
9
3
B 7 D E
1 4
8
C
B
90 18 &
71848
T
D 3 0 40 0
E 50808
2. Performing DFS on a connected graph produces a spanning tree. Use the graph in quesFon 1 to
answer the following quesFons.
(a) (20 min) There are 4 possible spanning trees when starFng DFS from A. Draw all of them.
⑰
⑰
⑰_ND
⑬ _d
⑰ ⑬
-
⑬
-
⑬
1
DFS
I D ⑰-D
⑬ _.
B
&
Start from
⑬ A
B
⑯A
eg-
-
C
⑬ E
-
⑰- BFS
③ DFS ->
Le
B:A, 9+2, 1
2:A, F + B, 1t b, 4 + E,8
!
A, 3 - C,4
A, 5 -
2,8
(b) (20 min) There are 6 possible spanning trees when starFng DFS from B. Draw all of them.
-
~ -
-g
3. Performing BFS on a connected graph also produces a spanning tree. Use the graph in quesFon 1 to
answer the following quesFons.
(a) (10 min) There is only one possible spanning tree if we start BFS from A. Draw it.
-
(b) (20 min) There are 2 possible spanning trees when starFng BFS from B. Draw all of them.
·
A: wigE,z In
Hoi
Hi
2
4. (10 min) The following directed acyclic graph (DAG) is provided.
(a) Write down its adjacency matrix representaFon and adjacency list representaFon.
A
5
9
3
B 7 D E
1 4
8
C
B & ⑧ 0 & 8
&400
E
⑧
80800
(b) Is the answer you obtained here the same as the one from QuesFon 1?
5. (20 min) Topological SorFng for a graph allows us to Wnd the sequence that saFsWes the precedence
constraints. Find the Topological SorFng for the graph in QuesFon 4. There are 2 possible answers.
wo ise
BADEC BAEDC
6. What is the Fme complexity for DFS, BFS, and Topological SorFng? What is the data structure that
must be used in order to achieve the Fme complexity you menFoned?
007 35
A
8
0
1
B 90
008
100
D & &4&8
= - 0808
A: 1 1 0 vertex
=
3
-
5.
B: O
12
-
1 1
=
-
1 0
=
1 3
2:4
=
-
=
1 0
D:1
-
=
E: 1 - 1 0
=