Repository navigation
Mehlorn Steiner Tree - #8052
Mehlorn Steiner Tree#8052
Conversation
amcandio
left a comment
There was a problem hiding this comment.
Can you add a unit test that showcases the bug?
rossbar
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
This should be:
| distances, paths = nx.multi_source_dijkstra(G, terminal_nodes) | |
| distances, paths = nx.multi_source_dijkstra(G, terminal_nodes, weight=weight) |
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 |
|
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 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 Be careful here though. The graphs created ( |
|
I have edited the necessary |
dschult
left a comment
There was a problem hiding this comment.
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!
|
Now the tests run with a |
|
|
||
| assert list(G) == [4, 5, 6] # only the terminal nodes are left | ||
|
|
||
| def test_weighted(self): |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
LGTM, thanks @Hadrien-Cr !
| ), ( | ||
| f"S edges, {S_no_weights.edges(data=True)} G edges, {G.edges(data=True)}" | ||
| ) |
There was a problem hiding this comment.
Just a nit, but I'd prefer to leave off the specialized assertion messages. Not a blocker though!
dschult
left a comment
There was a problem hiding this comment.
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.
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