This preview shows pages 1–2. Sign up to view the full content.
CSE450/598 Design And Analysis of Algorithms
HW02 Grading Keys
Please note that you have to typeset your assignment using either L
A
T
E
X or Microsoft
Word. Handwritten assignment will not be graded. Also, it is recommended that you
save a copy of your assignment on
general.asu.edu
.
1. (10 pts) Exercise 3.2 (p.107)
Solution:
We can run either BFS or DFS on
G
to compute all connected components of
G
in
O
(
m
+
n
)
time.
G
does not contain a cycle if and only if none of its connected components contains a
cycle. Therefore we may assume that the graph is connected as we can work with each of the
connected components.
Starting from any node, we run BFS to compute a BFS tree
T
. If
T
contains all edges in
G
,
G
does not contain a cycle. Otherwise, there must be an edge (
u, v
) in
G
that is not used by
T
. We can ±nd the smallest common ancestor of
u
and
v
in
T
in linear time (If
u
and
v
are
on the same layer, we back o² one layer from both nodes at each step. If
u
has a higher layer,
we ±rst ±nd the ancestor of
u
which is on the same layer as
v
, then back o².).
Let it be
w
.
Then the
w
–
u
path, the edge (
u, v
), and the
v
–
w
path form a cycle of
G
.
It takes
O
(
m
+
n
) time to compute all connected components of
G
.
For each connected
component of
G
, our algorithm requires time proportional to the summation of the number
of nodes and number of edges in that component. Therefore the algorithm has
O
(
m
+
n
) time
complexity.
Grading Keys:
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '08
 GuoliangXue
 Algorithms

Click to edit the document details