0% found this document useful (0 votes)
189 views

Introduction To Algorithms, Cormen Et Al, Chap22 Solutions

This document contains solutions to exercises from a chapter on elementary graph algorithms. The first solution shows that the betweenness centrality of vertices i and j in a graph equals the degree of i if i=j, and the number of edges connecting i and j if i≠j. The second solution explains that the correctness of breadth-first search does not depend on the order of adjacency lists. The third solution modifies depth-first search to assign connected component labels to vertices.

Uploaded by

eab
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)
189 views

Introduction To Algorithms, Cormen Et Al, Chap22 Solutions

This document contains solutions to exercises from a chapter on elementary graph algorithms. The first solution shows that the betweenness centrality of vertices i and j in a graph equals the degree of i if i=j, and the number of edges connecting i and j if i≠j. The second solution explains that the correctness of breadth-first search does not depend on the order of adjacency lists. The third solution modifies depth-first search to assign connected component labels to vertices.

Uploaded by

eab
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/ 3

Selected Solutions for Chapter 22:

Elementary Graph Algorithms

Solution to Exercise 22.1-7


BB T .i; j / D

X
e2E

T
D
bie bej

bie bje

e2E

If i D j , then bi e bje D 1 (it is 1  1 or . 1/  . 1/) whenever e enters or leaves


vertex i , and 0 otherwise.
If i j , then bi e bje D 1 when e D .i; j / or e D .j; i/, and 0 otherwise.

Thus,
BB T .i; j / D

degree of i D in-degree C out-degree if i D j ;


.# of edges connecting i and j /
if i j :

Solution to Exercise 22.2-5


The correctness proof for the BFS algorithm shows that u:d D .s; u/, and the
algorithm doesnt assume that the adjacency lists are in any particular order.
In Figure 22.3, if t precedes x in Adjw, we can get the breadth-first tree shown
in the figure. But if x precedes t in Adjw and u precedes y in Adjx, we can get
edge .x; u/ in the breadth-first tree.

Solution to Exercise 22.3-12


The following pseudocode modifies the DFS and DFS-V ISIT procedures to assign
values to the cc attributes of vertices.

22-2

Selected Solutions for Chapter 22: Elementary Graph Algorithms

DFS.G/
for each vertex u 2 G:V
u:color D WHITE
u: D NIL
time D 0
counter D 0
for each vertex u 2 G:V
if u:color == WHITE
counter D counter C 1
DFS-V ISIT.G; u; counter/
DFS-V ISIT.G; u; counter/
u:cc D counter
// label the vertex
time D time C 1
u:d D time
u:color D GRAY
for each  2 G:Adju
if :color == WHITE
: D u
DFS-V ISIT.G; ; counter/
u:color D BLACK
time D time C 1
u:f D time
This DFS increments a counter each time DFS-V ISIT is called to grow a new tree
in the DFS forest. Every vertex visited (and added to the tree) by DFS-V ISIT is
labeled with that same counter value. Thus u:cc D :cc if and only if u and  are
visited in the same call to DFS-V ISIT from DFS, and the final value of the counter
is the number of calls that were made to DFS-V ISIT by DFS. Also, since every
vertex is visited eventually, every vertex is labeled.
Thus all we need to show is that the vertices visited by each call to DFS-V ISIT
from DFS are exactly the vertices in one connected component of G.


All vertices in a connected component are visited by one call to DFS-V ISIT
from DFS:
Let u be the first vertex in component C visited by DFS-V ISIT. Since a vertex
becomes non-white only when it is visited, all vertices in C are white when
DFS-V ISIT is called for u. Thus, by the white-path theorem, all vertices in C
become descendants of u in the forest, which means that all vertices in C are
visited (by recursive calls to DFS-V ISIT) before DFS-V ISIT returns to DFS.
All vertices visited by one call to DFS-V ISIT from DFS are in the same connected component:
If two vertices are visited in the same call to DFS-V ISIT from DFS, they are in
the same connected component, because vertices are visited only by following
paths in G (by following edges found in adjacency lists, starting from some
vertex).

Selected Solutions for Chapter 22: Elementary Graph Algorithms

22-3

Solution to Exercise 22.4-3


An undirected graph is acyclic (i.e., a forest) if and only if a DFS yields no back
edges.



If theres a back edge, theres a cycle.


If theres no back edge, then by Theorem 22.10, there are only tree edges.
Hence, the graph is acyclic.

Thus, we can run DFS: if we find a back edge, theres a cycle.




Time: O.V /. (Not O.V C E/!)


If we ever see jV j distinct edges, we must have seen a back edge because (by
Theorem B.2 on p. 1174) in an acyclic (undirected) forest, jEj  jV j 1.

Solution to Problem 22-1


a. 1. Suppose .u; / is a back edge or a forward edge in a BFS of an undirected
graph. Then one of u and , say u, is a proper ancestor of the other () in
the breadth-first tree. Since we explore all edges of u before exploring any
edges of any of us descendants, we must explore the edge .u; / at the time
we explore u. But then .u; / must be a tree edge.
2. In BFS, an edge .u; / is a tree edge when we set : D u. But we only
do so when we set :d D u:d C 1. Since neither u:d nor :d ever changes
thereafter, we have :d D u:d C 1 when BFS completes.
3. Consider a cross edge .u; / where, without loss of generality, u is visited
before . At the time we visit u, vertex  must already be on the queue, for
otherwise .u; / would be a tree edge. Because  is on the queue, we have
:d  u:d C 1 by Lemma 22.3. By Corollary 22.4, we have :d  u:d.
Thus, either :d D u:d or :d D u:d C 1.
b. 1. Suppose .u; / is a forward edge. Then we would have explored it while
visiting u, and it would have been a tree edge.
2. Same as for undirected graphs.
3. For any edge .u; /, whether or not its a cross edge, we cannot have
:d > u:d C 1, since we visit  at the latest when we explore edge .u; /.
Thus, :d  u:d C 1.
4. Clearly, :d  0 for all vertices . For a back edge .u; /,  is an ancestor
of u in the breadth-first tree, which means that :d  u:d. (Note that since
self-loops are considered to be back edges, we could have u D .)

You might also like