push (w,r0) onto S where r0 is a reference to the first
element in the adjacency list of w
(or it’s nil if that list is empty)
mark w as visited
5. (30 pts) Recall that a
simple
is a path in which no vertex is repeated. Give the pseudocode of an
algorithm that, given a
digraph
G
and a node
s
returns the length of the longest [TYPO: insert
“simple” here] path that starts at
s
.
[MISTAKE: I should not have said longest simple path but rather the longest among all the shortest
simple paths to other vertices. That’s why you can do it with BFS.]
3
Additional Problems
1. In any connected undirected graph without cycles and with at least two nodes it is possible to remove
a node such that the remaining graph is still connected, true or false?
Answer
TRUE. Such a graph is a tree. After removing a
leaf
(there must be at least one leaf) the
graph remains connected.
2. If we run DFS and BFS on the same undirected connected graph starting at the same node then the
resulting spanning trees will have the same number of edges, true or false?
Answer
TRUE. Let the graph have
n
nodes.
Any
of its spanning trees has
n

1 edges, no matter
how it was obtained, by DFS, BFS, or any other way.
3. There exist undirected graphs with 3 nodes one of which is labeled
s
such that DFS and BFS starting
at
s
produce the same spanning tree, true or false?
Answer
TRUE. Example:
asb
in both cases the spanning tree produced is the graph
itself.
4. Every undirected simple graph (i.e., without selfloops and parallel edges) with 4 nodes has 2 or more
distinct spanning trees, true or false?
Answer
FALSE. If the graph is actually a tree than it has only one spanning tree: itself (no matter
how many nodes there are).
5. Consider the digraph on eight nodes, labelled 0 through 7, with the 13 directed edges
0>1 6>3 6>5 1>6 7>1 7>3
1>4 4>2 6>0 0>7 3>4 5>2 2>3
Suppose we run the directed DFS algorithm on this digraph starting at node 0. We assume that DFS
explores the adjacency list of a node in the
numerical order
of the labels of the nodes.
(a) Draw the rooted tree of discovery edges produced by this algorithm.
Answer
DFS will explore the nodes and edges in the following order:
4
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document0
0>1
1
1>4
4
4>2
2
2>3
3
3>4
1>6
6
6>0
6>3
6>5
5
5>2
0>7
7
7>1
7>3
The nodes and the discovery edges form the following rooted tree
0
/ \
/
\
/
\
1
7
/ \
/
\
4
6
/
/
2
5
/
3
(b) The rooted tree that you obtained can be considered as a binary tree if the children of a node
are ordered in the
numerical order
of their labels. List the nodes of this tree in inorder.
Answer
3 2 4 1 5 6 0 7
(c) Give an example of a backedge found by the DFS algorithm.
Answer
The ﬁrst backedge found by the algorithm is
3>4
. It is a back edge because it links
the node 3 to a node that is an ancestor of 3 in the spanning tree, namely node 4. (FYI, We
call the other edges crossedges. The last crossedge found by the algorithm is
7>3
. When the
graph is directed, like here, we also have forwardedges. There is no forwardedge in this run
of DFS. However, if we also had the edge
4>3
in this graph then it would end up labeled as a
forward edge.)
6. We run the DFS algorithm on a connected simple undirected graph (recall that simple means no
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '09
 TANNEN
 Algorithms, Graph Theory, Data Structures, edges, discovery edges

Click to edit the document details