Preflows and HLPP overflow when the flow into a node exceeds the largest double
Summary
With a double flow, fifo_preflow_maximum_flow(),
heap_preflow_maximum_flow(), random_preflow_maximum_flow() and
hlpp_maximum_flow() give an infinite flow and leave the network
unbalanced (check_network() is false), or abort with assertions, when
the flows that the initial preflow sends into one node add up to more than
the largest double, about 1.8e308. The augmenting paths (Ford-Fulkerson
and Edmonds-Karp), Dinic and the capacity scaling give the right maximum
flow on the same networks.
It needs capacities near the largest double, so it is unlikely in
practice. It is listed among the known limits of
docs/release-notes/7.6.0.md.
Reproduction
Two parallel arcs from the source s into a node a, and a -> t:
- infinite capacities:
s -> atwice, of infinity, anda -> tof 1e308; the maximum flow is 1e308; - finite capacities:
s -> atwice, of 1e308, anda -> tof 1.5e308; the maximum flow is 1.5e308.
| 7.5.0 + #173 | 7.6.0 | |
|---|---|---|
| Preflows and HLPP, infinite arcs | infinity, as with any infinite arc out of the source | infinity, check_network() false; abort with assertions |
| Preflows and HLPP, finite arcs | infinity, check_network() true |
infinity, check_network() false; abort with assertions |
| Augmenting paths, Dinic, scaling | right (the scaling did not end with infinite arcs) | right |
With a single arc s -> a of infinity and a -> t of 1e308, every maximum
flow of 7.6.0 gives 1e308.
#include <tpl_net.H>
#include <tpl_maxflow.H>
using namespace Aleph;
using Net = Net_Graph<Net_Node<int>, Net_Arc<int, double>>;
Net net;
auto s = net.insert_node(0), a = net.insert_node(1), t = net.insert_node(2);
net.insert_arc(s, a, 1e308);
net.insert_arc(s, a, 1e308);
net.insert_arc(a, t, 1.5e308);
double f = fifo_preflow_maximum_flow(net); // inf; 1.5e308 expected
bool ok = net.check_network(); // false
Cause
The initial preflow saturates every arc out of the source: an arc of
finite capacity takes its capacity, and, since 7.6.0, one of infinite
capacity a finite bound of the maximum flow. The preflows sum the flows of
a node in a Flow_Sum, Knuth's error-free addition: adding 1e308 to
1e308 rounds to infinity, and the error term then takes inf - inf, NaN, so
that the excess of a is NaN, which the discharge can neither push nor
send back.
Before 7.6.0, the tolerance of a node with an infinite flow was infinite,
and check_network() took that flow; 7.6.0 balances infinite flows only
when they are equal, so the failure now shows. A bound of an infinite arc
that is itself a sum of finite capacities can overflow the same way.
Possible fixes
- Bound the initial excess of each node by a finite bound of the maximum flow, giving the preflow of its parallel arcs up to it: what goes into a node beyond the maximum flow goes back to the source anyway. A bound of the arcs into the sink, or of the finite capacities, must not overflow either.
- Detect an excess that is not finite and throw
std::overflow_error, as Johnson and the k shortest paths of Eppstein do for distances that do not fit, and document the limit.
The first keeps the result of the augmenting paths; the second is smaller and makes the failure explicit.
Where it comes from
Found by CodeRabbit in the review of #179, and reproduced; #179 left it for later, as it does not come from it.