Moshe pointed me to http://drupal.org/node/211182 which was dancing between D6 and D7 version.
The patch http://drupal.org/node/220945#comment-738292 uses an - Acyclic Directed Graph to find out the right order to enable/disable/uninstall - Topological Sorted List to do the actual work
(as nifty side effect is being able to create http://drupal.org/files/issues/graph.gif)
Hello, brother! Finally, I am not alone with speaking DAGs. Fine, fine. If you look into _module_build_dependencies in Drupal 6 you will find a fairly naive implementation to determine whether the dependency graph is acyclic or not and then drupal_install_modules has another naive implementation of topological sort. It should use DFS but -- I thought the graph is too small for that. I have DFS based topological sort in weights.php in my sandbox. Let's discuss more. A very happy Karoly Negyesi who is finally not alone with bringing graph theory to Drupal.