First page Back Continue Last page Graphics
Iterative DFS
Dfs(v)
{
Stack s = new Stack();
s.push(v);
mark v as visited;
while (!s.isEmpty())
if (s.peek() has no unvisited, adjacent nodes)
s.pop(); // done with node on top
else {
u = select unvisited, adjacent node to s.peek();
s.push(u);
mark u as visited;
}
}