Repository navigation
Implementing iterative removal of non_terminal_leaves in Steiner Tree approximation - #7422
Conversation
|
@OrionSehn there is already a PR open for this issue at #7136 with a fair bit of discussion - perhaps you could take a look at that one and weigh in there? |
rossbar
left a comment
There was a problem hiding this comment.
This approach makes sense to me and the test adequately demonstrates the problem with the current implementation (i.e. non-terminal nodes along a path become terminal with subsequent removals).
There are potential "gotchas" related to this approach implicitly assuming that leaf nodes have degree one. This may not always be true: two cases I can think of are 1) graphs with self-loop edges (see example) and 2) MultGraphs with multi-edges.
# Desired behavior
>>> G = nx.path_graph(10)
>>> _remove_nonterminal_nodes(G, [4, 5, 6])
>>> list(G)
[4, 5, 6]
>>> H = nx.path_graph(10)
>>> H.add_edge(9, 9) # self-loop on one terminal node
>>> _remove_nonterminal_edges(G, [4, 5, 6])
>>> list(G)
[4, 5, 6, 7, 8, 9]Since this function is only to be used in the context of the steiner_tree approximations, these may be fine - but it's worth double-checking how steiner_tree handles these cases to make sure the _remove_nonterminal_leaves gives consistent results for these cases!
improve test so that there are multiple non-terminal nodes on both ends of the path Co-authored-by: Ross Barnowski <[email protected]>
…minal leaves improvement
Hey @rossbar, I was able to handle both of these cases that you've brought up! It does end up being a little slower than what I had written in April, but does handle both the self looped case and the multigraph case as expected. I've added a few extra tests to make it clear that this behaves successfully. Hopefully its sensible enough how this works just by looking at it. Thank you so much for your very thoughtful comments and contributions. Lemme know if there's other things we should consider. |
dschult
left a comment
There was a problem hiding this comment.
I have some nit-picky suggestions -- but I think they might help performance and readability. I think the last part forming the nonterminal_leaves can use similar treatment too.
Thanks for this!
| self_looped_nodes = {n for n in G if n in G[n] and len(set(G[n])) == 2} | ||
| degree_1_not_looped = {n for n in G if (G.degree(n) == 1) and (n not in G[n])} |
There was a problem hiding this comment.
Why are we computing these two types of nodes as two separate sets instead of creating a single set that is either? We don't seem to use the separated sets anywhere.
Also, self_looped_nodes does not find all nodes that have self_loops. It seems like it is finding the selfloop_nodes that would be degree 1 if they did not have the self-loop. Maybe we can simplify the logic using something like:
leaves = {n for n, nbrs in G.adjacency if len(nbrs) - (n in nbrs) == 1}
nonterminal_leaves = leaves - terminal_setThere was a problem hiding this comment.
Hmmm yes, I like what you're thinking, only, I don't want to have to iterate through the whole graph on each iteration, because if you have a large graph (like what I hope to use this for in the future) this will start to get expensive quick. I don't want to pay O(k*n), where k is undetermined, for something that's providing a very small improvement on an approximation.
My idea is to leverage the fact that we know that if we remove a leaf, that we only have to check that leaf's neighbors for if they should be removed as well, to inform us to iterate only through those. That's what the possible_nonterminal_leaves is supposed to be doing.
I totally understand what you're saying though saying, self_looped_nodes isn't explaining what the cases are, and my approach was a little over-complicated. I've simplified the leaf calculating step significantly, inspired by your suggestion, while still only iterating through the possibly newly created leaves.
Thanks very much for your comments, you've given me some good ideas.
| for n in nonterminal_leaves: | ||
| possible_nonterminal_leaves = possible_nonterminal_leaves | set( | ||
| G.neighbors(n) | ||
| ) | ||
| ) - {n} |
There was a problem hiding this comment.
While I know the existing code makes the code a little ugly, while we're here can we collapse this to something like:
nbrs = ([nbr for nbr in G.neighbors(n) if n != nbr] for n in nonterminal_leaves)
possible_nonterminal_leaves = set().union(*nbrs) - terminals_setThere was a problem hiding this comment.
I think the previous code was a little easier to read, but I'm sure that purely subjective.
I've implemented your suggestion.
|
I've revised my amendment thanks to dan's very good suggestions, thank you guys very much for taking the time to take a look at this. Please do continue to give any further suggestions if you can think of improvements! |
Co-authored-by: Ross Barnowski <[email protected]>
Co-authored-by: Ross Barnowski <[email protected]>
|
Thank you for your review, your code is much much more readable. I added an extra few words to clarify the problem this resolves slightly further. I also fixed a super minor style issue with your submission Thanks for all your help on this. Orion |
rossbar
left a comment
There was a problem hiding this comment.
Thanks @OrionSehn - I took the liberty of pushing up one last style nit, but this LGTM! Thanks for sticking with it through all the discussion!
|
Happy to help! Glad we could get her sorted! |
dschult
left a comment
There was a problem hiding this comment.
Two very minor nits...
I approve this PR!
… approximation (networkx#7422) * linting * Update networkx/algorithms/approximation/tests/test_steinertree.py improve test so that there are multiple non-terminal nodes on both ends of the path Co-authored-by: Ross Barnowski <[email protected]> * handled self loop and multigraph cases for steiner tree remove nonterminal leaves improvement * simplifying removal of non-terminal edges * Revert "simplifying removal of non-terminal edges" This reverts commit 2a40328. * simplified leaf assertation * Update networkx/algorithms/approximation/steinertree.py Co-authored-by: Ross Barnowski <[email protected]> * Update networkx/algorithms/approximation/steinertree.py Co-authored-by: Ross Barnowski <[email protected]> * fix for linting, slightly more explaination added * Style nit, use add_edges_from. * two minor nits --------- Co-authored-by: Orion Sehn <[email protected]> Co-authored-by: Orion Sehn <[email protected]> Co-authored-by: Ross Barnowski <[email protected]> Co-authored-by: Ross Barnowski <[email protected]> Co-authored-by: Dan Schult <[email protected]>
This should resolve #6881
Might not be the fastest possible solution, it checks the neighbors of non-terminal leaves, and removes them in bunches of sets using set comprehension and differences.
I added a relatively simple test to ensure that this function acts as intended, and trims down any non-terminal leaves, but any consequential non-terminal leaves created by that trimming.
This might slow down the approximation significantly however, I didn't do speed testing on this, and I'm not sure if we should consider adding an optional flag in the Steiner tree approximation to perform this additional optimization.