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,
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