Introduction to Algorithms, Second Edition

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The University of Texas at Austin Depth-first Search Department of Computer Sciences Professor Vijaya Ramachandran Lectures 23-24 CS357: ALGORITHMS, Spring 2006 1 Depth-first Search Let G = ( V, E ) be a directed or undirected graph. Given a vertex a V , depth-first search identifies all vertices v such that either v = a or there is a path from a to v in G . It does so by setting mark ( v ) = T for all such vertices, where initially, mark ( v ) = F for all vertices. (Note that mark = F and mark = T correspond to colors white and gray, respectively, in the pseudocode in the textbook. The color black is not really needed in the code, it is used only to facilitate understanding the algorithm.) Dfs ( v ) 1. mark ( v ) := T 2. for each vertex w Adj [ v ] do 3. if mark ( w ) = F then 4. ( w ) := v 5. Dfs ( w ) fi rof ( color ( v ) := black ) end { procedure Dfs } Main Program for each u V do mark ( u ) := F ( u ) := NIL rof Dfs ( a ) end { Main Program } Bound on Running Time Let | V | = n and | E | = m . For an analysis of the running time of this algorithm, we note the following: 0. The initialization in the Main Program takes time O ( n ). 1. For each vertex v V , dfs ( v ) is called at most once. The reason for this is that (i) a call to dfs ( v ) is made only only if mark ( v ) = F , (ii) mark ( v ) is set to T upon execution of dfs ( v ), and (iii) mark ( v ) is never re-set to F . 2. The time taken by the call dfs ( v ), outside of the time taken by recursive calls within dfs ( v ) is 1 + O ( deg + ( v )) (the out-degree of v ), since the only computation outside of the recursive calls is to set mark ( v ) to T and to check if mark ( w ) = F for each edge ( v, w ) that is outgoing from v . By 0, 1, and 2, the running time of this algorithm is O ( n ) + summationdisplay v V (1 + O ( deg + ( v )) = O ( n ) + n + O ( m ) = O ( m + n ) In deriving the above expression, we used the fact that the sum of the out-degrees of the vertices in G is m . The above analysis also shows that the algorithm terminates. Correctness. In the following, we will say that a vertex v is unmarked if mark ( v ) = F , and it is marked if mark ( v ) = T . Note that unmarked vertices are white vertices and marked vertices are vertices that are either gray or black according to the pseudocode in the textbook. Theorem 1.1 Consider a call to Dfs ( u ) invoked during the execution of the main program. We claim that this call to Dfs ( u ) makes a recursive call to Dfs ( v ) iff there is a path of unmarked vertices from u to v at the time when Dfs ( u ) is called....
View Full Document

This note was uploaded on 01/30/2008 for the course CS 357 taught by Professor Ramachandran during the Spring '06 term at University of Texas at Austin.

Page1 / 6

Lecture 23-24 - The University of Texas at Austin...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online