MAD 5305 Graph Theory, Spring 2012
CRN: 21139
Classes: PHY 109, MWF 10:4511:35am
Assignment 4
– solution
Question 1
.
(2 points) (a) Describe the breadth frst search (BFS) algorithm when applied to a
connected graph.
(b) Explain how BFS can be used to fnd the components o± a disconnected graph.
(c) The timecomplexity o± BFS when applied to a connected graph with
n
vertices and
m
edges
is
O
(
m
+
n
), where
O
(
x
) denotes a ±unction
f
(
x
) satis±ying
f
(
x
)
≤
Cx
±or some constant
C >
0
and ±or all large enough
x
. Give an explanation o± the term
timecomplexity
, and explain why the
timecomplexity o± BFS is
O
(
m
+
n
).
Solution
. (a) Here is a description o± BFS using a queue
Q
(which is a datastructure):
The Breadth First Search (BFS) algorithm
Input
. A connected graph
G
= (
V, E
), and an initial vertex
u
∈
V
.
Output
. A BFS tree (
V, E
(
T
)) with root
u
so that ±or every vertex
v
, the unique path ±rom
u
to
v
is a shortest path between
u, v
in
G
.
1. (Initialization)
E
(
T
) :=
∅
,
first
:= 1,
last
:= 1,
Q
[
] :=
u
.
Set
visited
[
u
] :=
true
, and
visited
[
v
] :=
false
±or all
v
n
=
u
.
2. (Iteration)
while (
≤
last
) do
(that is, while queue is not empty)
3.
x
:=
Q
[
],
:=
+ 1;
4.
±or all neighbors
y
o±
x
do
5.
i± (
visited
[
y
] =
) then
6.
last
:=
last
+ 1,
Q
[
last
] :=
y
, add
{
x, y
}
to
E
(
T
),
visited
[
y
] :=
true
;
7.
end i±;
8.
end do;
9. end do;
(b) Given a disconnected graph, apply BFS to an aribtrarily chosen vertex
u
, and obtain the set o±
vertices (±rom the spanning tree) o± the component containing
u
. Repeat BFS ±or any unvisited
vertex, and obtain (the vertices ±rom) another component. Oviously this process can be repeated
until all components are ±ound.
(c) The timecomplexity o± an algorithm is the “number o± steps” required by the algorithm in the
worst case
among a set o± instances (inputs) with given parameter(s). For example, in BFS, the
set o± instances is the set o± connected graphs with
n
vertices and
m
edges. In order to fnd the
timecomplexity o± BFS, we see that ±or each execution o± the while loop in line 2, the operations
in line 3 are executed once. Thus line 3 is executed
n
times (since graph is connected, and there
are
n
vertices), and hence at most
C
′
n
opertations, ±or some constant
C
′
, are incurred by line 3.
The ±or loop in line 4 is executed
s
x
∈
V
deg(
x
) = 2
m
times, and each time that the ±or loop is iterated, a constant number o± operations are per±ormed.
Thus, the number o± steps incurred by the ±or loop in line 4 is at most
C
′′
m
±or some constant
C
′′
.
This shows that the total number o± operations required by BFS is at most
C
(
n
+
m
) ±or some
constant
C
. In timecomplexity, we use
O
(
m
+
n
), denoting that the timecomplexity o± BFS is
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Documentlinear
in
m
and
n
(as opposed to square, cubic, exp, or log). (For example, a lineartime algorithm
is obviously better than an exponentialtime algorithm, and the diference is huge when the problem
size is big.)
Question 2
.
(2 points) (a) Exercise 2.3.3. Use Kruskal’s algorithm to ±nd a minimum weight
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '12
 Suen
 Graph Theory, Shortest path problem, minimum weight, G′

Click to edit the document details