Skip to content

Steinertree kou enhancement in response to issue 5889 type:Enhancements - #7767

Merged
MridulS merged 6 commits into
networkx:mainfrom
xavieronassis:steinertree-kou-enhancement
Jan 19, 2025
Merged

MridulS merged 6 commits into
networkx:mainfrom
xavieronassis:steinertree-kou-enhancement

Conversation

@xavieronassis

@xavieronassis xavieronassis commented Dec 11, 2024 •

Copy link
Copy Markdown
Contributor

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.

@xavieronassis xavieronassis changed the title Steinertree kou enhancement in response to issue 5889 Steinertree kou enhancement in response to issue 5889 type:Enhancements Dec 11, 2024

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@xavieronassis

xavieronassis commented Dec 13, 2024 •

Copy link
Copy Markdown
Contributor Author

@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!
I also removed the is_connected check and added the check in the iteration for the first node, similarly how it was done in metric_closure function. I had to separate the first node iteration from the rest of the terminals, so we don't check if the graph is connected in a loop. I also added tracking of visited terminals, so that we dont count edges (u,v) and (v,u)
twice.
I also replaced the comparison in metric closure as you suggested, your approach is more efficient and more readable.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The set function here actually copies the terminal nodes already. So no .copy() needed here.

@xavieronassis

Copy link
Copy Markdown
Contributor Author

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)

@dschult

dschult commented Dec 16, 2024

Copy link
Copy Markdown
Member

That's a good question.
I think you indicated that the complexity changed since the publication because the complexity of dijkstra changed. Is that correct? If so, is NetworkX using the faster version of Dijkstra? Then -- we have to make sure that we actually implemented the new Kou algorithm as the correct complexity. That is, the publication proved it could have a certain complexity. But did we do that?

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. :)

@xavieronassis

Copy link
Copy Markdown
Contributor Author

@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!

@dschult

dschult commented Dec 17, 2024

Copy link
Copy Markdown
Member

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.
See a python implementation and description of the results
Also see this stack overflow discussion on whether anyone has actually implemented a performant fibonacci heap.

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 MridulS left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

5 participants