mod2
mod2
April 5, 2023
For any vertex v reachable from s, the path in the breadth-first tree
from s to v corresponds to a ”shortest path” from s to v in G, that is,
a path containing the smallest number of edges.
The algorithm discovers all vertices at distance k from s before
discovering any vertices at distance k + 1.
The algorithm works on both directed and undirected graphs.
Lines 1-3 paint all vertices white and initialize their π fields to NIL.
Line 4 resets the global time counter.
Lines 5-7 check each vertex in V in turn and when a white vertex is
found, visit it using DFS-VISIT.
Every time DFS-VISIT(u) is called in line 7, vertex u becomes the
root of a new tree in the depth-first forest.
When DFS returns, every vertex u has been assigned a discovery time
u.d and a finishing time u.f.
The results of depth-first search may depend upon the order in which
the vertices are examined in line 5 of DFS, and upon the order in
which the neighbors of a vertex are visited in line 4 of DFS-VISIT.
The loops on lines 1-3 and lines 5-7 of DFS take time θ(V ), exclusive
of the time to execute the calls to DFS-VISIT.
By aggregate analysis, the procedure DFS-VISIT is called exactly
once for each vertex v ∈ V , since DFS-VISIT is invoked only on white
vertices and the first thing it does is paint the vertex gray. During an
execution of DFS-VISIT(v), the loop on lines 4-7 is executed |Adj[v ]|
times.
Since the total cost of executing lines 4-7 of DFS-VISIT is θ(E ).
The running time of DFS is therefore θ(V + E ).
Show how depth-first search works on the following graph. Assume that
the DFS procedure considers the vertices in alphabetical order, and assume
that each adjacency list is ordered alphabetically. Show the discovery and
finishing times for each vertex, and show the classification of each edge.
Also show the parenthesis structure of this depth-first search.
The tree edges are: (q, s), (s, v), (v, w), (q, t), (t, x), (x, z), (t, y), (r, u).
The back edges are: (w, s), (y, q), (z, x).
The forward edge is: (q, w).
The cross edges are: (u, y), (r, y).
The parentheses structure of the DFS of is (((())()))(()()).
Application of DFS
It is performed in directed acyclic graph(dag)
A topological sort of a graph can be viewed as an ordering of its
vertices along a horizontal line so that all directed edges go from left
to right.
Topological sorting is thus different from the usual kind of sorting.
(b) The same graph shown topologically sorted. Its vertices are arranged
from left to right in order of decreasing finishing time. Note that all
directed edges go from left to right.
TOPOLOGICAL-SORT(G)
STRONGLY-CONNECTED-COMPONENTS (G)
2 compute G T
3 call DFS(G T ), but in the main loop of DFS, consider the vertices in
order of decreasing u.f (as computed in line 1)
4 output the vertices of each tree in the depth-first forest formed in line 3
as a separate strongly connected component
The idea behind this algorithm comes from a key property of the
component graph G SCC = (V SCC , E SCC ), which we define as follows.