Skip to content

Added code to handle multi-graph in mst - #7454

Merged
dschult merged 7 commits into
networkx:mainfrom
Aditya-Shandilya1182:main
Jul 9, 2024
Merged

dschult merged 7 commits into
networkx:mainfrom
Aditya-Shandilya1182:main

Conversation

@Aditya-Shandilya1182

Copy link
Copy Markdown
Contributor

No description provided.

@Aditya-Shandilya1182

Copy link
Copy Markdown
Contributor Author

This code is written to address issue #7451.

@Aditya-Shandilya1182

Copy link
Copy Markdown
Contributor Author

Code used to test the feature:

for g in nx.SpanningTreeIterator(nx.cycle_graph(3,create_using=nx.MultiGraph)):
    print(g.edges())

Result before changes:
fli

Result after changes:
mst

@mjschwenne

Copy link
Copy Markdown
Contributor

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.

@Aditya-Shandilya1182

Copy link
Copy Markdown
Contributor Author

I will try to add the test cases.

Comment on lines +1150 to +1153
edges = (
G.edges(keys=True, data=True) if G.is_multigraph() else G.edges(data=True)
)
for *e, d in edges:

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.

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.

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.

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

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)?

G : nx.Graph
The directed graph which we need to iterate trees over

Comment thread networkx/algorithms/tree/mst.py Resolved
Comment thread networkx/algorithms/tree/mst.py Resolved
mjschwenne and others added 2 commits July 8, 2024 20:53
Co-authored-by: Gilles Peiffer <[email protected]>
Co-authored-by: Gilles Peiffer <[email protected]>

@Peiffap Peiffap 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 should fix the test failures.

Comment thread networkx/algorithms/tree/mst.py Outdated
Comment on lines +1133 to +1135
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)

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.

Whoops, that's my bad.

Suggested change
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)

Comment on lines +1142 to +1144
edges = (
G.edges(keys=True, data=True) if G.is_multigraph() else G.edges(data=True)
)

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 we're here, I think this looks cleaner. Feel free to disregard.

Suggested change
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)

@mjschwenne

Copy link
Copy Markdown
Contributor

I've added a pair of tests which mirror the ones for the simple graphs. It highlighted a bug with Erik's suggestion since *e returns a list and not a tuple which can't be used as a dictionary key.

I think that this is ready to merge now!

@Peiffap

Peiffap commented Jul 8, 2024

Copy link
Copy Markdown
Member

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

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

LGTM!

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

6 participants