In this question pseudocode for DFS is supplied:
DFS(source):
s <- new stack
visited <- {} // empty set
s.push(source)
while (s is not empty):
current <- s.pop()
if (current is in visited):
continue
visited.add(current)
// do something with current
for each node v such that (current,v) is an edge:
s.push(v)
However, it has a very glaring detail -- the same node can -- and often will -- be pushed to the stack twice!!!!!!
1
| \
| 2
| /
3
Push 1
Pop 1
Add 1 to visited
Push 2, 3
Pop 2
Add 2 to visited
PUSH 1 TO STACK AGAIN
...
...
Surely, that can't be right??