Tutorial
Question 1
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. Prove that in a breadth-first search
of a directed graph, the following properties hold:
- There are no forward edges.
- For each tree edge
, we have
.
- For each cross edge
, we have
.
- For each back edge
, we have
.
Background: Breadth First Search
Breadth First Search starts at a source
. Each vertex,
, has a
distance,
, from
. Intuitively,
. If the graph is
not a connected graph (there is a path from each vertex to every other
vertex), then vertices not reachable from
will have an infinite
distance from
. That is,
, where
is not reachable
from
.
Figure 1: Example BFS
Tree. Bold directed edges indicate tree edges that point from
parent to child. Numbers indicate the distance from source
.
def bfs(V, adj, s):
for each u in V:
color[u] = WHITE
d[u] = INFINITY
p[u] = NIL
color[s] = GRAY
d[s] = 0
Q = {s}
while Q is not empty:
u = head of Q
for each v in adj[u]:
if color[v] == WHITE:
color[v] = GRAY
d[v] = d[u] + 1
p[v] = u
append v to end of Q
remove u from head of Q
color[u] = BLACK
return (d, p)
Background: Edge Classification
- Tree Edge - A tree edge is an edge that is in the output tree of
some graph search algorithm, such as the breadth first search.
-
Forward Edge - An edge not in the output tree that connects a
vertex to a descendent in the output tree.
-
Back Edge - An edge of the original graph that connects a vertex
to its ancestor in the output tree.
-
Cross Edge - All other edges. Edges not in the output tree that is
neither a forward edge nor a back edge.
Answer
- There are no forward edges.
For a forward edge
to exist,
must not be in
the BFS tree and
is an ancestor of
in the BFS tree. Thus,
must hold. That is, there must be at least one
vertex in the predecessor path from
to
. Otherwise, it would
imply that
was in the BFS tree and would not be
considered a forward edge.
Figure 2: For a forward edge
to exist, the path from
to
in the BFS tree must contain at least one intermediate
node. Thus the following condition must hold
.
Statement 1: When
is the head of the GRAY queue of
discovered vertices, then it is made the ancestor of all adjacent
vertices that are WHITE. Each is also made GRAY and added to the
end of the queue of discovered vertices. For
to exist
in the graph but not be included in the BFS tree,
could not have
been WHITE when
was the head of the queue. Which means that
was either BLACK or GRAY.
Statement 2: If
was BLACK, then
has been visited
before
. Thus,
. So
cannot be an ancestor
of
.
Statement 3: If
was GRAY, then
has been discovered
before
became the head of the queue. In fact,
is in the
queue behind
when
was the head of the queue. Any new
vertices discovered while
is the head of the queue will have a
distance of
. But since
was in the queue before any of
these newly discovered nodes,
.
Thus, whether
was BLACK or GRAY when
was the head of the
queue, none of the conditions allow forward edges to exist. Proving
that there are no forward edges in a BFS tree.
- For each tree edge
, we have
.
Following the BFS tree construction, if
is a tree edge,
then
, because
is a parent of
in the BFS
tree.
- For each cross edge
, we have
.
From statement 3, any edge
excluded from the BFS tree
must have
.
- For each back edge
, we have
.
If
is a back edge, then
must be an ancestor of
.
So
. Since
may very well be the source
,
.
Question 2
There are two well-known algorithms for finding
minimum spanning trees. Point out the difference between Prim's
algorithm and Kruskal's algorithm in terms of the construction of the
MST tree.
The construction of an MST using Prim's algorithm begins from
a single vertex. From here, the tree is expanded until it covers all
the vertices in the graph. In other words, there is only one partial
tree during the construction of the MST from the very beginning to the
end. However, Kruskal's algorithm starts from a forest of
single vertex trees. At each step, it merges two trees in the forest,
continuing until there is only one tree left; the MST.
Question 3
Assume that the weight assigned to each edge in graph
is distinct. Let
be a maximum-weighted edge on a
cycle of
. Show that a minimum spanning tree in
is also a minimum spanning tree in
.
Let
be a minimal spanning tree of
. We claim that
is also a minimal spanning tree of
. The only difference
between
and
is that
has the additional edge
. Thus,
our claim will only be wrong if the minimal spanning tree of
contains the edge
. Let
be such a tree. Removing
from
disconnects the tree into two subtrees, one
containing
and the other containing
. If there exist an edge
of lower weight than
that connects the two subtrees, then
was
not a MST!
Since
is part of a cycle, then by including
in
, at least
one of the other edges in the cycle must have been excluded from
(because there are no cycles in trees). And we know that any edge of
the cycle not used in
has a lower weight than
(because the
weights are distinct). In other words, there exist an edge of lower
weight than
that connects the two subtrees. Thus, any tree
containing
cannot be a MST of
. Removing
from
consideration, gives us
. Hence, any MST of
is also a MST of
.
Question 4
Given a directed weighted graph
with source
, under what circumstances should the Bellman-Ford algorithm be
used, instead of Dijkstra's algorithm, to solve the single-source
shortest paths problem with source
.
When there exist a negative edge in
; because Dijkstra's
algorithm doesn't work for graphs with negative edges.
Question 5
Given a directed weighted graph
, let
be the distance in
from
to
in terms of the
minimum number of edges used in the path,
and
.
Define the diameter
of
as
. What is the time complexity for finding all
pairs of shortest paths in
if the repeated squaring approach is
applied?
Background: Matrix Multiplication for Graphs
Using the graph depicted in Figure 3, let
be a
weighted adjacency matrix of the graph
. Let the matrix
contain the all pairs shortest paths of
of at most
edges.
Figure 3: Example Graph
|
Multiplying
will give us
. This
is the shortest path between any pair of vertices using at
most two edges.
|
Thus, in order to find all pairs of shortest paths in
, we need to
find
; because that would give us all pairs shortest paths
of
of at most
edges. Hence, we calculate:
, where
.
With repeated squaring, we only need to calculate:
|
where
.
Each multiplication is
time. Since the number of
multiplication is
, the runtime of repeated squaring is
. If
is given, then the runtime is
.
File translated from
TEX
by
TTM,
version 3.67.
On 4 Apr 2006, 15:41.