Skip to content

Mehlorn Steiner Tree - #8052

Merged
dschult merged 4 commits into
networkx:mainfrom
Hadrien-Cr:main
Sep 9, 2025
Merged

dschult merged 4 commits into
networkx:mainfrom
Hadrien-Cr:main

Conversation

@Hadrien-Cr

@Hadrien-Cr Hadrien-Cr commented May 18, 2025 •

Copy link
Copy Markdown
Contributor

Hi, I noticed a mismatch between the paper pseudo code and the code.

At the first step of the algo the papers :
Construct the complete distance graph G₁ =(V, E1, d1), where V₁= S and, for every (Vi,Vj) in E, d₁(Vi,Vj) is equal to the distance
of a shortest path from Vi to Vj in G.

The previous implementation mistook the distance for the length of the path (number of nodes in the path). So I changed this to the distances provided by the call to nx.multi_source_dijkstra

Capture d'écran 2025-05-18 195908

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

Can you add a unit test that showcases the bug?

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

Doing so would change behavior for weighted graphs. When edges have a "weight" attribute, it is used in calculating the distances - I don't think that is the intention here, so this strikes me (without any real expertise on the Mehlhorn algorithm) as incorrect.

If the motivation here is to represent distance as the number-of-hops-between-nodes instead of the length of the node sequence, then I don't think the change is necessary. This scenario strikes me as a distinction without a difference - so long as the path representation is self-consistent.


def _mehlhorn_steiner_tree(G, terminal_nodes, weight):
paths = nx.multi_source_dijkstra_path(G, terminal_nodes)
distances, paths = nx.multi_source_dijkstra(G, terminal_nodes)

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 should be:

Suggested change
distances, paths = nx.multi_source_dijkstra(G, terminal_nodes)
distances, paths = nx.multi_source_dijkstra(G, terminal_nodes, weight=weight)

@amcandio

Copy link
Copy Markdown
Contributor

Doing so would change behavior for weighted graphs. When edges have a "weight" attribute, it is used in calculating the distances - I don't think that is the intention here, so this strikes me (without any real expertise on the Mehlhorn algorithm) as incorrect.

If the motivation here is to represent distance as the number-of-hops-between-nodes instead of the length of the node sequence, then I don't think the change is necessary. This scenario strikes me as a distinction without a difference - so long as the path representation is self-consistent.

I think the PR change makes sense and it's consistent with other methods. Current implementation seems to focus only on the unweighted case. If you check MST method calls, weight attribute is never propagated.

@Hadrien-Cr can you also propagate weight keyword to the minimum spanning tree computations? Adding unit tests here would help to catch these

@dschult

dschult commented May 19, 2025 •

Copy link
Copy Markdown
Member

This is another case where we have to be careful with the definitions! The screenshot from the paper does not tell us whether they are using distance to mean "number of hops" or "sum of weights".

It is pretty clear that the original code was only made to work for unweighted graphs. It uses len(path..) to be the distance, and it doesn't include a weight keyword when calling minimum_spanning_tree. The discussion from the original PR suggests that the weight parameter was an after-thought--but I didn't look hard at that. But a look at the papers shows that the algorithms were intended to be used with weighted edges.

So I think we should consider this as a bug in the weighted steiner tree problem. While the unweighted case is presumably OK, the case with weights needs to be considered more carefully. We will need to make all the distance measurements and all the minimum_spanning_tree calls use the edge weights.

Be careful here though.

The graphs created (G1 in mehlhorn and M in Kou) have named the edge attributes "weight" and "distance" respectively. So we should not simply call the minimum_spanning _tree function with weight=weight. The value of the weight keyword has to match the distance measure stored in the graph being acted on.

@Hadrien-Cr

Hadrien-Cr commented May 19, 2025 •

Copy link
Copy Markdown
Contributor Author

I have edited the necessary mst calls and added a test with unweighted and weighted steiner tree and 2 different expected results for respective inputs. let me know if everything is nx-idiomatic

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

This looks good! Thanks!

Do we need any changes to the kou method? A brief look makes me think it already handled the weighted graph while melhorn didn't. But I do see a place where it subscripts weight instead of using get(weight, 1). Perhaps we should include a weightless edge in one of the tests to make sure that works.

And, the new test uses the name "weight" for the edge weight, so perhaps it'd be more thorough testing to name it something other than "distance" or "weight" -- like "my_weight" or "test_wt" or whatever you want. The idea being to test that we actually use a non-default edge weight attribute when requested.

Your test with G4 works with the kou method right? The name says mehlorn. But I think it does both.

Maybe its enough to rename the new test test_weighted and make edge (2,4) have no weight and name the edge weights some non-default name. And you might have to adjust the kou method to make that pass. I think that would do it for me.

Thanks!

@Hadrien-Cr

Hadrien-Cr commented Jun 3, 2025 •

Copy link
Copy Markdown
Contributor Author

Now the tests run with a "my_weight" keyword for weights. for both methods, the weights are taken into account and not overwritten but the call (the output still use the "my_weight" keyword.
no_weights_expected_edges = [ (0, 2, {"my_weight": 2}), (2, 3, {"my_weight": 1}), (2, 4, {}), ]
with_weights_expected_edges = [ (0, 1, {"my_weight": 0.1}), (1, 2, {"my_weight": 0.1}), (2, 3, {"my_weight": 1}), (2, 4, {}), ]
I renamed to test_weighted. I added the missing .get(weight, 1) G4.add_edge(2, 3, my_weight=1) G4.add_edge(2, 4)


assert list(G) == [4, 5, 6] # only the terminal nodes are left

def test_weighted(self):

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.

Nit: You can factor these out into parametrized tests to avoid having two very similar loops. And then you can parametrize by method and get rid of the loop entirely.

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

LGTM, thanks @Hadrien-Cr !

Comment on lines +226 to +228
), (
f"S edges, {S_no_weights.edges(data=True)} G edges, {G.edges(data=True)}"
)

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.

Just a nit, but I'd prefer to leave off the specialized assertion messages. Not a blocker though!

@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 approve this PR. There are a couple of "nit" comments about the testing routines. So we can address those in a follow-up if desired.

@dschult
dschult merged commit 06a9961 into networkx:main Sep 9, 2025
@dschult dschult added this to the 3.6 milestone Sep 9, 2025
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