Skip to content

graphs: fix the DFS-based initialization for digraphs with multiple edges - #42838

Merged
vbraun merged 3 commits into
sagemath:developfrom
ProfTR55:dfs-init-multiple-edges
Sep 29, 2026
Merged

vbraun merged 3 commits into
sagemath:developfrom
ProfTR55:dfs-init-multiple-edges

Conversation

@ProfTR55

Copy link
Copy Markdown
Contributor

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_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. That array 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. The DFS
therefore 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 (MemoryError raised
from fundamental_cycle_step), which is why the initialization was disabled
there.

The change

Test my_edge_state directly:

if self.my_edge_state[e_id] == self.UNUSED:

This is what the reference implementation of the authors does, it is
direction-correct by construction, and it lets T, its allocation, its
initialisation and its maintenance in re_init all go. The diff is 26 lines
added 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 T also removes two passes over m
per call to re_init, which makes it faster than before on large instances:

n=100000   m=500000     685.9ms -> 600.0ms
n=250000   m=1250000   2392.7ms -> 2090.3ms

On simple digraphs the initialization keeps paying, from 1.16x on
RandomGNP(200, 0.40) to 2.64x on rome99 with lambda=16. The exception is
complete digraphs, where it loses about 10%.

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have created tests covering the changes.
  • I have updated the documentation accordingly.

…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.
@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Documentation preview for this PR (built with commit 771ad39; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

@dcoudert dcoudert left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Thanks !

vbraun pushed a commit to vbraun/sage that referenced this pull request Sep 29, 2026
Release Manager
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
@vbraun
vbraun merged commit 0f62b74 into sagemath:develop Sep 29, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants