Repository navigation
Added code to handle multi-graph in mst - #7454
Conversation
|
This code is written to address issue #7451. |
|
The code itself looks pretty good, but you should definitely add some test cases using multi-graphs to the iterator. Things that are worth testing would be that the number of generated trees is correct, and then that the trees themself are presented in a non-decreasing (and non-increasing for the reverse mode). For a small tests, enumerating the trees explicitly can be a good sanity check. The current tests for the iterator should provide a good roadmap. |
|
I will try to add the test cases. |
| edges = ( | ||
| G.edges(keys=True, data=True) if G.is_multigraph() else G.edges(data=True) | ||
| ) | ||
| for *e, d in edges: |
There was a problem hiding this comment.
I like this pattern! What do you think about using it above when setting d[partition_key]? This way, we wouldn't need separate for loops for if G.is_multigraph() and else.
There was a problem hiding this comment.
It looked neat this way.
Will it affect the performance of the code? I would love to hear your suggestions. I will make sure to change the code accordingly.
Peiffap
left a comment
There was a problem hiding this comment.
Thanks! I like how you've refactored things cleanly. Small suggestion below based on Erik's review.
It'd be nice to have some test cases for this new behavior too. Could you add those?
This isn't new to this PR, but could you also fix the typo Dan mentioned (it should say "undirected" here)?
networkx/networkx/algorithms/tree/mst.py
Lines 1015 to 1016 in 4dca079
Co-authored-by: Gilles Peiffer <[email protected]>
Co-authored-by: Gilles Peiffer <[email protected]>
Peiffap
left a comment
There was a problem hiding this comment.
This should fix the test failures.
| edges = G.edges(keys=True, data=True) if G.is_multigraph() else G.edges(data=True) | ||
| for *e, d in edges: | ||
| d[partition_key] = partition_dict.get(e, EdgePartition.OPEN) |
There was a problem hiding this comment.
Whoops, that's my bad.
| edges = G.edges(keys=True, data=True) if G.is_multigraph() else G.edges(data=True) | |
| for *e, d in edges: | |
| d[partition_key] = partition_dict.get(e, EdgePartition.OPEN) | |
| edges = G.edges(keys=True, data=True) if G.is_multigraph() else G.edges(data=True) | |
| for *e, d in edges: | |
| d[partition_key] = partition_dict.get(tuple(e), EdgePartition.OPEN) |
| edges = ( | ||
| G.edges(keys=True, data=True) if G.is_multigraph() else G.edges(data=True) | ||
| ) |
There was a problem hiding this comment.
While we're here, I think this looks cleaner. Feel free to disregard.
| edges = ( | |
| G.edges(keys=True, data=True) if G.is_multigraph() else G.edges(data=True) | |
| ) | |
| edges = G.edges(keys=True, data=True) if G.is_multigraph() else G.edges(data=True) |
|
I've added a pair of tests which mirror the ones for the simple graphs. It highlighted a bug with Erik's suggestion since I think that this is ready to merge now! |
|
Erik's suggestion was fine, I just didn't think it through/test it locally when I implemented it! Thanks for taking a look at this! |
dschult
left a comment
There was a problem hiding this comment.
This looks good! Thanks @Aditya-Shandilya1182 @mjschwenne and @Peiffap !
Could we revert the switch in the test file for nx.is_tree -> is_tree? The idiom nx.is_tree leaves no doubt where it comes from, and simplifies/stabilizes the import at the top of the file. (The long chain of subpackages is fragile to changes in our codebase organization. It is also not strictly needed, but then again, neither is the import statement as nx is already defined.)
More succinctly, could we switch is_tree back to nx.is_tree and remove the import statement? Thanks...
* Added code to handle multi-graph in mst * Added code to handle multi-graph in mst * Apply Erik's suggested change Co-authored-by: Gilles Peiffer <[email protected]> * remove extra comment Co-authored-by: Gilles Peiffer <[email protected]> * add multigraph tests for SpanningTreeIterator * revert to nx.is_tree --------- Co-authored-by: Maverick18 <[email protected]> Co-authored-by: Matt Schwennesen <[email protected]> Co-authored-by: Gilles Peiffer <[email protected]>


No description provided.