Graphs Part 3: Applications of DFS: Application 1 DFS: Topological Sort
Graphs Part 3: Applications of DFS: Application 1 DFS: Topological Sort
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)
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)