The DFS Algorithm

DFS simply replaces the queue Q with a stack S:

DFS(G,s)
   for each u ∈ V[G]-{s} do
      open[u] = true
      d[u] = ∞ 
      pred[u] = null
   open[s] = false
   d[s] = 0
   pred[s] = null
   S = {s}
   while S ≠ {} do
      u = Pop[S]
      for each v ∈ Adj[u] do
         if open[v] = true
            then open[v] = false
                 d[v] = d[u] + 1
                 pred[v] = u
                 Push(S,v)