scan_utils.py: Make traversal routines remember visited nodes

As discussed on [theano-dev](https://groups.google.com/forum/?fromgroups=#!topic/theano-dev/yMJMZxGpd4Y), a particular graph I was working with took extraordinarily long to be traversed within scan_utils.py:traverse and scan_utils.py:forced_replace.traverse -- so long that I actually never saw it finish. The reason was that large parts of the graph were reachable via multiple paths and thus where traversed again and again, without producing any new findings. The attached pull request makes the traversal routines visit each node at most once, cutting down the computation time from >80 minutes to 2 seconds in my case without changing the result.