Introduction to Algorithms, Second Edition

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

View Full Document Right Arrow Icon
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 ).
Image of page 1

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

View Full Document Right 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 ) + 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. Proof: Only if. Let Dfs ( v ) be called during the call to Dfs ( u ). Then, we show by induction on the number of recursive calls made, that there is a path of unmarked vertices from u to v at the time when Dfs ( u ) is called.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern