Slight speed improvement.
Pass graph to depth functions to improve efficiency.