Repository navigation
Steinertree kou enhancement in response to issue 5889 type:Enhancements - #7767
Conversation
dschult
left a comment
There was a problem hiding this comment.
Nice! Thanks for this!
It looks like we still loop over all the nodes in G in the inner loop (ignoring most of them). Could we loop over terminal_nodes on line 98 instead of paths? That allows removing the if for terminal_nodes too.
Also, it seems a shame to do is_connected() which involves a traversal when we are going to do a traversal right away anyway. What do you think about putting something like this inside the outer loop:
distances, paths = nx.single_source_dijkstra(G, source=u, weight=weight)
if len(G) != len(paths):
raise nx.NetworkXError("G is not a connected graph.")That replaces a traversal with a bunch of length comparisons.
Finally, can you replace the comparison in metric_closure to check lengths instead of doing a set subtraction? That is, replace line 34 if Gnodes - set(distance):, ... with something like: if len(G) != len(distance):
Push back if any of this doesn't sound good to you.
|
@dschult Thank you for your thorough and insightful review! You are correct, we can loop over terminal nodes and make code more efficient and concise! |
dschult
left a comment
There was a problem hiding this comment.
Thanks! Looks good to me!
I have one minor suggestion.
And I approve this PR!
| # Compute the metric closure only for terminal nodes | ||
| # Create a complete graph H from the metric edges | ||
| H = nx.Graph() | ||
| unvisited_terminals = set(terminal_nodes).copy() |
There was a problem hiding this comment.
The set function here actually copies the terminal nodes already. So no .copy() needed here.
|
Amazing, thank you! This was a great experience! Just a side question, should the documentation be updated to show the real time complexity of Kou? O(|S|(|E|+|V|log|V|)), instead of O(|S||V|^2) |
|
That's a good question. Also, O(|S|(|E|+|V|log|V|)) seems to also be O(|S||V|^2)... At least when |E| ~= |V|^2. So what are the constraints on these asymptotic statements? Are we restricting to sparse networks? OK... my rant is over... go ahead and put in the docstring what the complexity is. :) |
|
@dschult, you asked really good questions and I went to explore them. I was surprised to find out that networkx is implemented in Python, and not in C/C++ with interface in Python like numpy and pandas does. I was also surprised than the Dijkstra implementation is the O(V^2) one. In this case, the time complexity of Kou in the documentation is correct, but it is Mehlhorn one that is wrong, since in the paper they are using Dijkstra with time complexity O(E+VlogV). I guess, instead of changing the docstring it will be better to impement Fredman and Tarjan's shortest path somewhere in the library and then put it in Mehlhorn's implementation. I will probably create a separate issue and PR for it. Thank you so much! |
|
Be careful with Fibonacci heaps :) The original paper admits that they are often slower even though they are asymptotically faster. And that has been our experience as well. I'm not saying you shouldn't do this -- but you should not just dive into it without a plan for how to overcome the troubles involved. :) Theoretical complexity measures are not always indicators of the best way to do something. They are valid in a limit. And we don't live in a limit. To paraphrase a great economist (Keynes) who was countering works that considered only the long term impacts on the economy, "The long run is a misleading guide to current affairs. In the long run we are all dead". |
MridulS
left a comment
There was a problem hiding this comment.
Thanks for the pull request @xavieronassis, for the documentation changes I think you can send in a new pull request. Let's merge this in :)
In follow up to issue #5889 I implemented a backward compatible enhancement by only changing the _kou_steiner_tree function in steinertree.py. So far it passed all tests and shows significant speed up.
This is my first ever time using GitHub, I am not sure if I am contributing correctly.
There is also a possibe improvement in the for loop on line 98, where it currently adds edges (u, v) and (v, u), although one may argue that it adds a generalization to directed graphs.
PS: It may also be considered to change the documentation of steinertree to reflect that time complexity of Kou algorithm is O(|S|(|E|+|V|log|V|)), the reason why in original paper the time complexity was shown as O(|S||V|^2) is because back when Kou's paper was published the more efficient Dijkstra implementation was not known.