Graphs
Part 3: applications of DFS
Application 1 DFS: topological sort:
A directed edge from u to v, means that v happens after u. Topological order: All
directed edges only go forward (needs to be acyclic graph). When we have tasks
that one needs to be done before another. Again linear time in vertices and
edges O(n+m), since doing DFS.
22.4 Topological sort 613
11/16 undershorts socks 17/18
watch 9/10
12/15 pants shoes 13/14
shirt 1/8
(a) 6/7 belt
tie 2/5
jacket 3/4
(b) socks undershorts pants shoes watch shirt belt tie jacket
17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4
Figure 22.7 (a) Professor Bumstead topologically sorts his clothing when getting dressed. Each
directed edge .u; !/ means that garment u must be put on before garment !. The discovery and
finishing times from a depth-first search are shown next to each vertex. (b) The same graph shown
topologically sorted, with its vertices arranged from left to right in order of decreasing finishing time.
All directed edges go from left to right.
pants). A directed edge .u; !/ in the dag of Figure 22.7(a) indicates that garment u
must be donned before garment !. A topological sort of this dag therefore gives an
Pseudo code: Topological Sort (G)
order for getting dressed. Figure 22.7(b) shows the topologically sorted dag as an
ordering of vertices along a horizontal line such that all directed edges go from left
1. Call DFS(G) to compute finish times v.f for each vertex v
to right.
The following simple algorithm topologically sorts a dag:
2. As each vertex is finished, insert into the front of a linked list
3. return the linked list of vertices
T OPOLOGICAL -S ORT .G/
Application 2 DFS: Strongly Connected Components
1 call DFS.G/ to compute finishing times !:f for each vertex !
2 as each vertex is finished, insert it onto the front of a linked list
This is another example application of Depth First Search (DFS).
3 return the linked list of vertices
Figure 22.7(b) shows how the topologically sorted vertices appear in reverse order
Definition: Maximal set of vertices for a directed graph, so that for each pair of
of their finishing times.
vertices, u and v, there is a path both from u to v and from v to u.
We can perform a topological sort in time ‚.V C E/, since depth-first search
takes ‚.V C E/ time and it takes O.1/ time to insert each of the jV j vertices onto
Usefulness: Groups of people or populations that are connected amongst
the front of the linked list.
themselves;; brain regions that are strongly connected;; figuring out if a network
We prove the correctness of this algorithm using the following key lemma char-
such as telephone lines are strongly connected (you can dial anyone in the
acterizing directed acyclic graphs.
network);; Web connectivity.
We will use Depth First Search (DFS), but not just by itself.
616
Consider panel (a) in the figure below (we will look at panels b and c afterwards):
Chapter 22 Elementary Graph Algorithms
a b c d
13/14 11/16 1/10 8/9
(a)
12/15 3/4 2/7 5/6
e f g h
a b c d
(b)
e f g h
cd
(c) abe
fg h
Figure 22.9 (a) A directed graph G. Each shaded region is a strongly connected component of G.
Each vertex is labeled with its discovery and finishing times in a depth-first search, and tree edges
are shaded. (b) The graph G T , the transpose of G, with the depth-first forest computed in line 3
of S TRONGLY-C ONNECTED -C OMPONENTS shown and tree edges shaded. Each strongly connected
component corresponds to one depth-first tree. Vertices b, c, g, and h, which are heavily shaded, are
the roots of the depth-first trees produced by the depth-first search of G T . (c) The acyclic component
graph G SCC obtained by contracting all edges within each strongly connected component of G so
that only a single vertex remains in each component.
Our algorithm for finding strongly connected components of a graph G D
Each cluster of strongly connected components is in gray. For example, there is
.V; E/ uses the transpose of G, which we defined in Exercise 22.1-3 to be the
a path from node c to d, and from node d to c. There is also a path from any
graph G T D .V; E T /, where E T D f.u; !/ W .!; u/ 2 Eg. That is, E T consists of
node in a,b,e to other nodes in that cluster. If we started DFS from node c, we
the edges of G with their directions reversed. Given an adjacency-list representa-
could discover strongly connected component cluster c and d. But if we started
tion of G, the time to create G T is O.V C E/. It is interesting to observe that G
the DFS search from node a, we will actually discover all the nodes of this graph,
and G T have exactly the same strongly connected components: u and ! are reach-
and not an individual strongly connected component cluster (because b has an
able from each other in G if and only if they are reachable from each other in G T .
arrow out of the cluster going to c, so c would be discovered too, although it is
Figure 22.9(b) shows the transpose of the graph in Figure 22.9(a), with the strongly
not strongly connected with a,b,e).
connected components shaded.
Solution: We need to call BFS from the right places to find the strongly connected
components!
Algorithm:
1. Call DFS on graph G to compute finish times u.f for each vertex u in graph G
(this is step a in the figure above)
2. Compute the transpose of graph G: G T
This is the graph G, but with all edge directions reversed (see panel b in figure
above). Note that G and its transpose
have the same strongly connected
component clusters.
3. Call DFS on the transpose graph, but in the DFS loop consider vertices in
decreasing order of finish time from the DFS we ran on G (starting from highest
finish time). This process is also shown in panel b. Since we are considering
vertices in decreasing order of finish times from the DFS in panel (a), we first
consider vertex b and perform DFS in panel (b) on the transposed graph. We find
strongly connected components b,a,e in this way. From this cluster of 3 vertices,
there are no are no more vertices going out in the transpose graph. So as in
DFS, we choose a new starting node, and again take the largest remaining finish
time from panel (a) and therefore start in panel (b) from node c. We find nodes c
and d in this way. Again, there are no more nodes going out of the cluster. We
start from g, and find strongly connected component cluster g,f. Finally, we also
start from vertex h and find only the vertex itself as the last strongly connected
component (with itself).
4. Panel c shows the resulting strongly connected components, with each cluster
written out as its own vertex. This also shows the idea of strongly connected
components. Each cluster has a path between all nodes in the cluster, but
between clusters there is only a single arrow (otherwise if there were arrows
going both ways, both clusters would form a larger strongly connected
component). You could also see that the strongly connected components of
graph G and its transpose are the same (just the arrow between strongly
connected components is reversed in the transpose graph).
Why it works: The main intuition is that the finish times in the original DFS on
graph G, give us the proper starting points for our second DFS, such that we
each time discover a strongly connected component cluster and there are no
arrows to other undiscovered clusters. For example, the first cluster b,a,e in
panel (b), had no out going arrows from these vertices. The second cluster c,d
only had out going arrows to the cluster that was already discovered. And so on.
There is a lemma and proof in the book, which we did not go over.