Skip to content

Implementing iterative removal of non_terminal_leaves in Steiner Tree approximation - #7422

Merged
dschult merged 15 commits into
networkx:mainfrom
OrionSehn-personal:steiner_tree_remove_nt_leaves
Sep 6, 2024
Merged

dschult merged 15 commits into
networkx:mainfrom
OrionSehn-personal:steiner_tree_remove_nt_leaves

Conversation

@OrionSehn-personal

Copy link
Copy Markdown
Contributor

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.

@OrionSehn-personal OrionSehn-personal changed the title Implementing iterative removal of non_terminal_leaves #6881 Implementing iterative removal of non_terminal_leaves Apr 18, 2024
@OrionSehn-personal OrionSehn-personal changed the title Implementing iterative removal of non_terminal_leaves Implementing iterative removal of non_terminal_leaves in Steiner Tree approximation Apr 18, 2024
@rossbar

rossbar commented Apr 18, 2024

Copy link
Copy Markdown
Contributor

@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 rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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!

@OrionSehn

Copy link
Copy Markdown
Contributor

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!

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

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!

Comment on lines +117 to +118
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])}

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.

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_set

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment on lines +123 to +126
for n in nonterminal_leaves:
possible_nonterminal_leaves = possible_nonterminal_leaves | set(
G.neighbors(n)
)
) - {n}

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.

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_set

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think the previous code was a little easier to read, but I'm sure that purely subjective.

I've implemented your suggestion.

@OrionSehn-personal

Copy link
Copy Markdown
Contributor Author

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!

@OrionSehn

Copy link
Copy Markdown
Contributor

@rossbar

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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!

@OrionSehn

Copy link
Copy Markdown
Contributor

Happy to help! Glad we could get her sorted!

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

Two very minor nits...
I approve this PR!

@dschult
dschult merged commit 44d50eb into networkx:main Sep 6, 2024
@jarrodmillman jarrodmillman added this to the 3.4 milestone Sep 6, 2024
rossbar added a commit to Peiffap/networkx that referenced this pull request Sep 6, 2024
… 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]>
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.

Steiner tree approximation does not iteratively remove nonterminal leaves

5 participants