I added tests, and moved method to internal package so that it will not become API.

With tests ready, I studied what else can be done for the algorithm, and finally I decided to make it a bit more complex for two profits: 1. It does not modify the passed set any more. 2. It is faster for trees, and correct dependencies are trees. 

Improvement in performance on wildfly workspace is 30%, but for new tests with larger workspace size it is much greater.