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
The University of Texas at Austin Department of Computer Sciences Professor Vijaya Ramachandran Depth-frst Search CS357: ALGORITHMS, Spring 2006 1 Depth-frst Search Let G = ( V, E ) be a directed or undirected graph. Given a vertex a V , depth-Frst search identiFes 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 ) f 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 . ±or an analysis of the running time of this algorithm, we note the following: 0. The initialization in the Main Program takes time O ( n ).
Background image of page 1

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

View Full DocumentRight Arrow Icon
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 ) + s 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 ) iF there is a path of unmarked vertices from u to v at the time when Dfs ( u ) is called.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the 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 - The University of Texas at Austin Department...

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