Repository navigation
graphs: fix the DFS-based initialization for digraphs with multiple edges - #42838
Merged
Merged
Conversation
…dges
[GKLP2021] initializes the forest T_k of the k-th iteration to a DFS
spanning forest of G \ T, that is, of the subgraph induced by the unused
edges. The implementation tested a boolean array T instead, which is only
ever given a value other than False in re_init, and always as
T[j] = (my_edge_state[j] != UNUSED)
so it holds nothing my_edge_state does not already hold. my_edge_state is
one of the arrays construct_trees swaps per direction; T was not, so the
DFS filtered on a copy made while exploring the other direction. On
digraphs with multiple edges that could return a wrong value, and on some
instances send the augmentation into an allocation loop, which is why the
initialization was disabled there.
Test my_edge_state directly. That is what the reference implementation
does, it is direction-correct by construction, and T, its allocation, its
initialisation and its maintenance in re_init can all go. The DFS is now
enabled for digraphs with multiple edges, where it is worth about 1.2x to
1.4x, and dropping two passes over m per call to re_init makes it faster
than before on large instances as well.
|
Documentation preview for this PR (built with commit 771ad39; changes) is ready! 🎉 |
This was referenced Sep 24, 2026
dcoudert
approved these changes
Sep 26, 2026
vbraun
pushed a commit
to vbraun/sage
that referenced
this pull request
Sep 29, 2026
sagemathgh-42846: graphs: pack arborescences beyond the edge connectivity `GabowEdgeConnectivity.edge_disjoint_spanning_trees` raises `EmptySetError` on digraphs that do contain the requested packing. Since sagemath#42633, `DiGraph.edge_disjoint_spanning_trees` uses this backend by default, so the public method is affected as well: ``` sage: DiGraph([(0, 1), (1, 2)]).edge_disjoint_spanning_trees(1, root=0) Traceback (most recent call last): ... EmptySetError: this digraph does not contain 1 edge-disjoint spanning arborescences ``` The path itself is a spanning arborescence rooted at 0. ``` sage: D = digraphs.Complete(5) sage: D.add_edges([(i, 5) for i in range(4)] + [(5, 0)]) sage: D.edge_disjoint_spanning_trees(4, root=0) Traceback (most recent call last): ... EmptySetError: this digraph does not contain 4 edge-disjoint spanning arborescences rooted at 0 ``` The complete digraph on `{0, ..., 4}` packs 4 arborescences rooted at 0, and each one can take a distinct arc `(i, 5)`. There are two causes: - The per-tree data structures were sized by `max_ec`, the smallest indegree or outdegree over all vertices, and any `k > max_ec` was rejected. By Edmonds' branching theorem, the maximum number of arborescences rooted at `r` is the minimum `r`-cut. It is bounded only by the outdegree of `r` and the indegrees of the other vertices, so it can exceed `max_ec`: in the second example, vertex 5 has outdegree 1. - `__init__` returned before building the data structures when the digraph is not strongly connected, whereas a packing only requires every vertex to be reachable from the root. This PR: - sizes the per-tree data structures by the largest rooted degree bound over all roots, computed in linear time; - builds the data structures for every nonempty digraph. The edge connectivity of a digraph that is not strongly connected is still 0, and `k=None` still returns that many arborescences; - checks the rooted degree bound and the reachability from the root before packing; - adds doctests for both examples, and a randomized doctest checking that the packing size is exactly `min_v len(D.edge_disjoint_paths(r, v))` on random digraphs that need not be strongly connected. The output is unchanged on the inputs that were already supported. Besides the doctests, run with several random seeds, the change was checked on 1500 random digraphs, including multigraphs, digraphs with loops and digraphs that are not strongly connected. For every root, the packing reaches the minimum `r`-cut and one more arborescence is rejected, and the packings are identical to those of the previous code whenever `k` is at most the edge connectivity. Side note: `edge_disjoint_paths(s, t)` raises a `ValueError` instead of returning `[]` when `t` is not reachable from `s`, so the randomized doctest calls it only when every vertex is reachable from the root. This can be fixed separately. ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies - sagemath#42838 URL: sagemath#42846 Reported by: Muhammet Doğukan Bingöl Reviewer(s): David Coudert, Muhammet Doğukan Bingöl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the DFS-based initialization of [GKLP2021] for digraphs with multiple
edges, for which it was currently disabled, and removes the corresponding item
from the module's
TODO.What was wrong
[GKLP2021] initializes the forest
T_kof the k-th iteration to a DFS spanningforest of
G \ T, that is, of the subgraph induced by the unused edges. Theimplementation tested a boolean array
Tinstead. That array is only ever givena value other than
Falseinre_init, and always asso it holds nothing
my_edge_statedoes not already hold.my_edge_stateisone of the arrays
construct_treesswaps per direction;Twas not. The DFStherefore filtered on a copy made while exploring the other direction.
On digraphs with multiple edges this could return a wrong value, and on some
instances send the augmentation into an allocation loop (
MemoryErrorraisedfrom
fundamental_cycle_step), which is why the initialization was disabledthere.
The change
Test
my_edge_statedirectly:This is what the reference implementation of the authors does, it is
direction-correct by construction, and it lets
T, its allocation, itsinitialisation and its maintenance in
re_initall go. The diff is 26 linesadded and 24 removed, but 17 of the additions are a doctest, so the code itself
shrinks by 15 lines.
Effect
The initialization is now enabled for digraphs with multiple edges, where it is
worth about 1.2x to 1.4x. Not maintaining
Talso removes two passes overmper call to
re_init, which makes it faster than before on large instances:On simple digraphs the initialization keeps paying, from 1.16x on
RandomGNP(200, 0.40)to 2.64x onrome99withlambda=16. The exception iscomplete digraphs, where it loses about 10%.
📝 Checklist