Skip to content

graphs: pack arborescences beyond the edge connectivity - #42846

Merged
vbraun merged 6 commits into
sagemath:developfrom
ProfTR55:fix-gabow-arborescence-packing
Sep 29, 2026
Merged

vbraun merged 6 commits into
sagemath:developfrom
ProfTR55:fix-gabow-arborescence-packing

Conversation

@ProfTR55

@ProfTR55 ProfTR55 commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

GabowEdgeConnectivity.edge_disjoint_spanning_trees raises EmptySetError on digraphs that do contain the requested packing. Since #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

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies

…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 24, 2026 •

Copy link
Copy Markdown

Documentation preview for this PR (built with commit 1b96595; 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.

Why not building this PR based on #42838 ?

Comment thread src/sage/graphs/edge_connectivity.pyx Resolved
@ProfTR55

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I noticed this bug only later, while running some research experiments with the arborescence packing, and it did not occur to me to put the fix on the same branch as #42838. I can rebase this PR on top of #42838, or move the commit there and close this one, whichever you prefer.

@dcoudert

Copy link
Copy Markdown
Collaborator

I think it should be on top of #42838 to ensure that all improvements/corrections are consistent

GabowEdgeConnectivity.edge_disjoint_spanning_trees raised EmptySetError
on valid inputs, and so did DiGraph.edge_disjoint_spanning_trees, which
uses it by default since sagemathgh-42633:

- when k exceeded the smallest indegree or outdegree over all vertices,
  as the per-tree data structures were sized by this bound;
- when the digraph was not strongly connected, as __init__ returned
  before building the data structures.

By Edmonds' branching theorem, the maximum number of arborescences rooted
at r is the minimum r-cut, which is bounded by the outdegree of r and the
indegrees of the other vertices only, and which requires every vertex to
be reachable from r, not strong connectivity.

The per-tree data structures are now sized by the largest such bound over
all roots, the data structures are built for every nonempty digraph, and
the packing checks the rooted degree bound and the reachability from the
root. The output is unchanged on the inputs that were already supported.
@ProfTR55
ProfTR55 force-pushed the fix-gabow-arborescence-packing branch from d6c0667 to 95c3448 Compare September 25, 2026 14:06
@ProfTR55

Copy link
Copy Markdown
Contributor Author

Rebased on top of #42838, which is now listed as a dependency.

@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
vbraun merged commit c43fc26 into sagemath:develop Sep 29, 2026
25 of 26 checks passed
vbraun pushed a commit to vbraun/sage that referenced this pull request Oct 4, 2026
Release Manager
sagemathgh-42903: graphs: Return no path in edge_disjoint_paths when the target is unreachable
    
`edge_disjoint_paths(s, t)` raises a misleading `ValueError` when `t` is
not reachable from `s`:

```
sage: DiGraph([(0, 1)]).edge_disjoint_paths(1, 0)
Traceback (most recent call last):
...
ValueError: vertex '1' is not in the (di)graph
```

Vertex 1 is in the digraph, and the expected answer is `[]`. The flow
graph returned by `flow(..., value_only=False)` only contains the edges
carrying flow, so when the flow is 0 it does not contain `s`, and
`shortest_path(s, t)` raises.

This PR returns `[]` when the flow is 0, as `vertex_disjoint_paths`
already does, and adds doctests for a digraph and for a disconnected
graph. This was noticed in sagemath#42846.

### 📝 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

None.
    
URL: sagemath#42903
Reported by: Muhammet Doğukan Bingöl
Reviewer(s): David Coudert
vbraun pushed a commit to vbraun/sage that referenced this pull request Oct 6, 2026
Release Manager
sagemathgh-42903: graphs: Return no path in edge_disjoint_paths when the target is unreachable
    
`edge_disjoint_paths(s, t)` raises a misleading `ValueError` when `t` is
not reachable from `s`:

```
sage: DiGraph([(0, 1)]).edge_disjoint_paths(1, 0)
Traceback (most recent call last):
...
ValueError: vertex '1' is not in the (di)graph
```

Vertex 1 is in the digraph, and the expected answer is `[]`. The flow
graph returned by `flow(..., value_only=False)` only contains the edges
carrying flow, so when the flow is 0 it does not contain `s`, and
`shortest_path(s, t)` raises.

This PR returns `[]` when the flow is 0, as `vertex_disjoint_paths`
already does, and adds doctests for a digraph and for a disconnected
graph. This was noticed in sagemath#42846.

### 📝 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

None.
    
URL: sagemath#42903
Reported by: Muhammet Doğukan Bingöl
Reviewer(s): David Coudert
vbraun pushed a commit to vbraun/sage that referenced this pull request Oct 8, 2026
Release Manager
sagemathgh-42903: graphs: Return no path in edge_disjoint_paths when the target is unreachable
    
`edge_disjoint_paths(s, t)` raises a misleading `ValueError` when `t` is
not reachable from `s`:

```
sage: DiGraph([(0, 1)]).edge_disjoint_paths(1, 0)
Traceback (most recent call last):
...
ValueError: vertex '1' is not in the (di)graph
```

Vertex 1 is in the digraph, and the expected answer is `[]`. The flow
graph returned by `flow(..., value_only=False)` only contains the edges
carrying flow, so when the flow is 0 it does not contain `s`, and
`shortest_path(s, t)` raises.

This PR returns `[]` when the flow is 0, as `vertex_disjoint_paths`
already does, and adds doctests for a digraph and for a disconnected
graph. This was noticed in sagemath#42846.

### 📝 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

None.
    
URL: sagemath#42903
Reported by: Muhammet Doğukan Bingöl
Reviewer(s): David Coudert
vbraun pushed a commit to vbraun/sage that referenced this pull request Oct 11, 2026
Release Manager
sagemathgh-42903: graphs: Return no path in edge_disjoint_paths when the target is unreachable
    
`edge_disjoint_paths(s, t)` raises a misleading `ValueError` when `t` is
not reachable from `s`:

```
sage: DiGraph([(0, 1)]).edge_disjoint_paths(1, 0)
Traceback (most recent call last):
...
ValueError: vertex '1' is not in the (di)graph
```

Vertex 1 is in the digraph, and the expected answer is `[]`. The flow
graph returned by `flow(..., value_only=False)` only contains the edges
carrying flow, so when the flow is 0 it does not contain `s`, and
`shortest_path(s, t)` raises.

This PR returns `[]` when the flow is 0, as `vertex_disjoint_paths`
already does, and adds doctests for a digraph and for a disconnected
graph. This was noticed in sagemath#42846.

### 📝 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

None.
    
URL: sagemath#42903
Reported by: Muhammet Doğukan Bingöl
Reviewer(s): David Coudert
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