



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




As we traverse the graph in DFS order, we will associate two numbers with each vertex. When we first discover a vertex u, store a counter in d[u]. When we are finished processing a vertex, we store a counter in f[u]. These two numbers are time stamps. Consider the recursive version of depth-first traversal
The DFSVISIT routine is as follows:
Figures 8.21 through 8.25 present a trace of the execution of the time stamping algorithm. Terms like โ2/5โ indicate the value of the counter (time). The number before the โ/โ is the time when a vertex was discovered (colored gray) and the number after the โ/โ is the time when the processing of the vertex finished (colored black).
Figure 8.21: DFS with time stamps: recursive calls initiated at vertex โaโ
Figure 8.22: DFS with time stamps: processing of โbโ and โcโ completed
Figure 8.25: DFS with time stamps: processing of โdโ and โeโ
Notice that the DFS tree structure (actually a collection of trees, or a forest) on the structure of the graph is just the recursion tree, where the edge (u, v) arises when processing vertex u we call DFSVISIT ( V) for some neighbor v. For directed graphs the edges that are not part of the tree (indicated as dashed edges in Figures 8.21 through 8.25) edges of the graph can be classified as follows:
Back edge: (u, v) where v is an ancestor of u in the tree.
Forward edge: (u, v) where v is a proper descendent of u in the tree.
Cross edge: (u, v) where u and v are not ancestor or descendent of one another. In fact, the edge may go between different trees of the forest.
The ancestor and descendent relation can be nicely inferred by the parenthesis lemma. u is a descendent of v if and only if [d[u], f[u]] โ [d[v], f[v]]. u is a ancestor of v if and only if [d[u], f[u]] โ [d[v], f[v]]. u is unrelated to v if and only if [d[u], f[u]] and [d[v], f[v]] are disjoint. The is shown in Figure 8.26. The width of the rectangle associated with a vertex is equal to the time the vertex was discovered till the time the vertex was completely processed (colored black). Imagine an opening parenthesis โ(โ at the start of the rectangle and and closing parenthesis โ)โ at the end of the rectangle. The rectangle (parentheses) for vertex โbโ is completely enclosed by the rectangle for โaโ. Rectangle for โcโ is completely enclosed by vertex โbโ rectangle.
Figure 8.26: Parenthesis lemma
Figure 8.27 shows the classification of the non-tree edges based on the parenthesis lemma. Edges are labeled โFโ, โBโ and โCโ for forward, back and cross edge respectively.
processing u, v was not white (otherwise (u, v) would be a tree edge), implying that v was started before u. Because the intervals are disjoint, v must have also finished before u.
Lemma: Consider a digraph G = (V, E) and any DFS forest for G. G has a cycle if and only if the DFS forest has a back edge.
Proof: If there is a back edge (u, v) then v is an ancestor of u and by following tree edge from v to u, we get a cycle.
We show the contra positive: suppose there are no back edges. By the lemma above, each of the remaining types of edges, tree, forward, and cross all have the property that they go from vertices with higher finishing time to vertices with lower finishing time. Thus along any path, finish times decrease monotonically, implying there can be no cycle.
The DFS forest in Figure 8.27 has a back edge from vertex โgโ to vertex โaโ. The cycle is โa-g-fโ. Beware : No back edges means no cycles. But you should not infer that there is some simple relationship between the number of back edges and the number of cycles. For example, a DFS tree may only have a single back edge, and there may anywhere from one up to an exponential number of simple cycles in the graph. A similar theorem applies to undirected graphs, and is not hard to prove.