Inf recursion fix

This patch adds a test case to reproduce an infinite recursion problem and then fixes the problem.  This is also an optimization to prevent nodes in the dependency graph to be revisited in the traversal algorithm.