A non-recursive implementation of DFS with worst-case space complexity , with the possibility of duplicate vertices on the stack:Page 93, Algorithm Design, Kleinberg and Tardos procedure DFS_iterative(G, v) is let S be a stack S.push(v) while S is not empty do v = S.pop() if v is not labeled as discovered then label v as discovered for all edges from v to w in G.adjacentEdges(v) do if w is not labeled as discovered then S.push(w) alt=An undirected graph with edges AB, BD, BF, FE, AC, CG, AE|right|200x200px|The example graph, copied from above These two variations of DFS visit the neighbors of each vertex in the opposite order from each other: the first neighbor of v visited by the recursive variation is the first one in the list of adjacent edges, while in the iterative variation the first visited neighbor is the last one in the list of adjacent edges. The recursive implementation will visit the nodes from the example graph in the following order: A, B, D, F, E, C, G. The non-recursive implementation will visit the nodes as: A, E, F, B, D, C, G.
A non-recursive implementation of DFS with worst-case space complexity  with the possibility of duplicate vertices on the stack:Page 93, Algorithm Design, Kleinberg and Tardos procedure DFS_iterative(G, v) is let S be a stack S.push(v) while S is not empty do v = S.pop() if v is not labeled as discovered then label v as discovered for all edges from v to w in G.adjacentEdges(v) do if w is not labeled as discovered then S.push(w) alt=An undirected graph with edges AB, BD, BF, FE, AC, CG, AE|right|200x200px|The example graph, copied from above These two variations of DFS visit the neighbors of each vertex in the opposite order from each other: the first neighbor of v visited by the recursive variation is the first one in the list of adjacent edges, while in the iterative variation the first visited neighbor is the last one in the list of adjac,ent edges. The recursive implementation will visit the nodes from the example graph in the following order: A, B, D, F, E, C, G. The non-recursive implementation will visit the nodes as: A, E, F, B, D, C, G.