Skip to content

Added example for SpanningTreeIterator to find min spanning trees - #8556

Merged
rossbar merged 20 commits into
networkx:mainfrom
roboGOD:enhancement/7985-mst-iteration
Mar 23, 2026
Merged

rossbar merged 20 commits into
networkx:mainfrom
roboGOD:enhancement/7985-mst-iteration

Conversation

@roboGOD

@roboGOD roboGOD commented Mar 7, 2026 •

Copy link
Copy Markdown
Contributor

As mentioned in #7985, SpanningTreeIterator can be used to find all minimum or maximum spanning trees as the output of the iterator is sorted based on size.

Added example on how to use SpanningTreeIterator to find all minimum spanning trees.

@roboGOD
roboGOD force-pushed the enhancement/7985-mst-iteration branch from 57f16d8 to b08c9a0 Compare March 7, 2026 08:03

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

I'm -1 on expanding the API surface in this way - my vote would be to expand the SpanningTreeIterator docstring examples

roboGOD added 2 commits March 10, 2026 22:15
This reverts commit b364c93.

Revert "Created find_all_optimal_spanning_trees"

This reverts commit 1a116dd.

Revert "Rename to `optimal_spanning_trees`"

This reverts commit 983ff11.

Revert "doc string"

This reverts commit c0dceb0.

Revert "Added tests"

This reverts commit 57f16d8.

Revert "Update mst.py"

This reverts commit b08c9a0.
@roboGOD
roboGOD requested a review from rossbar March 10, 2026 17:03
@roboGOD

roboGOD commented Mar 10, 2026

Copy link
Copy Markdown
Contributor Author

As per the suggestion, removed the helper function and added doc string example.

@roboGOD roboGOD changed the title Created MST helper optimal_spanning_trees using SpanningTreeIterator Added example for SpanningTreeIterator to find min spanning trees Mar 10, 2026
Comment thread networkx/algorithms/tree/mst.py Resolved
Comment thread networkx/algorithms/tree/mst.py Resolved
@roboGOD
roboGOD requested a review from dschult March 13, 2026 17:59

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

I think this is headed in the right direction! Just a couple minor suggestions

Comment thread networkx/algorithms/tree/mst.py Resolved
Comment thread networkx/algorithms/tree/mst.py Resolved
@roboGOD
roboGOD requested a review from rossbar March 14, 2026 16:11

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

Looks good do me. You could use itertools to have a more compact version:

min_spanning_trees = list(next(
    group for _, group in itertools.groupby(
        nx.SpanningTreeIterator(G),
        key=lambda t: t.size(weight="weight")
    )
))

Co-authored-by: Alejandro Candioti <[email protected]>

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

I took the liberty of applying @amcandio 's excellent suggestion and pushing up a few minor wording tweaks. This LGTM, thanks @roboGOD and @dschult and @amcandio for reviewing!

@rossbar
rossbar merged commit 620c37e into networkx:main Mar 23, 2026
38 checks passed
@dschult dschult added this to the 3.7 milestone Mar 23, 2026
mcbosch pushed a commit to mcbosch/networkx that referenced this pull request Apr 7, 2026
…etworkx#8556)

* Created find_all_optimal_spanning_trees

* Rename to `optimal_spanning_trees`

* doc string

* Added example

* Added tests

* Update mst.py

* Revert "Added example"

This reverts commit b364c93.

Revert "Created find_all_optimal_spanning_trees"

This reverts commit 1a116dd.

Revert "Rename to `optimal_spanning_trees`"

This reverts commit 983ff11.

Revert "doc string"

This reverts commit c0dceb0.

Revert "Added tests"

This reverts commit 57f16d8.

Revert "Update mst.py"

This reverts commit b08c9a0.

* Added example for SpanningTreeIterator

* dummy commit

* Fix doc string

* Update mst.py

* Update mst.py

* Update networkx/algorithms/tree/mst.py

Co-authored-by: Ross Barnowski <[email protected]>

* Update networkx/algorithms/tree/mst.py

Co-authored-by: Ross Barnowski <[email protected]>

* Update mst.py

* Single max spanning tree

* Update mst.py

* Use itertools.groupby for example.

Co-authored-by: Alejandro Candioti <[email protected]>

---------

Co-authored-by: Ross Barnowski <[email protected]>
Co-authored-by: Alejandro Candioti <[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.

4 participants