dis03-sol
dis03-sol
Garg
Note: Your TA probably will not cover all the problems. This is totally fine, the discussion worksheets
are deliberately made long so they can serve as a resource you can use to practice, reinforce, and build
upon concepts discussed in lecture, readings, and the homework.
1 Graph Traversal
A
B E C
I D
G H F J
(a) Recall that given a DFS tree, we can classify edges into one of four types:
• Tree edges are edges in the DFS tree,
• Back edges are edges (u, v) not in the DFS tree where v is the ancestor of u in the DFS
tree
• Forward edges are edges (u, v) not in the DFS tree where u is the ancestor of v in the
DFS tree
• Cross edges are edges (u, v) not in the DFS tree where u is not the ancestor of v, nor is v
the ancestor of u.
For the directed graph above, perform DFS starting from vertex A, breaking ties alphabetically.
As you go, label each node with its pre- and post-number, and mark each edge as Tree, Back,
Forward or Cross.
(b) A strongly connected component (SCC) is defined as a subset of vertices in which there exists
a path from each vertex to another vertex. What are the SCCs of the above graph?
(c) Collapse each SCC you found in part (b) into a meta-node, so that you end up with a graph of
the SCC meta-nodes. Draw this graph below, and describe its structure.
Solution:
A61
T C
T
C C B
C 16 11
I13 D10 T
T T T
B T T
G15
14
17
H12 F918 J819
(a)
(b)
{A}, {B}, {E}, {G, H, I}, {C, J, F, D}
CJF D GHI A E B
(c)
(a) If (u, v) is an edge in an undirected graph and during DFS, post(v) < post(u), then u is an
ancestor of v in the DFS tree.
(b) In a directed graph, if there is a path from u to v and pre(u) < pre(v) then u is an ancestor of
v in the DFS tree.
(c) We can modify the SCC algorithm from lecture, so that, the DFS on G is done in decreasing
pre-order of G instead of decreasing post-order of GR . This modified algorithm is also correct.
(d) We can modify the SCC algorithm from lecture so that the first DFS is run on G and the second
DFS is run on GR . This modified algorithm is also correct.
Solution:
(a) True. There are two possible cases: pre(u) < pre(v) < post(v) < post(u) or pre(v) < post(v) <
pre(u) < post(u). In the first case, u is an ancestor of v. In the second case, v was popped off
the stack without looking at u. However, since there is an edge between them and we look at
all neighbors of v, this cannot happen.
(b) False. Consider the following case where we DFS starting from w:
w16
T
B
u32 v45
(c) False. Consider a 2-vertex graph with a single directed edge, i.e. G = (V = {v1 , v2 }, E =
{(v1 , v2 )}). Then if we start DFS from v2 , the vertex with the largest pre-order in G would be
v1 . However, we know that v1 is not the sink SCC and so this modified algorithm would return
the wrong answer.
(d) True. The SCCs of G are the same as the SCCs of GR .
3 Finding Clusters
We are given a directed graph G = (V, E), where V = {1, . . . , n}, i.e. the vertices are integers in the
range 1 to n. For every vertex i we would like to compute the value m(i) defined as follows: m(i) is
the smallest j such from which you can reach vertex i. (As a convention, we assume that i is reachable
from i.)
(a) Show that the values m(1), . . . , m(n) can be computed in O(|V | + |E|) time. Please provide
an algorithm description and runtime analysis; proof of correctness is not required.
5 Biconnected Components
Consider any undirected connected graph G = (V, E). We say that an edge (u, v) ∈ E is critical if
removing it disconnects the graph. In other words, the graph (V, E \ (u, v)) is no longer connected.
Similarly, we call a vertex v ∈ V critical if removing v (and all its incident edges) leaves the graph
disconnected.
(a) Can you always find a vertex v ∈ V that is not critical? What about an edge that is not
critical?
(b) Give a linear time algorithm to find all the critical edges of G.
(c) Modify your algorithm above to find all the critical vertices of G.
(d) A biconnected component of G is a connected subgraph that does not contain critical vertices;
in other words, if you remove a vertex from the component, then it will remain connected. If
we collapse all the biconnected components of G into a meta-node (similar to what we did with
SCCs), what does the resulting graph look like?
Solution:
(a) Consider running DFS from any vertex on the graph, and any leaf in the resulting DFS tree.
The leaf can be removed without disconnecting the graph, since the remaining vertices are
connected using the DFS tree edges. But we can’t always find such an edge. For example, every
edge in a tree is critical.
(b) Perform DFS on G while keeping track of pre[v] for each vertex. We also maintain a low value
for each vertex, where low[v] denotes the smallest pre[u] such that there is a back edge to u
from the subtree of v. The only potential critical edges are the tree edges in the DFS. An edge
between v and its parent p is critical if and only if low[v] > pre[p] (i.e. there does not exist a
back edge from v to p or any of its ancestors).
(c) The root of the DFS tree is critical iff it has more than one child. A non-root vertex v is critical
iff for at least one of its children c, low[c] > pre[v].
(d) The resulting graph is a tree.
To prove this, show that the if there is a cycle in the resulting graph then all the meta nodes
in the cycle will belong to the same biconnected component. This gives a contradiction.