Depth First Search (DFS)

This is the DFS algorithm that we discussed in the class.  It consists of two
procedures. DFS takes a graph as a parameter and makes sure that all nodes
are visited. DFS_visit(v) is the recursive core of the algorithm.

Complexity: O(N^2) for an adjacency  matrix representation
                      O(N+E) for an adjacency list representation.

We did not discuss time in the class.

    DFS(G)

        ·       Initialize:        Color each vertex white.
                       For all v  inV,
                               c[v]  = white                :color  - white: unvisited,

                                                                        grey: visited but has unvisited children,
                                                                        black: visited with visited children


                              parent[v] = none                  :parent

                time =0;
 
        ·     For each vertex v
               If color[v] == white
               Then
                         DFS_VISIT(v)
               End if
 
 

DFS_VISIT(v)

        ·       Initialize:
                              c[v] = grey
                              time++
                               t[v] = time     // the time you start visitng the node
        ·       For each w adjacent to v
               If color[w] == white
               Then
                         parent[w] = u
                         DFS_VISIT(w)
               End if

               color[v] = black
               time++
               f[u] = time    // time when you are done visiting the node
        End while