hw07 Solution PDF
hw07 Solution PDF
CSCI-GA.1170-001/Summer 2016
Solution to Homework 7
Problem 1 (CLRS 22.2-2). (1 point) Show the d and values that result from running breadthfirst search on the undirected graph of Figure 22.3, using vertex u as the source.
Solution: Running BFS on the graph of Figure 22.3 gives:
u
r
s
t
u
v
w
x
y
u.d
4
3
1
0
5
2
1
1
u.
s
w
u
null
r
t
u
u
Problem 2 (CLRS 22.2-8). (3 points) The diameter of a tree T = (V, E) is defined as maxu,vV (u, v),
that is, the largest of all shortest-path distances in the tree. Give an efficient algorithm to compute the diameter of a tree, prove the algorithm correct, analyze the running time.
Solution: We first note that in a tree (an acyclic connected graph), any two vertices are connected by exactly one path, and thus (u, v) = d(u, v) for all u and v. Our main tool for finding
shortest paths is BFS. Intuitively, the closer BFS starts to the center of the graph, the more it
can underestimate the diameter. However, if we first find the farthest vertex (a leaf node in the
tree) and run a second BFS from there, the longest of the resulting paths should correspond to
the diameter of the tree.
Assuming the existence of procedure BFS(T, v), performing breadth-first search of tree T from
vertex v and returning the last discovered vertex, the algorithm would run in O(V + E) (same
as BFS) and could be coded as follows:
We first show that d(u, b) = d(a, b). Consider three possible cases:
1. s is not on t u and t is not on s u:
t
b
Then d(t, u) d(s, u). At the same time, d(s, u) d(s, a), as otherwise u would not
be the last vertex discovered. s a includes t a, and so d(s, a) d(t, a). Thus,
d(t, u) d(t, a) and, adding d(b, t) to both sides, d(b, u) d(b, a).
But d(a, b) is a diameter, so d(b, u) d(a, b). Thus, d(u, b) = d(a, b).
3. t is on s u:
a
b
d(t, u) d(t, a), as otherwise u would not be the last vertex discovered. At the same
time, d(t, a) d(t, u), as otherwise diameter d(b, a) < d(b, u). Thus, d(t, u) = d(t, a)
and, adding d(b, t) to both sides, d(u, b) = d(a, b).
d(u, v) d(a, b) because d(a, b) is a diameter, and d(u, v) d(u, b) because otherwise v
would not be the last vertex discovered by the second BFS. Thus, d(u, b) d(u, v) d(a, b).
Recall that we showed d(u, b) = d(a, b). Thus, d(a, b) d(u, v) d(a, b) or d(u, v) = d(a, b).
Problem 3 (CLRS 22.3-2). (1 point) Show how depth-first search works on the graph of
Figure 22.6. Assume that the for loop of lines 5-7 of the DFS procedure considers the vertices
in alphabetical order, and assume that each adjacency list is ordered alphabetically. Show the
discovery and finishing times for each vertex, and show the classification of each edge.
Solution: We know that when DFS first explores an edge (u, v), the color of v and discovery
time of u and v allow us to determine edge type:
White v indicates a tree edge.
Gray v indicates a back edge.
Black v with u.d < v.d indicates a forward edge.
Black v with u.d > v.d indicates a cross edge.
Running DFS on the graph of Figure 22.6 gives the following vertex information:
u
q
r
s
t
u
v
w
x
y
z
u.d
1
17
2
8
18
3
4
9
13
10
u. f
16
20
7
15
19
6
5
12
14
11
u.
null
null
q
q
r
s
v
t
t
x
Type
tree
tree
forward
tree
cross
tree
tree
tree
cross
tree
back
tree
back
back
Problem 4 (CLRS 22.4-1). (1 point) Show the ordering of vertices produced by Topological-Sort
when it is run on the dag of Figure 22.8, under the assumption of Exercise 22.3-2.
Solution: Running DFS on the graph in Figure 22.8 gives:
u
m
n
o
p
q
r
s
t
u
v
w
x
y
z
u.d
1
21
22
27
2
6
23
3
7
10
11
15
9
12
u. f
20
26
25
28
5
19
24
4
8
17
14
16
18
13
u.
null
null
n
null
m
m
o
q
r
y
v
v
r
w
def has_cycle (G ):
for u in G.V:
u. color = white
seen = {}
for u in G.V:
if u. color == white :
4
if has_cycle_visit (G , u ):
return true
return false
def has_cycle_visit (G , u ):
u . color = gray
for v in G. adj [u ]:
if (u , v) not in seen :
seen . add ((u , v ))
seen . add ((v , u ))
if v. color == gray :
return true
if v. color == white :
if has_cycle_visit (G , v ):
return true
u . color = black
return false
Problem 6 (CLRS 22.5-2). (1 point) Show how the procedure Strongly-Connected-Components
works on the graph of Figure 22.6. Specifically, show the finishing times computed in line 1
and the forest produced in line 3. Assume that the loop of lines 5-7 of DFS considers vertices
in alphabetical order and that the adjacency lists are in alphabetical order.
Solution: Running DFS on the graph of Figure 22.6 gives (same as in Problem 3):
u
q
r
s
t
u
v
w
x
y
z
u.d
1
17
2
8
18
3
4
9
13
10
u. f
16
20
7
15
19
6
5
12
14
11
u.
null
null
q
q
r
s
v
t
t
x
u
q
r
s
t
u
v
w
x
y
z
u.d
5
1
15
7
3
17
16
11
6
12
u. f
10
2
20
8
4
18
19
14
9
13
u.
null
null
null
y
null
w
s
null
q
x
Or, graphically:
q
x
z
Problem 7 (CLRS 22-1). (2 points) A depth-first forest classifies the edges of a graph into
tree, back, forward, and cross edges. A breadth-first tree can also be used to classify the edges
reachable from the source of the search into the same four categories.
Solution:
(a) Prove that in a breadth-first search of an undirected graph, the following properties hold:
1. There are no back edges and no forward edges.
A forward edge (u, v), by definition, connects vertex u to a descendant v in the
breadth-first tree. BFS explores all edges of u, including (u, v), before exploring
edges of any descendants of u, so (u, v) must be a tree edge. Analogous argument
(with u and v reversed) can be made for back edges.
2. For each tree edge (u, v) we have v.d = u.d + 1.
(u, v) is a tree edge when BFS sets v. = u. At the same time the algorithm sets
v.d = u.d + 1 and never changes v.d and u.d again.
3. For each cross edge (u, v), we have v.d = u.d or v.d = u.d + 1.
Consider the BFS queue right before dequeuing u. (u, v) being a cross edge indicates
that at this time v has already been enqueued (otherwise (u, v) would be a tree edge)
and thus u, the head of the queue, is coming before v in the queue. If vr is the tail of
the queue, by Lemma 22.3: v.d vr .d u.d + 1 or v.d u.d + 1. At the same time,
u has been enqueued before v, and so by Corollary 22.4: u.d v.d. Combining
both results shows that either v.d = u.d or v.d = u.d + 1.
(b) Prove that in a breadth-first search of a directed graph, the following properties hold:
1. There are no forward edges.
Forward edges occur when DFS encounters edge (u, v), where v has been discovered
after u, but has been explored from a vertex other than u. This cannot happen in
BFS, as all neighbors of u, including v, would have been explored from u, making
(u, v) a tree edge.
2. For each tree edge (u, v), we have v.d = u.d + 1.
(u, v) is a tree edge when BFS sets v. = u. At the same time the algorithm sets
v.d = u.d + 1 and never changes v.d and u.d again.
3. For each cross edge (u, v), we have v.d u.d + 1.
v.d > u.d + 1 would only be possible if the algorithm does not explore edge (u, v)
when visiting u, which cannot happen.
4. For each back edge (u, v), we have 0 v.d u.d.
The distances cannot be negative, so v.d 0 for all vertices v. A back edge (u, v), by
definition, connects vertex u to an ancestor v in the breadth-first tree, and v cannot
be farther from the root than its descendant u.