DependencyGraph: Don't choke on recursive import

* Don't choke on recursive import.
* Fixed #853.
* Added unit test.