Here is the standard pseudocode for breadth first search:
{ seen(x) is false for all x at this point }
push(q, x0)
seen(x0) := true
while (!empty(q))
x := pop(q)
visit(x)
for each y reachable from x by one edge
if not seen(y)
push(q, y)
seen(y) := true
Here push
and pop
are assumed to be queue operations. But what if they are stack operations? Does the resulting algorithm visit vertices in depth-first order?
If you voted for the comment "this is trivial", I'd ask you to explain why it is trivial. I find the problem quite tricky.
pop
to a stack or a queue operation, we get dfs or bfs. It's also easy to write pseudo-code for which it at first appears that this is true, but it isn't. ics.uci.edu//~eppstein/161/960215.html is a relevant reference. $\endgroup$